From f24dc2c63c509175736b4b8bfac8fffebdf988aa Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sat, 13 Aug 2022 00:40:24 +0100 Subject: [PATCH] Remove unidentified-script user agent when running the API proxy server --- src/cli/nso/http-server.ts | 4 ++-- src/util/useragent.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cli/nso/http-server.ts b/src/cli/nso/http-server.ts index 885b61f..abe8b88 100644 --- a/src/cli/nso/http-server.ts +++ b/src/cli/nso/http-server.ts @@ -14,7 +14,7 @@ import { NotificationManager, PresenceEvent, ZncNotifications } from '../../comm import { product } from '../../util/product.js'; import { parseListenAddress } from '../../util/net.js'; import { AuthPolicy, AuthToken, ZncPresenceEventStreamEvent } from '../../api/znc-proxy.js'; -import { addUserAgent } from '../../util/useragent.js'; +import { addCliFeatureUserAgent } from '../../util/useragent.js'; import { ErrorResponse } from '../../api/util.js'; declare global { @@ -54,7 +54,7 @@ export function builder(yargs: Argv) { type Arguments = YargsArguments>; export async function handler(argv: ArgumentsCamelCase) { - addUserAgent('http-server'); + addCliFeatureUserAgent('http-server'); const storage = await initStorage(argv.dataPath); diff --git a/src/util/useragent.ts b/src/util/useragent.ts index 0540df1..bc644ac 100644 --- a/src/util/useragent.ts +++ b/src/util/useragent.ts @@ -17,6 +17,21 @@ export function addUserAgent(...useragent: string[]) { additional_useragents.push(...useragent); } +/** + * Only used by cli/nso/http-server.ts. + * This command is intended to be run automatically and doesn't make any requests itself, so this function removes + * the unidentified-script user agent string. + */ +export function addCliFeatureUserAgent(...useragent: string[]) { + if (additional_useragents[0] === 'nxapi-cli' && additional_useragents[1] === 'unidentified-script') { + additional_useragents.splice(1, 1, ...useragent); + } else if (additional_useragents[0] === 'nxapi-cli') { + additional_useragents.splice(1, 0, ...useragent); + } else { + additional_useragents.splice(0, 0, ...useragent); + } +} + let warned_no_useragent = false; export function getUserAgent(additional = getAdditionalUserAgents()) {