summaryrefslogtreecommitdiffhomepage
path: root/app/containers/AdvancedSettingsPage.js
blob: 6f456abceadc0ab080bd3c7a0b7297d75ba2bd10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @flow

import { connect } from 'react-redux';
import { push } from 'react-router-redux';
import { AdvancedSettings } from '../components/AdvancedSettings';
import RelaySettingsBuilder from '../lib/relay-settings-builder';
import log from 'electron-log';

import type { ReduxState, ReduxDispatch } from '../redux/store';
import type { SharedRouteProps } from '../routes';

const mapStateToProps = (state: ReduxState) => {
  const relaySettings = state.settings.relaySettings;
  if(relaySettings.normal) {
    const { protocol, port } = relaySettings.normal;
    return {
      protocol: protocol === 'any' ? 'Automatic' : protocol,
      port: port === 'any' ? 'Automatic' : port,
    };
  } else if(relaySettings.custom_tunnel_endpoint) {
    const { protocol, port } = relaySettings.custom_tunnel_endpoint;
    return { protocol, port };
  } else {
    throw new Error('Unknown type of relay settings.');
  }
};

const mapDispatchToProps = (dispatch: ReduxDispatch, props: SharedRouteProps) => {
  const { backend } = props;
  return {
    onClose: () => dispatch(push('/settings')),

    onUpdate: async (protocol, port) => {
      const relayUpdate = RelaySettingsBuilder.normal()
        .tunnel.openvpn((openvpn) => {
          if(protocol === 'Automatic') {
            openvpn.protocol.any();
          } else {
            openvpn.protocol.exact(protocol.toLowerCase());
          }
          if(port === 'Automatic') {
            openvpn.port.any();
          } else {
            openvpn.port.exact(port);
          }
        }).build();

      try {
        await backend.updateRelaySettings(relayUpdate);
        await backend.fetchRelaySettings();
      } catch(e) {
        log.error('Failed to update relay settings', e.message);
      }
    },
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(AdvancedSettings);