1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| const http = require('http'); const net = require('net'); const url = require('url');
const PORT = process.env.PORT || 1337; const HOST = process.env.HOST || '127.0.0.1'; const MY_IP = '0.0.0.0';
const SERVER_TIMEOUT = 2 * 60 * 1000; const SEND_TIMEOUT = 1000; const PROXY_TIMEOUT = 60 * 1000;
const proxy = http.createServer();
proxy.on('request', (cReq, cRes) => { const cUrl = cReq.url; console.log(cUrl);
if (cUrl.startsWith('http')) { const { method, headers } = cReq; const { hostname, port = 80, path } = url.parse(cReq.url);
let via = headers['via']; if (via) { via = via += ', 1.1 proxy.kekek.cc (KEKE-Proxy)'; } else { via = '1.1 proxy.kekek.cc (KEKE-Proxy)'; } headers['via'] = via;
let ips = headers['x-forward-for']; if (ips) { ips += `, ${MY_IP}`; } else { ips = MY_IP; } headers['x-forward-for'] = ips;
const pReq = http.request({ hostname, port, path, method, headers }); pReq .setTimeout(PROXY_TIMEOUT) .on('response', (pRes) => { console.log(`[response] [proxy] ${cUrl}`);
cRes.writeHead(pRes.statusCode, pRes.headers); pRes.pipe(cRes); }) .on('timeout', () => { console.log(`[timeout] [proxy-req] ${cUrl}`); pReq.abort(); }) .on('abort', () => { console.log(`[abort] [proxy-req] ${cUrl}`); }) .on('close', () => { console.log(`[close] [proxy-req] ${cUrl}`); }) .on('error', (err) => { console.log(`[error] [proxy-req] ${cUrl} \r\n${err.stack}`); cRes.writeHead(500, { 'content-type': 'text/plain' }); cRes.end(`Proxy Error: ${err.code || err.message || err.name}`); });
cReq.pipe(pReq);
cReq.on('aborted', () => { console.log(`[aborted] [client] ${cUrl}`); pReq.abort(); }); } else { if (cUrl === '/proxy.pac') { cRes.setHeader('content-type', 'application/x-ns-proxy-autoconfig'); const pac = `// Proxy Auto-Configuration (PAC) file // https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file // DIRECT 不经过任何代理,直接进行连接 // PROXY host:port 应该使用指定的代理 // SOCKS host:port 应该使用指定的 SOCKS 服务器
var proxy = "PROXY proxy.kekek.cc:1337; DIRECT;"; var direct = 'DIRECT;'; function FindProxyForURL(url, host){ if (/\.\kekek\.cc$/.test(host)) { return proxy; } else { return direct; } }`;
cRes.end(pac); } else { cRes.setHeader('content-type', 'text/plain'); cRes.end('proxy: proxy.kekek.cc\r\nport: 1337\r\npac: http://proxy.kekek.cc:1337/proxy.pac'); } } });
proxy.on('connect', (req, cltSocket, head) => { console.log(req.url, head.length) const { port, hostname } = url.parse(`http://${req.url}`);
const srvSocket = net.connect(port, hostname);
srvSocket.on('connect', () => { cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node.js-Proxy\r\n' + '\r\n'); srvSocket.write(head); srvSocket.pipe(cltSocket); cltSocket.pipe(srvSocket); });
srvSocket.on('error', (err) => { console.log(`[error] ${req.url} \r\n${err.stack}`); const error = `Proxy Error: ${err.code || err.message || err.name}` cltSocket.end(`HTTP/1.1 500 Internal Server Error\r\ncontent-type:text/plain\r\ncontent-length:${err.length}\r\n\r\n${error}`); }); });
proxy.on('listening', () => { console.log(`Proxy-Server running on http://${HOST}:${PORT}`); });
proxy.listen(PORT, HOST);
|