From 595f0eddf19e8e67aff4ecf56f5ec76cedc41222 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 16 Aug 2021 16:12:03 -0700 Subject: [PATCH] Fix deprecations --- lib/api-functions.ts | 3 +-- lib/connection-request.ts | 26 +++++++++++++------------- lib/debug.ts | 27 ++++++++++++--------------- lib/fs.ts | 3 +-- lib/nbi.ts | 27 +++++++++++++-------------- lib/ui/api.ts | 3 +-- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/lib/api-functions.ts b/lib/api-functions.ts index e98e2dc..eeeb2a2 100644 --- a/lib/api-functions.ts +++ b/lib/api-functions.ts @@ -17,7 +17,6 @@ * along with GenieACS. If not, see . */ -import { parse } from "url"; import * as db from "./db"; import * as common from "./common"; import * as cache from "./cache"; @@ -80,7 +79,7 @@ export async function connectionRequest( ).value || [""])[0]; } - const remoteAddress = parse(connectionRequestUrl).host; + const remoteAddress = new URL(connectionRequestUrl).hostname; const evalCallback = (exp): Expression => { if (!Array.isArray(exp)) return exp; diff --git a/lib/connection-request.ts b/lib/connection-request.ts index 686243c..e20595d 100644 --- a/lib/connection-request.ts +++ b/lib/connection-request.ts @@ -19,7 +19,7 @@ import * as crypto from "crypto"; import * as dgram from "dgram"; -import { parse } from "url"; +import { URL } from "url"; import * as http from "http"; import { evaluateAsync } from "./common/expression"; import { Expression } from "./types"; @@ -61,24 +61,26 @@ async function extractAuth( } function httpGet( + url: URL, options: http.RequestOptions, _debug: boolean, deviceId: string ): Promise<{ statusCode: number; headers: http.IncomingHttpHeaders }> { return new Promise((resolve, reject) => { const req = http - .get(options, (res) => { + .get(url, options, (res) => { res.resume(); resolve({ statusCode: res.statusCode, headers: res.headers }); if (_debug) { - debug.outgoingHttpRequest(req, deviceId, options, null); + debug.outgoingHttpRequest(req, deviceId, "GET", url, null); debug.incomingHttpResponse(res, deviceId, null); } }) .on("error", (err) => { req.destroy(); reject(err); - if (_debug) debug.outgoingHttpRequestError(req, deviceId, options, err); + if (_debug) + debug.outgoingHttpRequestError(req, deviceId, "GET", url, err); }) .on("timeout", () => { req.destroy(); @@ -94,15 +96,13 @@ export async function httpConnectionRequest( _debug: boolean, deviceId: string ): Promise { - const options: http.RequestOptions = parse(address); - if (options.protocol !== "http:") + const url = new URL(address); + if (url.protocol !== "http:") return "Invalid connection request URL or protocol"; - options.agent = new http.Agent({ - maxSockets: 1, - keepAlive: true, - timeout: timeout, - }); + const options: http.RequestOptions = { + agent: new http.Agent({ maxSockets: 1, keepAlive: true, timeout: timeout }), + }; let authHeader: Record; let username: string; @@ -145,12 +145,12 @@ export async function httpConnectionRequest( let res: { statusCode: number; headers: http.IncomingHttpHeaders }; try { - res = await httpGet(opts, _debug, deviceId); + res = await httpGet(url, opts, _debug, deviceId); } catch (err) { // Workaround for some devices unexpectedly closing the connection if (authHeader) { try { - res = await httpGet(opts, _debug, deviceId); + res = await httpGet(url, opts, _debug, deviceId); } catch (err) { return `Connection request error: ${err.message}`; } diff --git a/lib/debug.ts b/lib/debug.ts index d2a8de1..e1dee3c 100644 --- a/lib/debug.ts +++ b/lib/debug.ts @@ -17,12 +17,7 @@ * along with GenieACS. If not, see . */ -import { - IncomingMessage, - ServerResponse, - ClientRequest, - RequestOptions, -} from "http"; +import { IncomingMessage, ServerResponse, ClientRequest } from "http"; import { Socket } from "net"; import { appendFileSync } from "fs"; import { stringify } from "./common/yaml"; @@ -99,7 +94,8 @@ export function outgoingHttpResponse( export function outgoingHttpRequest( httpRequest: ClientRequest, deviceId: string, - options: RequestOptions, + method: "GET" | "PUT" | "POST" | "DELETE", + url: URL, body: string ): void { if (!DEBUG_FILE) return; @@ -111,9 +107,9 @@ export function outgoingHttpRequest( remoteAddress: con.remoteAddress, deviceId: deviceId, connection: getConnectionTimestamp(con), - remotePort: options.port, - method: options.method || "GET", - url: options.path, + remotePort: url.port, + method: method, + url: url.pathname + url.search, headers: httpRequest.getHeaders(), body: body, }; @@ -128,7 +124,8 @@ export function outgoingHttpRequest( export function outgoingHttpRequestError( httpRequest: ClientRequest, deviceId: string, - options: RequestOptions, + method: "GET" | "PUT" | "POST" | "DELETE", + url: URL, err: Error ): void { if (!DEBUG_FILE) return; @@ -136,12 +133,12 @@ export function outgoingHttpRequestError( const msg = { event: "outgoing HTTP request", timestamp: now, - remoteAddress: options.hostname, + remoteAddress: url.hostname, deviceId: deviceId, connection: null, - remotePort: options.port, - method: options.method, - url: options.path, + remotePort: url.port, + method: method, + url: url.pathname + url.search, headers: httpRequest.getHeaders(), error: err.message, }; diff --git a/lib/fs.ts b/lib/fs.ts index 22a6f66..f1bf1fb 100644 --- a/lib/fs.ts +++ b/lib/fs.ts @@ -18,7 +18,6 @@ */ import * as url from "url"; -import * as querystring from "querystring"; import { IncomingMessage, ServerResponse } from "http"; import { Collection, GridFSBucket } from "mongodb"; import { onConnect } from "./db"; @@ -64,7 +63,7 @@ export async function listener( } const urlParts = url.parse(request.url, true); - const filename = querystring.unescape(urlParts.pathname.substring(1)); + const filename = decodeURIComponent(urlParts.pathname.substring(1)); const log = { message: "Fetch file", diff --git a/lib/nbi.ts b/lib/nbi.ts index 1c8e0c0..3230409 100644 --- a/lib/nbi.ts +++ b/lib/nbi.ts @@ -19,7 +19,6 @@ import * as url from "url"; import { Collection, GridFSBucket, ObjectId } from "mongodb"; -import * as querystring from "querystring"; import * as vm from "vm"; import * as config from "./config"; import { onConnect } from "./db"; @@ -103,13 +102,13 @@ export function listener( logger.accessInfo( Object.assign({}, urlParts.query, { - remoteAddress: request.connection.remoteAddress, + remoteAddress: request.socket.remoteAddress, message: `${request.method} ${urlParts.pathname}`, }) ); if (PRESETS_REGEX.test(urlParts.pathname)) { - const presetName = querystring.unescape( + const presetName = decodeURIComponent( PRESETS_REGEX.exec(urlParts.pathname)[1] ); if (request.method === "PUT") { @@ -156,7 +155,7 @@ export function listener( response.end("405 Method Not Allowed"); } } else if (OBJECTS_REGEX.test(urlParts.pathname)) { - const objectName = querystring.unescape( + const objectName = decodeURIComponent( OBJECTS_REGEX.exec(urlParts.pathname)[1] ); if (request.method === "PUT") { @@ -203,7 +202,7 @@ export function listener( response.end("405 Method Not Allowed"); } } else if (PROVISIONS_REGEX.test(urlParts.pathname)) { - const provisionName = querystring.unescape( + const provisionName = decodeURIComponent( PROVISIONS_REGEX.exec(urlParts.pathname)[1] ); if (request.method === "PUT") { @@ -261,7 +260,7 @@ export function listener( response.end("405 Method Not Allowed"); } } else if (VIRTUAL_PARAMETERS_REGEX.test(urlParts.pathname)) { - const virtualParameterName = querystring.unescape( + const virtualParameterName = decodeURIComponent( VIRTUAL_PARAMETERS_REGEX.exec(urlParts.pathname)[1] ); if (request.method === "PUT") { @@ -323,8 +322,8 @@ export function listener( } } else if (TAGS_REGEX.test(urlParts.pathname)) { const r = TAGS_REGEX.exec(urlParts.pathname); - const deviceId = querystring.unescape(r[1]); - const tag = querystring.unescape(r[2]); + const deviceId = decodeURIComponent(r[1]); + const tag = decodeURIComponent(r[2]); if (request.method === "POST") { collections.devices.updateOne( { _id: deviceId }, @@ -352,7 +351,7 @@ export function listener( } } else if (FAULTS_REGEX.test(urlParts.pathname)) { if (request.method === "DELETE") { - const faultId = querystring.unescape( + const faultId = decodeURIComponent( FAULTS_REGEX.exec(urlParts.pathname)[1] ); const deviceId = faultId.split(":", 1)[0]; @@ -397,7 +396,7 @@ export function listener( } } else if (DEVICE_TASKS_REGEX.test(urlParts.pathname)) { if (request.method === "POST") { - const deviceId = querystring.unescape( + const deviceId = decodeURIComponent( DEVICE_TASKS_REGEX.exec(urlParts.pathname)[1] ); if (body.length) { @@ -564,7 +563,7 @@ export function listener( } } else if (TASKS_REGEX.test(urlParts.pathname)) { const r = TASKS_REGEX.exec(urlParts.pathname); - const taskId = querystring.unescape(r[1]); + const taskId = decodeURIComponent(r[1]); const action = r[2]; if (!action || action === "/") { if (request.method === "DELETE") { @@ -650,7 +649,7 @@ export function listener( response.end(); } } else if (FILES_REGEX.test(urlParts.pathname)) { - const filename = querystring.unescape( + const filename = decodeURIComponent( FILES_REGEX.exec(urlParts.pathname)[1] ); if (request.method === "PUT") { @@ -697,7 +696,7 @@ export function listener( response.end("405 Method Not Allowed"); } } else if (PING_REGEX.test(urlParts.pathname)) { - const host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1]); + const host = decodeURIComponent(PING_REGEX.exec(urlParts.pathname)[1]); ping(host, (err, res, stdout) => { if (err) { if (!res) { @@ -723,7 +722,7 @@ export function listener( return; } - const deviceId = querystring.unescape( + const deviceId = decodeURIComponent( DELETE_DEVICE_REGEX.exec(urlParts.pathname)[1] ); apiFunctions diff --git a/lib/ui/api.ts b/lib/ui/api.ts index 8d279c2..8867f58 100644 --- a/lib/ui/api.ts +++ b/lib/ui/api.ts @@ -30,7 +30,6 @@ import { generateSalt, hashPassword } from "../auth"; import { del } from "../cache"; import Authorizer from "../common/authorizer"; import { ping } from "../ping"; -import * as url from "url"; import { decodeTag } from "../common"; import { stringify as yamlStringify } from "../common/yaml"; @@ -579,7 +578,7 @@ router.post("/devices/:id/tasks", async (ctx) => { if (exp[1] === "REMOTE_ADDRESS") { for (const root of ["InternetGatewayDevice", "Device"]) { const p = device[`${root}.ManagementServer.ConnectionRequestURL`]; - if (p && p.value) return url.parse(p.value[0]).host; + if (p && p.value) return new URL(p.value[0]).hostname; } return null; }