-
Notifications
You must be signed in to change notification settings - Fork 3
/
v2rayjsonconfig.pas
577 lines (543 loc) · 17 KB
/
v2rayjsonconfig.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
unit V2rayJsonConfig;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FPJson, RegExpr;
type
TRemoteTransport = (rtTCP, rtKCP, rtWS, rtHTTP, rtQUIC);
TRemoteProtocol = (rpVMESS, rpSHADOWSOCKS, rpVLESS, rpTROJAN);
TV2rayLogLevel = (llDEBUG, llINFO, llWARNING, llERROR, llNONE);
TRouteListType = (rlDIRECT, rlPROXY, rlDENY);
TRouteDomainStrategy = (dsASIS, dsNONMATCH, dsDEMAND);
TSecurityOptions = (soNONE, soTLS, soXTLS);
TV2rayJsonConfig = class
RemoteAddr: string;
RemotePort: word;
EnableLocalHTTPProxy: boolean;
EnableLocalSocksProxy: boolean;
LocalHTTPProxyPort: word;
LocalSocksProxyPort: word;
EnableLocalSocksUDP: boolean;
StreamSecurity: TSecurityOptions;
TLSServerName: string;
TLSAllowInsecure: boolean;
RemoteHostname: string;
RemotePath: string;
WSMaxEarlyData: word;
WSEarlyDataHeaderName: string;
KCPMTU: word;
KCPTTI: byte;
KCPUplinkCapacity: word;
KCPDownlinkCapacity: word;
KCPReadBufferSize: byte;
KCPWriteBufferSize: byte;
KCPCongestionAlgorithm: boolean;
UDPHeaderType: string;
constructor Create(Address: string; Port: word);
procedure SetLogLevel(Level: TV2rayLogLevel);
procedure SetVMessUser(ID: string; Alter: word);
procedure SetShadowsocks(Password: string; EncryptMethod: string);
procedure SetVLESS(ID: string; Encryption: string = 'none'; VLESSFlow: string = '');
procedure SetTrojan(Password: string; TrojanFlow: string = '');
procedure SetTransport(Transport: TRemoteTransport; Security: TSecurityOptions);
procedure SetMux(Concurrency: word);
procedure SetRoute(RouteType: TRouteListType; RouteList: TStrings);
procedure SetRouteDomainStrategy(Strategy: TRouteDomainStrategy);
procedure SetSocksProxy(Port: word);
procedure SetHTTPProxy(Port: word);
procedure SetDNSServers(ServerListString: string);
procedure SetHostPath(Hostname: string = ''; Path: string = '');
procedure SetWSEarlyData(Length: word = 0; HeaderName: string = '');
procedure SetQUIC(Security: string = 'none'; Key: string = '');
function ToJSON: TJSONObject;
protected
LogLevel: string;
DomainStrategy: string;
NetworkTransport: TRemoteTransport;
QUICSecurity: string;
QUICKey: string;
Flow: string;
VMessUserID: string;
VMessUserAlterID: word;
SSPassword: string;
SSEncryption: string;
VLESSUserID: string;
VLESSEncryption: string;
TrojanPassword: string;
MuxEnabled: boolean;
MuxConcurrency: word;
private
Protocol: TRemoteProtocol;
RouteDirectDomainList: TStrings;
RouteDirectIPList: TStrings;
RouteProxyDomainList: TStrings;
RouteProxyIPList: TStrings;
RouteDenyIPList: TStrings;
RouteDenyDomainList: TStrings;
DNSServers: TStrings;
function GenerateRouteJSON: TJSONArray;
function GenerateDNSServerJSON: TJSONArray;
function GenerateProxyJSON: TJSONObject;
function GenerateStreamSettingsJSON: TJSONObject;
end;
function CommaStringList(CommaStr: string): TStrings;
function SecurityOptionToString(SecurityOption: TSecurityOptions): string;
function TransportToString(Transport: TRemoteTransport): string;
function RemoteProtocolToString(Protocol: TRemoteProtocol): string;
function GetTransportFromString(TransportString: string): TRemoteTransport;
implementation
function CommaStringList(CommaStr: string): TStrings;
var
A: TStringArray;
X: string;
Y: string;
begin
A := CommaStr.Split(',');
Result := TStringList.Create;
for X in A do
begin
Y := X.Trim;
if Length(Y) > 0 then
Result.Add(Y);
end;
end;
function SecurityOptionToString(SecurityOption: TSecurityOptions): string;
begin
case SecurityOption of
soNONE: Result := 'none';
soTLS: Result := 'tls';
soXTLS: Result := 'xtls';
else Result := 'none';
end;
end;
function TransportToString(Transport: TRemoteTransport): string;
begin
case Transport of
rtTCP: Result := 'tcp';
rtKCP: Result := 'kcp';
rtWS: Result := 'ws';
rtHTTP: Result := 'http';
rtQUIC: Result := 'quic';
else
Result := 'tcp';
end;
end;
function RemoteProtocolToString(Protocol: TRemoteProtocol): string;
begin
case Protocol of
rpVMESS: Result := 'VMess';
rpSHADOWSOCKS: Result := 'Shadowsocks';
rpVLESS: Result := 'VLESS';
rpTROJAN: Result := 'Trojan';
else Result := 'Unknown';
end;
end;
function GetTransportFromString(TransportString: string): TRemoteTransport;
var
S: string;
begin
S := TransportString.ToLower;
case S of
'tcp': Result := rtTCP;
'kcp': Result := rtKCP;
'ws': Result := rtWS;
'http': Result := rtHTTP;
'quic': Result := rtQUIC;
else Result := rtTCP;
end;
end;
constructor TV2rayJsonConfig.Create(Address: string; Port: word);
begin
RemoteAddr := Address;
RemotePort := Port;
EnableLocalHTTPProxy := False;
EnableLocalSocksProxy := False;
EnableLocalSocksUDP := True;
RemoteHostname := Address;
RemotePath := '';
LogLevel := 'debug';
NetworkTransport := rtTCP;
TLSServerName := '';
TLSAllowInsecure := False;
WSMaxEarlyData := 0;
WSEarlyDataHeaderName := '';
UDPHeaderType := 'none';
KCPMTU := 1350;
KCPTTI := 20;
KCPUplinkCapacity := 5;
KCPDownlinkCapacity := 50;
KCPReadBufferSize := 1;
KCPWriteBufferSize := 1;
DomainStrategy := 'IPIfNonMatch';
KCPCongestionAlgorithm := False;
RouteDirectDomainList := TStringList.Create;
RouteDirectIPList := TStringList.Create;
RouteProxyDomainList := TStringList.Create;
RouteProxyIPList := TStringList.Create;
RouteDenyDomainList := TStringList.Create;
RouteDenyIPList := TStringList.Create;
DNSServers := TStringList.Create;
SetQUIC;
end;
procedure TV2rayJsonConfig.SetLogLevel(Level: TV2rayLogLevel);
begin
case Level of
llDEBUG: LogLevel := 'debug';
llINFO: LogLevel := 'info';
llWARNING: LogLevel := 'warning';
llERROR: LogLevel := 'error';
llNONE: LogLevel := 'none';
else
LogLevel := 'debug';
end;
end;
procedure TV2rayJsonConfig.SetVMessUser(ID: string; Alter: word);
begin
VMessUserID := ID;
VMessUserAlterID := Alter;
Protocol := rpVMESS;
end;
procedure TV2rayJsonConfig.SetShadowsocks(Password: string; EncryptMethod: string);
begin
SSPassword := Password;
SSEncryption := EncryptMethod;
Protocol := rpSHADOWSOCKS;
end;
procedure TV2rayJsonConfig.SetVLESS(ID: string; Encryption: string = 'none'; VLESSFlow: string = '');
begin
VLESSUserID := ID;
VLESSEncryption := Encryption;
Flow := VLESSFlow;
Protocol := rpVLESS;
end;
procedure TV2rayJsonConfig.SetTrojan(Password: string; TrojanFlow: string = '');
begin
TrojanPassword := Password;
Flow := TrojanFlow;
Protocol := rpTROJAN;
end;
procedure TV2rayJsonConfig.SetTransport(Transport: TRemoteTransport; Security: TSecurityOptions);
begin
case Transport of
rtKCP, rtQUIC: StreamSecurity := soNONE;
else StreamSecurity := Security;
end;
NetworkTransport := Transport;
end;
procedure TV2rayJsonConfig.SetMux(Concurrency: word);
begin
if Concurrency < 1 then MuxEnabled := False
else
begin
MuxEnabled := True;
MuxConcurrency := Concurrency;
end;
end;
procedure TV2rayJsonConfig.SetHostPath(Hostname: string = ''; Path: string = '');
var
I: integer;
begin
if Hostname <> '' then
begin
RemoteHostname := Hostname;
I := RemoteHostname.IndexOf(':');
if I <> -1 then TLSServerName := RemoteHostname.Substring(0, I)
else TLSServerName := Hostname;
end;
RemotePath := Path;
end;
procedure TV2rayJsonConfig.SetQUIC(Security: string = 'none'; Key: string = '');
begin
if Security = 'none' then QUICKey := ''
else QUICKey := Key;
QUICSecurity := Security;
end;
procedure TV2rayJsonConfig.SetWSEarlyData(Length: word = 0; HeaderName: string = '');
begin
if Length = 0 then WSEarlyDataHeaderName := ''
else WSEarlyDataHeaderName := HeaderName;
WSMaxEarlyData := Length;
end;
procedure TV2rayJsonConfig.SetRoute(RouteType: TRouteListType; RouteList: TStrings);
var
IPList: TStrings;
DomainList: TStrings;
R: string;
I: integer;
IPRegex: TRegExpr;
IP6Regex: TRegExpr;
begin
case RouteType of
rlDIRECT:
begin
IPList := RouteDirectIPList;
DomainList := RouteDirectDomainList;
end;
rlPROXY:
begin
IPList := RouteProxyIPList;
DomainList := RouteProxyDomainList;
end;
rlDENY:
begin
IPList := RouteDenyIPList;
DomainList := RouteDenyDomainList;
end;
end;
IPRegex := TRegExpr.Create;
IP6Regex := TRegExpr.Create;
IPRegex.Expression := '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?';
IP6Regex.Expression :=
'(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(/\d{1,3})?';
for R in RouteList do
begin
I := R.IndexOf(':');
if I <> -1 then
begin
case R.Substring(0, I) of
'regexp': DomainList.Add(R);
'domain': DomainList.Add(R);
'full': DomainList.Add(R);
'geosite': DomainList.Add(R);
'geoip': IPList.Add(R);
else
if IP6Regex.Exec(R) then
IPList.Add(R);
end;
end
else if IPRegex.Exec(R) then
IPList.Add(R);
end;
end;
procedure TV2rayJsonConfig.SetRouteDomainStrategy(Strategy: TRouteDomainStrategy);
begin
case Strategy of
dsASIS: DomainStrategy := 'AsIs';
dsNONMATCH: DomainStrategy := 'IPIfNonMatch';
dsDEMAND: DomainStrategy := 'IPOnDemand';
end;
end;
procedure TV2rayJsonConfig.SetSocksProxy(Port: word);
begin
if (Port > 0) and (Port < 65535) then
begin
LocalSocksProxyPort := Port;
EnableLocalSocksProxy := True;
end
else
EnableLocalSocksProxy := False;
end;
procedure TV2rayJsonConfig.SetHTTPProxy(Port: word);
begin
if (Port > 0) and (Port < 65535) then
begin
LocalHTTPProxyPort := Port;
EnableLocalHTTPProxy := True;
end
else
EnableLocalHTTPProxy := False;
end;
procedure TV2rayJsonConfig.SetDNSServers(ServerListString: string);
begin
DNSServers := CommaStringList(ServerListString);
end;
function TV2rayJsonConfig.GenerateDNSServerJSON: TJSONArray;
var
S: string;
begin
Result := TJSONArray.Create;
for S in DNSServers do
Result.Add(S);
end;
function TV2rayJsonConfig.GenerateRouteJSON: TJSONArray;
var
L: TJSONArray;
X: string;
begin
Result := TJSONArray.Create;
if RouteDirectDomainList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteDirectDomainList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'direct', 'domain', L]));
end;
if RouteProxyDomainList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteProxyDomainList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'proxy', 'domain', L]));
end;
if RouteDenyDomainList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteDenyDomainList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'deny', 'domain', L]));
end;
if RouteDirectIPList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteDirectIPList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'direct', 'ip', L]));
end;
if RouteProxyIPList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteProxyIPList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'proxy', 'ip', L]));
end;
if RouteDenyIPList.Count <> 0 then
begin
L := TJSONArray.Create;
for X in RouteDenyIPList do
L.Add(X);
Result.Add(TJSONObject.Create(['type', 'field', 'outboundTag', 'deny', 'ip', L]));
end;
end;
function TV2rayJsonConfig.GenerateProxyJSON: TJSONObject;
var
P: string;
S: TJSONObject;
begin
case Protocol of
rpVMESS:
begin
P := 'vmess';
S := TJSONObject.Create([
'vnext', TJSONArray.Create([
TJSONObject.Create([
'address', RemoteAddr,
'port', RemotePort,
'users', TJSONArray.Create([
TJSONObject.Create([
'id', VMessUserID,
'alterId', VMessUserAlterID,
'level', 0])])])])]);
end;
rpSHADOWSOCKS:
begin
P := 'shadowsocks';
S := TJSONObject.Create([
'servers', TJSONArray.Create([
TJSONObject.Create([
'address', RemoteAddr,
'port', RemotePort,
'method', SSEncryption,
'password', SSPassword,
'level', 0])])]);
end;
rpVLESS:
begin
P := 'vless';
S := TJSONObject.Create([
'vnext', TJSONArray.Create([
TJSONObject.Create([
'address', RemoteAddr,
'port', RemotePort,
'users', TJSONArray.Create([
TJSONObject.Create([
'id', VLESSUserID,
'flow', Flow,
'encryption', VLESSEncryption])])])])]);
end;
rpTROJAN:
begin
P := 'trojan';
S := TJSONObject.Create([
'servers', TJSONArray.Create([
TJSONObject.Create([
'address', RemoteAddr,
'port', RemotePort,
'password', TrojanPassword,
'flow', Flow,
'level', 0])])]);
end;
end;
Result := TJSONObject.Create([
'tag', 'proxy',
'protocol', P,
'settings', S,
'streamSettings', GenerateStreamSettingsJSON]);
if MuxEnabled then Result.Add('mux', TJSONObject.Create([
'enabled', True,
'concurrency', MuxConcurrency]));
end;
function TV2rayJsonConfig.GenerateStreamSettingsJSON: TJSONObject;
var
SecurityOption: string;
begin
Result := TJSONObject.Create([
'network', TransportToString(NetworkTransport),
'security', SecurityOptionToString(StreamSecurity)]);
case StreamSecurity of
soTLS: Result.Add('tlsSettings', TJSONObject.Create([
'serverName', TLSServerName,
'allowInsecure', TLSAllowInsecure]));
soXTLS: Result.Add('xtlsSettings', TJSONObject.Create([
'serverName', TLSServerName,
'allowInsecure', False]));
end;
case NetworkTransport of
rtWS: Result.Add('wsSettings', TJSONObject.Create([
'path', RemotePath,
'headers', TJSONObject.Create(['Host', RemoteHostname]),
'maxEarlyData', WSMaxEarlyData, 'earlyDataHeaderName', WSEarlyDataHeaderName]));
rtHTTP: Result.Add('httpSettings', TJSONObject.Create([
'host', TJSONArray.Create([RemoteHostname]),
'path', RemotePath]));
rtKCP: Result.Add('kcpSettings', TJSONObject.Create([
'header', TJSONObject.Create(['type', UDPHeaderType]),
'congestion', KCPCongestionAlgorithm,
'mtu', KCPMTU,
'tti', KCPTTI,
'readBufferSize', KCPReadBufferSize,
'writeBufferSize', KCPWriteBufferSize,
'uplinkCapacity', KCPUplinkCapacity,
'downlinkCapacity', KCPDownlinkCapacity]));
rtQUIC: Result.Add('quicSettings', TJSONObject.Create([
'security', QUICSecurity,
'key', QUICKey,
'header', TJSONObject.Create(['type', UDPHeaderType])]));
end;
end;
function TV2rayJsonConfig.ToJSON: TJSONObject;
var
InboundList: TJSONArray;
begin
InboundList := TJSONArray.Create;
if EnableLocalHTTPProxy then
InboundList.Add(TJSONObject.Create([
'port', LocalHTTPProxyPort,
'listen', '127.0.0.1',
'protocol', 'http',
'settings', TJSONObject.Create]));
if EnableLocalSocksProxy then
InboundList.Add(TJSONObject.Create([
'port', LocalSocksProxyPort,
'listen', '127.0.0.1',
'protocol', 'socks',
'settings', TJSONObject.Create([
'udp', EnableLocalSocksUDP])]));
Result := TJSONObject.Create([
'log', TJSONObject.Create(['loglevel', LogLevel]),
'dns', TJSONObject.Create(['servers', GenerateDNSServerJSON]),
'routing', TJSONObject.Create([
'domainStrategy', DomainStrategy,
'rules', GenerateRouteJSON]),
'inbounds', InboundList,
'outbounds', TJSONArray.Create([
GenerateProxyJSON,
TJSONObject.Create([
'tag', 'direct',
'protocol', 'freedom',
'settings', TJSONObject.Create]),
TJSONObject.Create([
'tag', 'deny',
'protocol', 'blackhole',
'settings', TJSONObject.Create])])]);
end;
end.