3xui-api-client 2.0.0 → 2.1.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,686 +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
- };
157
- return this;
158
- }
159
-
160
- /**
161
- * Configure WebSocket transport
162
- * @param {Object} options - WebSocket options
163
- * @returns {this} Builder instance for chaining
164
- */
165
- websocket(options = {}) {
166
- this.network('ws');
167
- this.config.streamSettings.wsSettings = {
168
- path: options.path || '/',
169
- headers: options.headers || {}
170
- };
171
- return this;
172
- }
173
-
174
- /**
175
- * Configure HTTP/2 transport
176
- * @param {Object} options - HTTP/2 options
177
- * @returns {this} Builder instance for chaining
178
- */
179
- http2(options = {}) {
180
- this.network('h2');
181
- this.config.streamSettings.httpSettings = {
182
- path: options.path || '/',
183
- host: options.host || []
184
- };
185
- return this;
186
- }
187
-
188
- /**
189
- * Configure gRPC transport
190
- * @param {Object} options - gRPC options
191
- * @returns {this} Builder instance for chaining
192
- */
193
- grpc(options = {}) {
194
- this.network('grpc');
195
- this.config.streamSettings.grpcSettings = {
196
- serviceName: options.serviceName || ''
197
- };
198
- return this;
199
- }
200
-
201
- /**
202
- * Set XTLS flow control
203
- * @param {string} flow - Flow type (xtls-rprx-vision, etc.)
204
- * @returns {this} Builder instance for chaining
205
- */
206
- flow(flow) {
207
- if (this.config.settings.clients.length > 0) {
208
- this.config.settings.clients.forEach(client => {
209
- client.flow = flow;
210
- });
211
- }
212
- return this;
213
- }
214
-
215
- /**
216
- * Build and return the configuration with JSON stringified settings
217
- * @returns {Object} Built configuration for 3x-ui API
218
- */
219
- build() {
220
- const config = { ...this.config };
221
- // 3x-ui API expects settings and streamSettings as JSON strings
222
- if (config.settings) {
223
- config.settings = JSON.stringify(config.settings);
224
- }
225
- if (config.streamSettings) {
226
- config.streamSettings = JSON.stringify(config.streamSettings);
227
- }
228
- return config;
229
- }
230
- }
231
-
232
- /**
233
- * VMess Protocol Builder
234
- */
235
- class VMESSBuilder extends BaseBuilder {
236
- constructor() {
237
- super();
238
- this.config = {
239
- protocol: 'vmess',
240
- settings: {
241
- clients: []
242
- },
243
- streamSettings: {
244
- network: 'tcp',
245
- security: 'none'
246
- }
247
- };
248
- }
249
-
250
- /**
251
- * Add client with automatic credential generation
252
- * @param {Object} options - Client options
253
- * @returns {this} Builder instance for chaining
254
- */
255
- addClient(options = {}) {
256
- const credentials = CredentialGenerator.generateForProtocol('vmess', options);
257
- this.config.settings.clients.push({
258
- ...credentials,
259
- limitIp: options.limitIp || 0,
260
- totalGB: options.totalGB || 0,
261
- expiryTime: options.expiryTime || 0,
262
- enable: options.enable !== false
263
- });
264
- return this;
265
- }
266
-
267
- /**
268
- * Set network type (tcp, ws, h2, grpc)
269
- * @param {string} network - Network type
270
- * @returns {this} Builder instance for chaining
271
- */
272
- network(network) {
273
- this.config.streamSettings.network = network;
274
- return this;
275
- }
276
-
277
- /**
278
- * Configure TLS security
279
- * @param {Object} options - TLS options
280
- * @returns {this} Builder instance for chaining
281
- */
282
- tls(options = {}) {
283
- this.config.streamSettings.security = 'tls';
284
- this.config.streamSettings.tlsSettings = {
285
- serverName: options.serverName || '',
286
- certificates: options.certificates || [{
287
- certificateFile: options.certFile || '',
288
- keyFile: options.keyFile || ''
289
- }]
290
- };
291
- return this;
292
- }
293
-
294
- /**
295
- * Configure WebSocket transport
296
- * @param {Object} options - WebSocket options
297
- * @returns {this} Builder instance for chaining
298
- */
299
- websocket(options = {}) {
300
- this.network('ws');
301
- this.config.streamSettings.wsSettings = {
302
- path: options.path || '/',
303
- headers: options.headers || {}
304
- };
305
- return this;
306
- }
307
-
308
- /**
309
- * Build and return the configuration with JSON stringified settings
310
- * @returns {Object} Built configuration for 3x-ui API
311
- */
312
- build() {
313
- const config = { ...this.config };
314
- // 3x-ui API expects settings and streamSettings as JSON strings
315
- if (config.settings) {
316
- config.settings = JSON.stringify(config.settings);
317
- }
318
- if (config.streamSettings) {
319
- config.streamSettings = JSON.stringify(config.streamSettings);
320
- }
321
- return config;
322
- }
323
- }
324
-
325
- /**
326
- * Trojan Protocol Builder
327
- */
328
- class TrojanBuilder extends BaseBuilder {
329
- constructor() {
330
- super();
331
- this.config = {
332
- protocol: 'trojan',
333
- settings: {
334
- clients: [],
335
- fallbacks: []
336
- },
337
- streamSettings: {
338
- network: 'tcp',
339
- security: 'tls'
340
- }
341
- };
342
- }
343
-
344
- /**
345
- * Add client with automatic credential generation
346
- * @param {Object} options - Client options
347
- * @returns {this} Builder instance for chaining
348
- */
349
- addClient(options = {}) {
350
- const credentials = CredentialGenerator.generateForProtocol('trojan', options);
351
- this.config.settings.clients.push({
352
- ...credentials,
353
- limitIp: options.limitIp || 0,
354
- totalGB: options.totalGB || 0,
355
- expiryTime: options.expiryTime || 0,
356
- enable: options.enable !== false
357
- });
358
- return this;
359
- }
360
-
361
- /**
362
- * Configure TLS security (required for Trojan)
363
- * @param {Object} options - TLS options
364
- * @returns {this} Builder instance for chaining
365
- */
366
- tls(options = {}) {
367
- this.config.streamSettings.security = 'tls';
368
- this.config.streamSettings.tlsSettings = {
369
- serverName: options.serverName || '',
370
- certificates: options.certificates || [{
371
- certificateFile: options.certFile || '',
372
- keyFile: options.keyFile || ''
373
- }]
374
- };
375
- return this;
376
- }
377
-
378
- /**
379
- * Configure fallback destinations
380
- * @param {Array} fallbacks - Fallback configurations
381
- * @returns {this} Builder instance for chaining
382
- */
383
- fallbacks(fallbacks) {
384
- this.config.settings.fallbacks = fallbacks;
385
- return this;
386
- }
387
-
388
- /**
389
- * Build and return the configuration with JSON stringified settings
390
- * @returns {Object} Built configuration for 3x-ui API
391
- */
392
- build() {
393
- const config = { ...this.config };
394
- // 3x-ui API expects settings and streamSettings as JSON strings
395
- if (config.settings) {
396
- config.settings = JSON.stringify(config.settings);
397
- }
398
- if (config.streamSettings) {
399
- config.streamSettings = JSON.stringify(config.streamSettings);
400
- }
401
- return config;
402
- }
403
- }
404
-
405
- /**
406
- * Shadowsocks Protocol Builder
407
- */
408
- class ShadowsocksBuilder extends BaseBuilder {
409
- constructor() {
410
- super();
411
- this.config = {
412
- protocol: 'shadowsocks',
413
- settings: {
414
- method: CredentialGenerator.getRecommendedShadowsocksCipher(),
415
- password: '',
416
- network: 'tcp,udp'
417
- }
418
- };
419
- }
420
-
421
- /**
422
- * Set encryption method
423
- * @param {string} method - Cipher method
424
- * @returns {this} Builder instance for chaining
425
- */
426
- method(method) {
427
- this.config.settings.method = method;
428
- return this;
429
- }
430
-
431
- /**
432
- * Set password or generate automatically
433
- * @param {string} password - Password (optional)
434
- * @returns {this} Builder instance for chaining
435
- */
436
- password(password) {
437
- this.config.settings.password = password || CredentialGenerator.generatePassword(16);
438
- return this;
439
- }
440
-
441
- /**
442
- * Generate password automatically
443
- * @param {number} length - Password length
444
- * @returns {this} Builder instance for chaining
445
- */
446
- generatePassword(length = 16) {
447
- this.config.settings.password = CredentialGenerator.generatePassword(length);
448
- return this;
449
- }
450
-
451
- /**
452
- * Set supported networks
453
- * @param {string} network - Supported networks (tcp, udp, tcp,udp)
454
- * @returns {this} Builder instance for chaining
455
- */
456
- network(network) {
457
- this.config.settings.network = network;
458
- return this;
459
- }
460
-
461
- /**
462
- * Build and return the configuration with JSON stringified settings
463
- * @returns {Object} Built configuration for 3x-ui API
464
- */
465
- build() {
466
- const config = { ...this.config };
467
- // 3x-ui API expects settings as JSON string
468
- if (config.settings) {
469
- config.settings = JSON.stringify(config.settings);
470
- }
471
- // Shadowsocks typically doesn't use streamSettings, but add for completeness
472
- if (config.streamSettings) {
473
- config.streamSettings = JSON.stringify(config.streamSettings);
474
- }
475
- return config;
476
- }
477
- }
478
-
479
- /**
480
- * WireGuard Protocol Builder
481
- */
482
- class WireGuardBuilder extends BaseBuilder {
483
- constructor() {
484
- super();
485
- this.config = {
486
- protocol: 'wireguard',
487
- settings: {
488
- secretKey: '',
489
- address: ['10.0.0.1/24'],
490
- peers: [],
491
- mtu: 1420
492
- }
493
- };
494
- }
495
-
496
- /**
497
- * Generate or set server keys
498
- * @param {Object} keys - Key pair (optional)
499
- * @returns {this} Builder instance for chaining
500
- */
501
- serverKeys(keys) {
502
- const keyPair = keys || CredentialGenerator.generateWireGuardKeys();
503
- this.config.settings.secretKey = keyPair.privateKey;
504
- return this;
505
- }
506
-
507
- /**
508
- * Set server address
509
- * @param {Array} addresses - Server addresses
510
- * @returns {this} Builder instance for chaining
511
- */
512
- address(addresses) {
513
- this.config.settings.address = Array.isArray(addresses) ? addresses : [addresses];
514
- return this;
515
- }
516
-
517
- /**
518
- * Add peer with automatic key generation
519
- * @param {Object} options - Peer options
520
- * @returns {this} Builder instance for chaining
521
- */
522
- addPeer(options = {}) {
523
- const keys = options.keys || CredentialGenerator.generateWireGuardKeys();
524
- this.config.settings.peers.push({
525
- publicKey: keys.publicKey,
526
- allowedIPs: options.allowedIPs || ['10.0.0.2/32'],
527
- keepAlive: options.keepAlive || 25
528
- });
529
- return this;
530
- }
531
-
532
- /**
533
- * Set MTU size
534
- * @param {number} mtu - MTU size
535
- * @returns {this} Builder instance for chaining
536
- */
537
- mtu(mtu) {
538
- this.config.settings.mtu = mtu;
539
- return this;
540
- }
541
-
542
- /**
543
- * Build and return the configuration with JSON stringified settings
544
- * @returns {Object} Built configuration for 3x-ui API
545
- */
546
- build() {
547
- const config = { ...this.config };
548
- // 3x-ui API expects settings as JSON string
549
- if (config.settings) {
550
- config.settings = JSON.stringify(config.settings);
551
- }
552
- // WireGuard typically doesn't use streamSettings, but add for completeness
553
- if (config.streamSettings) {
554
- config.streamSettings = JSON.stringify(config.streamSettings);
555
- }
556
- return config;
557
- }
558
- }
559
-
560
- /**
561
- * Protocol Builder Factory
562
- */
563
- class ProtocolBuilder {
564
- /**
565
- * Create VLESS protocol builder
566
- * @returns {VLESSBuilder} VLESS builder instance
567
- */
568
- static vless() {
569
- return new VLESSBuilder();
570
- }
571
-
572
- /**
573
- * Create VMess protocol builder
574
- * @returns {VMESSBuilder} VMess builder instance
575
- */
576
- static vmess() {
577
- return new VMESSBuilder();
578
- }
579
-
580
- /**
581
- * Create Trojan protocol builder
582
- * @returns {TrojanBuilder} Trojan builder instance
583
- */
584
- static trojan() {
585
- return new TrojanBuilder();
586
- }
587
-
588
- /**
589
- * Create Shadowsocks protocol builder
590
- * @returns {ShadowsocksBuilder} Shadowsocks builder instance
591
- */
592
- static shadowsocks() {
593
- return new ShadowsocksBuilder();
594
- }
595
-
596
- /**
597
- * Create WireGuard protocol builder
598
- * @returns {WireGuardBuilder} WireGuard builder instance
599
- */
600
- static wireguard() {
601
- return new WireGuardBuilder();
602
- }
603
- }
604
-
605
- /**
606
- * Quick inbound configuration templates
607
- */
608
- ProtocolBuilder.templates = {
609
- /**
610
- * VLESS with Reality (recommended for anti-censorship)
611
- * @param {Object} options - Template options
612
- * @returns {Object} Built configuration
613
- */
614
- vlessReality(options = {}) {
615
- return ProtocolBuilder.vless()
616
- .remark(options.remark || 'VLESS-Reality')
617
- .randomPort()
618
- .reality({
619
- dest: options.dest || 'google.com:443',
620
- serverNames: options.serverNames || ['google.com']
621
- })
622
- .addClient(options.client || {})
623
- .build();
624
- },
625
-
626
- /**
627
- * VMess with WebSocket + TLS (web-compatible)
628
- * @param {Object} options - Template options
629
- * @returns {Object} Built configuration
630
- */
631
- vmessWsTls(options = {}) {
632
- return ProtocolBuilder.vmess()
633
- .remark(options.remark || 'VMess-WS-TLS')
634
- .port(options.port || 443)
635
- .websocket({ path: options.path || '/ws' })
636
- .tls({
637
- serverName: options.serverName || '',
638
- certFile: options.certFile || '',
639
- keyFile: options.keyFile || ''
640
- })
641
- .addClient(options.client || {})
642
- .build();
643
- },
644
-
645
- /**
646
- * Trojan with TLS (simple and effective)
647
- * @param {Object} options - Template options
648
- * @returns {Object} Built configuration
649
- */
650
- trojanTls(options = {}) {
651
- return ProtocolBuilder.trojan()
652
- .remark(options.remark || 'Trojan-TLS')
653
- .port(options.port || 443)
654
- .tls({
655
- serverName: options.serverName || '',
656
- certFile: options.certFile || '',
657
- keyFile: options.keyFile || ''
658
- })
659
- .addClient(options.client || {})
660
- .build();
661
- },
662
-
663
- /**
664
- * Shadowsocks with recommended cipher
665
- * @param {Object} options - Template options
666
- * @returns {Object} Built configuration
667
- */
668
- shadowsocks(options = {}) {
669
- return ProtocolBuilder.shadowsocks()
670
- .remark(options.remark || 'Shadowsocks')
671
- .randomPort()
672
- .method(options.method || CredentialGenerator.getRecommendedShadowsocksCipher())
673
- .generatePassword(options.passwordLength || 16)
674
- .build();
675
- }
676
- };
677
-
678
- module.exports = {
679
- ProtocolBuilder,
680
- VLESSBuilder,
681
- VMESSBuilder,
682
- TrojanBuilder,
683
- ShadowsocksBuilder,
684
- WireGuardBuilder,
685
- 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
686
690
  };