x3ui-api 1.0.3 → 1.0.4

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.
package/src/index.js CHANGED
@@ -1,456 +1,3 @@
1
- const axios = require('axios');
2
- const crypto = require('crypto');
1
+ const X3UIClient = require('./core/X3UIClient');
3
2
 
4
- module.exports = class X3UIClient {
5
- constructor(config) {
6
- this.parseJSONSettings = config.parseJSONSettings || true;
7
- this.client = axios.create({
8
- baseURL: config.baseURL,
9
- headers: config.token ? {
10
- 'Cookie': `lang=en-US; 3x-ui=${config.token}`,
11
- } : {
12
- 'Cookie': 'lang=en-US'
13
- }
14
- });
15
- }
16
-
17
- /**
18
- * Login to the x3ui panel
19
- * @param {string} username Panel username
20
- * @param {string} password Panel password
21
- * @returns {Promise<{success: boolean, msg: string, obj: any}>} Login response with success status and token
22
- */
23
- async login(username, password) {
24
- const formData = new URLSearchParams();
25
- formData.append('username', username);
26
- formData.append('password', password);
27
-
28
- const response = await this.client.post('/login', formData, {
29
- headers: {
30
- 'Content-Type': 'application/x-www-form-urlencoded'
31
- }
32
- });
33
-
34
- if (response.data.success) {
35
- this.client.defaults.headers.Cookie = `lang=en-US; ${response.headers.get("set-cookie")[0].split(";")[0]}`;
36
- }
37
-
38
- return response.data;
39
- }
40
-
41
- async getSystemStats() {
42
- const response = await this.client.post('/server/status');
43
- return response.data.obj;
44
- }
45
-
46
- /**
47
- * Get all inbounds
48
- * @returns {Promise<Array<{
49
- * id?: number,
50
- * up: number,
51
- * down: number,
52
- * total: number,
53
- * remark: string,
54
- * enable: boolean,
55
- * expiryTime: number | null,
56
- * listen: string,
57
- * port: number,
58
- * protocol: string,
59
- * settings: any,
60
- * streamSettings: any,
61
- * sniffing: any
62
- * }>>}
63
- */
64
- async getInbounds() {
65
- const response = await this.client.get('/panel/api/inbounds/list');
66
- let inbounds = response.data.obj;
67
- if (this.parseJSONSettings) {
68
- inbounds = inbounds.map(inbound => this.parseInbound(inbound));
69
- }
70
- return inbounds;
71
- }
72
-
73
- parseInbound(inbound) {
74
- if (this.parseJSONSettings) {
75
- inbound.settings = inbound.settings && inbound.settings.length > 0 ? JSON.parse(inbound.settings) : {};
76
- inbound.streamSettings = inbound.streamSettings && inbound.streamSettings.length > 0 ? JSON.parse(inbound.streamSettings) : {};
77
- inbound.sniffing = inbound.sniffing && inbound.sniffing.length > 0 ? JSON.parse(inbound.sniffing) : {};
78
- inbound.allocate = inbound.allocate && inbound.allocate.length > 0 ? JSON.parse(inbound.allocate) : {};
79
- }
80
- return inbound;
81
- }
82
-
83
- stringifyInbound(inbound) {
84
- inbound.settings = JSON.stringify(inbound.settings);
85
- inbound.streamSettings = JSON.stringify(inbound.streamSettings);
86
- inbound.sniffing = JSON.stringify(inbound.sniffing);
87
- inbound.allocate = JSON.stringify(inbound.allocate);
88
- return inbound;
89
- }
90
-
91
- /**
92
- * Add new inbound
93
- * @param {Object} config Inbound configuration
94
- * @param {number} config.up Upload traffic in bytes
95
- * @param {number} config.down Download traffic in bytes
96
- * @param {number} config.total Total traffic limit in bytes
97
- * @param {string} config.remark Inbound remark/name
98
- * @param {boolean} config.enable Enable status
99
- * @param {number|null} config.expiryTime Expiry timestamp
100
- * @param {string} config.listen Listen address
101
- * @param {number} config.port Port number
102
- * @param {string} config.protocol Protocol type
103
- * @param {Object} config.settings Protocol settings
104
- * @param {Object} config.streamSettings Stream settings
105
- * @param {Object} config.sniffing Sniffing settings
106
- * @param {Object} config.allocate Allocation settings
107
- * @returns {Promise<void>}
108
- */
109
- async addInbound(config) {
110
- config = this.stringifyInbound(config);
111
- const response = await this.client.post('/panel/api/inbounds/add', config);
112
- if(!response.data.success)
113
- throw new Error(response.data.msg);
114
- return this.parseInbound(response.data.obj);
115
- }
116
-
117
- /**
118
- * Update existing inbound
119
- * @param {number} id Inbound ID
120
- * @param {Object} config Partial inbound configuration
121
- * @returns {Promise<void>}
122
- */
123
- async updateInbound(id, config) {
124
- config = this.stringifyInbound(config);
125
- const response = await this.client.post(`/panel/api/inbounds/update/${id}`, config);
126
- if(!response.data.success)
127
- throw new Error(response.data.msg);
128
- return this.parseInbound(response.data.obj);
129
- }
130
-
131
- /**
132
- * Delete inbound
133
- * @param {number} id Inbound ID
134
- * @returns {Promise<void>}
135
- */
136
- async deleteInbound(id) {
137
- await this.client.post(`/panel/api/inbounds/del/${id}`);
138
- }
139
-
140
- /**
141
- * Get new X25519 certificate
142
- * @returns {Promise<{privateKey: string, publicKey: string}>} New X25519 key pair
143
- */
144
- async getNewX25519Cert() {
145
- const response = await this.client.post('/server/getNewX25519Cert');
146
- return response.data.obj;
147
- }
148
-
149
- /**
150
- * Create a new Reality inbound builder
151
- * @param {Partial<import('./index').RealityInboundOptions>} options Initial options
152
- * @returns {import('./index').RealityBuilder} Reality builder instance
153
- */
154
- createRealityBuilder(options = {}) {
155
- return new RealityBuilder(this, options);
156
- }
157
- }
158
-
159
- class ClientBuilder {
160
- constructor(parent) {
161
- this.parent = parent;
162
- this.id = '';
163
- this.flow = '';
164
- this.email = '';
165
- this.limitIp = 0;
166
- this.totalGB = 0;
167
- this.expiryTime = 0;
168
- this.enable = true;
169
- this.tgId = '';
170
- this.subId = '';
171
- this.reset = 0;
172
- }
173
-
174
- setId(id) {
175
- this.id = id;
176
- return this;
177
- }
178
-
179
- setEmail(email) {
180
- this.email = email;
181
- return this;
182
- }
183
-
184
- setTotalGB(gb) {
185
- this.totalGB = gb;
186
- return this;
187
- }
188
-
189
- setExpiryTime(timestamp) {
190
- this.expiryTime = timestamp;
191
- return this;
192
- }
193
-
194
- setTgId(id) {
195
- this.tgId = id;
196
- return this;
197
- }
198
-
199
- build() {
200
- return {
201
- id: this.id || crypto.randomUUID(),
202
- flow: this.flow,
203
- email: this.email || Math.random().toString(36).substring(2, 10),
204
- limitIp: this.limitIp,
205
- totalGB: this.totalGB,
206
- expiryTime: this.expiryTime,
207
- enable: this.enable,
208
- tgId: this.tgId,
209
- subId: this.subId || Math.random().toString(36).substring(2, 18),
210
- reset: this.reset
211
- };
212
- }
213
-
214
- getLink(host, port, protocol) {
215
- const id = this.id || crypto.randomUUID();
216
- const settings = this.parent.streamSettings?.realitySettings;
217
- if (!settings) throw new Error('Reality settings not found');
218
-
219
- port = port || this.parent.port;
220
- protocol = protocol || this.parent.protocol || 'vless';
221
-
222
- const params = new URLSearchParams({
223
- security: this.parent.streamSettings?.security || 'reality',
224
- sni: settings.serverNames[0],
225
- fp: settings.settings.fingerprint,
226
- pbk: settings.settings.publicKey,
227
- sid: settings.shortIds[0],
228
- spx: settings.settings.spiderX || '/',
229
- type: this.parent.streamSettings?.network || 'tcp',
230
- encryption: this.parent.settings?.decryption || 'none'
231
- });
232
-
233
- return `${protocol}://${id}@${host}:${port}?${params.toString()}#${encodeURIComponent(this.email || 'default')}`;
234
- }
235
- }
236
-
237
- class RealityBuilder {
238
- constructor(client, options = {}) {
239
- this.client = client;
240
- // Initialize from InboundConfig
241
- this.id = options.id || undefined;
242
- this.port = options.port || 0;
243
- this.remark = options.remark || '';
244
- this.listenIP = options.listen || '';
245
- this.expiryTime = options.expiryTime || 0;
246
- this.enable = true;
247
-
248
- // Initialize from StreamSettings and RealitySettings
249
- const streamSettings = typeof options.streamSettings === 'string'
250
- ? JSON.parse(options.streamSettings)
251
- : options.streamSettings || {};
252
-
253
- const realitySettings = streamSettings?.realitySettings || {};
254
-
255
- this.dest = realitySettings.dest || 'yahoo.com:443';
256
- this.serverNames = realitySettings.serverNames || ['yahoo.com', 'www.yahoo.com'];
257
- this.privateKey = realitySettings.privateKey || '';
258
- this.publicKey = realitySettings.settings?.publicKey || '';
259
- this.shortIds = realitySettings.shortIds;
260
- this.fingerprint = realitySettings.settings?.fingerprint || 'chrome';
261
-
262
- // Initialize clients
263
- this.clients = [];
264
- const settings = typeof options.settings === 'string'
265
- ? JSON.parse(options.settings)
266
- : options.settings;
267
-
268
- if (settings?.clients) {
269
- settings.clients.forEach(client => {
270
- this.addClient(client);
271
- });
272
- }
273
- }
274
-
275
- setPort(port) {
276
- this.port = port;
277
- return this;
278
- }
279
-
280
- setRemark(remark) {
281
- this.remark = remark;
282
- return this;
283
- }
284
-
285
- setDest(dest) {
286
- this.dest = dest;
287
- return this;
288
- }
289
-
290
- setServerNames(names) {
291
- this.serverNames = names;
292
- return this;
293
- }
294
-
295
- setKeyPair(privateKey, publicKey) {
296
- this.privateKey = privateKey;
297
- this.publicKey = publicKey;
298
- return this;
299
- }
300
-
301
- setShortIds(ids) {
302
- this.shortIds = ids;
303
- return this;
304
- }
305
-
306
- setFingerprint(fingerprint) {
307
- this.fingerprint = fingerprint;
308
- return this;
309
- }
310
-
311
- setListenIP(ip) {
312
- this.listenIP = ip;
313
- return this;
314
- }
315
-
316
- setExpiryTime(timestamp) {
317
- this.expiryTime = timestamp;
318
- return this;
319
- }
320
-
321
- addClient(options = {}) {
322
- const builder = new ClientBuilder(this);
323
- if (options.id) builder.setId(options.id);
324
- if (options.email) builder.setEmail(options.email);
325
- if (options.totalGB) builder.setTotalGB(options.totalGB);
326
- if (options.expiryTime) builder.setExpiryTime(options.expiryTime);
327
- if (options.tgId) builder.setTgId(options.tgId);
328
- builder.parent.streamSettings = {
329
- realitySettings: {
330
- serverNames: this.serverNames,
331
- settings: {
332
- fingerprint: this.fingerprint,
333
- publicKey: this.publicKey,
334
- spiderX: '/'
335
- },
336
- shortIds: this.shortIds
337
- }
338
- };
339
- this.clients.push(builder);
340
- return builder;
341
- }
342
-
343
- getClientLink(clientIndex = 0, host) {
344
- if (clientIndex < 0 || clientIndex >= this.clients.length) {
345
- throw new Error('Invalid client index');
346
- }
347
- const client = this.clients[clientIndex];
348
- return client.getLink(host || this.listenIP || 'localhost', this.port);
349
- }
350
-
351
- generateRandomPort() {
352
- return Math.floor(Math.random() * (65535 - 1024) + 1024);
353
- }
354
-
355
- generateShortId() {
356
- const length = Math.floor(Math.random() * 7) * 2 + 2; // Random length between 2 and 16
357
- return crypto.randomBytes(Math.ceil(length / 2))
358
- .toString('hex')
359
- .slice(0, length);
360
- }
361
-
362
- async build() {
363
- if (!this.remark) {
364
- throw new Error('Remark is required');
365
- }
366
-
367
- // If no port specified, find unused one
368
- if (!this.port) {
369
- const inbounds = await this.client.getInbounds();
370
- const usedPorts = new Set(inbounds.map(i => i.port));
371
- let port;
372
- do {
373
- port = this.generateRandomPort();
374
- } while (usedPorts.has(port));
375
- this.port = port;
376
- }
377
-
378
- // If no keypair provided, generate one
379
- if (!this.privateKey || !this.publicKey) {
380
- const cert = await this.client.getNewX25519Cert();
381
- this.privateKey = cert.privateKey;
382
- this.publicKey = cert.publicKey;
383
- }
384
-
385
- // If no shortIds provided, generate random ones
386
- if (!this.shortIds) {
387
- this.shortIds = Array.from({ length: 8 }, () => this.generateShortId());
388
- }
389
-
390
- // If no clients added, create one default client
391
- if (this.clients.length === 0) {
392
- this.addClient();
393
- }
394
-
395
- // Build all clients
396
- const clientConfigs = await Promise.all(this.clients.map(builder => builder.build()));
397
-
398
- return {
399
- id: this.id,
400
- up: 0,
401
- down: 0,
402
- total: 0,
403
- remark: this.remark,
404
- enable: true,
405
- expiryTime: this.expiryTime,
406
- listen: this.listenIP,
407
- port: this.port,
408
- protocol: 'vless',
409
- settings: {
410
- clients: clientConfigs,
411
- decryption: 'none',
412
- fallbacks: []
413
- },
414
- streamSettings: {
415
- network: 'tcp',
416
- security: 'reality',
417
- externalProxy: [],
418
- realitySettings: {
419
- show: false,
420
- xver: 0,
421
- dest: this.dest,
422
- serverNames: this.serverNames,
423
- privateKey: this.privateKey,
424
- minClient: '',
425
- maxClient: '',
426
- maxTimediff: 0,
427
- shortIds: this.shortIds,
428
- settings: {
429
- publicKey: this.publicKey,
430
- fingerprint: this.fingerprint,
431
- serverName: '',
432
- spiderX: '/'
433
- }
434
- },
435
- tcpSettings: {
436
- acceptProxyProtocol: false,
437
- header: {
438
- type: 'none'
439
- }
440
- }
441
- },
442
- tag: `inbound-${this.port}`,
443
- sniffing: {
444
- enabled: false,
445
- destOverride: ['http', 'tls', 'quic', 'fakedns'],
446
- metadataOnly: false,
447
- routeOnly: false
448
- },
449
- allocate: {
450
- strategy: 'always',
451
- refresh: 5,
452
- concurrency: 3
453
- }
454
- };
455
- }
456
- }
3
+ module.exports = X3UIClient;
@@ -0,0 +1,69 @@
1
+ import X3UIClient, {ClientSettings, InboundConfig} from "../../index";
2
+ import ClientBuilder from "../../core/ClientBuilder";
3
+
4
+ export default class RealityBuilder {
5
+
6
+ constructor(client: X3UIClient, inbound?: Partial<InboundConfig>);
7
+
8
+ /**
9
+ * Set the port for the inbound. If not provided, will auto-generate unused port
10
+ */
11
+ setPort(port: number): this;
12
+
13
+ /**
14
+ * Set the remark/name for the inbound
15
+ */
16
+ setRemark(remark: string): this;
17
+
18
+ /**
19
+ * Set the destination address (e.g. "yahoo.com:443")
20
+ */
21
+ setDest(dest: string): this;
22
+
23
+ /**
24
+ * Set server names for SNI
25
+ */
26
+ setServerNames(names: string[]): this;
27
+
28
+ /**
29
+ * Set Reality keypair. If not provided, will be auto-generated
30
+ */
31
+ setKeyPair(privateKey: string, publicKey: string): this;
32
+
33
+ /**
34
+ * Set short IDs for Reality. If not provided, random ones will be generated
35
+ */
36
+ setShortIds(ids: string[]): this;
37
+
38
+ /**
39
+ * Set browser fingerprint. Defaults to "chrome"
40
+ */
41
+ setFingerprint(fingerprint: string): this;
42
+
43
+ /**
44
+ * Set listen IP address. Defaults to empty string
45
+ */
46
+ setListenIP(ip: string): this;
47
+
48
+ /**
49
+ * Set inbound expiry time. Defaults to 0 (no expiry)
50
+ */
51
+ setExpiryTime(timestamp: number): this;
52
+
53
+ /**
54
+ * Add a new client to the inbound
55
+ */
56
+ addClient(options?: Partial<ClientSettings>): ClientBuilder;
57
+
58
+ /**
59
+ * Get connection link for a client
60
+ * @param clientIndex Index of the client (defaults to 0)
61
+ * @param host Optional host address (defaults to listenIP or 'localhost')
62
+ */
63
+ getClientLink(clientIndex?: number, host?: string): string;
64
+
65
+ /**
66
+ * Build the final inbound config
67
+ */
68
+ build(): Promise<InboundConfig>;
69
+ }