3 Commits

Author SHA1 Message Date
dependabot[bot] 0cd189395b Bump ws from 6.0.0 to 7.4.6
Bumps [ws](https://github.com/websockets/ws) from 6.0.0 to 7.4.6.
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/6.0.0...7.4.6)

Signed-off-by: dependabot[bot] <support@github.com>
2021-05-29 00:13:38 +00:00
Lamp 61ca9d54d2 Update README.md 2020-04-19 10:29:42 -07:00
Lamp fa028e4af0 Update README.md 2020-04-19 10:27:52 -07:00
4 changed files with 54 additions and 72 deletions
+3 -1
View File
@@ -69,4 +69,6 @@ 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.
**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.
## 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.
+46 -58
View File
@@ -1,66 +1,54 @@
var WebSocket = require("ws");
var SocksProxyAgent = require("socks-proxy-agent");
var { parse: parseQueryString } = require("query-string");
var WebSocket = require('ws');
var {parse:parseQueryString} = require('query-string');
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;
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}`
);
});
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}`);
});
var querystring = req.url.substr(1);
if (!querystring) return cws.close();
var params = parseQueryString(querystring);
var target = params.target;
if (!target) return cws.close();
var headers = {};
for (let key in params) {
if (key != "target" && key != "agent") headers[key] = params[key];
}
var querystring = req.url.substr(1);
if (!querystring) return cws.close();
var params = parseQueryString(querystring);
var target = params.target;
if (!target) return cws.close();
var headers = {};
for (let key in params) if (key != "target") headers[key] = params[key];
try {
var tws = new WebSocket(target, {
headers,
agent: params.agent
? new SocksProxyAgent("socks://" + params.agent)
: undefined
});
} catch (e) {
console.error(e);
cws.close();
return;
}
try {
var tws = new WebSocket(target, {headers});
} catch(e) {
console.error(e);
cws.close();
return;
}
// client to target
var messageBuffer = [];
tws.on("open", function() {
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);
// client to target
var messageBuffer = [];
tws.on('open', function(){
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
tws.on("message", function(message) {
if (cws.readyState == WebSocket.OPEN) cws.send(message);
});
tws.on("close", function() {
cws.close();
});
tws.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);
});
+3 -11
View File
@@ -2,11 +2,6 @@
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"async-limiter": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
},
"decode-uri-component": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz",
@@ -27,12 +22,9 @@
"integrity": "sha1-ucczDHBChi9rFC3CdLvMWGbONUY="
},
"ws": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.0.0.tgz",
"integrity": "sha512-c2UlYcAZp1VS8AORtpq6y4RJIkJ9dQz18W32SpR/qXGfLDZ2jU4y4wKvvZwqbi7U6gxFQTeE+urMbXU/tsDy4w==",
"requires": {
"async-limiter": "~1.0.0"
}
"version": "7.4.6",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A=="
}
}
}
+1 -1
View File
@@ -4,6 +4,6 @@
},
"dependencies": {
"query-string": "^6.2.0",
"ws": "^6.0.0"
"ws": "^7.4.6"
}
}