3xui-api-client 2.1.1 → 3.1.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.
- package/CHANGELOG.md +50 -0
- package/README.md +300 -286
- package/index.d.ts +390 -2
- package/index.js +703 -69
- package/package.json +4 -8
- package/src/generators/CredentialGenerator.js +21 -18
- package/src/session/SessionManager.js +24 -1
- package/src/utils/ByteConversion.js +132 -0
package/README.md
CHANGED
|
@@ -3,17 +3,44 @@
|
|
|
3
3
|
A Node.js client library for 3x-ui panel API that provides easy-to-use methods for managing your 3x-ui server.
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/3xui-api-client)
|
|
6
|
+
[](https://www.npmjs.com/package/3xui-api-client)
|
|
7
|
+
[](https://github.com/iamhelitha/3xui-api-client)
|
|
8
|
+
[](https://nodejs.org/)
|
|
6
9
|
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
[](https://github.com/iamhelitha/3xui-api-client/graphs/commit-activity)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
12
|
+
|
|
13
|
+
## 🚀 Context7 MCP Integration
|
|
14
|
+
|
|
15
|
+
This library is available through **[Context7 MCP](https://context7.com/iamhelitha/3xui-api-client)** for enhanced development experience with intelligent documentation integration. Use the package name `3xui-api-client` to get AI-powered context and documentation.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Table of Contents
|
|
20
|
+
|
|
21
|
+
- [Features](#features)
|
|
22
|
+
- [Installation](#installation)
|
|
23
|
+
- [Quick Start](#quick-start)
|
|
24
|
+
- [API Methods](#api-methods)
|
|
25
|
+
- [Documentation](#documentation)
|
|
26
|
+
- [Requirements](#requirements)
|
|
27
|
+
- [Contributing](#contributing)
|
|
28
|
+
- [Testing](#testing)
|
|
29
|
+
- [FAQ](#faq)
|
|
30
|
+
- [License](#license)
|
|
7
31
|
|
|
8
32
|
## Features
|
|
9
33
|
|
|
10
|
-
- ✅ **
|
|
11
|
-
- ✅ **
|
|
12
|
-
- ✅ **
|
|
13
|
-
- ✅ **
|
|
14
|
-
- ✅ **
|
|
15
|
-
- ✅ **
|
|
16
|
-
- ✅ **
|
|
34
|
+
- ✅ **Dual Panel Support** - Works with both modern (React, v2.x+) and legacy (Vue, v1.x) 3x-ui panels
|
|
35
|
+
- ✅ **Auto-Detection** - Automatically detects panel version and uses correct endpoints
|
|
36
|
+
- ✅ **API Token Authentication** - Support for API token auth (3x-ui v3.0.2+) and cookie-based login
|
|
37
|
+
- ✅ **Automatic Credential Generation** - Built-in UUID, password, and key pair generators
|
|
38
|
+
- ✅ **Session Management** - Automatic login, session caching, and expiry handling
|
|
39
|
+
- ✅ **Security** - Input validation, secure headers, rate limiting, and error sanitization
|
|
40
|
+
- ✅ **Modern API Support** - Complete modern API (v2.x+) with advanced client management
|
|
41
|
+
- ✅ **Legacy API Support** - Full backward compatibility with legacy API methods
|
|
42
|
+
- ✅ **TypeScript Definitions** - Complete type definitions for IDE support
|
|
43
|
+
- ✅ **124 API Methods** - Comprehensive coverage of all 3x-ui panel operations
|
|
17
44
|
|
|
18
45
|
## Installation
|
|
19
46
|
|
|
@@ -21,339 +48,325 @@ A Node.js client library for 3x-ui panel API that provides easy-to-use methods f
|
|
|
21
48
|
npm install 3xui-api-client
|
|
22
49
|
```
|
|
23
50
|
|
|
24
|
-
## Context7 MCP Integration
|
|
25
|
-
|
|
26
|
-
This library can be implemented with the help of [Context7 MCP](https://context7.com/iamhelitha/3xui-api-client). Use the package name `3xui-api-client` to get context and documentation through Context7's Model Context Protocol integration.
|
|
27
|
-
|
|
28
|
-
Learn more about [Context7 MCP](https://context7.com) for enhanced development experience.
|
|
29
|
-
|
|
30
51
|
## Quick Start
|
|
31
52
|
|
|
53
|
+
### Cookie-Based Authentication
|
|
54
|
+
|
|
32
55
|
```javascript
|
|
33
56
|
const ThreeXUI = require('3xui-api-client');
|
|
34
57
|
|
|
35
58
|
const client = new ThreeXUI('https://your-3xui-server.com', 'username', 'password');
|
|
36
59
|
|
|
37
60
|
// Get all inbounds
|
|
38
|
-
client.getInbounds()
|
|
39
|
-
|
|
40
|
-
console.log('Inbounds:', inbounds);
|
|
41
|
-
})
|
|
42
|
-
.catch(error => {
|
|
43
|
-
console.error('Error:', error.message);
|
|
44
|
-
});
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Configuration & Best Practices
|
|
48
|
-
|
|
49
|
-
### Using Environment Variables (Recommended)
|
|
50
|
-
Never hardcode your credentials in your code. Use environment variables to store sensitive information.
|
|
51
|
-
|
|
52
|
-
> **IMPORTANT NOTE**: The `PANEL_URL` should be the root URL of your server **WITHOUT** the `/panel` suffix.
|
|
53
|
-
> - ✅ Correct: `https://your-domain.com:2053` or `https://your-domain.com/secret-path`
|
|
54
|
-
> - ❌ Incorrect: `https://your-domain.com:2053/panel`
|
|
55
|
-
>
|
|
56
|
-
> The library automatically appends `/panel` and other necessary paths. Including it in your configuration will cause 404 errors.
|
|
57
|
-
|
|
58
|
-
1. Create a `.env` file in your project root:
|
|
59
|
-
```env
|
|
60
|
-
PANEL_URL=http://your-server-ip:2053
|
|
61
|
-
PANEL_USERNAME=admin
|
|
62
|
-
PANEL_PASSWORD=your_secure_password
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
2. Install `dotenv`:
|
|
66
|
-
```bash
|
|
67
|
-
npm install dotenv
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
3. Initialize the client:
|
|
71
|
-
```javascript
|
|
72
|
-
require('dotenv').config();
|
|
73
|
-
const ThreeXUI = require('3xui-api-client');
|
|
74
|
-
|
|
75
|
-
const client = new ThreeXUI(
|
|
76
|
-
process.env.PANEL_URL,
|
|
77
|
-
process.env.PANEL_USERNAME,
|
|
78
|
-
process.env.PANEL_PASSWORD
|
|
79
|
-
);
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## Authentication & Security
|
|
83
|
-
|
|
84
|
-
### Automatic Login
|
|
85
|
-
The client automatically handles authentication. When you make your first API call, it will:
|
|
86
|
-
1. Login with your credentials
|
|
87
|
-
2. Store the session cookie
|
|
88
|
-
3. Use the cookie for subsequent requests
|
|
89
|
-
4. Automatically re-login if the session expires
|
|
90
|
-
|
|
91
|
-
### Server-Side Cookie Storage (Recommended)
|
|
92
|
-
For production applications, store the session cookie securely on your server. The `login()` method returns the cookie for this purpose:
|
|
93
|
-
|
|
94
|
-
```javascript
|
|
95
|
-
const ThreeXUI = require('3xui-api-client');
|
|
96
|
-
|
|
97
|
-
class SecureThreeXUIManager {
|
|
98
|
-
constructor(baseURL, username, password) {
|
|
99
|
-
this.client = new ThreeXUI(baseURL, username, password);
|
|
100
|
-
this.sessionCookie = null;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async ensureAuthenticated() {
|
|
104
|
-
if (!this.sessionCookie) {
|
|
105
|
-
const loginResult = await this.client.login();
|
|
106
|
-
|
|
107
|
-
// The cookie is returned in the login result
|
|
108
|
-
this.sessionCookie = loginResult.cookie;
|
|
109
|
-
|
|
110
|
-
// Store in secure session storage (Redis, database, etc.)
|
|
111
|
-
await this.storeSessionSecurely(this.sessionCookie);
|
|
112
|
-
} else {
|
|
113
|
-
// Restore from secure storage
|
|
114
|
-
this.client.cookie = this.sessionCookie;
|
|
115
|
-
this.client.api.defaults.headers.Cookie = this.sessionCookie;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async storeSessionSecurely(cookie) {
|
|
120
|
-
// Example: Store in Redis with expiration
|
|
121
|
-
// await redis.setex('3xui_session', 3600, cookie);
|
|
122
|
-
|
|
123
|
-
// Example: Store in database
|
|
124
|
-
// await db.sessions.upsert({ service: '3xui', cookie, expires_at: new Date(Date.now() + 3600000) });
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async getInbounds() {
|
|
128
|
-
await this.ensureAuthenticated();
|
|
129
|
-
return this.client.getInbounds();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
61
|
+
const inbounds = await client.getInbounds();
|
|
62
|
+
console.log(inbounds);
|
|
132
63
|
```
|
|
133
64
|
|
|
134
|
-
###
|
|
135
|
-
The library includes several security enhancements:
|
|
136
|
-
- **Input Validation**: Validates URLs, usernames, and passwords to prevent injection attacks.
|
|
137
|
-
- **Secure Headers**: Automatically adds security headers to requests.
|
|
138
|
-
- **Rate Limiting**: Prevents abuse by limiting request rates (configurable).
|
|
139
|
-
- **Error Sanitization**: Hides sensitive details in production errors.
|
|
140
|
-
|
|
141
|
-
## API Reference
|
|
65
|
+
### API Token Authentication (v3.0.2+)
|
|
142
66
|
|
|
143
|
-
### Constructor
|
|
144
67
|
```javascript
|
|
145
|
-
new ThreeXUI(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
- `baseURL` (string): Your 3x-ui server URL (e.g., 'https://your-server.com')
|
|
149
|
-
- `username` (string): Admin username
|
|
150
|
-
- `password` (string): Admin password
|
|
151
|
-
|
|
152
|
-
### Inbound Management (✅ Tested & Working)
|
|
68
|
+
const client = new ThreeXUI('https://your-3xui-server.com', {
|
|
69
|
+
token: 'your-api-token'
|
|
70
|
+
});
|
|
153
71
|
|
|
154
|
-
#### Get All Inbounds
|
|
155
|
-
```javascript
|
|
156
72
|
const inbounds = await client.getInbounds();
|
|
157
|
-
console.log(inbounds);
|
|
158
73
|
```
|
|
159
74
|
|
|
160
|
-
|
|
161
|
-
```javascript
|
|
162
|
-
const inbound = await client.getInbound(inboundId);
|
|
163
|
-
console.log(inbound);
|
|
164
|
-
```
|
|
75
|
+
### Panel Version Support
|
|
165
76
|
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
const inboundConfig = {
|
|
169
|
-
remark: "My VPN Server",
|
|
170
|
-
port: 443,
|
|
171
|
-
protocol: "vless",
|
|
172
|
-
settings: {
|
|
173
|
-
// Your inbound settings
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
const result = await client.addInbound(inboundConfig);
|
|
178
|
-
console.log('Inbound added:', result);
|
|
179
|
-
```
|
|
77
|
+
This library **automatically supports both modern and legacy 3x-ui panels**:
|
|
180
78
|
|
|
181
|
-
#### Update Inbound
|
|
182
79
|
```javascript
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
80
|
+
// Automatic detection (recommended)
|
|
81
|
+
// Works with both modern and legacy panels
|
|
82
|
+
const client = new ThreeXUI('https://your-3xui-server.com', 'username', 'password');
|
|
83
|
+
await client.login();
|
|
84
|
+
// Automatically detects panel version and uses correct endpoints
|
|
187
85
|
|
|
188
|
-
|
|
189
|
-
|
|
86
|
+
// Explicit version (optional, for performance)
|
|
87
|
+
const client = new ThreeXUI('https://your-3xui-server.com', 'username', 'password', {
|
|
88
|
+
panelVersion: 'modern' // or 'legacy' or 'auto' (default)
|
|
89
|
+
});
|
|
190
90
|
```
|
|
191
91
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
92
|
+
**Supported Panel Types:**
|
|
93
|
+
- ✅ **Modern Panels** - React-based, v2.x+ with `/panel/api/*` endpoints
|
|
94
|
+
- ✅ **Legacy Panels** - Vue-based, v1.x with `/login` endpoint
|
|
95
|
+
- ✅ **Auto-Detection** - Tries modern first, falls back to legacy if needed
|
|
96
|
+
- ✅ **Session Caching** - Detected version is cached for faster subsequent logins
|
|
97
|
+
|
|
98
|
+
See [PANEL-VERSION-SUPPORT.md](./PANEL-VERSION-SUPPORT.md) for detailed version support documentation.
|
|
99
|
+
|
|
100
|
+
## API Methods
|
|
101
|
+
|
|
102
|
+
### Client Management (Modern API - v2.x+)
|
|
103
|
+
|
|
104
|
+
#### Read Operations
|
|
105
|
+
- `getClients()` - Get all clients
|
|
106
|
+
- `getPagedClients(params)` - Get paginated client list
|
|
107
|
+
- `getClient(email)` - Get specific client by email
|
|
108
|
+
- `getClientTraffic(email)` - Get client traffic statistics
|
|
109
|
+
- `getSubLinks(subId)` - Get subscription links by subscription ID
|
|
110
|
+
- `getClientLinks(email)` - Get generic client links
|
|
111
|
+
- `getGroups()` - Get all client groups
|
|
112
|
+
- `getGroupEmails(groupName)` - Get emails in specific group
|
|
113
|
+
- `getOnlines()` - Get currently online clients
|
|
114
|
+
- `getModernLastOnline()` - Get last online status
|
|
115
|
+
- `getModernClientIps(email)` - Get client IP list
|
|
116
|
+
|
|
117
|
+
#### Write Operations
|
|
118
|
+
- `addModernClient(data)` - Add new client (modern API)
|
|
119
|
+
- `updateModernClient(email, data)` - Update client details
|
|
120
|
+
- `deleteModernClient(email)` - Delete client
|
|
121
|
+
- `attachClientToInbounds(email, data)` - Attach client to inbounds
|
|
122
|
+
- `detachClientFromInbounds(email, data)` - Detach client from inbounds
|
|
123
|
+
- `resetModernClientTrafficByEmail(email)` - Reset client traffic
|
|
124
|
+
- `updateModernClientTrafficByEmail(email, data)` - Update traffic limits
|
|
125
|
+
- `clearModernClientIps(email)` - Clear client IP restrictions
|
|
126
|
+
|
|
127
|
+
#### Bulk Operations
|
|
128
|
+
- `bulkCreateModernClients(data)` - Bulk create clients
|
|
129
|
+
- `bulkDeleteModernClients(data)` - Bulk delete clients
|
|
130
|
+
- `bulkAttachModernClients(data)` - Bulk attach to inbounds
|
|
131
|
+
- `bulkDetachModernClients(data)` - Bulk detach from inbounds
|
|
132
|
+
- `bulkAdjustModernClients(data)` - Bulk adjust settings
|
|
133
|
+
- `bulkResetTrafficModernClients(data)` - Bulk reset traffic
|
|
134
|
+
- `resetAllModernClientTraffics()` - Reset all client traffic
|
|
135
|
+
- `deleteDepletedModernClients()` - Delete depleted clients
|
|
136
|
+
|
|
137
|
+
#### Group Management
|
|
138
|
+
- `createGroup(data)` - Create client group
|
|
139
|
+
- `renameGroup(data)` - Rename existing group
|
|
140
|
+
- `deleteGroup(data)` - Delete group
|
|
141
|
+
- `bulkAddGroups(data)` - Bulk add groups
|
|
142
|
+
- `bulkRemoveGroups(data)` - Bulk remove groups
|
|
143
|
+
|
|
144
|
+
### Client Management (Legacy API)
|
|
145
|
+
|
|
146
|
+
- `addClient(clientConfig)` - Add client
|
|
147
|
+
- `updateClient(clientId, clientConfig)` - Update client
|
|
148
|
+
- `deleteClient(inboundId, clientId)` - Delete client
|
|
149
|
+
- `deleteClientByEmail(inboundId, email)` - Delete by email
|
|
150
|
+
- `getClientTrafficsByEmail(email)` - Get traffic by email
|
|
151
|
+
- `getClientTrafficsById(id)` - Get traffic by ID
|
|
152
|
+
- `getClientIps(email)` - Get client IPs
|
|
153
|
+
- `clearClientIps(email)` - Clear IP restrictions
|
|
154
|
+
- `updateClientTraffic(email, trafficConfig)` - Update traffic limits
|
|
155
|
+
- `resetClientTraffic(inboundId, email)` - Reset traffic
|
|
156
|
+
- `resetAllTraffics()` - Reset all traffic globally
|
|
157
|
+
- `resetAllClientTraffics(inboundId)` - Reset inbound traffic
|
|
158
|
+
- `deleteDepletedClients(inboundId)` - Delete depleted clients
|
|
159
|
+
|
|
160
|
+
### Inbound Management
|
|
161
|
+
|
|
162
|
+
- `getInbounds()` - Get all inbounds
|
|
163
|
+
- `getInbound(id)` - Get specific inbound
|
|
164
|
+
- `addInbound(inboundConfig)` - Add new inbound
|
|
165
|
+
- `updateInbound(id, inboundConfig)` - Update inbound
|
|
166
|
+
- `deleteInbound(id)` - Delete inbound
|
|
167
|
+
- `importInbounds(inbounds)` - Bulk import inbounds
|
|
168
|
+
- `getLastOnline()` - Get last online info
|
|
169
|
+
|
|
170
|
+
### Node Management
|
|
171
|
+
|
|
172
|
+
- `getNodes()` - Get all nodes
|
|
173
|
+
- `getNode(id)` - Get specific node
|
|
174
|
+
- `getNodeHistory(id, metric, bucket)` - Get node metrics history
|
|
175
|
+
- `addNode(data)` - Add new node
|
|
176
|
+
- `updateNode(id, data)` - Update node
|
|
177
|
+
- `deleteNode(id)` - Delete node
|
|
178
|
+
- `setNodeEnable(id)` - Enable node
|
|
179
|
+
- `testNode(data)` - Test node connectivity
|
|
180
|
+
- `probeNode(id)` - Probe node status
|
|
181
|
+
|
|
182
|
+
### Custom Geo Management
|
|
183
|
+
|
|
184
|
+
- `getCustomGeos()` - Get custom geo sites/IPs
|
|
185
|
+
- `getGeoAliases()` - Get geo aliases
|
|
186
|
+
- `addCustomGeo(data)` - Add custom geo
|
|
187
|
+
- `updateCustomGeo(id, data)` - Update geo
|
|
188
|
+
- `deleteCustomGeo(id)` - Delete geo
|
|
189
|
+
- `downloadCustomGeo(id)` - Download geo data
|
|
190
|
+
- `updateAllCustomGeo()` - Update all geo data
|
|
191
|
+
|
|
192
|
+
### Server Management
|
|
193
|
+
|
|
194
|
+
- `getServerStatus()` - Get CPU, RAM, uptime
|
|
195
|
+
- `getCPUHistory(bucket)` - Get CPU usage history
|
|
196
|
+
- `getXrayVersion()` - Get Xray version
|
|
197
|
+
- `getConfigJson()` - Get Xray config JSON
|
|
198
|
+
- `getDb()` - Download database
|
|
199
|
+
- `stopXrayService()` - Stop Xray core
|
|
200
|
+
- `restartXrayService()` - Restart Xray core
|
|
201
|
+
- `installXray(version)` - Install specific Xray version
|
|
202
|
+
- `getPanelLogs(count)` - Get panel logs
|
|
203
|
+
- `getXrayLogs(count)` - Get Xray logs
|
|
204
|
+
- `updateGeofile(fileName)` - Update GeoIP/GeoSite files
|
|
205
|
+
- `importDB(formData)` - Import database
|
|
206
|
+
|
|
207
|
+
### Panel Settings
|
|
208
|
+
|
|
209
|
+
- `getAllSettings()` - Get all panel settings
|
|
210
|
+
- `updateSetting(settings)` - Update settings
|
|
211
|
+
- `updateUser(oldUsername, oldPassword, newUsername, newPassword)` - Change admin credentials
|
|
212
|
+
- `restartPanel()` - Restart panel
|
|
213
|
+
- `getDefaultSettings()` - Get default settings
|
|
214
|
+
- `getDefaultJsonConfig()` - Get default Xray JSON config
|
|
215
|
+
|
|
216
|
+
### Xray Configuration
|
|
217
|
+
|
|
218
|
+
- `getXrayConfig()` - Get Xray configuration
|
|
219
|
+
- `updateXrayConfig(config)` - Update Xray configuration
|
|
220
|
+
- `manageWarp(action, data)` - Manage WARP settings
|
|
221
|
+
- `getOutboundsTraffic()` - Get outbound traffic statistics
|
|
222
|
+
- `resetOutboundsTraffic()` - Reset outbound traffic
|
|
223
|
+
- `getXrayResult()` - Get Xray execution result
|
|
224
|
+
|
|
225
|
+
### Server-Side Generators
|
|
226
|
+
|
|
227
|
+
- `getNewUUID()` - Generate UUID server-side
|
|
228
|
+
- `getNewX25519Cert()` - Generate X25519 certificate
|
|
229
|
+
- `getNewmldsa65()` - Generate MLDSA65 key
|
|
230
|
+
- `getNewmlkem768()` - Generate ML-KEM-768 key
|
|
231
|
+
- `getNewVlessEnc()` - Generate VLESS encryption
|
|
232
|
+
- `getNewEchCert()` - Generate ECH certificate
|
|
233
|
+
|
|
234
|
+
### Credential Generation
|
|
235
|
+
|
|
236
|
+
- `generateCredentials(protocol, options)` - Generate protocol-specific credentials
|
|
237
|
+
- `generateUUID(secure)` - Generate UUID
|
|
238
|
+
- `generatePassword(length, options)` - Generate password
|
|
239
|
+
- `generateBulkCredentials(protocol, count, options)` - Bulk generate credentials
|
|
240
|
+
- `generateWireGuardKeys()` - Generate WireGuard key pair
|
|
241
|
+
- `generateRealityKeys()` - Generate Reality key pair
|
|
242
|
+
- `generatePort(min, max)` - Generate random port
|
|
243
|
+
- `getShadowsocksCiphers()` - List SS cipher methods
|
|
244
|
+
- `getRecommendedShadowsocksCipher()` - Get recommended cipher
|
|
245
|
+
- `validateCredentials(credentials, protocol)` - Validate credentials
|
|
246
|
+
|
|
247
|
+
### Enhanced Client Management
|
|
248
|
+
|
|
249
|
+
- `addClientWithCredentials(inboundId, protocol, options)` - Add client with auto-generated credentials
|
|
250
|
+
- `updateClientWithCredentials(clientId, inboundId, options)` - Update client with credential management
|
|
251
|
+
|
|
252
|
+
### Session Management
|
|
253
|
+
|
|
254
|
+
- `login(forceRefresh)` - Authenticate with credentials/token
|
|
255
|
+
- `logout()` - Clear session
|
|
256
|
+
- `isSessionValid()` - Check session validity
|
|
257
|
+
- `getSessionStats()` - Get session statistics
|
|
258
|
+
- `clearAllSessions()` - Clear all cached sessions
|
|
259
|
+
- `getTwoFactorEnable()` - Check 2FA status
|
|
260
|
+
|
|
261
|
+
### System
|
|
262
|
+
|
|
263
|
+
- `getOnlineClients()` - Get online clients
|
|
264
|
+
- `createBackup()` - Create system backup
|
|
265
|
+
- `backupToTgBot()` - Send backup to Telegram
|
|
266
|
+
- `getSecurityStats()` - Get security monitoring data
|
|
197
267
|
|
|
198
|
-
|
|
268
|
+
## Documentation
|
|
199
269
|
|
|
200
|
-
|
|
201
|
-
```javascript
|
|
202
|
-
const clientConfig = {
|
|
203
|
-
id: inboundId,
|
|
204
|
-
settings: JSON.stringify({
|
|
205
|
-
clients: [{
|
|
206
|
-
id: "client-uuid-here",
|
|
207
|
-
email: "user23c5n7",
|
|
208
|
-
limitIp: 0,
|
|
209
|
-
totalGB: 0,
|
|
210
|
-
expiryTime: 0,
|
|
211
|
-
enable: true
|
|
212
|
-
}]
|
|
213
|
-
})
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const result = await client.addClient(clientConfig);
|
|
217
|
-
```
|
|
270
|
+
For comprehensive guides, examples, and implementation patterns, visit our **[Wiki](https://github.com/iamhelitha/3xui-api-client/wiki)**:
|
|
218
271
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
const result = await client.updateClient(clientUUID, updateConfig);
|
|
229
|
-
```
|
|
272
|
+
- 📚 [Use Cases & Examples](https://github.com/iamhelitha/3xui-api-client/wiki/Use-Cases)
|
|
273
|
+
- 🔐 [Authentication Guide](https://github.com/iamhelitha/3xui-api-client/wiki/Authentication-Guide)
|
|
274
|
+
- 🌐 [Inbound Management](https://github.com/iamhelitha/3xui-api-client/wiki/Inbound-Management)
|
|
275
|
+
- 👥 [Client Management](https://github.com/iamhelitha/3xui-api-client/wiki/Client-Management)
|
|
276
|
+
- 📊 [Traffic Management](https://github.com/iamhelitha/3xui-api-client/wiki/Traffic-Management)
|
|
277
|
+
- ⚙️ [System Operations](https://github.com/iamhelitha/3xui-api-client/wiki/System-Operations)
|
|
278
|
+
- 🌐 [Modern API Guide](https://github.com/iamhelitha/3xui-api-client/wiki/Modern-API)
|
|
230
279
|
|
|
231
|
-
|
|
232
|
-
```javascript
|
|
233
|
-
const result = await client.deleteClient(inboundId, clientUUID);
|
|
234
|
-
```
|
|
280
|
+
## Requirements
|
|
235
281
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
console.log('Client traffic:', traffic);
|
|
240
|
-
```
|
|
282
|
+
- Node.js >= 14.0.0
|
|
283
|
+
- 3x-ui panel v2.0+ (or v3.0.2+ for API token authentication)
|
|
284
|
+
- API access enabled on your 3x-ui server
|
|
241
285
|
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
const traffic = await client.getClientTrafficsById("client-uuid");
|
|
245
|
-
console.log('Client traffic:', traffic);
|
|
246
|
-
```
|
|
286
|
+
## Contributing
|
|
247
287
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
288
|
+
1. Fork the repository
|
|
289
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
290
|
+
3. Commit your changes (`git commit -m 'feat: add amazing feature'`)
|
|
291
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
292
|
+
5. Open a Pull Request
|
|
252
293
|
|
|
253
|
-
|
|
254
|
-
const result = await client.clearClientIps("user23c5n7");
|
|
255
|
-
```
|
|
294
|
+
## Testing
|
|
256
295
|
|
|
257
|
-
|
|
296
|
+
```bash
|
|
297
|
+
# Run login test
|
|
298
|
+
npm run test:login
|
|
258
299
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const result = await client.resetClientTraffic(inboundId, "user23c5n7");
|
|
300
|
+
# Run all tests
|
|
301
|
+
npm test
|
|
262
302
|
```
|
|
263
303
|
|
|
264
|
-
|
|
265
|
-
```javascript
|
|
266
|
-
const result = await client.resetAllTraffics();
|
|
267
|
-
```
|
|
304
|
+
## FAQ
|
|
268
305
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const result = await client.resetAllClientTraffics(inboundId);
|
|
272
|
-
```
|
|
306
|
+
**Q: Do I need to call `login()` explicitly?**
|
|
307
|
+
A: No, the client automatically logs in before the first API call. Just create the client and start using it.
|
|
273
308
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const result = await client.deleteDepletedClients(inboundId);
|
|
277
|
-
```
|
|
309
|
+
**Q: Can I use API tokens instead of username/password?**
|
|
310
|
+
A: Yes, use `{ token: 'your-api-token' }` instead of username/password (requires 3x-ui v3.0.2+).
|
|
278
311
|
|
|
279
|
-
|
|
312
|
+
**Q: How do I store session cookies securely?**
|
|
313
|
+
A: See the [Session Security](#session-security) section below and the [Authentication Guide](https://github.com/iamhelitha/3xui-api-client/wiki/Authentication-Guide) for server-side storage patterns.
|
|
280
314
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const onlineClients = await client.getOnlineClients();
|
|
284
|
-
console.log('Currently online:', onlineClients);
|
|
285
|
-
```
|
|
315
|
+
**Q: Which authentication method is more secure?**
|
|
316
|
+
A: API tokens are recommended for production. See [Authentication Guide](https://github.com/iamhelitha/3xui-api-client/wiki/Authentication-Guide) for comparison.
|
|
286
317
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const result = await client.createBackup();
|
|
290
|
-
console.log('Backup created:', result);
|
|
291
|
-
```
|
|
318
|
+
**Q: Can I use the client in browsers?**
|
|
319
|
+
A: No, this is a Node.js library. Use it on your server side with proper CORS/security headers.
|
|
292
320
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
const tgResult = await client.backupToTgBot();
|
|
296
|
-
console.log('Backup sent to Telegram:', tgResult);
|
|
297
|
-
```
|
|
321
|
+
**Q: How do I migrate from v2.x to v3.x?**
|
|
322
|
+
A: The library is backward compatible. See [CHANGELOG.md](CHANGELOG.md) for breaking changes (if any).
|
|
298
323
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const status = await client.getServerStatus();
|
|
302
|
-
console.log('Server status:', status);
|
|
303
|
-
```
|
|
324
|
+
**Q: Can I bulk operations with this client?**
|
|
325
|
+
A: Yes, use the `bulk*` methods for efficient batch operations. See [Client Management](https://github.com/iamhelitha/3xui-api-client/wiki/Client-Management) for examples.
|
|
304
326
|
|
|
305
|
-
##
|
|
327
|
+
## Session Security
|
|
306
328
|
|
|
307
|
-
|
|
329
|
+
> [!IMPORTANT]
|
|
330
|
+
> The 3x-ui session cookie grants **full admin access** to your panel. Treat it with the same care as a password or API secret.
|
|
308
331
|
|
|
309
|
-
|
|
310
|
-
- 🔐 [**Authentication Guide**](https://github.com/iamhelitha/3xui-api-client/wiki/Authentication-Guide) - Secure login and session management
|
|
311
|
-
- 🌐 [**Inbound Management**](https://github.com/iamhelitha/3xui-api-client/wiki/Inbound-Management) - Server configuration and setup
|
|
312
|
-
- 👥 [**Client Management**](https://github.com/iamhelitha/3xui-api-client/wiki/Client-Management) - User account operations
|
|
313
|
-
- 📊 [**Traffic Management**](https://github.com/iamhelitha/3xui-api-client/wiki/Traffic-Management) - Usage monitoring and billing
|
|
314
|
-
- ⚙️ [**System Operations**](https://github.com/iamhelitha/3xui-api-client/wiki/System-Operations) - Backup and maintenance
|
|
332
|
+
### Cookie storage is unencrypted by default
|
|
315
333
|
|
|
316
|
-
|
|
334
|
+
Both built-in session stores save the cookie in plaintext:
|
|
317
335
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (error.message.includes('Login failed')) {
|
|
324
|
-
console.error('Authentication error:', error.message);
|
|
325
|
-
} else if (error.response?.status === 401) {
|
|
326
|
-
console.error('Unauthorized - check your credentials');
|
|
327
|
-
} else {
|
|
328
|
-
console.error('API error:', error.message);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
```
|
|
336
|
+
| Store | Where the cookie lives |
|
|
337
|
+
|---|---|
|
|
338
|
+
| `MemorySessionStore` (default) | Plain JS `Map` in process heap |
|
|
339
|
+
| `DatabaseSessionStore` | `session_data TEXT` column, no encryption |
|
|
340
|
+
| `RedisSessionStore` | Plain string value in Redis |
|
|
332
341
|
|
|
333
|
-
|
|
342
|
+
**Recommendations:**
|
|
343
|
+
- **Memory store** — safe for single-process, single-instance deployments. In shared/multi-tenant environments a heap dump exposes live admin cookies. Prefer Redis or DB stores with proper access controls.
|
|
344
|
+
- **Database store** — restrict the DB user to only the sessions table with the minimum required privileges, and enable encryption-at-rest on the database itself.
|
|
345
|
+
- **Redis store** — use Redis ACLs to restrict key-space access and enable TLS for the Redis connection.
|
|
346
|
+
- For the highest security, use **API tokens** (`{ token: 'your-api-token' }`) instead of username/password — tokens can be scoped and revoked without a session cookie.
|
|
334
347
|
|
|
335
|
-
|
|
336
|
-
- 3x-ui panel with API access enabled
|
|
348
|
+
### Session key is deterministic
|
|
337
349
|
|
|
338
|
-
|
|
350
|
+
The cache key is `sha256(baseURL:username)`. It is an intentionally stable lookup key, not a secret. Anyone with read access to the session store can correlate rows to specific panels and users. Protect the **values** (cookies), not the keys.
|
|
339
351
|
|
|
340
|
-
|
|
341
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
342
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
343
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
344
|
-
5. Open a Pull Request
|
|
352
|
+
### Serverless and multi-instance deployments
|
|
345
353
|
|
|
346
|
-
|
|
354
|
+
Each serverless cold start (e.g. Vercel, AWS Lambda) creates a fresh in-memory session, triggering a `/login` call per instance. A few things to keep in mind:
|
|
347
355
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
npm run test:login
|
|
356
|
+
1. **Use a shared external store** (`RedisSessionStore` or `DatabaseSessionStore`) so instances can reuse existing sessions instead of re-authenticating every time.
|
|
357
|
+
2. **Re-login backoff** — the client applies a configurable delay (default **500 ms**) before each forced re-login on a `401` response. This prevents a burst of cold starts from simultaneously hammering the panel login endpoint and tripping rate-limiting or fail2ban rules. You can tune it:
|
|
351
358
|
|
|
352
|
-
|
|
353
|
-
|
|
359
|
+
```js
|
|
360
|
+
const client = new XuiApiClient({
|
|
361
|
+
baseURL: 'https://your-panel.example.com',
|
|
362
|
+
username: 'admin',
|
|
363
|
+
password: 'secret',
|
|
364
|
+
maxLoginRetries: 3, // max 401-triggered re-login attempts (default: 3)
|
|
365
|
+
loginRetryBackoff: 1000, // ms to wait before each retry (default: 500, set 0 to disable)
|
|
366
|
+
});
|
|
354
367
|
```
|
|
355
368
|
|
|
356
|
-
|
|
369
|
+
3. **Rate limit awareness** — `maxLoginRetries` is per-instance. Across many concurrent instances you can still exceed the panel's login-attempt limit. If your panel has fail2ban enabled, ensure your egress IP is whitelisted or use API token auth to avoid the login flow entirely.
|
|
357
370
|
|
|
358
371
|
## License
|
|
359
372
|
|
|
@@ -371,4 +384,5 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
371
384
|
|
|
372
385
|
---
|
|
373
386
|
|
|
374
|
-
⚠️ **Security Notice**: Always store credentials and session cookies securely. Never expose them in client-side code or commit them to version control.
|
|
387
|
+
⚠️ **Security Notice**: Always store credentials and session cookies securely. Never expose them in client-side code or commit them to version control.
|
|
388
|
+
|