mirror of
https://github.com/lush2020/edgetunnel.git
synced 2026-03-24 00:48:39 +08:00
enhance logic
This commit is contained in:
@@ -3,18 +3,19 @@ import { pipeline, Readable } from 'node:stream';
|
||||
import { config } from './lib/cmd';
|
||||
import * as url from 'node:url';
|
||||
import * as undici from 'undici';
|
||||
import {
|
||||
concatStreams,
|
||||
rawHTTPHeader,
|
||||
rawHTTPPackage,
|
||||
rawHTTPPackageWithDelay,
|
||||
} from './lib/helper';
|
||||
import { concatStreams, rawHTTPPackage } from './lib/helper';
|
||||
|
||||
const isLocal = process.env.env === 'LOCAL';
|
||||
const httpProxyServer = createServer(async (req, resp) => {
|
||||
const reqUrl = url.parse(req.url);
|
||||
const clientSocketLoggerInfo = `[proxy to ${req.url}(http)]`;
|
||||
const clientSocketLoggerInfo = `[proxy to ${req.url}](http)`;
|
||||
try {
|
||||
console.log(`${clientSocketLoggerInfo} Client use HTTP/${req.httpVersion}`);
|
||||
|
||||
// for await (const chunk of req.socket) {
|
||||
// console.log(chunk.toString());
|
||||
// }
|
||||
|
||||
// make call to edge http server
|
||||
// 1. forward all package remote, socket over http body
|
||||
const { body, headers, statusCode, trailers } = await undici.request(
|
||||
@@ -27,8 +28,7 @@ const httpProxyServer = createServer(async (req, resp) => {
|
||||
'x-http': 'true',
|
||||
},
|
||||
method: 'POST',
|
||||
// append few ms for body
|
||||
body: Readable.from(rawHTTPPackageWithDelay(req)),
|
||||
body: Readable.from(rawHTTPPackage(req)),
|
||||
}
|
||||
);
|
||||
console.log(
|
||||
@@ -36,6 +36,9 @@ const httpProxyServer = createServer(async (req, resp) => {
|
||||
);
|
||||
// 2. forward remote reponse body to clientSocket
|
||||
for await (const chunk of body) {
|
||||
if (isLocal) {
|
||||
console.log(chunk.toString());
|
||||
}
|
||||
req.socket.write(chunk);
|
||||
}
|
||||
body.on('error', (err) => {
|
||||
@@ -44,16 +47,6 @@ const httpProxyServer = createServer(async (req, resp) => {
|
||||
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) {
|
||||
req.socket.end();
|
||||
req.socket.destroy();
|
||||
@@ -76,6 +69,7 @@ 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(
|
||||
@@ -100,14 +94,6 @@ httpProxyServer.on('connect', async (req, clientSocket, head) => {
|
||||
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();
|
||||
@@ -133,7 +119,7 @@ httpProxyServer.on('clientError', (err, clientSocket) => {
|
||||
});
|
||||
|
||||
httpProxyServer.on('close', () => {
|
||||
console.log('Client Disconnected');
|
||||
console.log('Server close');
|
||||
});
|
||||
|
||||
httpProxyServer.listen(Number(config.port), () => {
|
||||
|
||||
Reference in New Issue
Block a user