3xui-api-client 1.0.0 → 2.0.0

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.
@@ -0,0 +1,686 @@
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
686
+ };