@@ -189,7 +189,7 @@ function validateAccessKeyId(accessKeyId: unknown): string {
189
189
return accessKeyId ;
190
190
}
191
191
192
- function validateDataLimit ( limit : unknown ) : DataLimit {
192
+ function validateDataLimit ( limit : unknown ) : DataLimit | undefined {
193
193
if ( typeof limit === 'undefined' ) {
194
194
return undefined ;
195
195
}
@@ -204,7 +204,7 @@ function validateDataLimit(limit: unknown): DataLimit {
204
204
return limit as DataLimit ;
205
205
}
206
206
207
- function validateStringParam ( param : unknown , paramName : string ) : string {
207
+ function validateStringParam ( param : unknown , paramName : string ) : string | undefined {
208
208
if ( typeof param === 'undefined' ) {
209
209
return undefined ;
210
210
}
@@ -218,6 +218,20 @@ function validateStringParam(param: unknown, paramName: string): string {
218
218
return param ;
219
219
}
220
220
221
+ function validateNumberParam ( param : unknown , paramName : string ) : number | undefined {
222
+ if ( typeof param === 'undefined' ) {
223
+ return undefined ;
224
+ }
225
+
226
+ if ( typeof param !== 'number' ) {
227
+ throw new restifyErrors . InvalidArgumentError (
228
+ { statusCode : 400 } ,
229
+ `Expected a number for ${ paramName } , instead got ${ param } of type ${ typeof param } `
230
+ ) ;
231
+ }
232
+ return param ;
233
+ }
234
+
221
235
// The ShadowsocksManagerService manages the access keys that can use the server
222
236
// as a proxy using Shadowsocks. It runs an instance of the Shadowsocks server
223
237
// for each existing access key, with the port and password assigned for that access key.
@@ -342,6 +356,7 @@ export class ShadowsocksManagerService {
342
356
const name = validateStringParam ( req . params . name || '' , 'name' ) ;
343
357
const dataLimit = validateDataLimit ( req . params . limit ) ;
344
358
const password = validateStringParam ( req . params . password , 'password' ) ;
359
+ const portNumber = validateNumberParam ( req . params . port , 'port' ) ;
345
360
346
361
const accessKeyJson = accessKeyToApiJson (
347
362
await this . accessKeys . createNewAccessKey ( {
@@ -350,13 +365,16 @@ export class ShadowsocksManagerService {
350
365
name,
351
366
dataLimit,
352
367
password,
368
+ portNumber,
353
369
} )
354
370
) ;
355
371
return accessKeyJson ;
356
372
} catch ( error ) {
357
373
logging . error ( error ) ;
358
- if ( error instanceof errors . InvalidCipher ) {
374
+ if ( error instanceof errors . InvalidCipher || error instanceof errors . InvalidPortNumber ) {
359
375
throw new restifyErrors . InvalidArgumentError ( { statusCode : 400 } , error . message ) ;
376
+ } else if ( error instanceof errors . PortUnavailable ) {
377
+ throw new restifyErrors . ConflictError ( error . message ) ;
360
378
}
361
379
throw error ;
362
380
}
@@ -381,10 +399,7 @@ export class ShadowsocksManagerService {
381
399
return next ( ) ;
382
400
} catch ( error ) {
383
401
logging . error ( error ) ;
384
- if (
385
- error instanceof restifyErrors . InvalidArgumentError ||
386
- error instanceof restifyErrors . MissingParameterError
387
- ) {
402
+ if ( error instanceof restifyErrors . HttpError ) {
388
403
return next ( error ) ;
389
404
}
390
405
return next ( new restifyErrors . InternalServerError ( ) ) ;
@@ -409,10 +424,7 @@ export class ShadowsocksManagerService {
409
424
if ( error instanceof errors . AccessKeyConflict ) {
410
425
return next ( new restifyErrors . ConflictError ( error . message ) ) ;
411
426
}
412
- if (
413
- error instanceof restifyErrors . InvalidArgumentError ||
414
- error instanceof restifyErrors . MissingParameterError
415
- ) {
427
+ if ( error instanceof restifyErrors . HttpError ) {
416
428
return next ( error ) ;
417
429
}
418
430
return next ( new restifyErrors . InternalServerError ( ) ) ;
@@ -427,18 +439,11 @@ export class ShadowsocksManagerService {
427
439
) : Promise < void > {
428
440
try {
429
441
logging . debug ( `setPortForNewAccessKeys request ${ JSON . stringify ( req . params ) } ` ) ;
430
- const port = req . params . port ;
431
- if ( ! port ) {
442
+ const port = validateNumberParam ( req . params . port , 'port' ) ;
443
+ if ( port === undefined ) {
432
444
return next (
433
445
new restifyErrors . MissingParameterError ( { statusCode : 400 } , 'Parameter `port` is missing' )
434
446
) ;
435
- } else if ( typeof port !== 'number' ) {
436
- return next (
437
- new restifyErrors . InvalidArgumentError (
438
- { statusCode : 400 } ,
439
- `Expected a numeric port, instead got ${ port } of type ${ typeof port } `
440
- )
441
- ) ;
442
447
}
443
448
await this . accessKeys . setPortForNewAccessKeys ( port ) ;
444
449
this . serverConfig . data ( ) . portForNewAccessKeys = port ;
@@ -451,6 +456,8 @@ export class ShadowsocksManagerService {
451
456
return next ( new restifyErrors . InvalidArgumentError ( { statusCode : 400 } , error . message ) ) ;
452
457
} else if ( error instanceof errors . PortUnavailable ) {
453
458
return next ( new restifyErrors . ConflictError ( error . message ) ) ;
459
+ } else if ( error instanceof restifyErrors . HttpError ) {
460
+ return next ( error ) ;
454
461
}
455
462
return next ( new restifyErrors . InternalServerError ( error ) ) ;
456
463
}
@@ -556,10 +563,7 @@ export class ShadowsocksManagerService {
556
563
return next ( ) ;
557
564
} catch ( error ) {
558
565
logging . error ( error ) ;
559
- if (
560
- error instanceof restifyErrors . InvalidArgumentError ||
561
- error instanceof restifyErrors . MissingParameterError
562
- ) {
566
+ if ( error instanceof restifyErrors . HttpError ) {
563
567
return next ( error ) ;
564
568
}
565
569
return next ( new restifyErrors . InternalServerError ( ) ) ;
0 commit comments