3xui-api-client 2.1.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,690 +1,690 @@
1
- /**
2
- * Protocol Builders for easier 3x-ui configuration
3
- * Provides fluent API for building inbound and client configurations
4
- */
5
-
6
- const CredentialGenerator = require('../generators/CredentialGenerator');
7
-
8
- /**
9
- * Base builder class with common functionality
10
- */
11
- class BaseBuilder {
12
- constructor() {
13
- this.config = {};
14
- }
15
-
16
- /**
17
- * Set remark/name for the configuration
18
- * @param {string} remark - Configuration name
19
- * @returns {this} Builder instance for chaining
20
- */
21
- remark(remark) {
22
- this.config.remark = remark;
23
- return this;
24
- }
25
-
26
- /**
27
- * Set port number
28
- * @param {number} port - Port number
29
- * @returns {this} Builder instance for chaining
30
- */
31
- port(port) {
32
- this.config.port = port;
33
- return this;
34
- }
35
-
36
- /**
37
- * Generate random port
38
- * @param {number} min - Minimum port (default: 10000)
39
- * @param {number} max - Maximum port (default: 65535)
40
- * @returns {this} Builder instance for chaining
41
- */
42
- randomPort(min = 10000, max = 65535) {
43
- this.config.port = CredentialGenerator.generatePort(min, max);
44
- return this;
45
- }
46
-
47
- /**
48
- * Set listen address
49
- * @param {string} listen - Listen address (default: '0.0.0.0')
50
- * @returns {this} Builder instance for chaining
51
- */
52
- listen(listen = '0.0.0.0') {
53
- this.config.listen = listen;
54
- return this;
55
- }
56
-
57
- /**
58
- * Enable/disable configuration
59
- * @param {boolean} enabled - Enable status (default: true)
60
- * @returns {this} Builder instance for chaining
61
- */
62
- enable(enabled = true) {
63
- this.config.enable = enabled;
64
- return this;
65
- }
66
-
67
- /**
68
- * Build and return the configuration
69
- * @returns {Object} Built configuration
70
- */
71
- build() {
72
- return { ...this.config };
73
- }
74
- }
75
-
76
- /**
77
- * VLESS Protocol Builder
78
- */
79
- class VLESSBuilder extends BaseBuilder {
80
- constructor() {
81
- super();
82
- this.config = {
83
- protocol: 'vless',
84
- settings: {
85
- clients: [],
86
- decryption: 'none',
87
- fallbacks: []
88
- },
89
- streamSettings: {
90
- network: 'tcp',
91
- security: 'none'
92
- }
93
- };
94
- }
95
-
96
- /**
97
- * Add client with automatic credential generation
98
- * @param {Object} options - Client options
99
- * @returns {this} Builder instance for chaining
100
- */
101
- addClient(options = {}) {
102
- const credentials = CredentialGenerator.generateForProtocol('vless', options);
103
- this.config.settings.clients.push({
104
- ...credentials,
105
- limitIp: options.limitIp || 0,
106
- totalGB: options.totalGB || 0,
107
- expiryTime: options.expiryTime || 0,
108
- enable: options.enable !== false,
109
- subId: options.subId || CredentialGenerator.generateSecureUUID()
110
- });
111
- return this;
112
- }
113
-
114
- /**
115
- * Set network type (tcp, ws, h2, grpc)
116
- * @param {string} network - Network type
117
- * @returns {this} Builder instance for chaining
118
- */
119
- network(network) {
120
- this.config.streamSettings.network = network;
121
- return this;
122
- }
123
-
124
- /**
125
- * Configure TLS security
126
- * @param {Object} options - TLS options
127
- * @returns {this} Builder instance for chaining
128
- */
129
- tls(options = {}) {
130
- this.config.streamSettings.security = 'tls';
131
- this.config.streamSettings.tlsSettings = {
132
- serverName: options.serverName || '',
133
- certificates: options.certificates || [{
134
- certificateFile: options.certFile || '',
135
- keyFile: options.keyFile || ''
136
- }]
137
- };
138
- return this;
139
- }
140
-
141
- /**
142
- * Configure Reality security (anti-censorship)
143
- * @param {Object} options - Reality options
144
- * @returns {this} Builder instance for chaining
145
- */
146
- reality(options = {}) {
147
- const keys = options.keys || CredentialGenerator.generateRealityKeys();
148
- this.config.streamSettings.security = 'reality';
149
- this.config.streamSettings.realitySettings = {
150
- show: false,
151
- dest: options.dest || 'google.com:443',
152
- xver: 0,
153
- serverNames: options.serverNames || ['google.com'],
154
- privateKey: keys.privateKey,
155
- shortIds: options.shortIds || [''],
156
- settings: {
157
- publicKey: keys.publicKey,
158
- fingerprint: options.fingerprint || 'chrome'
159
- }
160
- };
161
- return this;
162
- }
163
-
164
- /**
165
- * Configure WebSocket transport
166
- * @param {Object} options - WebSocket options
167
- * @returns {this} Builder instance for chaining
168
- */
169
- websocket(options = {}) {
170
- this.network('ws');
171
- this.config.streamSettings.wsSettings = {
172
- path: options.path || '/',
173
- headers: options.headers || {}
174
- };
175
- return this;
176
- }
177
-
178
- /**
179
- * Configure HTTP/2 transport
180
- * @param {Object} options - HTTP/2 options
181
- * @returns {this} Builder instance for chaining
182
- */
183
- http2(options = {}) {
184
- this.network('h2');
185
- this.config.streamSettings.httpSettings = {
186
- path: options.path || '/',
187
- host: options.host || []
188
- };
189
- return this;
190
- }
191
-
192
- /**
193
- * Configure gRPC transport
194
- * @param {Object} options - gRPC options
195
- * @returns {this} Builder instance for chaining
196
- */
197
- grpc(options = {}) {
198
- this.network('grpc');
199
- this.config.streamSettings.grpcSettings = {
200
- serviceName: options.serviceName || ''
201
- };
202
- return this;
203
- }
204
-
205
- /**
206
- * Set XTLS flow control
207
- * @param {string} flow - Flow type (xtls-rprx-vision, etc.)
208
- * @returns {this} Builder instance for chaining
209
- */
210
- flow(flow) {
211
- if (this.config.settings.clients.length > 0) {
212
- this.config.settings.clients.forEach(client => {
213
- client.flow = flow;
214
- });
215
- }
216
- return this;
217
- }
218
-
219
- /**
220
- * Build and return the configuration with JSON stringified settings
221
- * @returns {Object} Built configuration for 3x-ui API
222
- */
223
- build() {
224
- const config = { ...this.config };
225
- // 3x-ui API expects settings and streamSettings as JSON strings
226
- if (config.settings) {
227
- config.settings = JSON.stringify(config.settings);
228
- }
229
- if (config.streamSettings) {
230
- config.streamSettings = JSON.stringify(config.streamSettings);
231
- }
232
- return config;
233
- }
234
- }
235
-
236
- /**
237
- * VMess Protocol Builder
238
- */
239
- class VMESSBuilder extends BaseBuilder {
240
- constructor() {
241
- super();
242
- this.config = {
243
- protocol: 'vmess',
244
- settings: {
245
- clients: []
246
- },
247
- streamSettings: {
248
- network: 'tcp',
249
- security: 'none'
250
- }
251
- };
252
- }
253
-
254
- /**
255
- * Add client with automatic credential generation
256
- * @param {Object} options - Client options
257
- * @returns {this} Builder instance for chaining
258
- */
259
- addClient(options = {}) {
260
- const credentials = CredentialGenerator.generateForProtocol('vmess', options);
261
- this.config.settings.clients.push({
262
- ...credentials,
263
- limitIp: options.limitIp || 0,
264
- totalGB: options.totalGB || 0,
265
- expiryTime: options.expiryTime || 0,
266
- enable: options.enable !== false
267
- });
268
- return this;
269
- }
270
-
271
- /**
272
- * Set network type (tcp, ws, h2, grpc)
273
- * @param {string} network - Network type
274
- * @returns {this} Builder instance for chaining
275
- */
276
- network(network) {
277
- this.config.streamSettings.network = network;
278
- return this;
279
- }
280
-
281
- /**
282
- * Configure TLS security
283
- * @param {Object} options - TLS options
284
- * @returns {this} Builder instance for chaining
285
- */
286
- tls(options = {}) {
287
- this.config.streamSettings.security = 'tls';
288
- this.config.streamSettings.tlsSettings = {
289
- serverName: options.serverName || '',
290
- certificates: options.certificates || [{
291
- certificateFile: options.certFile || '',
292
- keyFile: options.keyFile || ''
293
- }]
294
- };
295
- return this;
296
- }
297
-
298
- /**
299
- * Configure WebSocket transport
300
- * @param {Object} options - WebSocket options
301
- * @returns {this} Builder instance for chaining
302
- */
303
- websocket(options = {}) {
304
- this.network('ws');
305
- this.config.streamSettings.wsSettings = {
306
- path: options.path || '/',
307
- headers: options.headers || {}
308
- };
309
- return this;
310
- }
311
-
312
- /**
313
- * Build and return the configuration with JSON stringified settings
314
- * @returns {Object} Built configuration for 3x-ui API
315
- */
316
- build() {
317
- const config = { ...this.config };
318
- // 3x-ui API expects settings and streamSettings as JSON strings
319
- if (config.settings) {
320
- config.settings = JSON.stringify(config.settings);
321
- }
322
- if (config.streamSettings) {
323
- config.streamSettings = JSON.stringify(config.streamSettings);
324
- }
325
- return config;
326
- }
327
- }
328
-
329
- /**
330
- * Trojan Protocol Builder
331
- */
332
- class TrojanBuilder extends BaseBuilder {
333
- constructor() {
334
- super();
335
- this.config = {
336
- protocol: 'trojan',
337
- settings: {
338
- clients: [],
339
- fallbacks: []
340
- },
341
- streamSettings: {
342
- network: 'tcp',
343
- security: 'tls'
344
- }
345
- };
346
- }
347
-
348
- /**
349
- * Add client with automatic credential generation
350
- * @param {Object} options - Client options
351
- * @returns {this} Builder instance for chaining
352
- */
353
- addClient(options = {}) {
354
- const credentials = CredentialGenerator.generateForProtocol('trojan', options);
355
- this.config.settings.clients.push({
356
- ...credentials,
357
- limitIp: options.limitIp || 0,
358
- totalGB: options.totalGB || 0,
359
- expiryTime: options.expiryTime || 0,
360
- enable: options.enable !== false
361
- });
362
- return this;
363
- }
364
-
365
- /**
366
- * Configure TLS security (required for Trojan)
367
- * @param {Object} options - TLS options
368
- * @returns {this} Builder instance for chaining
369
- */
370
- tls(options = {}) {
371
- this.config.streamSettings.security = 'tls';
372
- this.config.streamSettings.tlsSettings = {
373
- serverName: options.serverName || '',
374
- certificates: options.certificates || [{
375
- certificateFile: options.certFile || '',
376
- keyFile: options.keyFile || ''
377
- }]
378
- };
379
- return this;
380
- }
381
-
382
- /**
383
- * Configure fallback destinations
384
- * @param {Array} fallbacks - Fallback configurations
385
- * @returns {this} Builder instance for chaining
386
- */
387
- fallbacks(fallbacks) {
388
- this.config.settings.fallbacks = fallbacks;
389
- return this;
390
- }
391
-
392
- /**
393
- * Build and return the configuration with JSON stringified settings
394
- * @returns {Object} Built configuration for 3x-ui API
395
- */
396
- build() {
397
- const config = { ...this.config };
398
- // 3x-ui API expects settings and streamSettings as JSON strings
399
- if (config.settings) {
400
- config.settings = JSON.stringify(config.settings);
401
- }
402
- if (config.streamSettings) {
403
- config.streamSettings = JSON.stringify(config.streamSettings);
404
- }
405
- return config;
406
- }
407
- }
408
-
409
- /**
410
- * Shadowsocks Protocol Builder
411
- */
412
- class ShadowsocksBuilder extends BaseBuilder {
413
- constructor() {
414
- super();
415
- this.config = {
416
- protocol: 'shadowsocks',
417
- settings: {
418
- method: CredentialGenerator.getRecommendedShadowsocksCipher(),
419
- password: '',
420
- network: 'tcp,udp'
421
- }
422
- };
423
- }
424
-
425
- /**
426
- * Set encryption method
427
- * @param {string} method - Cipher method
428
- * @returns {this} Builder instance for chaining
429
- */
430
- method(method) {
431
- this.config.settings.method = method;
432
- return this;
433
- }
434
-
435
- /**
436
- * Set password or generate automatically
437
- * @param {string} password - Password (optional)
438
- * @returns {this} Builder instance for chaining
439
- */
440
- password(password) {
441
- this.config.settings.password = password || CredentialGenerator.generatePassword(16);
442
- return this;
443
- }
444
-
445
- /**
446
- * Generate password automatically
447
- * @param {number} length - Password length
448
- * @returns {this} Builder instance for chaining
449
- */
450
- generatePassword(length = 16) {
451
- this.config.settings.password = CredentialGenerator.generatePassword(length);
452
- return this;
453
- }
454
-
455
- /**
456
- * Set supported networks
457
- * @param {string} network - Supported networks (tcp, udp, tcp,udp)
458
- * @returns {this} Builder instance for chaining
459
- */
460
- network(network) {
461
- this.config.settings.network = network;
462
- return this;
463
- }
464
-
465
- /**
466
- * Build and return the configuration with JSON stringified settings
467
- * @returns {Object} Built configuration for 3x-ui API
468
- */
469
- build() {
470
- const config = { ...this.config };
471
- // 3x-ui API expects settings as JSON string
472
- if (config.settings) {
473
- config.settings = JSON.stringify(config.settings);
474
- }
475
- // Shadowsocks typically doesn't use streamSettings, but add for completeness
476
- if (config.streamSettings) {
477
- config.streamSettings = JSON.stringify(config.streamSettings);
478
- }
479
- return config;
480
- }
481
- }
482
-
483
- /**
484
- * WireGuard Protocol Builder
485
- */
486
- class WireGuardBuilder extends BaseBuilder {
487
- constructor() {
488
- super();
489
- this.config = {
490
- protocol: 'wireguard',
491
- settings: {
492
- secretKey: '',
493
- address: ['10.0.0.1/24'],
494
- peers: [],
495
- mtu: 1420
496
- }
497
- };
498
- }
499
-
500
- /**
501
- * Generate or set server keys
502
- * @param {Object} keys - Key pair (optional)
503
- * @returns {this} Builder instance for chaining
504
- */
505
- serverKeys(keys) {
506
- const keyPair = keys || CredentialGenerator.generateWireGuardKeys();
507
- this.config.settings.secretKey = keyPair.privateKey;
508
- return this;
509
- }
510
-
511
- /**
512
- * Set server address
513
- * @param {Array} addresses - Server addresses
514
- * @returns {this} Builder instance for chaining
515
- */
516
- address(addresses) {
517
- this.config.settings.address = Array.isArray(addresses) ? addresses : [addresses];
518
- return this;
519
- }
520
-
521
- /**
522
- * Add peer with automatic key generation
523
- * @param {Object} options - Peer options
524
- * @returns {this} Builder instance for chaining
525
- */
526
- addPeer(options = {}) {
527
- const keys = options.keys || CredentialGenerator.generateWireGuardKeys();
528
- this.config.settings.peers.push({
529
- publicKey: keys.publicKey,
530
- allowedIPs: options.allowedIPs || ['10.0.0.2/32'],
531
- keepAlive: options.keepAlive || 25
532
- });
533
- return this;
534
- }
535
-
536
- /**
537
- * Set MTU size
538
- * @param {number} mtu - MTU size
539
- * @returns {this} Builder instance for chaining
540
- */
541
- mtu(mtu) {
542
- this.config.settings.mtu = mtu;
543
- return this;
544
- }
545
-
546
- /**
547
- * Build and return the configuration with JSON stringified settings
548
- * @returns {Object} Built configuration for 3x-ui API
549
- */
550
- build() {
551
- const config = { ...this.config };
552
- // 3x-ui API expects settings as JSON string
553
- if (config.settings) {
554
- config.settings = JSON.stringify(config.settings);
555
- }
556
- // WireGuard typically doesn't use streamSettings, but add for completeness
557
- if (config.streamSettings) {
558
- config.streamSettings = JSON.stringify(config.streamSettings);
559
- }
560
- return config;
561
- }
562
- }
563
-
564
- /**
565
- * Protocol Builder Factory
566
- */
567
- class ProtocolBuilder {
568
- /**
569
- * Create VLESS protocol builder
570
- * @returns {VLESSBuilder} VLESS builder instance
571
- */
572
- static vless() {
573
- return new VLESSBuilder();
574
- }
575
-
576
- /**
577
- * Create VMess protocol builder
578
- * @returns {VMESSBuilder} VMess builder instance
579
- */
580
- static vmess() {
581
- return new VMESSBuilder();
582
- }
583
-
584
- /**
585
- * Create Trojan protocol builder
586
- * @returns {TrojanBuilder} Trojan builder instance
587
- */
588
- static trojan() {
589
- return new TrojanBuilder();
590
- }
591
-
592
- /**
593
- * Create Shadowsocks protocol builder
594
- * @returns {ShadowsocksBuilder} Shadowsocks builder instance
595
- */
596
- static shadowsocks() {
597
- return new ShadowsocksBuilder();
598
- }
599
-
600
- /**
601
- * Create WireGuard protocol builder
602
- * @returns {WireGuardBuilder} WireGuard builder instance
603
- */
604
- static wireguard() {
605
- return new WireGuardBuilder();
606
- }
607
- }
608
-
609
- /**
610
- * Quick inbound configuration templates
611
- */
612
- ProtocolBuilder.templates = {
613
- /**
614
- * VLESS with Reality (recommended for anti-censorship)
615
- * @param {Object} options - Template options
616
- * @returns {Object} Built configuration
617
- */
618
- vlessReality(options = {}) {
619
- return ProtocolBuilder.vless()
620
- .remark(options.remark || 'VLESS-Reality')
621
- .randomPort()
622
- .reality({
623
- dest: options.dest || 'google.com:443',
624
- serverNames: options.serverNames || ['google.com']
625
- })
626
- .addClient(options.client || {})
627
- .build();
628
- },
629
-
630
- /**
631
- * VMess with WebSocket + TLS (web-compatible)
632
- * @param {Object} options - Template options
633
- * @returns {Object} Built configuration
634
- */
635
- vmessWsTls(options = {}) {
636
- return ProtocolBuilder.vmess()
637
- .remark(options.remark || 'VMess-WS-TLS')
638
- .port(options.port || 443)
639
- .websocket({ path: options.path || '/ws' })
640
- .tls({
641
- serverName: options.serverName || '',
642
- certFile: options.certFile || '',
643
- keyFile: options.keyFile || ''
644
- })
645
- .addClient(options.client || {})
646
- .build();
647
- },
648
-
649
- /**
650
- * Trojan with TLS (simple and effective)
651
- * @param {Object} options - Template options
652
- * @returns {Object} Built configuration
653
- */
654
- trojanTls(options = {}) {
655
- return ProtocolBuilder.trojan()
656
- .remark(options.remark || 'Trojan-TLS')
657
- .port(options.port || 443)
658
- .tls({
659
- serverName: options.serverName || '',
660
- certFile: options.certFile || '',
661
- keyFile: options.keyFile || ''
662
- })
663
- .addClient(options.client || {})
664
- .build();
665
- },
666
-
667
- /**
668
- * Shadowsocks with recommended cipher
669
- * @param {Object} options - Template options
670
- * @returns {Object} Built configuration
671
- */
672
- shadowsocks(options = {}) {
673
- return ProtocolBuilder.shadowsocks()
674
- .remark(options.remark || 'Shadowsocks')
675
- .randomPort()
676
- .method(options.method || CredentialGenerator.getRecommendedShadowsocksCipher())
677
- .generatePassword(options.passwordLength || 16)
678
- .build();
679
- }
680
- };
681
-
682
- module.exports = {
683
- ProtocolBuilder,
684
- VLESSBuilder,
685
- VMESSBuilder,
686
- TrojanBuilder,
687
- ShadowsocksBuilder,
688
- WireGuardBuilder,
689
- BaseBuilder
1
+ /**
2
+ * Protocol Builders for easier 3x-ui configuration
3
+ * Provides fluent API for building inbound and client configurations
4
+ */
5
+
6
+ const CredentialGenerator = require('../generators/CredentialGenerator');
7
+
8
+ /**
9
+ * Base builder class with common functionality
10
+ */
11
+ class BaseBuilder {
12
+ constructor() {
13
+ this.config = {};
14
+ }
15
+
16
+ /**
17
+ * Set remark/name for the configuration
18
+ * @param {string} remark - Configuration name
19
+ * @returns {this} Builder instance for chaining
20
+ */
21
+ remark(remark) {
22
+ this.config.remark = remark;
23
+ return this;
24
+ }
25
+
26
+ /**
27
+ * Set port number
28
+ * @param {number} port - Port number
29
+ * @returns {this} Builder instance for chaining
30
+ */
31
+ port(port) {
32
+ this.config.port = port;
33
+ return this;
34
+ }
35
+
36
+ /**
37
+ * Generate random port
38
+ * @param {number} min - Minimum port (default: 10000)
39
+ * @param {number} max - Maximum port (default: 65535)
40
+ * @returns {this} Builder instance for chaining
41
+ */
42
+ randomPort(min = 10000, max = 65535) {
43
+ this.config.port = CredentialGenerator.generatePort(min, max);
44
+ return this;
45
+ }
46
+
47
+ /**
48
+ * Set listen address
49
+ * @param {string} listen - Listen address (default: '0.0.0.0')
50
+ * @returns {this} Builder instance for chaining
51
+ */
52
+ listen(listen = '0.0.0.0') {
53
+ this.config.listen = listen;
54
+ return this;
55
+ }
56
+
57
+ /**
58
+ * Enable/disable configuration
59
+ * @param {boolean} enabled - Enable status (default: true)
60
+ * @returns {this} Builder instance for chaining
61
+ */
62
+ enable(enabled = true) {
63
+ this.config.enable = enabled;
64
+ return this;
65
+ }
66
+
67
+ /**
68
+ * Build and return the configuration
69
+ * @returns {Object} Built configuration
70
+ */
71
+ build() {
72
+ return { ...this.config };
73
+ }
74
+ }
75
+
76
+ /**
77
+ * VLESS Protocol Builder
78
+ */
79
+ class VLESSBuilder extends BaseBuilder {
80
+ constructor() {
81
+ super();
82
+ this.config = {
83
+ protocol: 'vless',
84
+ settings: {
85
+ clients: [],
86
+ decryption: 'none',
87
+ fallbacks: []
88
+ },
89
+ streamSettings: {
90
+ network: 'tcp',
91
+ security: 'none'
92
+ }
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Add client with automatic credential generation
98
+ * @param {Object} options - Client options
99
+ * @returns {this} Builder instance for chaining
100
+ */
101
+ addClient(options = {}) {
102
+ const credentials = CredentialGenerator.generateForProtocol('vless', options);
103
+ this.config.settings.clients.push({
104
+ ...credentials,
105
+ limitIp: options.limitIp || 0,
106
+ totalGB: options.totalGB || 0,
107
+ expiryTime: options.expiryTime || 0,
108
+ enable: options.enable !== false,
109
+ subId: options.subId || CredentialGenerator.generateSecureUUID()
110
+ });
111
+ return this;
112
+ }
113
+
114
+ /**
115
+ * Set network type (tcp, ws, h2, grpc)
116
+ * @param {string} network - Network type
117
+ * @returns {this} Builder instance for chaining
118
+ */
119
+ network(network) {
120
+ this.config.streamSettings.network = network;
121
+ return this;
122
+ }
123
+
124
+ /**
125
+ * Configure TLS security
126
+ * @param {Object} options - TLS options
127
+ * @returns {this} Builder instance for chaining
128
+ */
129
+ tls(options = {}) {
130
+ this.config.streamSettings.security = 'tls';
131
+ this.config.streamSettings.tlsSettings = {
132
+ serverName: options.serverName || '',
133
+ certificates: options.certificates || [{
134
+ certificateFile: options.certFile || '',
135
+ keyFile: options.keyFile || ''
136
+ }]
137
+ };
138
+ return this;
139
+ }
140
+
141
+ /**
142
+ * Configure Reality security (anti-censorship)
143
+ * @param {Object} options - Reality options
144
+ * @returns {this} Builder instance for chaining
145
+ */
146
+ reality(options = {}) {
147
+ const keys = options.keys || CredentialGenerator.generateRealityKeys();
148
+ this.config.streamSettings.security = 'reality';
149
+ this.config.streamSettings.realitySettings = {
150
+ show: false,
151
+ dest: options.dest || 'google.com:443',
152
+ xver: 0,
153
+ serverNames: options.serverNames || ['google.com'],
154
+ privateKey: keys.privateKey,
155
+ shortIds: options.shortIds || [''],
156
+ settings: {
157
+ publicKey: keys.publicKey,
158
+ fingerprint: options.fingerprint || 'chrome'
159
+ }
160
+ };
161
+ return this;
162
+ }
163
+
164
+ /**
165
+ * Configure WebSocket transport
166
+ * @param {Object} options - WebSocket options
167
+ * @returns {this} Builder instance for chaining
168
+ */
169
+ websocket(options = {}) {
170
+ this.network('ws');
171
+ this.config.streamSettings.wsSettings = {
172
+ path: options.path || '/',
173
+ headers: options.headers || {}
174
+ };
175
+ return this;
176
+ }
177
+
178
+ /**
179
+ * Configure HTTP/2 transport
180
+ * @param {Object} options - HTTP/2 options
181
+ * @returns {this} Builder instance for chaining
182
+ */
183
+ http2(options = {}) {
184
+ this.network('h2');
185
+ this.config.streamSettings.httpSettings = {
186
+ path: options.path || '/',
187
+ host: options.host || []
188
+ };
189
+ return this;
190
+ }
191
+
192
+ /**
193
+ * Configure gRPC transport
194
+ * @param {Object} options - gRPC options
195
+ * @returns {this} Builder instance for chaining
196
+ */
197
+ grpc(options = {}) {
198
+ this.network('grpc');
199
+ this.config.streamSettings.grpcSettings = {
200
+ serviceName: options.serviceName || ''
201
+ };
202
+ return this;
203
+ }
204
+
205
+ /**
206
+ * Set XTLS flow control
207
+ * @param {string} flow - Flow type (xtls-rprx-vision, etc.)
208
+ * @returns {this} Builder instance for chaining
209
+ */
210
+ flow(flow) {
211
+ if (this.config.settings.clients.length > 0) {
212
+ this.config.settings.clients.forEach(client => {
213
+ client.flow = flow;
214
+ });
215
+ }
216
+ return this;
217
+ }
218
+
219
+ /**
220
+ * Build and return the configuration with JSON stringified settings
221
+ * @returns {Object} Built configuration for 3x-ui API
222
+ */
223
+ build() {
224
+ const config = { ...this.config };
225
+ // 3x-ui API expects settings and streamSettings as JSON strings
226
+ if (config.settings) {
227
+ config.settings = JSON.stringify(config.settings);
228
+ }
229
+ if (config.streamSettings) {
230
+ config.streamSettings = JSON.stringify(config.streamSettings);
231
+ }
232
+ return config;
233
+ }
234
+ }
235
+
236
+ /**
237
+ * VMess Protocol Builder
238
+ */
239
+ class VMESSBuilder extends BaseBuilder {
240
+ constructor() {
241
+ super();
242
+ this.config = {
243
+ protocol: 'vmess',
244
+ settings: {
245
+ clients: []
246
+ },
247
+ streamSettings: {
248
+ network: 'tcp',
249
+ security: 'none'
250
+ }
251
+ };
252
+ }
253
+
254
+ /**
255
+ * Add client with automatic credential generation
256
+ * @param {Object} options - Client options
257
+ * @returns {this} Builder instance for chaining
258
+ */
259
+ addClient(options = {}) {
260
+ const credentials = CredentialGenerator.generateForProtocol('vmess', options);
261
+ this.config.settings.clients.push({
262
+ ...credentials,
263
+ limitIp: options.limitIp || 0,
264
+ totalGB: options.totalGB || 0,
265
+ expiryTime: options.expiryTime || 0,
266
+ enable: options.enable !== false
267
+ });
268
+ return this;
269
+ }
270
+
271
+ /**
272
+ * Set network type (tcp, ws, h2, grpc)
273
+ * @param {string} network - Network type
274
+ * @returns {this} Builder instance for chaining
275
+ */
276
+ network(network) {
277
+ this.config.streamSettings.network = network;
278
+ return this;
279
+ }
280
+
281
+ /**
282
+ * Configure TLS security
283
+ * @param {Object} options - TLS options
284
+ * @returns {this} Builder instance for chaining
285
+ */
286
+ tls(options = {}) {
287
+ this.config.streamSettings.security = 'tls';
288
+ this.config.streamSettings.tlsSettings = {
289
+ serverName: options.serverName || '',
290
+ certificates: options.certificates || [{
291
+ certificateFile: options.certFile || '',
292
+ keyFile: options.keyFile || ''
293
+ }]
294
+ };
295
+ return this;
296
+ }
297
+
298
+ /**
299
+ * Configure WebSocket transport
300
+ * @param {Object} options - WebSocket options
301
+ * @returns {this} Builder instance for chaining
302
+ */
303
+ websocket(options = {}) {
304
+ this.network('ws');
305
+ this.config.streamSettings.wsSettings = {
306
+ path: options.path || '/',
307
+ headers: options.headers || {}
308
+ };
309
+ return this;
310
+ }
311
+
312
+ /**
313
+ * Build and return the configuration with JSON stringified settings
314
+ * @returns {Object} Built configuration for 3x-ui API
315
+ */
316
+ build() {
317
+ const config = { ...this.config };
318
+ // 3x-ui API expects settings and streamSettings as JSON strings
319
+ if (config.settings) {
320
+ config.settings = JSON.stringify(config.settings);
321
+ }
322
+ if (config.streamSettings) {
323
+ config.streamSettings = JSON.stringify(config.streamSettings);
324
+ }
325
+ return config;
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Trojan Protocol Builder
331
+ */
332
+ class TrojanBuilder extends BaseBuilder {
333
+ constructor() {
334
+ super();
335
+ this.config = {
336
+ protocol: 'trojan',
337
+ settings: {
338
+ clients: [],
339
+ fallbacks: []
340
+ },
341
+ streamSettings: {
342
+ network: 'tcp',
343
+ security: 'tls'
344
+ }
345
+ };
346
+ }
347
+
348
+ /**
349
+ * Add client with automatic credential generation
350
+ * @param {Object} options - Client options
351
+ * @returns {this} Builder instance for chaining
352
+ */
353
+ addClient(options = {}) {
354
+ const credentials = CredentialGenerator.generateForProtocol('trojan', options);
355
+ this.config.settings.clients.push({
356
+ ...credentials,
357
+ limitIp: options.limitIp || 0,
358
+ totalGB: options.totalGB || 0,
359
+ expiryTime: options.expiryTime || 0,
360
+ enable: options.enable !== false
361
+ });
362
+ return this;
363
+ }
364
+
365
+ /**
366
+ * Configure TLS security (required for Trojan)
367
+ * @param {Object} options - TLS options
368
+ * @returns {this} Builder instance for chaining
369
+ */
370
+ tls(options = {}) {
371
+ this.config.streamSettings.security = 'tls';
372
+ this.config.streamSettings.tlsSettings = {
373
+ serverName: options.serverName || '',
374
+ certificates: options.certificates || [{
375
+ certificateFile: options.certFile || '',
376
+ keyFile: options.keyFile || ''
377
+ }]
378
+ };
379
+ return this;
380
+ }
381
+
382
+ /**
383
+ * Configure fallback destinations
384
+ * @param {Array} fallbacks - Fallback configurations
385
+ * @returns {this} Builder instance for chaining
386
+ */
387
+ fallbacks(fallbacks) {
388
+ this.config.settings.fallbacks = fallbacks;
389
+ return this;
390
+ }
391
+
392
+ /**
393
+ * Build and return the configuration with JSON stringified settings
394
+ * @returns {Object} Built configuration for 3x-ui API
395
+ */
396
+ build() {
397
+ const config = { ...this.config };
398
+ // 3x-ui API expects settings and streamSettings as JSON strings
399
+ if (config.settings) {
400
+ config.settings = JSON.stringify(config.settings);
401
+ }
402
+ if (config.streamSettings) {
403
+ config.streamSettings = JSON.stringify(config.streamSettings);
404
+ }
405
+ return config;
406
+ }
407
+ }
408
+
409
+ /**
410
+ * Shadowsocks Protocol Builder
411
+ */
412
+ class ShadowsocksBuilder extends BaseBuilder {
413
+ constructor() {
414
+ super();
415
+ this.config = {
416
+ protocol: 'shadowsocks',
417
+ settings: {
418
+ method: CredentialGenerator.getRecommendedShadowsocksCipher(),
419
+ password: '',
420
+ network: 'tcp,udp'
421
+ }
422
+ };
423
+ }
424
+
425
+ /**
426
+ * Set encryption method
427
+ * @param {string} method - Cipher method
428
+ * @returns {this} Builder instance for chaining
429
+ */
430
+ method(method) {
431
+ this.config.settings.method = method;
432
+ return this;
433
+ }
434
+
435
+ /**
436
+ * Set password or generate automatically
437
+ * @param {string} password - Password (optional)
438
+ * @returns {this} Builder instance for chaining
439
+ */
440
+ password(password) {
441
+ this.config.settings.password = password || CredentialGenerator.generatePassword(16);
442
+ return this;
443
+ }
444
+
445
+ /**
446
+ * Generate password automatically
447
+ * @param {number} length - Password length
448
+ * @returns {this} Builder instance for chaining
449
+ */
450
+ generatePassword(length = 16) {
451
+ this.config.settings.password = CredentialGenerator.generatePassword(length);
452
+ return this;
453
+ }
454
+
455
+ /**
456
+ * Set supported networks
457
+ * @param {string} network - Supported networks (tcp, udp, tcp,udp)
458
+ * @returns {this} Builder instance for chaining
459
+ */
460
+ network(network) {
461
+ this.config.settings.network = network;
462
+ return this;
463
+ }
464
+
465
+ /**
466
+ * Build and return the configuration with JSON stringified settings
467
+ * @returns {Object} Built configuration for 3x-ui API
468
+ */
469
+ build() {
470
+ const config = { ...this.config };
471
+ // 3x-ui API expects settings as JSON string
472
+ if (config.settings) {
473
+ config.settings = JSON.stringify(config.settings);
474
+ }
475
+ // Shadowsocks typically doesn't use streamSettings, but add for completeness
476
+ if (config.streamSettings) {
477
+ config.streamSettings = JSON.stringify(config.streamSettings);
478
+ }
479
+ return config;
480
+ }
481
+ }
482
+
483
+ /**
484
+ * WireGuard Protocol Builder
485
+ */
486
+ class WireGuardBuilder extends BaseBuilder {
487
+ constructor() {
488
+ super();
489
+ this.config = {
490
+ protocol: 'wireguard',
491
+ settings: {
492
+ secretKey: '',
493
+ address: ['10.0.0.1/24'],
494
+ peers: [],
495
+ mtu: 1420
496
+ }
497
+ };
498
+ }
499
+
500
+ /**
501
+ * Generate or set server keys
502
+ * @param {Object} keys - Key pair (optional)
503
+ * @returns {this} Builder instance for chaining
504
+ */
505
+ serverKeys(keys) {
506
+ const keyPair = keys || CredentialGenerator.generateWireGuardKeys();
507
+ this.config.settings.secretKey = keyPair.privateKey;
508
+ return this;
509
+ }
510
+
511
+ /**
512
+ * Set server address
513
+ * @param {Array} addresses - Server addresses
514
+ * @returns {this} Builder instance for chaining
515
+ */
516
+ address(addresses) {
517
+ this.config.settings.address = Array.isArray(addresses) ? addresses : [addresses];
518
+ return this;
519
+ }
520
+
521
+ /**
522
+ * Add peer with automatic key generation
523
+ * @param {Object} options - Peer options
524
+ * @returns {this} Builder instance for chaining
525
+ */
526
+ addPeer(options = {}) {
527
+ const keys = options.keys || CredentialGenerator.generateWireGuardKeys();
528
+ this.config.settings.peers.push({
529
+ publicKey: keys.publicKey,
530
+ allowedIPs: options.allowedIPs || ['10.0.0.2/32'],
531
+ keepAlive: options.keepAlive || 25
532
+ });
533
+ return this;
534
+ }
535
+
536
+ /**
537
+ * Set MTU size
538
+ * @param {number} mtu - MTU size
539
+ * @returns {this} Builder instance for chaining
540
+ */
541
+ mtu(mtu) {
542
+ this.config.settings.mtu = mtu;
543
+ return this;
544
+ }
545
+
546
+ /**
547
+ * Build and return the configuration with JSON stringified settings
548
+ * @returns {Object} Built configuration for 3x-ui API
549
+ */
550
+ build() {
551
+ const config = { ...this.config };
552
+ // 3x-ui API expects settings as JSON string
553
+ if (config.settings) {
554
+ config.settings = JSON.stringify(config.settings);
555
+ }
556
+ // WireGuard typically doesn't use streamSettings, but add for completeness
557
+ if (config.streamSettings) {
558
+ config.streamSettings = JSON.stringify(config.streamSettings);
559
+ }
560
+ return config;
561
+ }
562
+ }
563
+
564
+ /**
565
+ * Protocol Builder Factory
566
+ */
567
+ class ProtocolBuilder {
568
+ /**
569
+ * Create VLESS protocol builder
570
+ * @returns {VLESSBuilder} VLESS builder instance
571
+ */
572
+ static vless() {
573
+ return new VLESSBuilder();
574
+ }
575
+
576
+ /**
577
+ * Create VMess protocol builder
578
+ * @returns {VMESSBuilder} VMess builder instance
579
+ */
580
+ static vmess() {
581
+ return new VMESSBuilder();
582
+ }
583
+
584
+ /**
585
+ * Create Trojan protocol builder
586
+ * @returns {TrojanBuilder} Trojan builder instance
587
+ */
588
+ static trojan() {
589
+ return new TrojanBuilder();
590
+ }
591
+
592
+ /**
593
+ * Create Shadowsocks protocol builder
594
+ * @returns {ShadowsocksBuilder} Shadowsocks builder instance
595
+ */
596
+ static shadowsocks() {
597
+ return new ShadowsocksBuilder();
598
+ }
599
+
600
+ /**
601
+ * Create WireGuard protocol builder
602
+ * @returns {WireGuardBuilder} WireGuard builder instance
603
+ */
604
+ static wireguard() {
605
+ return new WireGuardBuilder();
606
+ }
607
+ }
608
+
609
+ /**
610
+ * Quick inbound configuration templates
611
+ */
612
+ ProtocolBuilder.templates = {
613
+ /**
614
+ * VLESS with Reality (recommended for anti-censorship)
615
+ * @param {Object} options - Template options
616
+ * @returns {Object} Built configuration
617
+ */
618
+ vlessReality(options = {}) {
619
+ return ProtocolBuilder.vless()
620
+ .remark(options.remark || 'VLESS-Reality')
621
+ .randomPort()
622
+ .reality({
623
+ dest: options.dest || 'google.com:443',
624
+ serverNames: options.serverNames || ['google.com']
625
+ })
626
+ .addClient(options.client || {})
627
+ .build();
628
+ },
629
+
630
+ /**
631
+ * VMess with WebSocket + TLS (web-compatible)
632
+ * @param {Object} options - Template options
633
+ * @returns {Object} Built configuration
634
+ */
635
+ vmessWsTls(options = {}) {
636
+ return ProtocolBuilder.vmess()
637
+ .remark(options.remark || 'VMess-WS-TLS')
638
+ .port(options.port || 443)
639
+ .websocket({ path: options.path || '/ws' })
640
+ .tls({
641
+ serverName: options.serverName || '',
642
+ certFile: options.certFile || '',
643
+ keyFile: options.keyFile || ''
644
+ })
645
+ .addClient(options.client || {})
646
+ .build();
647
+ },
648
+
649
+ /**
650
+ * Trojan with TLS (simple and effective)
651
+ * @param {Object} options - Template options
652
+ * @returns {Object} Built configuration
653
+ */
654
+ trojanTls(options = {}) {
655
+ return ProtocolBuilder.trojan()
656
+ .remark(options.remark || 'Trojan-TLS')
657
+ .port(options.port || 443)
658
+ .tls({
659
+ serverName: options.serverName || '',
660
+ certFile: options.certFile || '',
661
+ keyFile: options.keyFile || ''
662
+ })
663
+ .addClient(options.client || {})
664
+ .build();
665
+ },
666
+
667
+ /**
668
+ * Shadowsocks with recommended cipher
669
+ * @param {Object} options - Template options
670
+ * @returns {Object} Built configuration
671
+ */
672
+ shadowsocks(options = {}) {
673
+ return ProtocolBuilder.shadowsocks()
674
+ .remark(options.remark || 'Shadowsocks')
675
+ .randomPort()
676
+ .method(options.method || CredentialGenerator.getRecommendedShadowsocksCipher())
677
+ .generatePassword(options.passwordLength || 16)
678
+ .build();
679
+ }
680
+ };
681
+
682
+ module.exports = {
683
+ ProtocolBuilder,
684
+ VLESSBuilder,
685
+ VMESSBuilder,
686
+ TrojanBuilder,
687
+ ShadowsocksBuilder,
688
+ WireGuardBuilder,
689
+ BaseBuilder
690
690
  };