Skip to content

Instantly share code, notes, and snippets.

@wfjsw
Last active October 21, 2017 04:54
Show Gist options
  • Save wfjsw/dac68b16a56a3823267bcc6e1204d2c7 to your computer and use it in GitHub Desktop.
Save wfjsw/dac68b16a56a3823267bcc6e1204d2c7 to your computer and use it in GitHub Desktop.
Websocket to CONNECT middleware. Obsolete. Please check out https://github.com/wfjsw/madproxy
const http = require('http');
const net = require('net');
const url = require('url');
var proxy = http.createServer( (req, res) => {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end('400 Bad Request');
});
var processsocket = (req, cltSocket, head) => {
try{
console.log(`http://${req.headers['x-connect-to']}`);
var srvUrl = url.parse(`http://${req.headers['x-connect-to']}`);
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
cltSocket.write('HTTP/1.1 200 OK\r\n' + '\r\n');
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
srvSocket.on('error', (e) => {console.error(e);});
cltSocket.on('error', (e) => {console.error(e);});
} catch (e) {
console.error('bad request');
cltSocket.end('HTTP/1.1 404 Not Found\r\n' + '\r\n');
}
};
proxy.on('upgrade', processsocket);
proxy.on('connect', processsocket);
proxy.on('error', (e) => {console.error(e);});
proxy.listen(443, '0.0.0.0', () => {
console.log('running on port 443, change this on line 28 if you prefer(only 80,443,8080 allowed)')
console.log('Point Your HTTP Injector Remote Proxy to This-IP:8080');
console.log('Vaild Payload: GET http://wap.cmvideo.cn/ HTTP/1.1[crlf]Host: wap.cmvideo.cn[crlf]Connection: Upgrade[crlf]Upgrade: websocket[crlf]X-Connect-To: [host_port][crlf][crlf]')
console.log('No Warranty Provided - Use it at your OWN risk!')
});
@codexss
Copy link

codexss commented Jul 10, 2016

node proxy.js 执行时报错

node proxy.js

/root/proxy.js:10
var proxy = http.createServer( (req, res) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3

@wfjsw
Copy link
Author

wfjsw commented Jul 14, 2016

@codexss
ES6 支持需要安装不小于Node.js 5的版本

还有这东西重构了 看 https://github.com/wfjsw/madproxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment