From c8697d546b01264c52a73a88a5287192db38e412 Mon Sep 17 00:00:00 2001 From: zizifn <1803942+zizifn@users.noreply.github.com> Date: Sun, 4 Dec 2022 04:11:55 +0800 Subject: [PATCH] reback change --- apps/edge-bypass-client/src/main copy 4.ts | 118 ++++++++++++++++++ apps/edge-bypass-client/src/main copy.ts | 136 --------------------- apps/edge-bypass-client/src/main.ts | 118 ++++++++++-------- 3 files changed, 186 insertions(+), 186 deletions(-) create mode 100644 apps/edge-bypass-client/src/main copy 4.ts delete mode 100644 apps/edge-bypass-client/src/main copy.ts diff --git a/apps/edge-bypass-client/src/main copy 4.ts b/apps/edge-bypass-client/src/main copy 4.ts new file mode 100644 index 0000000..fda5832 --- /dev/null +++ b/apps/edge-bypass-client/src/main copy 4.ts @@ -0,0 +1,118 @@ +import { Socket } from 'node:net'; +import { createServer } from 'node:http'; +import { Duplex, pipeline, Readable, Writable } from 'node:stream'; +import { ReadableStream, WritableStream } from 'node:stream/web'; +import { Command } from 'commander'; +import { writeFileSync, existsSync, readFileSync } from 'fs'; +import { exit } from 'node:process'; +import * as url from 'node:url'; +import * as undici from 'undici'; +import { concatStreams } from './helper'; +import * as http from 'node:http'; + +let config: { + port: string; + address: string; + uuid: string; + config: string; +} = null; +const program = new Command(); +program + .command('run') + .description('launch local http proxy for edge pass') + .option( + '--config ', + 'address of remote proxy, etc https://***.deno.dev/' + ) + .option( + '--address
', + 'address of remote proxy, etc https://***.deno.dev/' + ) + .option('--port ', 'local port of http proxy proxy', '8134') + .option('--uuid ', 'uuid') + .option('--save', 'if this is pass, will save to config.json') + .action((options) => { + if (options.config) { + if (existsSync(options.config)) { + const content = readFileSync(options.config, { + encoding: 'utf-8', + }); + config = JSON.parse(content); + return; + } else { + console.error('config not exsit!'); + exit(); + } + } + config = options; + if (options.save) { + writeFileSync('./config.json', JSON.stringify(options), { + encoding: 'utf-8', + }); + } + }); +program.parse(); + +let httpProxyServer = createServer((req, resp) => { + console.log('start'); + const reqUrl = url.parse(req.url); + console.log('proxy for http request: ' + reqUrl.href); + + req.pipe(resp); +}); + +httpProxyServer.on('connect', async (req, clientSocket, head) => { + const reqUrl = url.parse('https://' + req.url); + console.log( + `Client Connected To Proxy, client http version is ${ + req.httpVersion + }, client url is ${req.url},head is ${head.toString()}` + ); + // We need only the data once, the starting packet + clientSocket.write( + `HTTP/${req.httpVersion} 200 Connection Established\r\n\r\n` + ); + + const { body, headers, statusCode, trailers } = await undici.request( + config.address, + { + headers: { + 'x-host': reqUrl.hostname, + 'x-port': reqUrl.port, + 'x-uuid': config.uuid, + // "Content-Type": "text/plain", + }, + method: 'POST', + body: clientSocket, + } + ); + + body.pipe(clientSocket).on('error', (error) => { + console.log('serever reponse to clientSocket: ' + error); + }); + + clientSocket.on('error', (e) => { + console.log('client socket error: ' + e); + }); + clientSocket.on('end', () => { + console.log('end-----'); + }); +}); + +httpProxyServer.on('error', (err) => { + console.log('SERVER ERROR'); + console.log(err); + throw err; +}); +httpProxyServer.on('clientError', (err, clientSocket) => { + console.log('client error: ' + err); + clientSocket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); +}); + +httpProxyServer.on('close', () => { + console.log('Client Disconnected'); +}); + +httpProxyServer.listen(Number(config.port), () => { + console.log('Server runnig at http://localhost:' + config.port); +}); diff --git a/apps/edge-bypass-client/src/main copy.ts b/apps/edge-bypass-client/src/main copy.ts deleted file mode 100644 index 0904ca4..0000000 --- a/apps/edge-bypass-client/src/main copy.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { createServer, Socket } from 'node:net'; -import { Duplex } from 'node:stream'; -import { fetch } from 'undici'; -import { ReadableStream, WritableStream } from 'node:stream/web'; -import { Command } from 'commander'; -import { writeFileSync, existsSync, readFileSync } from 'fs'; -import { exit } from 'node:process'; - -let config: { - port: string; - address: string; - uuid: string; - config: string; -} = null; -const program = new Command(); -program - .command('run') - .description('launch local http proxy for edge pass') - .option( - '--config ', - 'address of remote proxy, etc https://***.deno.dev/' - ) - .option( - '--address
', - 'address of remote proxy, etc https://***.deno.dev/' - ) - .option('--port ', 'local port of http proxy proxy', '8134') - .option('--uuid ', 'uuid') - .option('--save', 'if this is pass, will save to config.json') - .action((options) => { - if (options.config) { - if (existsSync(options.config)) { - const content = readFileSync(options.config, { - encoding: 'utf-8', - }); - config = JSON.parse(content); - return; - } else { - console.error('config not exsit!'); - exit(); - } - } - config = options; - if (options.save) { - writeFileSync('./config.json', JSON.stringify(options), { - encoding: 'utf-8', - }); - } - }); -program.parse(); - -const server = createServer(); -server.on('connection', (clientToProxySocket: Socket) => { - console.log('Client Connected To Proxy'); - // We need only the data once, the starting packet - clientToProxySocket.once('data', async (data) => { - // If you want to see the packet uncomment below - // console.log(data.toString()); - let isTLSConnection = data.toString().indexOf('CONNECT') !== -1; - let serverPort = '80'; - let serverAddress: string; - if (isTLSConnection) { - // Port changed if connection is TLS - serverPort = data - .toString() - .split('CONNECT ')[1] - .split(' ')[0] - .split(':')[1]; - serverAddress = data - .toString() - .split('CONNECT ')[1] - .split(' ')[0] - .split(':')[0]; - } else { - serverAddress = data.toString().split('Host: ')[1].split('\r\n')[0]; - } - - const { - readable: clientToProxySocketReadable, - writable: clientToProxySocketWritable, - } = Duplex.toWeb(clientToProxySocket) as any as { - readable: ReadableStream; - writable: WritableStream; - }; - - // console.log(serverAddress); - if (isTLSConnection) { - clientToProxySocket.write('HTTP/1.1 200 OK\r\n\n'); - } else { - // TODO - // proxyToServerSocket.write(data); - } - - fetch(config.address, { - headers: { - 'x-host': serverAddress, - 'x-port': serverPort, - 'x-uuid': config.uuid, - // "Content-Type": "text/plain", - }, - method: 'POST', - // body: Uint8Array.from(chunks), - body: clientToProxySocketReadable, - duplex: 'half', - }) - .then((resp) => { - console.log( - `proxy to ${serverAddress}:${serverPort} and remote return ${resp.status}` - ); - resp.body.pipeTo(clientToProxySocketWritable).catch((error) => { - console.error('pipe to', JSON.stringify(error)); - }); - }) - .catch((error) => { - console.log('fetch error', error); - }); - clientToProxySocket.on('error', (err) => { - console.log('CLIENT TO PROXY ERROR'); - console.log(err); - }); - }); -}); - -server.on('error', (err) => { - console.log('SERVER ERROR'); - console.log(err); - throw err; -}); - -server.on('close', () => { - console.log('Client Disconnected'); -}); - -server.listen(Number(config.port), () => { - console.log('Server runnig at http://localhost:' + config.port); -}); diff --git a/apps/edge-bypass-client/src/main.ts b/apps/edge-bypass-client/src/main.ts index fda5832..0904ca4 100644 --- a/apps/edge-bypass-client/src/main.ts +++ b/apps/edge-bypass-client/src/main.ts @@ -1,14 +1,10 @@ -import { Socket } from 'node:net'; -import { createServer } from 'node:http'; -import { Duplex, pipeline, Readable, Writable } from 'node:stream'; +import { createServer, Socket } from 'node:net'; +import { Duplex } from 'node:stream'; +import { fetch } from 'undici'; import { ReadableStream, WritableStream } from 'node:stream/web'; import { Command } from 'commander'; import { writeFileSync, existsSync, readFileSync } from 'fs'; import { exit } from 'node:process'; -import * as url from 'node:url'; -import * as undici from 'undici'; -import { concatStreams } from './helper'; -import * as http from 'node:http'; let config: { port: string; @@ -53,66 +49,88 @@ program }); program.parse(); -let httpProxyServer = createServer((req, resp) => { - console.log('start'); - const reqUrl = url.parse(req.url); - console.log('proxy for http request: ' + reqUrl.href); - - req.pipe(resp); -}); - -httpProxyServer.on('connect', async (req, clientSocket, head) => { - const reqUrl = url.parse('https://' + req.url); - console.log( - `Client Connected To Proxy, client http version is ${ - req.httpVersion - }, client url is ${req.url},head is ${head.toString()}` - ); +const server = createServer(); +server.on('connection', (clientToProxySocket: Socket) => { + console.log('Client Connected To Proxy'); // We need only the data once, the starting packet - clientSocket.write( - `HTTP/${req.httpVersion} 200 Connection Established\r\n\r\n` - ); + clientToProxySocket.once('data', async (data) => { + // If you want to see the packet uncomment below + // console.log(data.toString()); + let isTLSConnection = data.toString().indexOf('CONNECT') !== -1; + let serverPort = '80'; + let serverAddress: string; + if (isTLSConnection) { + // Port changed if connection is TLS + serverPort = data + .toString() + .split('CONNECT ')[1] + .split(' ')[0] + .split(':')[1]; + serverAddress = data + .toString() + .split('CONNECT ')[1] + .split(' ')[0] + .split(':')[0]; + } else { + serverAddress = data.toString().split('Host: ')[1].split('\r\n')[0]; + } - const { body, headers, statusCode, trailers } = await undici.request( - config.address, - { + const { + readable: clientToProxySocketReadable, + writable: clientToProxySocketWritable, + } = Duplex.toWeb(clientToProxySocket) as any as { + readable: ReadableStream; + writable: WritableStream; + }; + + // console.log(serverAddress); + if (isTLSConnection) { + clientToProxySocket.write('HTTP/1.1 200 OK\r\n\n'); + } else { + // TODO + // proxyToServerSocket.write(data); + } + + fetch(config.address, { headers: { - 'x-host': reqUrl.hostname, - 'x-port': reqUrl.port, + 'x-host': serverAddress, + 'x-port': serverPort, 'x-uuid': config.uuid, // "Content-Type": "text/plain", }, method: 'POST', - body: clientSocket, - } - ); - - body.pipe(clientSocket).on('error', (error) => { - console.log('serever reponse to clientSocket: ' + error); - }); - - clientSocket.on('error', (e) => { - console.log('client socket error: ' + e); - }); - clientSocket.on('end', () => { - console.log('end-----'); + // body: Uint8Array.from(chunks), + body: clientToProxySocketReadable, + duplex: 'half', + }) + .then((resp) => { + console.log( + `proxy to ${serverAddress}:${serverPort} and remote return ${resp.status}` + ); + resp.body.pipeTo(clientToProxySocketWritable).catch((error) => { + console.error('pipe to', JSON.stringify(error)); + }); + }) + .catch((error) => { + console.log('fetch error', error); + }); + clientToProxySocket.on('error', (err) => { + console.log('CLIENT TO PROXY ERROR'); + console.log(err); + }); }); }); -httpProxyServer.on('error', (err) => { +server.on('error', (err) => { console.log('SERVER ERROR'); console.log(err); throw err; }); -httpProxyServer.on('clientError', (err, clientSocket) => { - console.log('client error: ' + err); - clientSocket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); -}); -httpProxyServer.on('close', () => { +server.on('close', () => { console.log('Client Disconnected'); }); -httpProxyServer.listen(Number(config.port), () => { +server.listen(Number(config.port), () => { console.log('Server runnig at http://localhost:' + config.port); });