mirror of
https://github.com/lush2020/edgetunnel.git
synced 2026-03-23 16:38:34 +08:00
add support for http
This commit is contained in:
@@ -19,19 +19,6 @@ const httpProxyServer = createServer(async (req, resp) => {
|
||||
`Client Connected To Proxy, client http version is ${req.httpVersion}, ${clientSocketLoggerInfo}}`
|
||||
);
|
||||
|
||||
const raws = rawHTTPPackage(req);
|
||||
const readableStream = new Readable({
|
||||
async read() {
|
||||
const { value, done } = await raws.next();
|
||||
if (!done) {
|
||||
this.push(value);
|
||||
}
|
||||
},
|
||||
destroy() {
|
||||
this.push(null);
|
||||
},
|
||||
});
|
||||
|
||||
// make call to edge http server
|
||||
// 1. forward all package remote, socket over http body
|
||||
const { body, headers, statusCode, trailers } = await undici.request(
|
||||
@@ -44,39 +31,30 @@ const httpProxyServer = createServer(async (req, resp) => {
|
||||
// "Content-Type": "text/plain",
|
||||
},
|
||||
method: 'POST',
|
||||
// body: Readable.from(rawHTTPPackage(req)),
|
||||
body: readableStream,
|
||||
|
||||
// body: rawHTTPHeader(req),
|
||||
// body: req,
|
||||
body: Readable.from(concatStreams([rawHTTPPackage(req), req.socket])),
|
||||
}
|
||||
);
|
||||
// for await (const item of rawHTTPPackage(req)) {
|
||||
// myReadable.push(item);
|
||||
// }
|
||||
// console.log(headers, statusCode);
|
||||
// for await (let chunk of body) {
|
||||
// console.log(chunk.toString());
|
||||
// }
|
||||
console.log(`${clientSocketLoggerInfo} remote server return ${statusCode}`);
|
||||
// 2. forward remote reponse body to clientSocket
|
||||
|
||||
pipeline(body, resp, (error) => {
|
||||
console.log(
|
||||
`${clientSocketLoggerInfo} remote server to clientSocket has error: ` +
|
||||
error
|
||||
);
|
||||
resp.destroy();
|
||||
});
|
||||
for await (const chunk of body) {
|
||||
req.socket.write(chunk);
|
||||
}
|
||||
body.on('error', (err) => {
|
||||
console.log('body error', err);
|
||||
});
|
||||
body.on('data', () => {
|
||||
if (!readableStream.closed) {
|
||||
readableStream.push(null);
|
||||
}
|
||||
console.log(`${clientSocketLoggerInfo} body error`, err);
|
||||
});
|
||||
// issue with pipeline
|
||||
// https://stackoverflow.com/questions/55959479/error-err-stream-premature-close-premature-close-in-node-pipeline-stream
|
||||
// pipeline(body, req.socket, (error) => {
|
||||
// console.log(
|
||||
// `${clientSocketLoggerInfo} remote server to clientSocket has error: ` +
|
||||
// error
|
||||
// );
|
||||
// req.socket.end();
|
||||
// req.socket.destroy();
|
||||
// });
|
||||
} catch (error) {
|
||||
resp.destroy();
|
||||
req.socket.end();
|
||||
req.socket.destroy();
|
||||
console.log('${clientSocketLogger} has error ', error);
|
||||
}
|
||||
});
|
||||
@@ -96,7 +74,6 @@ httpProxyServer.on('connect', async (req, clientSocket, head) => {
|
||||
`HTTP/${req.httpVersion} 200 Connection Established\r\n\r\n`
|
||||
);
|
||||
|
||||
console.log(config);
|
||||
// make call to edge http server
|
||||
// 1. forward all package remote, socket over http body
|
||||
const { body, headers, statusCode, trailers } = await undici.request(
|
||||
@@ -114,14 +91,21 @@ httpProxyServer.on('connect', async (req, clientSocket, head) => {
|
||||
);
|
||||
console.log(`${clientSocketLoggerInfo} remote server return ${statusCode}`);
|
||||
// 2. forward remote reponse body to clientSocket
|
||||
pipeline(body, clientSocket, (error) => {
|
||||
console.log(
|
||||
`${clientSocketLoggerInfo} remote server to clientSocket has error: `,
|
||||
error
|
||||
);
|
||||
body?.destroy();
|
||||
clientSocket.destroy();
|
||||
// 2. forward remote reponse body to clientSocket
|
||||
for await (const chunk of body) {
|
||||
clientSocket.write(chunk);
|
||||
}
|
||||
body.on('error', (err) => {
|
||||
console.log(`${clientSocketLoggerInfo} body error`, err);
|
||||
});
|
||||
// pipeline(body, clientSocket, (error) => {
|
||||
// console.log(
|
||||
// `${clientSocketLoggerInfo} remote server to clientSocket has error: `,
|
||||
// error
|
||||
// );
|
||||
// body?.destroy();
|
||||
// clientSocket.destroy();
|
||||
// });
|
||||
clientSocket.on('error', (e) => {
|
||||
body?.destroy();
|
||||
clientSocket.destroy();
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
var net = require('net');
|
||||
var http = require('http');
|
||||
var url = require('url');
|
||||
|
||||
var proxyServer = http.createServer(httpOptions);
|
||||
|
||||
// handle http proxy requests
|
||||
function httpOptions(clientReq, clientRes) {
|
||||
var reqUrl = url.parse(clientReq.url);
|
||||
console.log(reqUrl);
|
||||
console.log('proxy for http request: ' + reqUrl.href);
|
||||
|
||||
var options = {
|
||||
hostname: reqUrl.hostname,
|
||||
port: reqUrl.port,
|
||||
path: reqUrl.path,
|
||||
method: clientReq.method,
|
||||
headers: clientReq.headers,
|
||||
};
|
||||
|
||||
// create socket connection on behalf of client, then pipe the response to client response (pass it on)
|
||||
var serverConnection = http.request(options, function (res) {
|
||||
clientRes.writeHead(res.statusCode, res.headers);
|
||||
res.pipe(clientRes);
|
||||
});
|
||||
|
||||
clientReq.pipe(serverConnection);
|
||||
|
||||
clientReq.on('error', (e) => {
|
||||
console.log('client socket error: ' + e);
|
||||
});
|
||||
|
||||
serverConnection.on('error', (e) => {
|
||||
console.log('server connection error: ' + e);
|
||||
});
|
||||
}
|
||||
|
||||
// handle https proxy requests (CONNECT method)
|
||||
proxyServer.on('connect', (clientReq, clientSocket, head) => {
|
||||
var reqUrl = url.parse('https://' + clientReq.url);
|
||||
console.log(
|
||||
'proxy for https request: ' + reqUrl.href + '(path encrypted by ssl)'
|
||||
);
|
||||
|
||||
var options = {
|
||||
port: reqUrl.port,
|
||||
host: reqUrl.hostname,
|
||||
};
|
||||
|
||||
// create socket connection for client, then pipe (redirect) it to client socket
|
||||
var serverSocket = net.connect(options, () => {
|
||||
clientSocket.write(
|
||||
'HTTP/' +
|
||||
clientReq.httpVersion +
|
||||
' 200 Connection Established\r\n' +
|
||||
'Proxy-agent: Node.js-Proxy\r\n' +
|
||||
'\r\n',
|
||||
'UTF-8',
|
||||
() => {
|
||||
// creating pipes in both ends
|
||||
serverSocket.write(head);
|
||||
serverSocket.pipe(clientSocket);
|
||||
clientSocket.pipe(serverSocket);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
clientSocket.on('error', (e) => {
|
||||
console.log('client socket error: ' + e);
|
||||
serverSocket.end();
|
||||
});
|
||||
|
||||
serverSocket.on('error', (e) => {
|
||||
console.log('forward proxy server connection error: ' + e);
|
||||
clientSocket.end();
|
||||
});
|
||||
});
|
||||
|
||||
proxyServer.on('clientError', (err, clientSocket) => {
|
||||
console.log('client error: ' + err);
|
||||
clientSocket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
||||
});
|
||||
|
||||
proxyServer.listen(2560);
|
||||
|
||||
console.log('forward proxy server started, listening on port 2560');
|
||||
|
||||
module.exports = proxyServer;
|
||||
@@ -7,7 +7,7 @@ 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 './lib/helper';
|
||||
import { concatStreams } from '../lib/helper';
|
||||
|
||||
let config: {
|
||||
port: string;
|
||||
@@ -7,7 +7,7 @@ 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 './lib/helper';
|
||||
|
||||
import * as http from 'node:http';
|
||||
|
||||
let config: {
|
||||
Reference in New Issue
Block a user