-
Notifications
You must be signed in to change notification settings - Fork 796
/
Copy pathmanager_service.ts
679 lines (630 loc) · 24.4 KB
/
manager_service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as crypto from 'crypto';
import * as ipRegex from 'ip-regex';
import * as restify from 'restify';
import * as restifyErrors from 'restify-errors';
import {makeConfig, SIP002_URI} from 'outline-shadowsocksconfig';
import {JsonConfig} from '../infrastructure/json_config';
import * as logging from '../infrastructure/logging';
import {AccessKey, AccessKeyRepository, DataLimit} from '../model/access_key';
import * as errors from '../model/errors';
import * as version from './version';
import {ManagerMetrics} from './manager_metrics';
import {ServerConfigJson} from './server_config';
import {SharedMetricsPublisher} from './shared_metrics';
import {ShadowsocksServer} from '../model/shadowsocks_server';
interface AccessKeyJson {
// The unique identifier of this access key.
id: string;
// Admin-controlled, editable name for this access key.
name: string;
// Shadowsocks-specific details and credentials.
password: string;
port: number;
method: string;
dataLimit: DataLimit;
accessUrl: string;
}
// Creates a AccessKey response.
function accessKeyToApiJson(accessKey: AccessKey): AccessKeyJson {
return {
id: accessKey.id,
name: accessKey.name,
password: accessKey.proxyParams.password,
port: accessKey.proxyParams.portNumber,
method: accessKey.proxyParams.encryptionMethod,
dataLimit: accessKey.dataLimit,
accessUrl: SIP002_URI.stringify(
makeConfig({
host: accessKey.proxyParams.hostname,
port: accessKey.proxyParams.portNumber,
method: accessKey.proxyParams.encryptionMethod,
password: accessKey.proxyParams.password,
outline: 1,
})
),
};
}
// Type to reflect that we receive untyped JSON request parameters.
interface RequestParams {
// Supported parameters:
// id: string
// name: string
// metricsEnabled: boolean
// limit: DataLimit
// port: number
// hours: number
// method: string
[param: string]: unknown;
}
// Simplified request and response type interfaces containing only the
// properties we actually use, to make testing easier.
interface RequestType {
params: RequestParams;
}
interface ResponseType {
send(code: number, data?: {}): void;
}
enum HttpSuccess {
OK = 200,
NO_CONTENT = 204,
}
// Similar to String.startsWith(), but constant-time.
function timingSafeStartsWith(input: string, prefix: string): boolean {
const prefixBuf = Buffer.from(prefix);
const inputBuf = Buffer.from(input);
const L = Math.min(inputBuf.length, prefixBuf.length);
const inputOverlap = inputBuf.slice(0, L);
const prefixOverlap = prefixBuf.slice(0, L);
const match = crypto.timingSafeEqual(inputOverlap, prefixOverlap);
return inputBuf.length >= prefixBuf.length && match;
}
// Returns a pre-routing hook that injects a 404 if the request does not
// start with `apiPrefix`. This filter runs in constant time.
function prefixFilter(apiPrefix: string): restify.RequestHandler {
return (req: restify.Request, res: restify.Response, next: restify.Next) => {
if (timingSafeStartsWith(req.path(), apiPrefix)) {
return next();
}
// This error matches the router's default 404 response.
next(new restifyErrors.ResourceNotFoundError('%s does not exist', req.path()));
};
}
export function bindService(
apiServer: restify.Server,
apiPrefix: string,
service: ShadowsocksManagerService
) {
// Reject unauthorized requests in constant time before they reach the routing step.
apiServer.pre(prefixFilter(apiPrefix));
apiServer.put(`${apiPrefix}/name`, service.renameServer.bind(service));
apiServer.get(`${apiPrefix}/server`, service.getServer.bind(service));
apiServer.put(
`${apiPrefix}/server/access-key-data-limit`,
service.setDefaultDataLimit.bind(service)
);
apiServer.del(
`${apiPrefix}/server/access-key-data-limit`,
service.removeDefaultDataLimit.bind(service)
);
apiServer.put(
`${apiPrefix}/server/hostname-for-access-keys`,
service.setHostnameForAccessKeys.bind(service)
);
apiServer.put(
`${apiPrefix}/server/port-for-new-access-keys`,
service.setPortForNewAccessKeys.bind(service)
);
apiServer.post(`${apiPrefix}/access-keys`, service.createNewAccessKey.bind(service));
apiServer.put(`${apiPrefix}/access-keys/:id`, service.createAccessKey.bind(service));
apiServer.get(`${apiPrefix}/access-keys`, service.listAccessKeys.bind(service));
apiServer.get(`${apiPrefix}/access-keys/:id`, service.getAccessKey.bind(service));
apiServer.del(`${apiPrefix}/access-keys/:id`, service.removeAccessKey.bind(service));
apiServer.put(`${apiPrefix}/access-keys/:id/name`, service.renameAccessKey.bind(service));
apiServer.put(
`${apiPrefix}/access-keys/:id/data-limit`,
service.setAccessKeyDataLimit.bind(service)
);
apiServer.del(
`${apiPrefix}/access-keys/:id/data-limit`,
service.removeAccessKeyDataLimit.bind(service)
);
apiServer.get(`${apiPrefix}/metrics/transfer`, service.getDataUsage.bind(service));
apiServer.get(`${apiPrefix}/metrics/enabled`, service.getShareMetrics.bind(service));
apiServer.put(`${apiPrefix}/metrics/enabled`, service.setShareMetrics.bind(service));
// Experimental APIs.
apiServer.put(
`${apiPrefix}/experimental/asn-metrics/enabled`,
service.enableAsnMetrics.bind(service)
);
// Redirect former experimental APIs
apiServer.put(
`${apiPrefix}/experimental/access-key-data-limit`,
redirect(`${apiPrefix}/server/access-key-data-limit`)
);
apiServer.del(
`${apiPrefix}/experimental/access-key-data-limit`,
redirect(`${apiPrefix}/server/access-key-data-limit`)
);
}
// Returns a request handler that redirects a bound request path to `url` with HTTP status code 308.
function redirect(url: string): restify.RequestHandlerType {
return (req: restify.Request, res: restify.Response, next: restify.Next) => {
logging.debug(`Redirecting ${req.url} => ${url}`);
res.redirect(308, url, next);
};
}
function validateAccessKeyId(accessKeyId: unknown): string {
if (!accessKeyId) {
throw new restifyErrors.MissingParameterError({statusCode: 400}, 'Parameter `id` is missing');
} else if (typeof accessKeyId !== 'string') {
throw new restifyErrors.InvalidArgumentError(
{statusCode: 400},
'Parameter `id` must be of type string'
);
}
return accessKeyId;
}
function validateDataLimit(limit: unknown): DataLimit | undefined {
if (typeof limit === 'undefined') {
return undefined;
}
const bytes = (limit as DataLimit).bytes;
if (!(Number.isInteger(bytes) && bytes >= 0)) {
throw new restifyErrors.InvalidArgumentError(
{statusCode: 400},
'`limit.bytes` must be an non-negative integer'
);
}
return limit as DataLimit;
}
function validateStringParam(param: unknown, paramName: string): string | undefined {
if (typeof param === 'undefined') {
return undefined;
}
if (typeof param !== 'string') {
throw new restifyErrors.InvalidArgumentError(
{statusCode: 400},
`Expected a string for ${paramName}, instead got ${param} of type ${typeof param}`
);
}
return param;
}
function validateNumberParam(param: unknown, paramName: string): number | undefined {
if (typeof param === 'undefined') {
return undefined;
}
if (typeof param !== 'number') {
throw new restifyErrors.InvalidArgumentError(
{statusCode: 400},
`Expected a number for ${paramName}, instead got ${param} of type ${typeof param}`
);
}
return param;
}
// The ShadowsocksManagerService manages the access keys that can use the server
// as a proxy using Shadowsocks. It runs an instance of the Shadowsocks server
// for each existing access key, with the port and password assigned for that access key.
export class ShadowsocksManagerService {
constructor(
private defaultServerName: string,
private serverConfig: JsonConfig<ServerConfigJson>,
private accessKeys: AccessKeyRepository,
private shadowsocksServer: ShadowsocksServer,
private managerMetrics: ManagerMetrics,
private metricsPublisher: SharedMetricsPublisher
) {}
public renameServer(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`renameServer request ${JSON.stringify(req.params)}`);
const name = req.params.name;
if (!name) {
return next(
new restifyErrors.MissingParameterError({statusCode: 400}, 'Parameter `name` is missing')
);
}
if (typeof name !== 'string' || name.length > 100) {
next(
new restifyErrors.InvalidArgumentError(
`Requested server name should be a string <= 100 characters long. Got ${name}`
)
);
return;
}
this.serverConfig.data().name = name;
this.serverConfig.write();
res.send(HttpSuccess.NO_CONTENT);
next();
}
public getServer(req: RequestType, res: ResponseType, next: restify.Next): void {
res.send(HttpSuccess.OK, {
name: this.serverConfig.data().name || this.defaultServerName,
serverId: this.serverConfig.data().serverId,
metricsEnabled: this.serverConfig.data().metricsEnabled || false,
createdTimestampMs: this.serverConfig.data().createdTimestampMs,
version: version.getPackageVersion(),
accessKeyDataLimit: this.serverConfig.data().accessKeyDataLimit,
portForNewAccessKeys: this.serverConfig.data().portForNewAccessKeys,
hostnameForAccessKeys: this.serverConfig.data().hostname,
experimental: this.serverConfig.data().experimental,
});
next();
}
// Changes the server's hostname. Hostname must be a valid domain or IP address
public setHostnameForAccessKeys(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`changeHostname request: ${JSON.stringify(req.params)}`);
const hostname = req.params.hostname;
if (typeof hostname === 'undefined') {
return next(
new restifyErrors.MissingParameterError({statusCode: 400}, 'hostname must be provided')
);
}
if (typeof hostname !== 'string') {
return next(
new restifyErrors.InvalidArgumentError(
{statusCode: 400},
`Expected hostname to be a string, instead got ${hostname} of type ${typeof hostname}`
)
);
}
// Hostnames can have any number of segments of alphanumeric characters and hyphens, separated
// by periods. No segment may start or end with a hyphen.
const hostnameRegex =
/^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)*[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?$/;
if (!hostnameRegex.test(hostname) && !ipRegex({includeBoundaries: true}).test(hostname)) {
return next(
new restifyErrors.InvalidArgumentError(
{statusCode: 400},
`Hostname ${hostname} isn't a valid hostname or IP address`
)
);
}
this.serverConfig.data().hostname = hostname;
this.serverConfig.write();
this.accessKeys.setHostname(hostname);
res.send(HttpSuccess.NO_CONTENT);
next();
}
// Get a access key
public getAccessKey(req: RequestType, res: ResponseType, next: restify.Next): void {
try {
logging.debug(`getAccessKey request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
const accessKey = this.accessKeys.getAccessKey(accessKeyId);
const accessKeyJson = accessKeyToApiJson(accessKey);
logging.debug(`getAccessKey response ${JSON.stringify(accessKeyJson)}`);
res.send(HttpSuccess.OK, accessKeyJson);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyNotFound) {
return next(new restifyErrors.NotFoundError(error.message));
}
return next(error);
}
}
// Lists all access keys
public listAccessKeys(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`listAccessKeys request ${JSON.stringify(req.params)}`);
const response = {accessKeys: []};
for (const accessKey of this.accessKeys.listAccessKeys()) {
response.accessKeys.push(accessKeyToApiJson(accessKey));
}
logging.debug(`listAccessKeys response ${JSON.stringify(response)}`);
res.send(HttpSuccess.OK, response);
return next();
}
private async createAccessKeyFromRequest(req: RequestType, id?: string): Promise<AccessKeyJson> {
try {
const encryptionMethod = validateStringParam(req.params.method || '', 'encryptionMethod');
const name = validateStringParam(req.params.name || '', 'name');
const dataLimit = validateDataLimit(req.params.limit);
const password = validateStringParam(req.params.password, 'password');
const portNumber = validateNumberParam(req.params.port, 'port');
const accessKeyJson = accessKeyToApiJson(
await this.accessKeys.createNewAccessKey({
encryptionMethod,
id,
name,
dataLimit,
password,
portNumber,
})
);
return accessKeyJson;
} catch (error) {
logging.error(error);
if (error instanceof errors.InvalidCipher || error instanceof errors.InvalidPortNumber) {
throw new restifyErrors.InvalidArgumentError({statusCode: 400}, error.message);
} else if (error instanceof errors.PortUnavailable) {
throw new restifyErrors.ConflictError(error.message);
}
throw error;
}
}
// Creates a new access key
public async createNewAccessKey(
req: RequestType,
res: ResponseType,
next: restify.Next
): Promise<void> {
try {
logging.debug(`createNewAccessKey request ${JSON.stringify(req.params)}`);
if (req.params.id) {
return next(
new restifyErrors.InvalidArgumentError({statusCode: 400}, 'Parameter `id` is not allowed')
);
}
const accessKeyJson = await this.createAccessKeyFromRequest(req);
res.send(201, accessKeyJson);
logging.debug(`createNewAccessKey response ${JSON.stringify(accessKeyJson)}`);
return next();
} catch (error) {
logging.error(error);
if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError());
}
}
// Creates an access key with a specific identifier
public async createAccessKey(
req: RequestType,
res: ResponseType,
next: restify.Next
): Promise<void> {
try {
logging.debug(`createAccessKey request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
const accessKeyJson = await this.createAccessKeyFromRequest(req, accessKeyId);
res.send(201, accessKeyJson);
logging.debug(`createAccessKey response ${JSON.stringify(accessKeyJson)}`);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyConflict) {
return next(new restifyErrors.ConflictError(error.message));
}
if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError());
}
}
// Sets the default ports for new access keys
public async setPortForNewAccessKeys(
req: RequestType,
res: ResponseType,
next: restify.Next
): Promise<void> {
try {
logging.debug(`setPortForNewAccessKeys request ${JSON.stringify(req.params)}`);
const port = validateNumberParam(req.params.port, 'port');
if (port === undefined) {
return next(
new restifyErrors.MissingParameterError({statusCode: 400}, 'Parameter `port` is missing')
);
}
await this.accessKeys.setPortForNewAccessKeys(port);
this.serverConfig.data().portForNewAccessKeys = port;
this.serverConfig.write();
res.send(HttpSuccess.NO_CONTENT);
next();
} catch (error) {
logging.error(error);
if (error instanceof errors.InvalidPortNumber) {
return next(new restifyErrors.InvalidArgumentError({statusCode: 400}, error.message));
} else if (error instanceof errors.PortUnavailable) {
return next(new restifyErrors.ConflictError(error.message));
} else if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError(error));
}
}
// Removes an existing access key
public removeAccessKey(req: RequestType, res: ResponseType, next: restify.Next): void {
try {
logging.debug(`removeAccessKey request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
this.accessKeys.removeAccessKey(accessKeyId);
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyNotFound) {
return next(new restifyErrors.NotFoundError(error.message));
} else if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError());
}
}
public renameAccessKey(req: RequestType, res: ResponseType, next: restify.Next): void {
try {
logging.debug(`renameAccessKey request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
const name = req.params.name;
if (!name) {
return next(
new restifyErrors.MissingParameterError({statusCode: 400}, 'Parameter `name` is missing')
);
} else if (typeof name !== 'string') {
return next(
new restifyErrors.InvalidArgumentError(
{statusCode: 400},
'Parameter `name` must be of type string'
)
);
}
this.accessKeys.renameAccessKey(accessKeyId, name);
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyNotFound) {
return next(new restifyErrors.NotFoundError(error.message));
} else if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError());
}
}
public async setAccessKeyDataLimit(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`setAccessKeyDataLimit request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
const limit = validateDataLimit(req.params.limit);
// Enforcement is done asynchronously in the proxy server. This is transparent to the manager
// so this doesn't introduce any race conditions between the server and UI.
this.accessKeys.setAccessKeyDataLimit(accessKeyId, limit);
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyNotFound) {
return next(new restifyErrors.NotFoundError(error.message));
}
return next(error);
}
}
public async removeAccessKeyDataLimit(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`removeAccessKeyDataLimit request ${JSON.stringify(req.params)}`);
const accessKeyId = validateAccessKeyId(req.params.id);
// Enforcement is done asynchronously in the proxy server. This is transparent to the manager
// so this doesn't introduce any race conditions between the server and UI.
this.accessKeys.removeAccessKeyDataLimit(accessKeyId);
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
if (error instanceof errors.AccessKeyNotFound) {
return next(new restifyErrors.NotFoundError(error.message));
}
return next(error);
}
}
public async setDefaultDataLimit(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`setDefaultDataLimit request ${JSON.stringify(req.params)}`);
const limit = validateDataLimit(req.params.limit);
// Enforcement is done asynchronously in the proxy server. This is transparent to the manager
// so this doesn't introduce any race conditions between the server and UI.
this.accessKeys.setDefaultDataLimit(limit);
this.serverConfig.data().accessKeyDataLimit = limit;
this.serverConfig.write();
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
if (error instanceof restifyErrors.HttpError) {
return next(error);
}
return next(new restifyErrors.InternalServerError());
}
}
public async removeDefaultDataLimit(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`removeDefaultDataLimit request ${JSON.stringify(req.params)}`);
// Enforcement is done asynchronously in the proxy server. This is transparent to the manager
// so this doesn't introduce any race conditions between the server and UI.
this.accessKeys.removeDefaultDataLimit();
delete this.serverConfig.data().accessKeyDataLimit;
this.serverConfig.write();
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
return next(new restifyErrors.InternalServerError());
}
}
public async getDataUsage(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`getDataUsage request ${JSON.stringify(req.params)}`);
const response = await this.managerMetrics.getOutboundByteTransfer({hours: 30 * 24});
res.send(HttpSuccess.OK, response);
logging.debug(`getDataUsage response ${JSON.stringify(response)}`);
return next();
} catch (error) {
logging.error(error);
return next(new restifyErrors.InternalServerError());
}
}
public getShareMetrics(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`getShareMetrics request ${JSON.stringify(req.params)}`);
const response = {metricsEnabled: this.metricsPublisher.isSharingEnabled()};
res.send(HttpSuccess.OK, response);
logging.debug(`getShareMetrics response: ${JSON.stringify(response)}`);
next();
}
public setShareMetrics(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`setShareMetrics request ${JSON.stringify(req.params)}`);
const metricsEnabled = req.params.metricsEnabled;
if (metricsEnabled === undefined || metricsEnabled === null) {
return next(
new restifyErrors.MissingParameterError(
{statusCode: 400},
'Parameter `metricsEnabled` is missing'
)
);
} else if (typeof metricsEnabled !== 'boolean') {
return next(
new restifyErrors.InvalidArgumentError(
{statusCode: 400},
'Parameter `metricsEnabled` must be a boolean'
)
);
}
if (metricsEnabled) {
this.metricsPublisher.startSharing();
} else {
this.metricsPublisher.stopSharing();
}
res.send(HttpSuccess.NO_CONTENT);
next();
}
public enableAsnMetrics(req: RequestType, res: ResponseType, next: restify.Next): void {
try {
logging.debug(`enableAsnMetrics request ${JSON.stringify(req.params)}`);
const asnMetricsEnabled = req.params.asnMetricsEnabled;
if (asnMetricsEnabled === undefined || asnMetricsEnabled === null) {
return next(
new restifyErrors.MissingParameterError(
{statusCode: 400},
'Parameter `asnMetricsEnabled` is missing'
)
);
} else if (typeof asnMetricsEnabled !== 'boolean') {
return next(
new restifyErrors.InvalidArgumentError(
{statusCode: 400},
'Parameter `asnMetricsEnabled` must be a boolean'
)
);
}
this.shadowsocksServer.enableAsnMetrics(asnMetricsEnabled);
if (this.serverConfig.data().experimental === undefined) {
this.serverConfig.data().experimental = {};
}
this.serverConfig.data().experimental.asnMetricsEnabled = asnMetricsEnabled;
this.serverConfig.write();
res.send(HttpSuccess.NO_CONTENT);
return next();
} catch (error) {
logging.error(error);
return next(new restifyErrors.InternalServerError());
}
}
}