horse/horse-server/server.js
mmm8955405 dc2dc7a8f3 xg
2024-06-21 18:24:11 +08:00

51 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { URL } = require('url')
const net = require('net')
const http = require('http')
const receive = require('./receive');
let server = http.createServer((req,res)=>{
let url = req.url;
res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
res.end('client-url'+url);
});
server.on('connect', (req, cltSocket, head) => {
//console.log(head);
// Connect to an origin server
const { port, hostname } = new URL(`http://${req.url}`);
const srcNet = net.connect(port || 80, hostname);
srcNet.on('connect',function () {
console.log(hostname + ':' + port + 'connect ok');
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'\r\n');
srcNet.write(head);
srcNet.pipe(cltSocket);
cltSocket.pipe(srcNet);
});
srcNet.on('error',function (msg) {
console.log("connect end : =====" + msg);
srcNet.destroy();//end()
});
srcNet.on('timeout', (msg) => {
console.log('timeout' + msg);
srcNet.destroy();//end()
});
});
server.listen(9999, () => {
console.log('https proxy server listen 9999');
});