summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/lib/relay-settings-builder.ts
blob: 72dc95e759e2f29eea98239937e14aaef63eff55 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
  IOpenVpnConstraints,
  RelayLocation,
  RelayProtocol,
  RelaySettingsNormalUpdate,
  RelaySettingsUpdate,
} from '../../shared/daemon-rpc-types';

interface ILocationBuilder<Self> {
  country: (country: string) => Self;
  city: (country: string, city: string) => Self;
  hostname: (country: string, city: string, hostname: string) => Self;
  any: () => Self;
  fromRaw: (location: 'any' | RelayLocation) => Self;
}

interface IExactOrAny<T, Self> {
  exact(value: T): Self;
  any(): Self;
}

interface IOpenVPNConfigurator {
  port: IExactOrAny<number, IOpenVPNConfigurator>;
  protocol: IExactOrAny<RelayProtocol, IOpenVPNConfigurator>;
}

interface ITunnelBuilder {
  openvpn(
    configurator: (openVpnConfigurator: IOpenVPNConfigurator) => void,
  ): NormalRelaySettingsBuilder;
  any(): NormalRelaySettingsBuilder;
}

class NormalRelaySettingsBuilder {
  private payload: RelaySettingsNormalUpdate = {};

  public build(): RelaySettingsUpdate {
    return {
      normal: this.payload,
    };
  }

  get location(): ILocationBuilder<NormalRelaySettingsBuilder> {
    return {
      country: (country: string) => {
        this.payload.location = { only: { country } };
        return this;
      },
      city: (country: string, city: string) => {
        this.payload.location = { only: { city: [country, city] } };
        return this;
      },
      hostname: (country: string, city: string, hostname: string) => {
        this.payload.location = { only: { hostname: [country, city, hostname] } };
        return this;
      },
      any: () => {
        this.payload.location = 'any';
        return this;
      },
      fromRaw(location: 'any' | RelayLocation) {
        if (location === 'any') {
          return this.any();
        } else if ('hostname' in location) {
          const [country, city, hostname] = location.hostname;
          return this.hostname(country, city, hostname);
        } else if ('city' in location) {
          const [country, city] = location.city;
          return this.city(country, city);
        } else if ('country' in location) {
          return this.country(location.country);
        }

        throw new Error(
          'Unsupported value of RelayLocation' + (location && JSON.stringify(location)),
        );
      },
    };
  }

  get tunnel(): ITunnelBuilder {
    const updateOpenvpn = (next: Partial<IOpenVpnConstraints>) => {
      const tunnel = this.payload.tunnel;
      if (typeof tunnel === 'string' || typeof tunnel === 'undefined') {
        this.payload.tunnel = {
          only: {
            openvpn: next,
          },
        };
      } else if (typeof tunnel === 'object') {
        const prev = tunnel.only && 'openvpn' in tunnel.only ? tunnel.only.openvpn : {};
        this.payload.tunnel = {
          only: {
            openvpn: { ...prev, ...next },
          },
        };
      }
    };

    return {
      openvpn: (configurator: (configurator: IOpenVPNConfigurator) => void) => {
        const openvpnBuilder: IOpenVPNConfigurator = {
          get port() {
            const apply = (port: 'any' | { only: number }) => {
              updateOpenvpn({ port });
              return this;
            };
            return {
              exact: (value: number) => apply({ only: value }),
              any: () => apply('any'),
            };
          },
          get protocol() {
            const apply = (protocol: 'any' | { only: RelayProtocol }) => {
              updateOpenvpn({ protocol });
              return this;
            };
            return {
              exact: (value: RelayProtocol) => apply({ only: value }),
              any: () => apply('any'),
            };
          },
        };

        configurator(openvpnBuilder);

        return this;
      },
      any: () => {
        this.payload.tunnel = 'any';
        return this;
      },
    };
  }
}

export default {
  normal: () => new NormalRelaySettingsBuilder(),
};