-
Notifications
You must be signed in to change notification settings - Fork 3
/
globalsettings.pas
403 lines (377 loc) · 14.1 KB
/
globalsettings.pas
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
unit GlobalSettings;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, SQLDB, Forms, Controls, Dialogs, ComCtrls, StdCtrls,
Spin, Menus, V2rayJsonConfig, ProgramSettings;
type
{ TFormGlobalSettings }
TFormGlobalSettings = class(TForm)
ButtonRouteSave: TButton;
ButtonRouteRestore: TButton;
ButtonRouteClear: TButton;
ButtonFindV2rayAssets: TButton;
ButtonCancel: TButton;
ButtonSaveConfigs: TButton;
ButtonFindV2rayBinary: TButton;
CheckBoxTLSAllowInsecure: TCheckBox;
CheckBoxEnableMux: TCheckBox;
CheckBoxCongestion: TCheckBox;
CheckBoxEnableHTTPProxy: TCheckBox;
CheckBoxEnableSocksProxy: TCheckBox;
ComboBoxLogLevel: TComboBox;
ComboBoxDomainStrategy: TComboBox;
EditV2rayAssetsPath: TEdit;
EditDnsServers: TEdit;
EditV2rayPath: TEdit;
GroupBoxOthers: TGroupBox;
GroupBoxDNSServers: TGroupBox;
GroupBoxKCPSettings: TGroupBox;
GroupBoxV2rayConfig: TGroupBox;
GroupBoxInbound: TGroupBox;
LabelMuxConcurrency: TLabel;
LabelLogLevel: TLabel;
LabelV2rayAssetsPath: TLabel;
LabelDomainStrategy: TLabel;
LabelWriteBufferSize: TLabel;
LabelReadBufferSize: TLabel;
LabelDownlinkCapacity: TLabel;
LabelUplinkCapacity: TLabel;
LabelKCPTTI: TLabel;
LabelKCPMTU: TLabel;
LabelV2RayBinaryPath: TLabel;
LabelHTTPPort: TLabel;
LabelSocksPort: TLabel;
MemoRuleList: TMemo;
OpenDialogFindV2rayBinary: TOpenDialog;
PageControlGlobalSettings: TPageControl;
SelectDirectoryDialogFindV2rayAssets: TSelectDirectoryDialog;
SpinEditMuxConcurrency: TSpinEdit;
SpinEditWriteBufferSize: TSpinEdit;
SpinEditReadBufferSize: TSpinEdit;
SpinEditDownlinkCapacity: TSpinEdit;
SpinEditUplinkCapacity: TSpinEdit;
SpinEditKCPTTI: TSpinEdit;
SpinEditKCPMTU: TSpinEdit;
SpinEditHTTPPort: TSpinEdit;
SpinEditSocksPort: TSpinEdit;
SQLQueryUpdateSettings: TSQLQuery;
SQLQueryReadSettings: TSQLQuery;
TabControlRouteSetting: TTabControl;
TabSheetMisc: TTabSheet;
TabSheetGeneralSettings: TTabSheet;
TabSheetRouteSettings: TTabSheet;
procedure ButtonCancelClick(Sender: TObject);
procedure ButtonFindV2rayAssetsClick(Sender: TObject);
procedure ButtonFindV2rayBinaryClick(Sender: TObject);
procedure ButtonRouteClearClick(Sender: TObject);
procedure ButtonRouteRestoreClick(Sender: TObject);
procedure ButtonRouteSaveClick(Sender: TObject);
procedure ButtonSaveConfigsClick(Sender: TObject);
procedure CheckBoxEnableHTTPProxyEditingDone(Sender: TObject);
procedure CheckBoxEnableMuxEditingDone(Sender: TObject);
procedure CheckBoxEnableSocksProxyEditingDone(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure LoadSettings(Settings: TProgramSettings);
procedure TabControlRouteSettingChange(Sender: TObject);
procedure UpdateSQL(SettingName: string; SettingValue: string);
function SaveSettings(Settings: TProgramSettings): boolean;
private
RouteListDirect: string;
RouteListProxy: string;
RouteListDeny: string;
end;
var
FormGlobalSettings: TFormGlobalSettings;
Settings: TProgramSettings;
SettingsJsonPath: string;
implementation
uses
PrayMainUnit;
{$R *.lfm}
procedure TFormGlobalSettings.ButtonFindV2rayBinaryClick(Sender: TObject);
begin
OpenDialogFindV2rayBinary.InitialDir := ExtractFileDir(EditV2rayPath.Text);
if OpenDialogFindV2rayBinary.Execute then
begin
EditV2rayPath.Text := OpenDialogFindV2rayBinary.FileName;
end;
end;
procedure TFormGlobalSettings.ButtonRouteClearClick(Sender: TObject);
begin
case TabControlRouteSetting.TabIndex of
0: MemoRuleList.Text := '';
1: MemoRuleList.Text := '';
2: MemoRuleList.Text := '';
end;
end;
procedure TFormGlobalSettings.ButtonRouteRestoreClick(Sender: TObject);
begin
case TabControlRouteSetting.TabIndex of
0: MemoRuleList.Text := RouteListDirect;
1: MemoRuleList.Text := RouteListProxy;
2: MemoRuleList.Text := RouteListDeny;
end;
end;
procedure TFormGlobalSettings.ButtonRouteSaveClick(Sender: TObject);
begin
case TabControlRouteSetting.TabIndex of
0: RouteListDirect := MemoRuleList.Text;
1: RouteListProxy := MemoRuleList.Text;
2: RouteListDeny := MemoRuleList.Text;
end;
end;
procedure TFormGlobalSettings.ButtonSaveConfigsClick(Sender: TObject);
begin
SaveSettings(Settings);
FormGlobalSettings.Close;
end;
procedure TFormGlobalSettings.CheckBoxEnableHTTPProxyEditingDone(Sender: TObject);
begin
SpinEditHTTPPort.Enabled := CheckBoxEnableHTTPProxy.Checked;
end;
procedure TFormGlobalSettings.CheckBoxEnableMuxEditingDone(Sender: TObject);
begin
SpinEditMuxConcurrency.Enabled := CheckBoxEnableMux.Checked;
end;
procedure TFormGlobalSettings.CheckBoxEnableSocksProxyEditingDone(Sender: TObject);
begin
SpinEditSocksPort.Enabled := CheckBoxEnableSocksProxy.Checked;
end;
procedure TFormGlobalSettings.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
LoadSettings(Settings);
end;
procedure TFormGlobalSettings.FormCreate(Sender: TObject);
var
ValueString: string;
begin
Settings := TProgramSettings.Create;
with SQLQueryReadSettings, Settings do
begin
Open;
while not EOF do
begin
ValueString := FieldByName('value').AsString;
case FieldByName('name').AsString of
'http_enabled': EnableHTTPProxy := ValueString.ToBoolean;
'socks_enabled': EnableSocksProxy := ValueString.ToBoolean;
'http_port': HTTPProxyPort := ValueString.ToInteger;
'socks_port': SocksProxyPort := ValueString.ToInteger;
'v2ray_binary': V2rayBinaryPath := ValueString;
'v2ray_assets': V2rayAssetsPath := ValueString;
'loglevel': V2rayLogLevel := TV2rayLogLevel(ValueString.ToInteger);
'route_direct': Routes[1] := ValueString;
'route_proxy': Routes[2] := ValueString;
'route_reject': Routes[3] := ValueString;
'kcp_mtu': KCPMTU := ValueString.ToInteger;
'kcp_tti': KCPTTI := ValueString.ToInteger;
'kcp_upcap': KCPUplinkCapacity := ValueString.ToInteger;
'kcp_downcap': KCPDownlinkCapacity := ValueString.ToInteger;
'kcp_rbufsize': KCPReadBufferSize := ValueString.ToInteger;
'kcp_wbufsize': KCPWriteBufferSize := ValueString.ToInteger;
'kcp_congestion': KCPCongestionAlgorithm := ValueString.ToBoolean;
'route_strategy': DomainStrategy := TRouteDomainStrategy(ValueString.ToInteger);
'dns': DNSServers := ValueString;
'mux_enabled': MuxEnabled := ValueString.ToBoolean;
'mux_concurrency': MuxConcurrency := ValueString.ToInteger;
'tls_allowinsecure': TLSAllowInsecure := ValueString.ToBoolean;
end;
Next;
end;
end;
LoadSettings(Settings);
end;
procedure TFormGlobalSettings.UpdateSQL(SettingName: string; SettingValue: string);
begin
with SQLQueryUpdateSettings do
begin
ParamByName('name').AsString := SettingName;
ParamByName('value').AsString := SettingValue;
ExecSQL;
end;
end;
procedure TFormGlobalSettings.LoadSettings(Settings: TProgramSettings);
begin
with Settings do
begin
CheckBoxEnableHTTPProxy.Checked := EnableHTTPProxy;
CheckBoxEnableSocksProxy.Checked := EnableSocksProxy;
SpinEditHTTPPort.Value := HTTPProxyPort;
SpinEditSocksPort.Value := SocksProxyPort;
EditV2rayPath.Text := V2rayBinaryPath;
EditV2rayAssetsPath.Text := V2rayAssetsPath;
ComboBoxLogLevel.ItemIndex := integer(V2rayLogLevel);
RouteListDirect := Routes[1];
RouteListProxy := Routes[2];
RouteListDeny := Routes[3];
SpinEditKCPMTU.Value := KCPMTU;
SpinEditKCPTTI.Value := KCPTTI;
SpinEditUplinkCapacity.Value := KCPUplinkCapacity;
SpinEditDownlinkCapacity.Value := KCPDownlinkCapacity;
SpinEditReadBufferSize.Value := KCPReadBufferSize;
SpinEditWriteBufferSize.Value := KCPWriteBufferSize;
CheckBoxCongestion.Checked := KCPCongestionAlgorithm;
EditDnsServers.Text := DNSServers;
ComboBoxDomainStrategy.ItemIndex := integer(DomainStrategy);
CheckBoxEnableMux.Checked := MuxEnabled;
SpinEditMuxConcurrency.Value := MuxConcurrency;
CheckBoxTLSAllowInsecure.Checked := TLSAllowInsecure;
CheckBoxEnableHTTPProxyEditingDone(nil);
CheckBoxEnableSocksProxyEditingDone(nil);
CheckBoxEnableMuxEditingDone(nil);
PageControlGlobalSettings.ActivePageIndex := 0;
TabControlRouteSetting.TabIndex := 0;
MemoRuleList.Text := RouteListDirect;
end;
end;
function TFormGlobalSettings.SaveSettings(Settings: TProgramSettings): boolean;
begin
if EditV2rayPath.Text = '' then
Result := False
else if (SpinEditKCPMTU.Value < 576) or (SpinEditKCPMTU.Value > 1460) then
Result := False
else if (SpinEditKCPTTI.Value < 10) or (SpinEditKCPTTI.Value > 100) then
Result := False
else if (ComboBoxDomainStrategy.ItemIndex < 0) or
(ComboBoxDomainStrategy.ItemIndex > 2) then
Result := False
else if (SpinEditSocksPort.Value < 1) or (SpinEditSocksPort.Value > 65534) or
(SpinEditHTTPPort.Value < 1) or (SpinEditHTTPPort.Value > 65534) then
Result := False
else
begin
with Settings do
begin
if EnableHTTPProxy <> CheckBoxEnableHTTPProxy.Checked then
begin
EnableHTTPProxy := CheckBoxEnableHTTPProxy.Checked;
UpdateSQL('http_enabled', EnableHTTPProxy.ToString);
end;
if EnableSocksProxy <> CheckBoxEnableSocksProxy.Checked then
begin
EnableSocksProxy := CheckBoxEnableSocksProxy.Checked;
UpdateSQL('socks_enabled', EnableSocksProxy.ToString);
end;
if HTTPProxyPort <> SpinEditHTTPPort.Value then
begin
HTTPProxyPort := SpinEditHTTPPort.Value;
UpdateSQL('http_port', HTTPProxyPort.ToString);
end;
if SocksProxyPort <> SpinEditSocksPort.Value then
begin
SocksProxyPort := SpinEditSocksPort.Value;
UpdateSQL('socks_port', SocksProxyPort.ToString);
end;
if V2rayBinaryPath <> EditV2rayPath.Text then
begin
V2rayBinaryPath := EditV2rayPath.Text;
UpdateSQL('v2ray_binary', V2rayBinaryPath);
end;
if V2rayAssetsPath <> EditV2rayAssetsPath.Text then
begin
V2rayAssetsPath := EditV2rayAssetsPath.Text;
UpdateSQL('v2ray_assets', V2rayAssetsPath);
end;
if V2rayLogLevel <> TV2rayLogLevel(ComboBoxLogLevel.ItemIndex) then
begin
V2rayLogLevel := TV2rayLogLevel(ComboBoxLogLevel.ItemIndex);
UpdateSQL('loglevel', ComboBoxLogLevel.ItemIndex.ToString);
end;
if Routes[1] <> RouteListDirect then
begin
Routes[1] := RouteListDirect;
UpdateSQL('route_direct', RouteListDirect);
end;
if Routes[2] <> RouteListProxy then
begin
Routes[2] := RouteListProxy;
UpdateSQL('route_proxy', RouteListProxy);
end;
if Routes[3] <> RouteListDeny then
begin
Routes[3] := RouteListDeny;
UpdateSQL('route_reject', RouteListDeny);
end;
if KCPMTU <> SpinEditKCPMTU.Value then
begin
KCPMTU := SpinEditKCPMTU.Value;
UpdateSQL('kcp_mtu', KCPMTU.ToString);
end;
if KCPTTI <> SpinEditKCPTTI.Value then
begin
KCPTTI := SpinEditKCPTTI.Value;
UpdateSQL('kcp_tti', KCPTTI.ToString);
end;
if KCPUplinkCapacity <> SpinEditUplinkCapacity.Value then
begin
KCPUplinkCapacity := SpinEditUplinkCapacity.Value;
UpdateSQL('kcp_upcap', KCPUplinkCapacity.ToString);
end;
if KCPDownlinkCapacity <> SpinEditDownlinkCapacity.Value then
begin
KCPDownlinkCapacity := SpinEditDownlinkCapacity.Value;
UpdateSQL('kcp_downcap', KCPDownlinkCapacity.ToString);
end;
if KCPReadBufferSize <> SpinEditReadBufferSize.Value then
begin
KCPReadBufferSize := SpinEditReadBufferSize.Value;
UpdateSQL('kcp_rbufsize', KCPReadBufferSize.ToString);
end;
if KCPWriteBufferSize <> SpinEditWriteBufferSize.Value then
begin
KCPWriteBufferSize := SpinEditWriteBufferSize.Value;
UpdateSQL('kcp_wbufsize', KCPWriteBufferSize.ToString);
end;
if TLSAllowInsecure <> CheckBoxTLSAllowInsecure.Checked then
begin
TLSAllowInsecure := CheckBoxTLSAllowInsecure.Checked;
UpdateSQL('tls_allowinsecure', KCPWriteBufferSize.ToString);
end;
if DNSServers <> EditDnsServers.Text then
begin
DNSServers := EditDnsServers.Text;
UpdateSQL('dns', DNSServers);
end;
if DomainStrategy <> TRouteDomainStrategy(ComboBoxDomainStrategy.ItemIndex) then
begin
DomainStrategy := TRouteDomainStrategy(ComboBoxDomainStrategy.ItemIndex);
UpdateSQL('route_strategy', ComboBoxDomainStrategy.ItemIndex.ToString);
end;
if MuxEnabled <> CheckBoxEnableMux.Checked then
begin
MuxEnabled := CheckBoxEnableMux.Checked;
UpdateSQL('mux_enabled', MuxEnabled.ToString);
end;
if MuxConcurrency <> SpinEditMuxConcurrency.Value then
begin
MuxConcurrency := SpinEditMuxConcurrency.Value;
UpdateSQL('mux_concurrency', MuxConcurrency.ToString);
end;
end;
PrayMainWindow.SQLTransactionPrayDB.Commit;
Result := True;
end;
end;
procedure TFormGlobalSettings.ButtonCancelClick(Sender: TObject);
begin
FormGlobalSettings.Close;
end;
procedure TFormGlobalSettings.ButtonFindV2rayAssetsClick(Sender: TObject);
begin
SelectDirectoryDialogFindV2rayAssets.InitialDir := EditV2rayAssetsPath.Text;
if SelectDirectoryDialogFindV2rayAssets.Execute then
begin
EditV2rayAssetsPath.Text := SelectDirectoryDialogFindV2rayAssets.FileName;
end;
end;
procedure TFormGlobalSettings.TabControlRouteSettingChange(Sender: TObject);
begin
case TabControlRouteSetting.TabIndex of
0: MemoRuleList.Text := RouteListDirect;
1: MemoRuleList.Text := RouteListProxy;
2: MemoRuleList.Text := RouteListDeny;
end;
end;
end.