proxy implementation #2
44
index.js
44
index.js
@ -1,14 +1,19 @@
|
|||||||
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);
|
||||||
@ -17,11 +22,18 @@ wss.on('connection', function (cws, req){
|
|||||||
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,
|
||||||
|
agent: params.agent
|
||||||
|
? new SocksProxyAgent("socks://" + params.agent)
|
||||||
|
: undefined
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
cws.close();
|
cws.close();
|
||||||
return;
|
return;
|
||||||
@ -29,26 +41,26 @@ wss.on('connection', function (cws, req){
|
|||||||
|
|
||||||
// client to target
|
// client to target
|
||||||
var messageBuffer = [];
|
var messageBuffer = [];
|
||||||
tws.on('open', function(){
|
tws.on("open", function() {
|
||||||
for (let message of messageBuffer) tws.send(message);
|
for (let message of messageBuffer) tws.send(message);
|
||||||
messageBuffer = undefined;
|
messageBuffer = undefined;
|
||||||
});
|
});
|
||||||
cws.on('message', function(message){
|
cws.on("message", function(message) {
|
||||||
if (tws.readyState == WebSocket.OPEN) tws.send(message);
|
if (tws.readyState == WebSocket.OPEN) tws.send(message);
|
||||||
else if (messageBuffer) messageBuffer.push(message);
|
else if (messageBuffer) messageBuffer.push(message);
|
||||||
});
|
});
|
||||||
cws.on('close', function(){
|
cws.on("close", function() {
|
||||||
tws.close();
|
tws.close();
|
||||||
messageBuffer = undefined;
|
messageBuffer = undefined;
|
||||||
});
|
});
|
||||||
cws.on('error', console.error);
|
cws.on("error", console.error);
|
||||||
|
|
||||||
// target to client
|
// target to client
|
||||||
tws.on('message', function(message){
|
tws.on("message", function(message) {
|
||||||
if (cws.readyState == WebSocket.OPEN) cws.send(message);
|
if (cws.readyState == WebSocket.OPEN) cws.send(message);
|
||||||
});
|
});
|
||||||
tws.on('close', function(){
|
tws.on("close", function() {
|
||||||
cws.close();
|
cws.close();
|
||||||
});
|
});
|
||||||
tws.on('error', console.error);
|
tws.on("error", console.error);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user