-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathsettings.ts
239 lines (211 loc) · 8.21 KB
/
settings.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import fs from 'fs/promises';
import { ISettings } from '../shared/daemon-rpc-types';
import { ICurrentAppVersionInfo } from '../shared/ipc-types';
import log from '../shared/logging';
import { getOpenAtLogin, setOpenAtLogin } from './autostart';
import { DaemonRpc } from './daemon-rpc';
import { getDefaultSettings } from './default-settings';
import GuiSettings from './gui-settings';
import { IpcMainEventChannel } from './ipc-event-channel';
export interface SettingsDelegate {
handleMonochromaticIconChange(value: boolean): void;
handleUnpinnedWindowChange(): void;
}
export default class Settings implements Readonly<ISettings> {
private guiSettings = new GuiSettings();
private settingsValue = getDefaultSettings();
public constructor(
private delegate: SettingsDelegate,
private daemonRpc: DaemonRpc,
private currentVersion: ICurrentAppVersionInfo,
) {}
public registerIpcListeners() {
this.registerGuiSettingsListener();
IpcMainEventChannel.settings.handleSetAllowLan((allowLan) =>
this.daemonRpc.setAllowLan(allowLan),
);
IpcMainEventChannel.settings.handleSetShowBetaReleases((showBetaReleases) =>
this.daemonRpc.setShowBetaReleases(showBetaReleases),
);
IpcMainEventChannel.settings.handleSetEnableIpv6((enableIpv6) =>
this.daemonRpc.setEnableIpv6(enableIpv6),
);
IpcMainEventChannel.settings.handleSetBlockWhenDisconnected((blockWhenDisconnected) =>
this.daemonRpc.setBlockWhenDisconnected(blockWhenDisconnected),
);
IpcMainEventChannel.settings.handleSetBridgeState(async (bridgeState) => {
await this.daemonRpc.setBridgeState(bridgeState);
// Reset bridge constraints to `any` when the state is set to auto or off if not custom
if (
(bridgeState === 'auto' || bridgeState === 'off') &&
this.bridgeSettings.type === 'normal'
) {
await this.daemonRpc.setBridgeSettings({
...this.bridgeSettings,
normal: { ...this.bridgeSettings.normal, location: 'any' },
});
}
});
IpcMainEventChannel.settings.handleSetOpenVpnMssfix((mssfix?: number) =>
this.daemonRpc.setOpenVpnMssfix(mssfix),
);
IpcMainEventChannel.settings.handleSetWireguardMtu((mtu?: number) =>
this.daemonRpc.setWireguardMtu(mtu),
);
IpcMainEventChannel.settings.handleSetWireguardQuantumResistant((quantumResistant?: boolean) =>
this.daemonRpc.setWireguardQuantumResistant(quantumResistant),
);
IpcMainEventChannel.settings.handleSetRelaySettings((relaySettings) =>
this.daemonRpc.setRelaySettings(relaySettings),
);
IpcMainEventChannel.settings.handleUpdateBridgeSettings((bridgeSettings) => {
return this.daemonRpc.setBridgeSettings(bridgeSettings);
});
IpcMainEventChannel.settings.handleSetDnsOptions((dns) => {
return this.daemonRpc.setDnsOptions(dns);
});
IpcMainEventChannel.autoStart.handleSet((autoStart: boolean) => {
return this.setAutoStart(autoStart);
});
IpcMainEventChannel.settings.handleSetObfuscationSettings((obfuscationSettings) => {
return this.daemonRpc.setObfuscationSettings(obfuscationSettings);
});
IpcMainEventChannel.settings.handleAddApiAccessMethod((method) => {
return this.daemonRpc.addApiAccessMethod(method);
});
IpcMainEventChannel.settings.handleUpdateApiAccessMethod((method) => {
return this.daemonRpc.updateApiAccessMethod(method);
});
IpcMainEventChannel.settings.handleRemoveApiAccessMethod((id) => {
return this.daemonRpc.removeApiAccessMethod(id);
});
IpcMainEventChannel.settings.handleSetApiAccessMethod((id) => {
return this.daemonRpc.setApiAccessMethod(id);
});
IpcMainEventChannel.settings.handleTestApiAccessMethodById((id) => {
return this.daemonRpc.testApiAccessMethodById(id);
});
IpcMainEventChannel.settings.handleTestCustomApiAccessMethod((method) => {
return this.daemonRpc.testCustomApiAccessMethod(method);
});
IpcMainEventChannel.settings.handleClearAllRelayOverrides(() => {
return this.daemonRpc.clearAllRelayOverrides();
});
IpcMainEventChannel.settings.handleImportText((text) => {
return this.daemonRpc.applyJsonSettings(text);
});
IpcMainEventChannel.settings.handleImportFile(async (path) => {
const settings = await fs.readFile(path);
return this.daemonRpc.applyJsonSettings(settings.toString());
});
IpcMainEventChannel.settings.handleSetEnableDaita((value) => {
return this.daemonRpc.setEnableDaita(value);
});
IpcMainEventChannel.settings.handleSetDaitaDirectOnly((value) => {
return this.daemonRpc.setDaitaDirectOnly(value);
});
IpcMainEventChannel.guiSettings.handleSetEnableSystemNotifications((flag: boolean) => {
this.guiSettings.enableSystemNotifications = flag;
});
IpcMainEventChannel.guiSettings.handleSetAutoConnect((autoConnect: boolean) => {
this.guiSettings.autoConnect = autoConnect;
});
IpcMainEventChannel.guiSettings.handleSetStartMinimized((startMinimized: boolean) => {
this.guiSettings.startMinimized = startMinimized;
});
IpcMainEventChannel.guiSettings.handleSetMonochromaticIcon((monochromaticIcon: boolean) => {
this.guiSettings.monochromaticIcon = monochromaticIcon;
});
IpcMainEventChannel.guiSettings.handleSetUnpinnedWindow((unpinnedWindow: boolean) => {
this.guiSettings.unpinnedWindow = unpinnedWindow;
this.delegate.handleUnpinnedWindowChange();
});
IpcMainEventChannel.guiSettings.handleSetAnimateMap((animateMap: boolean) => {
this.guiSettings.animateMap = animateMap;
});
IpcMainEventChannel.currentVersion.handleDisplayedChangelog(() => {
this.guiSettings.changelogDisplayedForVersion = this.currentVersion.gui;
});
IpcMainEventChannel.upgradeVersion.handleDismissedUpgrade((version: string) => {
this.guiSettings.updateDismissedForVersion = version;
});
}
public get all() {
return this.settingsValue;
}
public get allowLan() {
return this.settingsValue.allowLan;
}
public get autoConnect() {
return this.settingsValue.autoConnect;
}
public get blockWhenDisconnected() {
return this.settingsValue.blockWhenDisconnected;
}
public get showBetaReleases() {
return this.settingsValue.showBetaReleases;
}
public get relaySettings() {
return this.settingsValue.relaySettings;
}
public get tunnelOptions() {
return this.settingsValue.tunnelOptions;
}
public get bridgeSettings() {
return this.settingsValue.bridgeSettings;
}
public get bridgeState() {
return this.settingsValue.bridgeState;
}
public get splitTunnel() {
return this.settingsValue.splitTunnel;
}
public get obfuscationSettings() {
return this.settingsValue.obfuscationSettings;
}
public get customLists() {
return this.settingsValue.customLists;
}
public get apiAccessMethods() {
return this.settingsValue.apiAccessMethods;
}
public get relayOverrides() {
return this.settingsValue.relayOverrides;
}
public get gui() {
return this.guiSettings;
}
public handleNewSettings(newSettings: ISettings) {
this.settingsValue = newSettings;
}
private registerGuiSettingsListener() {
this.guiSettings.onChange = (newState, oldState) => {
if (oldState.monochromaticIcon !== newState.monochromaticIcon) {
this.delegate.handleMonochromaticIconChange(newState.monochromaticIcon);
}
if (newState.autoConnect !== oldState.autoConnect) {
this.updateDaemonsAutoConnect();
}
IpcMainEventChannel.guiSettings.notify?.(newState);
};
}
private async setAutoStart(autoStart: boolean): Promise<void> {
try {
await setOpenAtLogin(autoStart);
IpcMainEventChannel.autoStart.notify?.(autoStart);
this.updateDaemonsAutoConnect();
} catch (e) {
const error = e as Error;
log.error(
`Failed to update the autostart to ${autoStart.toString()}. ${error.message.toString()}`,
);
}
return Promise.resolve();
}
private updateDaemonsAutoConnect() {
const daemonAutoConnect = this.guiSettings.autoConnect && getOpenAtLogin();
if (daemonAutoConnect !== this.settingsValue.autoConnect) {
void this.daemonRpc.setAutoConnect(daemonAutoConnect);
}
}
}