-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpraymainunit.pas
More file actions
380 lines (350 loc) · 9.83 KB
/
Copy pathpraymainunit.pas
File metadata and controls
380 lines (350 loc) · 9.83 KB
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
unit PrayMainUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Buttons, ComCtrls, FPJson, Process, Profile, V2rayJsonConfig;
type
TV2rayWatchThread = class(TThread)
constructor Create(CreateSuspended: boolean; V2rayProcess: TProcess);
protected
procedure Execute; override;
private
Content: string;
V2Process: TProcess;
LineNum: word;
procedure UpdateUI;
procedure ProcessStoped;
end;
{ TPrayMainWindow }
TPrayMainWindow = class(TForm)
BitBtnDisconnect: TBitBtn;
BitBtnConnect: TBitBtn;
ButtonImport: TButton;
ButtonShareLink: TButton;
ButtonGlobalSettings: TButton;
ButtonAddProfile: TButton;
ButtonRemoveProfile: TButton;
ButtonEditProfile: TButton;
ListBoxProfiles: TListBox;
MemoV2rayOutput: TMemo;
MemoServerInfo: TMemo;
PanelMainPanel: TPanel;
StatusBarConnectionStatus: TStatusBar;
procedure BitBtnConnectClick(Sender: TObject);
procedure BitBtnDisconnectClick(Sender: TObject);
procedure ButtonAddProfileClick(Sender: TObject);
procedure ButtonEditProfileClick(Sender: TObject);
procedure ButtonGlobalSettingsClick(Sender: TObject);
procedure ButtonImportClick(Sender: TObject);
procedure ButtonRemoveProfileClick(Sender: TObject);
procedure ButtonShareLinkClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListBoxProfilesSelectionChange(Sender: TObject; User: boolean);
procedure LoadProfiles;
procedure SaveProfiles;
private
V2RayProcess: TProcess;
V2Thread: TV2rayWatchThread;
procedure IfProfileSelect(ASelected: boolean);
end;
var
ProfileList: TList;
PrayMainWindow: TPrayMainWindow;
ApplicationRootDirectory: string;
TemporaryDirectory: string;
ProfileJsonPath: string;
GeneratedJsonPath: string;
implementation
uses GlobalSettings, ProfileEditor, ShareLinkForm, ImportLinksForm;
constructor TV2rayWatchThread.Create(CreateSuspended: boolean; V2rayProcess: TProcess);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
V2Process := V2rayProcess;
LineNum := 0;
end;
procedure TV2rayWatchThread.Execute;
var
B: char;
begin
Content := '';
B := #10;
while (not Terminated) and V2Process.Running do
begin
try
V2Process.Output.ReadBuffer(B, 1);
Content := Content + B;
if B = #10 then
begin
Synchronize(@UpdateUI);
Content := '';
end;
except
on EReadError do
Synchronize(@ProcessStoped);
end;
end;
end;
procedure TV2rayWatchThread.UpdateUI;
begin
if LineNum < 1000 then
Inc(LineNum)
else
PrayMainWindow.MemoV2rayOutput.Lines.Delete(0);
PrayMainWindow.MemoV2rayOutput.SelStart := Length(PrayMainWindow.MemoV2rayOutput.Text);
PrayMainWindow.MemoV2rayOutput.Lines.Add(Content.Trim);
PrayMainWindow.StatusBarConnectionStatus.SimpleText := 'Connected';
end;
procedure TV2rayWatchThread.ProcessStoped;
begin
if not V2Process.Running then
begin
PrayMainWindow.StatusBarConnectionStatus.SimpleText := 'Disconnected';
Terminate;
end;
end;
{$R *.lfm}
procedure TPrayMainWindow.ButtonAddProfileClick(Sender: TObject);
var
P: TProfile;
begin
P := TProfile.Create;
FormEditProfile.ApplyProfile(P);
FormEditProfile.ShowModal;
if FormEditProfile.SaveAfterExit then
begin
ProfileList.Add(P);
ListBoxProfiles.Items.Add(P.Name);
ListBoxProfilesSelectionChange(nil, False);
SaveProfiles;
end;
end;
procedure TPrayMainWindow.BitBtnConnectClick(Sender: TObject);
var
F: TFileStream;
P: TProfile;
S: string;
I: integer;
begin
if ListBoxProfiles.ItemIndex <> -1 then
begin
F := TFileStream.Create(GeneratedJsonPath, fmCreate);
P := TProfile(ProfileList[ListBoxProfiles.ItemIndex]);
S := P.CreateJSON(Settings).FormatJSON;
F.WriteBuffer(Pointer(S)^, Length(S));
F.Free;
BitBtnDisconnectClick(nil);
MemoV2rayOutput.Lines.Clear;
MemoV2rayOutput.Lines.Add('! starting v2ray.');
V2RayProcess := TProcess.Create(nil);
with V2RayProcess do
begin
Executable := Settings.V2rayBinaryPath;
Parameters.Clear;
Parameters.Add('-config');
Parameters.Add(GeneratedJsonPath);
for I := 1 to GetEnvironmentVariableCount do
Environment.Add(GetEnvironmentString(I));
Environment.Add(Format('V2RAY_LOCATION_ASSET=%s', [Settings.V2rayAssetsPath]));
Options := [poNoConsole, poStderrToOutPut, poUsePipes];
Execute;
end;
V2Thread := TV2rayWatchThread.Create(True, V2RayProcess);
V2Thread.Start;
StatusBarConnectionStatus.SimpleText := 'Connected';
end;
end;
procedure TPrayMainWindow.BitBtnDisconnectClick(Sender: TObject);
begin
if Assigned(V2RayProcess) then
begin
MemoV2rayOutput.Lines.Add('! stoping v2ray.');
try
V2RayProcess.Terminate(0);
finally
end;
end;
if Assigned(V2Thread) then
try
V2Thread.Terminate;
finally
end;
end;
procedure TPrayMainWindow.ButtonEditProfileClick(Sender: TObject);
var
P: TProfile;
begin
if ListBoxProfiles.ItemIndex <> -1 then
begin
P := TProfile(ProfileList[ListBoxProfiles.ItemIndex]);
FormEditProfile.ApplyProfile(P);
FormEditProfile.ShowModal;
if FormEditProfile.SaveAfterExit then
begin
ListBoxProfiles.Items[ListBoxProfiles.ItemIndex] := P.Name;
ListBoxProfilesSelectionChange(nil, False);
SaveProfiles;
end;
end;
end;
procedure TPrayMainWindow.ButtonGlobalSettingsClick(Sender: TObject);
begin
FormGlobalSettings.Show;
end;
procedure TPrayMainWindow.ButtonImportClick(Sender: TObject);
var
P: Pointer;
begin
with FormImportLinks do
begin
Refresh;
ShowModal;
if SaveAfterExit then
begin
for P in ReadyProfileList do
begin
ProfileList.Add(P);
ListBoxProfiles.Items.Add(TProfile(P).Name);
end;
ListBoxProfilesSelectionChange(nil, False);
SaveProfiles;
end;
end;
end;
procedure TPrayMainWindow.ButtonRemoveProfileClick(Sender: TObject);
var
I: integer;
begin
I := ListBoxProfiles.ItemIndex;
if I <> -1 then
begin
ListBoxProfiles.DeleteSelected;
TProfile(ProfileList[I]).Free;
ProfileList.Delete(I);
ListBoxProfilesSelectionChange(nil, False);
SaveProfiles;
end;
end;
procedure TPrayMainWindow.ButtonShareLinkClick(Sender: TObject);
begin
if ListBoxProfiles.ItemIndex <> -1 then
begin
FormShareLink.ApplyProfile(TProfile(ProfileList[ListBoxProfiles.ItemIndex]));
FormShareLink.Show;
end;
end;
procedure TPrayMainWindow.FormCreate(Sender: TObject);
begin
ProfileList := TList.Create;
if FileExists(ProfileJsonPath) then
LoadProfiles;
end;
procedure TPrayMainWindow.FormDestroy(Sender: TObject);
begin
BitBtnDisconnectClick(nil);
end;
procedure TPrayMainWindow.IfProfileSelect(ASelected: boolean);
var
I: integer;
P: TProfile;
begin
ButtonEditProfile.Enabled := ASelected;
ButtonRemoveProfile.Enabled := ASelected;
BitBtnConnect.Enabled := ASelected;
MemoServerInfo.Lines.Clear;
if ASelected then
begin
I := ListBoxProfiles.ItemIndex;
P := TProfile(ProfileList[I]);
with MemoServerInfo.Lines do
begin
Add(Format('Remote: %s:%d', [P.Address, P.Port]));
Add(Format('Protocol: %s', [RemoteProtocolToString(P.Protocol)]));
Add(Format('Network: %s', [TransportToString(P.Network)]));
end;
end;
end;
procedure TPrayMainWindow.ListBoxProfilesSelectionChange(Sender: TObject;
User: boolean);
begin
MemoServerInfo.Lines.Clear;
if ListBoxProfiles.ItemIndex = -1 then IfProfileSelect(False)
else IfProfileSelect(True);
end;
procedure TPrayMainWindow.LoadProfiles;
var
F: TFileStream;
S: string;
J: TJSONArray;
I: TJSONEnum;
K: TJSONObject;
P: TProfile;
begin
F := TFileStream.Create(ProfileJsonPath, fmOpenRead);
SetLength(S, F.Size);
F.ReadBuffer(Pointer(S)^, Length(S));
F.Free;
J := TJSONArray(GetJSON(S));
ListBoxProfiles.Items.Clear;
ProfileList.Clear;
for I in J do
begin
P := TProfile.Create;
K := TJSONObject(I.Value);
P.Name := K.Get('name', 'Unamed');
P.Address := K.Get('addr', '0.0.0.0');
P.Port := K.Get('port', 0);
P.Protocol := TRemoteProtocol(K.Get('pc', 0));
P.UUID := K.Get('id', '');
P.AlterID := K.Get('aid', 0);
P.SSPassword := K.Get('sp', '');
P.SSMethod := TShadowsocksEncryption(K.Get('sm', 2));
P.Network := TRemoteTransport(K.Get('net', 0));
P.EnableTLS := K.Get('tls', False);
P.Hostname := K.Get('host', '');
P.Path := K.Get('path', '');
P.UDPHeaderType := TUDPHeaderType(K.Get('udph', 0));
P.QUICSecurity := TQUICSecurity(K.Get('qs', 0));
P.QUICKey := K.Get('qk', '');
ListBoxProfiles.Items.Add(P.Name);
ProfileList.Add(P);
end;
end;
procedure TPrayMainWindow.SaveProfiles;
var
F: TFileStream;
J: TJSONArray;
I: Pointer;
P: TProfile;
S: string;
begin
F := TFileStream.Create(ProfileJsonPath, fmCreate);
J := TJSONArray.Create();
for I in ProfileList do
begin
P := TProfile(I);
J.Add(TJSONObject.Create([
'name', P.Name,
'addr', P.Address,
'port', P.Port,
'pc', integer(P.Protocol),
'id', P.UUID,
'aid', P.AlterID,
'sp', P.SSPassword,
'sm', integer(P.SSMethod),
'net', integer(P.Network),
'tls', P.EnableTLS,
'host', P.Hostname,
'path', P.Path,
'udph', integer(P.UDPHeaderType),
'qs', integer(P.QUICSecurity),
'qk', P.QUICKey]));
end;
S := J.AsJSON;
F.WriteBuffer(Pointer(S)^, Length(S));
F.Free;
J.Free;
end;
end.