diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b42d25d..3c6a743 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,9 +28,9 @@ jobs: node-version: [14.x] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2.2.0 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} @@ -46,13 +46,13 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - node-version: [14.x, 16.x] + node-version: [14.x, 16.x, 18.x, 20.x] os: [ubuntu-latest, windows-latest, macos-latest] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2.2.0 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} diff --git a/package-lock.json b/package-lock.json index 83880b4..4ab410a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,33 +9,33 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "async": "3.2.x", - "debug": "4.3.x", - "mkdirp": "1.0.x", - "node-forge": "1.3.x", - "semaphore": "1.1.x", - "uuid": "9.0.x", - "ws": "8.11.x", - "yargs": "17.6.x" + "async": "^3.2.5", + "debug": "^4.3.4", + "mkdirp": "^1.0.4", + "node-forge": "^1.3.1", + "semaphore": "^1.1.0", + "uuid": "^9.0.1", + "ws": "^8.14.2", + "yargs": "^17.7.2" }, "bin": { "http-mitm-proxy": "dist/bin/mitm-proxy.js" }, "devDependencies": { - "@types/mocha": "10.0.x", - "@types/node": "18.11.x", - "cross-env": "7.0.x", - "mocha": "10.2.x", - "node-static": "0.7.x", - "prettier": "2.8.x", + "@types/mocha": "^10.0.6", + "@types/node": "^20.10.0", + "cross-env": "^7.0.3", + "mocha": "^10.2.0", + "node-static": "^0.7.11", + "prettier": "^3.1.0", "request": "^2.88.2", - "rimraf": "3.0.x", - "ts-mocha": "10.0.x", - "ts-node": "10.9.x", - "typescript": "4.9.x" + "rimraf": "^3.0.2", + "ts-mocha": "^10.0.0", + "ts-node": "^10.9.1", + "typescript": "^5.3.2" }, "engines": { - "node": ">=8.0 <=16" + "node": ">=16" } }, "node_modules/@cspotcode/source-map-support": { @@ -107,16 +107,19 @@ "optional": true }, "node_modules/@types/mocha": { - "version": "10.0.1", - "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz", - "integrity": "sha512-/fvYntiO1GeICvqbQ3doGDIP97vWmvFt83GKguJ6prmQM2iXZfFcq6YE8KteFyRtX2/h5Hf91BYvPodJKFYv5Q==", + "version": "10.0.6", + "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.6.tgz", + "integrity": "sha512-dJvrYWxP/UcXm36Qn36fxhUKu8A/xMRXVT2cliFF1Z7UA9liG5Psj3ezNSZw+5puH2czDXRLcXQxf8JbJt0ejg==", "dev": true }, "node_modules/@types/node": { - "version": "18.11.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", - "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", - "dev": true + "version": "20.10.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.0.tgz", + "integrity": "sha512-D0WfRmU9TQ8I9PFx9Yc+EBHw+vSpIub4IDvQivcp26PtPrdMGAq5SDcpXEo/epqa/DXotVpekHiLNTg3iaKXBQ==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/acorn": { "version": "8.7.1", @@ -239,9 +242,9 @@ } }, "node_modules/async": { - "version": "3.2.4", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", - "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==" + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", + "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" }, "node_modules/asynckit": { "version": "0.4.0", @@ -1299,15 +1302,15 @@ } }, "node_modules/prettier": { - "version": "2.8.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.1.tgz", - "integrity": "sha512-lqGoSJBQNJidqCHE80vqZJHWHRFoNYsSpP9AjFhlhi9ODCJA541svILes/+/1GM3VaL/abZi7cpFzOpdR9UPKg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.0.tgz", + "integrity": "sha512-TQLvXjq5IAibjh8EpBIkNKxO749UEWABoiIZehEPiY4GNpVdhaFKqSTu+QrlU6D2dPAfubRmtJTi4K4YkQ5eXw==", "dev": true, "bin": { - "prettier": "bin-prettier.js" + "prettier": "bin/prettier.cjs" }, "engines": { - "node": ">=10.13.0" + "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" @@ -1787,18 +1790,24 @@ "dev": true }, "node_modules/typescript": { - "version": "4.9.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", - "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.2.tgz", + "integrity": "sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ==", "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=14.17" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", @@ -1809,9 +1818,13 @@ } }, "node_modules/uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], "bin": { "uuid": "dist/bin/uuid" } @@ -1889,15 +1902,15 @@ "dev": true }, "node_modules/ws": { - "version": "8.11.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz", - "integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==", + "version": "8.14.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", + "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", "engines": { "node": ">=10.0.0" }, "peerDependencies": { "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" + "utf-8-validate": ">=5.0.2" }, "peerDependenciesMeta": { "bufferutil": { @@ -1917,9 +1930,9 @@ } }, "node_modules/yargs": { - "version": "17.6.2", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.6.2.tgz", - "integrity": "sha512-1/9UrdHjDZc0eOU0HxOHoS78C69UD3JRMvzlJ7S79S2nTaWRA/whGCTV8o9e/N/1Va9YIV7Q4sOxD8VV4pCWOw==", + "version": "17.7.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", + "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", "dependencies": { "cliui": "^8.0.1", "escalade": "^3.1.1", diff --git a/package.json b/package.json index 7ad6404..9c3747e 100644 --- a/package.json +++ b/package.json @@ -32,29 +32,29 @@ ], "license": "MIT", "engines": { - "node": ">=8.0 <=16" + "node": ">=16" }, "devDependencies": { - "@types/mocha": "10.0.x", - "@types/node": "18.11.x", - "cross-env": "7.0.x", - "mocha": "10.2.x", - "node-static": "0.7.x", - "prettier": "2.8.x", + "@types/mocha": "^10.0.6", + "@types/node": "^20.10.0", + "cross-env": "^7.0.3", + "mocha": "^10.2.0", + "node-static": "^0.7.11", + "prettier": "^3.1.0", "request": "^2.88.2", - "rimraf": "3.0.x", - "ts-mocha": "10.0.x", - "ts-node": "10.9.x", - "typescript": "4.9.x" + "rimraf": "^3.0.2", + "ts-mocha": "^10.0.0", + "ts-node": "^10.9.1", + "typescript": "^5.3.2" }, "dependencies": { - "async": "3.2.x", - "debug": "4.3.x", - "mkdirp": "1.0.x", - "node-forge": "1.3.x", - "semaphore": "1.1.x", - "uuid": "9.0.x", - "ws": "8.11.x", - "yargs": "17.6.x" + "async": "^3.2.5", + "debug": "^4.3.4", + "mkdirp": "^1.0.4", + "node-forge": "^1.3.1", + "semaphore": "^1.1.0", + "uuid": "^9.0.1", + "ws": "^8.14.2", + "yargs": "^17.7.2" } } diff --git a/test/01_proxy.ts b/test/01_proxy.ts index 7061a6d..6c7f269 100644 --- a/test/01_proxy.ts +++ b/test/01_proxy.ts @@ -7,7 +7,8 @@ import http from "http"; import net from "net"; import nodeStatic from "node-static"; import WebSocket from "ws"; -import { Proxy } from "../lib/proxy"; +import { Proxy } from "../"; +import dns from "dns"; const fileStaticA = new nodeStatic.Server(`${__dirname}/wwwA`); const fileStaticB = new nodeStatic.Server(`${__dirname}/wwwB`); @@ -19,6 +20,10 @@ const testWSPort = 40007; const testUrlA = `http://${testHost}:${testPortA}`; const testUrlB = `http://${testHost}:${testPortB}`; +if (typeof dns.setDefaultResultOrder === "function") { + dns.setDefaultResultOrder("ipv4first"); +} + const getHttp = (url, cb) => { request({ url }, (err, resp, body) => { cb(err, resp, body); @@ -58,12 +63,12 @@ const countString = (str, substr, cb) => { describe("proxy", function () { this.timeout(30000); - let srvA = null; - let srvB = null; - let proxy = null; + let srvA: http.Server | null = null; + let srvB: http.Server | null = null; + let proxy: Proxy | null = null; const testHashes = {}; const testFiles = ["1024.bin"]; - let wss = null; + let wss: WebSocket.Server | null = null; before((done) => { testFiles.forEach((val) => { @@ -116,23 +121,23 @@ describe("proxy", function () { }); afterEach(() => { - proxy.close(); + proxy?.close(); proxy = null; }); after(() => { - srvA.close(); + srvA?.close(); srvA = null; - srvB.close(); + srvB?.close(); srvB = null; - wss.close(); + wss?.close(); wss = null; }); describe("ca server", () => { it("should generate a root CA file", (done) => { fs.access(`${__dirname}/../.http-mitm-proxy/certs/ca.pem`, (err) => { - let rtv = null; + let rtv: string | boolean | null = null; if (err) { rtv = `${__dirname}/../.http-mitm-proxy/certs/ca.pem ${err}`; } else { @@ -300,13 +305,14 @@ describe("proxy", function () { describe("host match", () => { it("proxy and modify AAA 5 times if hostA", (done) => { + assert.ok(proxy); proxy.onRequest((ctx, callback) => { const testHostNameA = `127.0.0.1:${testPortA}`; if (ctx.clientToProxyRequest.headers.host === testHostNameA) { - const chunks = []; + const chunks: Buffer[] = []; ctx.onResponseData((ctx, chunk, callback) => { chunks.push(chunk); - return callback(null, null); + return callback(null, undefined); }); ctx.onResponseEnd((ctx, callback) => { let body = Buffer.concat(chunks).toString(); @@ -359,6 +365,7 @@ describe("proxy", function () { }); it("should use chunked transfer encoding when global onResponseData is active", (done) => { + assert.ok(proxy); proxy.onResponseData((ctx, chunk, callback) => { callback(null, chunk); }); @@ -375,6 +382,7 @@ describe("proxy", function () { }); it("should use chunked transfer encoding when context onResponseData is active", (done) => { + assert.ok(proxy); proxy.onResponse((ctx, callback) => { ctx.onResponseData((ctx, chunk, callback) => { callback(null, chunk); @@ -394,6 +402,7 @@ describe("proxy", function () { }); it("should use chunked transfer encoding when context ResponseFilter is active", (done) => { + assert.ok(proxy); proxy.onResponse((ctx, callback) => { ctx.addResponseFilter(zlib.createGzip()); callback(null); @@ -454,6 +463,7 @@ describe("proxy", function () { close: false, }; + assert.ok(proxy); proxy.onWebSocketConnection((ctx, callback) => { stats.connection = true; return callback(); diff --git a/test/mocha.setup.js b/test/mocha.setup.js new file mode 100644 index 0000000..e69de29