@@ -9,20 +9,10 @@ import { User } from '../xray-protos/common/protocol/user';
99import createTypedMessage from '../common/utils/create-typed-message/create-typed-message' ;
1010import { ISdkResponse } from '../common/types/sdk-response' ;
1111import { HANDLER_ERRORS } from '../common/errors' ;
12- import { AddUserResponseModel , GetInboundUsersResponseModel } from './models' ;
13- import {
14- IAddHttpUser ,
15- IAddShadowsocks2022User ,
16- IAddShadowsocksUser ,
17- IAddSocksUser ,
18- IAddTrojanUser ,
19- IAddVlessUser ,
20- } from './interfaces' ;
12+ import { AddUserResponseModel } from './models' ;
13+ import { IAddHttpUser , IAddTrojanUser , IAddVlessUser } from './interfaces' ;
2114import { Account as TrojanAccount } from '../xray-protos/proxy/trojan/config' ;
2215import { Account as VlessAccount } from '../xray-protos/proxy/vless/account' ;
23- import { Account as ShadowsocksAccount } from '../xray-protos/proxy/shadowsocks/config' ;
24- import { Account as Shadowsocks2022Account } from '../xray-protos/proxy/shadowsocks_2022/config' ;
25- import { Account as SocksAccount } from '../xray-protos/proxy/socks/config' ;
2616import { Account as HttpAccount } from '../xray-protos/proxy/http/config' ;
2717import { RemoveUserResponseModel } from './models/remove-user/remove-user.response.model' ;
2818
@@ -36,47 +26,6 @@ export class HandlerService {
3626 this . client = createClient ( HandlerServiceDefinition , channel ) ;
3727 }
3828
39- /**
40- * Retrieves all users from a specified inbound handler.
41- * This method fetches user information including their credentials and metadata from the Xray server.
42- *
43- * @param {string } tag - The tag identifying the inbound handler to query
44- * @returns {Promise<ISdkResponse<GetInboundUsersResponseModel>> } A promise that resolves to:
45- * - On success: An object with `isOk: true` and `data` containing an array of decoded user information
46- * - On failure: An object with `isOk: false` and error details from HANDLER_ERRORS
47- *
48- * @example
49- * ```typescript
50- * const handler = new HandlerService(channel);
51- * const response = await handler.getInboundUsers('Personal');
52- *
53- * if (response.isOk) {
54- * console.log(response.data.users); // Array of DecodedUser objects
55- * } else {
56- * console.error(response.message); // Error message
57- * }
58- * ```
59- */
60- public async getInboundUsers ( tag : string ) : Promise < ISdkResponse < GetInboundUsersResponseModel > > {
61- try {
62- const response = await this . client . getInboundUsers ( { tag } ) ;
63-
64- return {
65- isOk : true ,
66- data : new GetInboundUsersResponseModel ( response . users ) ,
67- } ;
68- } catch ( error ) {
69- let message = '' ;
70- if ( error instanceof Error ) {
71- message = error . message ;
72- }
73- return {
74- isOk : false ,
75- ...HANDLER_ERRORS . GET_ALL_USERS_ERROR ( message ) ,
76- } ;
77- }
78- }
79-
8029 /**
8130 * Adds a new Trojan user to a specified inbound handler.
8231 *
@@ -173,154 +122,6 @@ export class HandlerService {
173122 }
174123 }
175124
176- /**
177- * Adds a new Shadowsocks user to a specified inbound handler.
178- *
179- * @param {IAddShadowsocksUser } data - The user data containing tag, username, password, cipher type, IV check and level
180- * @returns {Promise<ISdkResponse<AddUserResponseModel>> } A promise that resolves to:
181- * - On success: An object with `isOk: true` and `data.success` indicating if user was added
182- * - On failure: An object with `isOk: false` and error details from HANDLER_ERRORS
183- */
184- public async addShadowsocksUser (
185- data : IAddShadowsocksUser ,
186- ) : Promise < ISdkResponse < AddUserResponseModel > > {
187- try {
188- const response = await this . client . alterInbound ( {
189- tag : data . tag ,
190- operation : createTypedMessage ( AddUserOperation , {
191- user : User . create ( {
192- email : data . username ,
193- level : data . level ,
194- account : createTypedMessage ( ShadowsocksAccount , {
195- password : data . password ,
196- cipherType : data . cipherType ,
197- ivCheck : data . ivCheck ,
198- } ) ,
199- } ) ,
200- } ) ,
201- } ) ;
202-
203- return {
204- isOk : true ,
205- data : new AddUserResponseModel ( true ) ,
206- } ;
207- } catch ( error ) {
208- let message = '' ;
209- if ( error instanceof Error ) {
210- message = error . message ;
211- }
212-
213- if ( message . includes ( 'already exists' ) ) {
214- return {
215- isOk : true ,
216- data : new AddUserResponseModel ( false ) ,
217- } ;
218- }
219-
220- return {
221- isOk : false ,
222- ...HANDLER_ERRORS . ADD_USER_ERROR ( message ) ,
223- } ;
224- }
225- }
226-
227- /**
228- * Adds a new Shadowsocks 2022 user to a specified inbound handler.
229- *
230- * @param {IAddShadowsocks2022User } data - The user data containing tag, username, key and level
231- * @returns {Promise<ISdkResponse<AddUserResponseModel>> } A promise that resolves to:
232- * - On success: An object with `isOk: true` and `data.success` indicating if user was added
233- * - On failure: An object with `isOk: false` and error details from HANDLER_ERRORS
234- */
235- public async addShadowsocks2022User (
236- data : IAddShadowsocks2022User ,
237- ) : Promise < ISdkResponse < AddUserResponseModel > > {
238- try {
239- const response = await this . client . alterInbound ( {
240- tag : data . tag ,
241- operation : createTypedMessage ( AddUserOperation , {
242- user : User . create ( {
243- email : data . username ,
244- level : data . level ,
245- account : createTypedMessage ( Shadowsocks2022Account , {
246- key : data . key ,
247- } ) ,
248- } ) ,
249- } ) ,
250- } ) ;
251-
252- return {
253- isOk : true ,
254- data : new AddUserResponseModel ( true ) ,
255- } ;
256- } catch ( error ) {
257- let message = '' ;
258- if ( error instanceof Error ) {
259- message = error . message ;
260- }
261-
262- if ( message . includes ( 'already exists' ) ) {
263- return {
264- isOk : true ,
265- data : new AddUserResponseModel ( false ) ,
266- } ;
267- }
268-
269- return {
270- isOk : false ,
271- ...HANDLER_ERRORS . ADD_USER_ERROR ( message ) ,
272- } ;
273- }
274- }
275-
276- /**
277- * Adds a new SOCKS user to a specified inbound handler.
278- *
279- * @param {IAddSocksUser } data - The user data containing tag, username, SOCKS username, SOCKS password and level
280- * @returns {Promise<ISdkResponse<AddUserResponseModel>> } A promise that resolves to:
281- * - On success: An object with `isOk: true` and `data.success` indicating if user was added
282- * - On failure: An object with `isOk: false` and error details from HANDLER_ERRORS
283- */
284- public async addSocksUser ( data : IAddSocksUser ) : Promise < ISdkResponse < AddUserResponseModel > > {
285- try {
286- const response = await this . client . alterInbound ( {
287- tag : data . tag ,
288- operation : createTypedMessage ( AddUserOperation , {
289- user : User . create ( {
290- email : data . username ,
291- level : data . level ,
292- account : createTypedMessage ( SocksAccount , {
293- username : data . socks_username ,
294- password : data . socks_password ,
295- } ) ,
296- } ) ,
297- } ) ,
298- } ) ;
299-
300- return {
301- isOk : true ,
302- data : new AddUserResponseModel ( true ) ,
303- } ;
304- } catch ( error ) {
305- let message = '' ;
306- if ( error instanceof Error ) {
307- message = error . message ;
308- }
309-
310- if ( message . includes ( 'already exists' ) ) {
311- return {
312- isOk : true ,
313- data : new AddUserResponseModel ( false ) ,
314- } ;
315- }
316-
317- return {
318- isOk : false ,
319- ...HANDLER_ERRORS . ADD_USER_ERROR ( message ) ,
320- } ;
321- }
322- }
323-
324125 /**
325126 * Adds a new HTTP user to a specified inbound handler.
326127 *
@@ -411,31 +212,4 @@ export class HandlerService {
411212 } ;
412213 }
413214 }
414-
415- /**
416- * Gets the count of users in a specified inbound handler.
417- *
418- * @param {string } tag - The tag identifying the inbound handler
419- * @returns {Promise<ISdkResponse<number>> } A promise that resolves to:
420- * - On success: An object with `isOk: true` and `data` containing the user count
421- * - On failure: An object with `isOk: false` and error details from HANDLER_ERRORS
422- */
423- public async getInboundUsersCount ( tag : string ) : Promise < ISdkResponse < number > > {
424- try {
425- const response = await this . client . getInboundUsersCount ( { tag } ) ;
426- return {
427- isOk : true ,
428- data : response . count ,
429- } ;
430- } catch ( error ) {
431- let message = '' ;
432- if ( error instanceof Error ) {
433- message = error . message ;
434- }
435- return {
436- isOk : false ,
437- ...HANDLER_ERRORS . GET_INBOUND_USERS_COUNT_ERROR ( message ) ,
438- } ;
439- }
440- }
441215}
0 commit comments