Updated code

This commit is contained in:
Sambo Chea 2023-07-11 12:20:33 +07:00
parent 9f61c8aebf
commit a1d7e01a7c
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
3 changed files with 145 additions and 0 deletions

47
src/proxy.ts Normal file
View File

@ -0,0 +1,47 @@
import express from 'express';
import { createProxyMiddleware, Options as ProxyOpts } from 'http-proxy-middleware';
interface ProxyOptions extends ProxyOpts {
basePath?: string;
proxyPort?: number | string;
}
interface SSLProxyOptions extends ProxyOptions {
ssl?: {
key: string;
cert: string;
};
}
const port = process.env.PROXY_PORT || 3000;
const app = express();
export const createProxyServer = (target: string, opts?: ProxyOptions) => {
if (!target) {
throw new Error('Proxy target is required');
}
app.use(
opts?.basePath || '',
createProxyMiddleware({
target,
changeOrigin: true,
ws: true,
onProxyReq: (proxyReq, req, res) => {
console.log(`[HTTP] Proxying ${req.method} ${req.url} to ${target}`);
},
onProxyReqWs: (proxyReq, req, socket, options, head) => {
console.log(`[WS] Proxying ${req.method} ${req.url} to ${target}`);
},
onError: (err, req, res) => {
console.error(`Proxy error: ${err.message}`);
},
...opts
})
);
const proxyPort = opts?.proxyPort || port;
return app.listen(proxyPort, () => {
console.log(`Proxy listening on port ${proxyPort} and forwarding to ${target}`);
});
};

67
src/proxy_tcp.ts Normal file
View File

@ -0,0 +1,67 @@
import * as net from 'net'
const port = process.env.PROXY_PORT || 3000;
interface ProxyOptions extends net.TcpNetConnectOpts {
proxyPort?: number | string;
}
// Create a TCP server that acts as a proxy
export const createProxyServer = (targetHost: string, targetPort: number, opts?: Partial<ProxyOptions>) => {
const server = net.createServer((clientSocket) => {
const targetSocket = net.createConnection({
host: targetHost,
port: targetPort,
...opts,
});
clientSocket.pipe(targetSocket);
targetSocket.pipe(clientSocket);
// Listen for target socket connection
targetSocket.on('connect', () => {
console.log(`Target socket connected to ${targetHost}:${targetPort}`);
});
// targetSocket.on('data', (data) => {
// console.log('Target socket received data length:', data.length);
// });
// Listen for client socket requests
clientSocket.on('data', (data) => {
console.log('Client socket request data', data.toString());
});
clientSocket.on('end', () => {
targetSocket.end();
});
targetSocket.on('end', () => {
clientSocket.end();
});
clientSocket.on('error', (err) => {
console.error('Client socket error:', err);
});
targetSocket.on('error', (err) => {
console.error('Target socket error:', err);
});
targetSocket.on('timeout', () => {
console.log('Target socket timeout');
});
clientSocket.on('timeout', () => {
console.log('Client socket timeout');
});
});
const proxyPort = opts?.proxyPort || port;
server.listen(proxyPort, () => {
console.log(`TCP proxy server listening on port ${proxyPort} and forwarding to ${targetHost}:${targetPort}`);
});
return server;
}

31
test/test.ts Normal file
View File

@ -0,0 +1,31 @@
import { startClient } from '../src/api';
async function main() {
const client = await startClient({
port: 3000,
options: {
profile: 'mytest',
},
});
setTimeout(async () => {
client?.stop();
}, 5000);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
// import { createProxyServer } from './proxy';
// import { createProxyServer } from './proxy_tcp';
// const proxy = createProxyServer('https://git.cubetiqs.com', {
// proxyPort: 3005,
// basePath: '/',
// });
// const proxy = createProxyServer('192.168.0.202', 8081, {
// proxyPort: 3005,
// });