From a1d7e01a7c755450ad0a894c4a202fe825e9fc8d Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 11 Jul 2023 12:20:33 +0700 Subject: [PATCH] Updated code --- src/proxy.ts | 47 +++++++++++++++++++++++++++++++++ src/proxy_tcp.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test.ts | 31 ++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 src/proxy.ts create mode 100644 src/proxy_tcp.ts create mode 100644 test/test.ts diff --git a/src/proxy.ts b/src/proxy.ts new file mode 100644 index 0000000..23cbf59 --- /dev/null +++ b/src/proxy.ts @@ -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}`); + }); +}; diff --git a/src/proxy_tcp.ts b/src/proxy_tcp.ts new file mode 100644 index 0000000..98e8075 --- /dev/null +++ b/src/proxy_tcp.ts @@ -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) => { + 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; +} \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..9a0b741 --- /dev/null +++ b/test/test.ts @@ -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, +// });