Compare commits

..

1 Commits

Author SHA1 Message Date
mathmakgakpak
a71c54d139
proxy implementation i think that it should work 2020-03-09 17:54:15 +01:00
2 changed files with 60 additions and 50 deletions

View File

@ -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.
## Issues
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.
**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.

View File

@ -1,14 +1,19 @@
var WebSocket = require('ws');
var {parse:parseQueryString} = require('query-string');
var WebSocket = require("ws");
var SocksProxyAgent = require("socks-proxy-agent");
var { parse: parseQueryString } = require("query-string");
var wss = new WebSocket.Server({
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080
});
var conncount = 0;
wss.on('connection', function (cws, req){
wss.on("connection", function(cws, req) {
var number = conncount++;
console.log(`New connection #${number} from ${req.connection.remoteAddress} with ${req.url}`);
cws.on('close', function(){
console.log(`Closing connection #${number} from ${req.connection.remoteAddress}`);
console.log(
`New connection #${number} from ${req.connection.remoteAddress} with ${req.url}`
);
cws.on("close", function() {
console.log(
`Closing connection #${number} from ${req.connection.remoteAddress}`
);
});
var querystring = req.url.substr(1);
@ -17,11 +22,18 @@ wss.on('connection', function (cws, req){
var target = params.target;
if (!target) return cws.close();
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 {
var tws = new WebSocket(target, {headers});
} catch(e) {
var tws = new WebSocket(target, {
headers,
agent: params.agent
? new SocksProxyAgent("socks://" + params.agent)
: undefined
});
} catch (e) {
console.error(e);
cws.close();
return;
@ -29,26 +41,26 @@ wss.on('connection', function (cws, req){
// client to target
var messageBuffer = [];
tws.on('open', function(){
tws.on("open", function() {
for (let message of messageBuffer) tws.send(message);
messageBuffer = undefined;
});
cws.on('message', function(message){
cws.on("message", function(message) {
if (tws.readyState == WebSocket.OPEN) tws.send(message);
else if (messageBuffer) messageBuffer.push(message);
});
cws.on('close', function(){
cws.on("close", function() {
tws.close();
messageBuffer = undefined;
});
cws.on('error', console.error);
cws.on("error", console.error);
// target to client
tws.on('message', function(message){
tws.on("message", function(message) {
if (cws.readyState == WebSocket.OPEN) cws.send(message);
});
tws.on('close', function(){
tws.on("close", function() {
cws.close();
});
tws.on('error', console.error);
tws.on("error", console.error);
});