Skip to content

Commit

Permalink
http2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrluanma committed Mar 14, 2024
1 parent a026833 commit cf83b15
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 106 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"server": "ws://127.0.0.1",
"server": "http://127.0.0.1",
"local_address": "127.0.0.1",
"local_port": 1080,
"remote_port": 8080,
Expand Down
48 changes: 9 additions & 39 deletions local.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import net from 'net';
import fs from 'fs';
import WebSocket, {createWebSocketStream} from 'ws';
import http2 from 'node:http2';
import parseArgs from 'minimist';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {Encryptor} from './encrypt.js';
import {inetNtoa, createTransform} from './utils.js';
import {pipeline} from 'node:stream/promises';
Expand Down Expand Up @@ -37,12 +36,6 @@ const LOCAL_ADDRESS = config.local_address;
const PORT = config.local_port;
const KEY = config.password;
let METHOD = config.method;
const timeout = Math.floor(config.timeout * 1000);
const HTTPPROXY = process.env.http_proxy;

if (HTTPPROXY) {
console.log('http proxy:', HTTPPROXY);
}

const prepareServer = function (address) {
const serverUrl = new URL(address);
Expand Down Expand Up @@ -76,7 +69,6 @@ var server = net.createServer(async (conn) => {
console.log('concurrent connections:', count);
});
const encryptor = new Encryptor(KEY, METHOD);
let ws;
let remoteAddr = null;
let remotePort = null;
let addrToSend = '';
Expand Down Expand Up @@ -163,44 +155,22 @@ var server = net.createServer(async (conn) => {
buf.write('\u0000\u0000\u0000\u0000', 4, 4, 'binary');
buf.writeUInt16BE(remotePort, 8);
conn.write(buf);
// connect to remote server
// ws = new WebSocket aServer, protocol: "binary"

if (HTTPPROXY) {
// WebSocket endpoint for the proxy to connect to
const endpoint = aServer;
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 = new URL(HTTPPROXY);

// IMPORTANT! Set the `secureEndpoint` option to `false` when connecting
// over "ws://", but `true` when connecting over "wss://"
opts.secureEndpoint = parsed.protocol ? parsed.protocol == 'wss:' : false;

const agent = new HttpsProxyAgent(opts);

ws = new WebSocket(aServer, {
protocol: 'binary',
agent,
});
} else {
ws = new WebSocket(aServer, {
protocol: 'binary',
});
}

const wss = createWebSocketStream(ws);
// connect to remote server
const h2c = http2.connect(aServer);
const out = h2c.request(
{':path': '/', ':method': 'post'},
{endStream: false},
);
console.log(`connecting ${remoteAddr} via ${aServer}`);

const writable = createTransform(encryptor.encrypt.bind(encryptor));
writable.pipe(wss);
writable.pipe(out);
writable.write(data.subarray(3));
pipeline(conn, writable).catch(
(e) => e.name !== 'AbortError' && console.error(`local: ${e}`),
);
pipeline(wss, createTransform(encryptor.decrypt.bind(encryptor)), conn).catch(
pipeline(out, createTransform(encryptor.decrypt.bind(encryptor)), conn).catch(
(e) => e.name !== 'AbortError' && console.error(`local: ${e}`),
);
});
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"version": "0.9.10",
"type": "module",
"dependencies": {
"bufferutil": "^4.0.1",
"https-proxy-agent": "^7.0.4",
"minimist": "^1.2.0",
"ws": "^8.16.0"
"minimist": "^1.2.0"
},
"scripts": {
"start": "node server.js -b 0.0.0.0",
Expand Down
21 changes: 4 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import net from 'net';
import fs from 'fs';
import http from 'http';
import {WebSocketServer, createWebSocketStream} from 'ws';
import http2 from 'node:http2';
import parseArgs from 'minimist';
import {Encryptor} from './encrypt.js';
import {inetNtoa, createTransform} from './utils.js';
Expand Down Expand Up @@ -46,25 +45,13 @@ const PORT = config.remote_port;
const KEY = config.password;
let METHOD = config.method;

const server = http.createServer(function (_, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('asdf.');
});

const wsserver = new WebSocketServer({
server,
autoPong: true,
allowSynchronousEvents: true,
perMessageDeflate: false,
});

wsserver.on('connection', async (ws) => {
console.log('concurrent connections:', wsserver.clients.size);
const server = http2.createServer();
server.on('stream', async (conn, _) => {
console.log('server connected');
const encryptor = new Encryptor(KEY, METHOD);
let remoteAddr;
let remotePort;

const conn = createWebSocketStream(ws);
const readable = conn.pipe(
createTransform(encryptor.decrypt.bind(encryptor)),
);
Expand Down
45 changes: 0 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: E3146EB5D1A7B6E0-9e360d3d2def5fac-22F8EAE3B40BE372-8d61ebf8f267f773


agent-base@^7.0.2:
version "7.1.0"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-7.1.0.tgz"
integrity sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg==
dependencies:
debug "^4.3.4"

bufferutil@^4.0.1:
version "4.0.8"
resolved "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.8.tgz"
integrity sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==
dependencies:
node-gyp-build "^4.3.0"

debug@4, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

https-proxy-agent@^7.0.4:
version "7.0.4"
resolved "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.4.tgz"
integrity sha512-wlwpilI7YdjSkWaQ/7omYBMTliDcmCN8OLihO6I9B86g06lMyAoqgoDpV0XqoaPOKj+0DIdAvnsWfyAAhmimcg==
dependencies:
agent-base "^7.0.2"
debug "4"

minimist@^1.2.0:
version "1.2.8"
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

[email protected]:
version "2.1.2"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

node-gyp-build@^4.3.0:
version "4.8.0"
resolved "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.0.tgz"
integrity sha512-u6fs2AEUljNho3EYTJNBfImO5QTo/J/1Etd+NVdCj7qWKUSN/bSLkZwhDv7I+w/MSC6qJ4cknepkAYykDdK8og==

prettier@^3.2.5:
version "3.2.5"
resolved "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz"
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==

ws@^8.16.0:
version "8.16.0"
resolved "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz"
integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==

0 comments on commit cf83b15

Please sign in to comment.