From e63904b4a7da55ba43a0b9833a75fb9e695a2e8c Mon Sep 17 00:00:00 2001 From: Zhao Xiaohong Date: Wed, 13 Mar 2024 18:25:02 +0800 Subject: [PATCH] WHATWG URL https://nodejs.org/api/url.html#class-url --- README.md | 2 +- config.json | 3 +-- local.js | 24 +++++------------------- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index db52111..98bfb9c 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ $ npm install Then run: ``` -$ node local.js -s still-tor-8707.herokuapp.com -l 1080 -m aes-128-cfb -k foobar -r 80 +$ node local.js -s ws://still-tor-8707.herokuapp.com -l 1080 -m aes-128-cfb -k foobar -r 80 server listening at { address: '127.0.0.1', family: 'IPv4', port: 1080 } ``` diff --git a/config.json b/config.json index 1c0d084..e766e4b 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,6 @@ { - "server": "127.0.0.1", + "server": "ws://127.0.0.1", "local_address": "127.0.0.1", - "scheme": "ws", "local_port": 1080, "remote_port": 8080, "password": "`try*(^^$some^$%^complex>:<>?~password/", diff --git a/local.js b/local.js index b46d916..017ec9c 100644 --- a/local.js +++ b/local.js @@ -1,6 +1,4 @@ import net from 'net'; -import url from 'url'; -import http from 'http'; import fs from 'fs'; import WebSocket from 'ws'; import parseArgs from 'minimist'; @@ -18,14 +16,7 @@ const options = { c: 'config_file', m: 'method', }, - string: [ - 'local_address', - 'server', - 'password', - 'config_file', - 'method', - 'scheme', - ], + string: ['local_address', 'server', 'password', 'config_file', 'method'], default: { config_file: './config.json', }, @@ -39,7 +30,6 @@ for (let k in configFromArgs) { config[k] = v; } -const SCHEME = config.scheme; let SERVER = config.server; const REMOTE_PORT = config.remote_port; const LOCAL_ADDRESS = config.local_address; @@ -54,11 +44,7 @@ if (HTTPPROXY) { } const prepareServer = function (address) { - const serverUrl = url.parse(address); - serverUrl.slashes = true; - if (!serverUrl.protocol) { - serverUrl.protocol = SCHEME; - } + const serverUrl = new URL(address); if (!serverUrl.hostname) { serverUrl.hostname = address; serverUrl.pathname = '/'; @@ -66,7 +52,7 @@ const prepareServer = function (address) { if (!serverUrl.port) { serverUrl.port = REMOTE_PORT; } - return url.format(serverUrl); + return serverUrl.toString(); }; if (SERVER instanceof Array) { @@ -181,11 +167,11 @@ var server = net.createServer(function (connection) { if (HTTPPROXY) { // WebSocket endpoint for the proxy to connect to const endpoint = aServer; - const parsed = url.parse(endpoint); + const parsed = new URL(endpoint); //console.log('attempting to connect to WebSocket %j', endpoint); // create an instance of the `HttpsProxyAgent` class with the proxy server information - const opts = url.parse(HTTPPROXY); + const opts = new URL(HTTPPROXY); // IMPORTANT! Set the `secureEndpoint` option to `false` when connecting // over "ws://", but `true` when connecting over "wss://"