Compare commits
1 Commits
master
...
mathmakgak
Author | SHA1 | Date | |
---|---|---|---|
|
a71c54d139 |
@ -69,6 +69,4 @@ ws://localhost:8080/?target=ws://www.multiplayerpiano.com:443&origin=http://www.
|
|||||||
|
|
||||||
Query parameters may or may not be encoded, but querystring chars (`&` and `=`) must be encoded to escape them.
|
Query parameters may or may not be encoded, but querystring chars (`&` and `=`) must be encoded to escape them.
|
||||||
|
|
||||||
## Issues
|
**Note:** If the `target` is missing or invalid, or if an error occurs when connecting to the remote host (such as if it responded with a 403), your connection is simply closed. Ideally, the proxy server would wait for the connection to the target to finish, before responding to the client with the same response of the target; however, I found this much too complicated to set up, so I just kept it simple.
|
||||||
|
|
||||||
If the `target` is missing or invalid, or if an error occurs when connecting to the remote host (such as if it responded with a 403), your connection is simply closed. Ideally, the proxy server would wait for the connection to the target to finish, before responding to the client with the same response of the target; however, I found this much too complicated to set up, so I just kept it simple.
|
|
||||||
|
106
index.js
106
index.js
@ -1,54 +1,66 @@
|
|||||||
var WebSocket = require('ws');
|
var WebSocket = require("ws");
|
||||||
var {parse:parseQueryString} = require('query-string');
|
var SocksProxyAgent = require("socks-proxy-agent");
|
||||||
|
var { parse: parseQueryString } = require("query-string");
|
||||||
var wss = new WebSocket.Server({
|
var wss = new WebSocket.Server({
|
||||||
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080
|
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080
|
||||||
});
|
});
|
||||||
var conncount = 0;
|
var conncount = 0;
|
||||||
wss.on('connection', function (cws, req){
|
wss.on("connection", function(cws, req) {
|
||||||
var number = conncount++;
|
var number = conncount++;
|
||||||
console.log(`New connection #${number} from ${req.connection.remoteAddress} with ${req.url}`);
|
console.log(
|
||||||
cws.on('close', function(){
|
`New connection #${number} from ${req.connection.remoteAddress} with ${req.url}`
|
||||||
console.log(`Closing connection #${number} from ${req.connection.remoteAddress}`);
|
);
|
||||||
});
|
cws.on("close", function() {
|
||||||
|
console.log(
|
||||||
|
`Closing connection #${number} from ${req.connection.remoteAddress}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
var querystring = req.url.substr(1);
|
var querystring = req.url.substr(1);
|
||||||
if (!querystring) return cws.close();
|
if (!querystring) return cws.close();
|
||||||
var params = parseQueryString(querystring);
|
var params = parseQueryString(querystring);
|
||||||
var target = params.target;
|
var target = params.target;
|
||||||
if (!target) return cws.close();
|
if (!target) return cws.close();
|
||||||
var headers = {};
|
var headers = {};
|
||||||
for (let key in params) if (key != "target") headers[key] = params[key];
|
for (let key in params) {
|
||||||
|
if (key != "target" && key != "agent") headers[key] = params[key];
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var tws = new WebSocket(target, {headers});
|
var tws = new WebSocket(target, {
|
||||||
} catch(e) {
|
headers,
|
||||||
console.error(e);
|
agent: params.agent
|
||||||
cws.close();
|
? new SocksProxyAgent("socks://" + params.agent)
|
||||||
return;
|
: undefined
|
||||||
}
|
});
|
||||||
|
} catch (e) {
|
||||||
// client to target
|
console.error(e);
|
||||||
var messageBuffer = [];
|
cws.close();
|
||||||
tws.on('open', function(){
|
return;
|
||||||
for (let message of messageBuffer) tws.send(message);
|
}
|
||||||
messageBuffer = undefined;
|
|
||||||
});
|
|
||||||
cws.on('message', function(message){
|
|
||||||
if (tws.readyState == WebSocket.OPEN) tws.send(message);
|
|
||||||
else if (messageBuffer) messageBuffer.push(message);
|
|
||||||
});
|
|
||||||
cws.on('close', function(){
|
|
||||||
tws.close();
|
|
||||||
messageBuffer = undefined;
|
|
||||||
});
|
|
||||||
cws.on('error', console.error);
|
|
||||||
|
|
||||||
// target to client
|
// client to target
|
||||||
tws.on('message', function(message){
|
var messageBuffer = [];
|
||||||
if (cws.readyState == WebSocket.OPEN) cws.send(message);
|
tws.on("open", function() {
|
||||||
});
|
for (let message of messageBuffer) tws.send(message);
|
||||||
tws.on('close', function(){
|
messageBuffer = undefined;
|
||||||
cws.close();
|
});
|
||||||
});
|
cws.on("message", function(message) {
|
||||||
tws.on('error', console.error);
|
if (tws.readyState == WebSocket.OPEN) tws.send(message);
|
||||||
|
else if (messageBuffer) messageBuffer.push(message);
|
||||||
|
});
|
||||||
|
cws.on("close", function() {
|
||||||
|
tws.close();
|
||||||
|
messageBuffer = undefined;
|
||||||
|
});
|
||||||
|
cws.on("error", console.error);
|
||||||
|
|
||||||
|
// target to client
|
||||||
|
tws.on("message", function(message) {
|
||||||
|
if (cws.readyState == WebSocket.OPEN) cws.send(message);
|
||||||
|
});
|
||||||
|
tws.on("close", function() {
|
||||||
|
cws.close();
|
||||||
|
});
|
||||||
|
tws.on("error", console.error);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user