Skip to content

Commit

Permalink
Issue #10 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
npeditto committed Nov 22, 2017
1 parent 5f0f32d commit 4b4a5d5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bin/wstun.js
Expand Up @@ -25,7 +25,7 @@ var _ = require("under_score");

optimist = require('optimist').usage("Tunnels and reverse tunnels over WebSocket.\n" +
"\nUsage: https://github.com/MDSLab/wstun/blob/master/readme.md")
.string("s").alias('s', "server").describe('s', 'run as server, specify listen port')
.string("s").alias('s', "server").describe('s', 'run as server, specify listening port')
.string("t").alias('t', "tunnel").describe('t', 'run as tunnel client, specify localport:host:port')
.boolean("r").alias('r', "reverse").describe('r', 'run in reverse tunneling mode')
.string("ssl").describe('ssl', '\"true\" | \"false\" to enable|disable HTTPS communication.')
Expand Down
19 changes: 19 additions & 0 deletions index.js
@@ -0,0 +1,19 @@
//###############################################################################
//##
//# Copyright (C) 2014-2015 Andrea Rocco Lotronto
//##
//# Licensed under the Apache License, Version 2.0 (the "License");
//# you may not use this file except in compliance with the License.
//# You may obtain a copy of the License at
//##
//# http://www.apache.org/licenses/LICENSE-2.0
//##
//# Unless required by applicable law or agreed to in writing, software
//# distributed under the License is distributed on an "AS IS" BASIS,
//# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//# See the License for the specific language governing permissions and
//# limitations under the License.
//##
//###############################################################################

module.exports = require('./lib/wrapper');
2 changes: 1 addition & 1 deletion lib/bindSockets_reverse.js
Expand Up @@ -59,7 +59,7 @@ bindSockets = function(wsconn, tcpconn) {
});

wsconn.on('close', function(reasonCode, description) {
console.log("[SYSTEM] - " + (new Date()) + ' --> WS Peer ' + wsconn.remoteAddress + ' disconnected - Reason: '+description);
console.log("[SYSTEM] - " + (new Date()) + ' --> WS Peer ' + wsconn.remoteAddress.split(":")[3] + ' disconnected - Reason: '+description);
return tcpconn.destroy();
});

Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Expand Up @@ -103,7 +103,7 @@

wst_server.prototype.start = function(port) {

if (https_flag == true)
if (https_flag == "true")
console.log("[SYSTEM] - " + (new Date()) + " - WS Tunnel Server starting on: wss://localhost:" + port + " - CERT: \n" + this.s4t_cert);
else
console.log("[SYSTEM] - " + (new Date()) + " - WS Tunnel Server starting on: ws://localhost:" + port);
Expand Down
33 changes: 21 additions & 12 deletions lib/server_reverse.js
Expand Up @@ -24,15 +24,20 @@ url = require("url");
net = require("net");
bindSockets = require("./bindSockets_reverse");

uuid = require('node-uuid');


https_flag = null;

var eventEmitter = require('events').EventEmitter;
eventEmitter.prototype._maxListeners = 1000;

var newWSTCP_DATA = new eventEmitter();

wst_server_reverse = function(options) {

if(options != undefined) {

console.log("[SYSTEM] - " + (new Date()) + " - WS Reverse Tunnel Server starting with these paramters:\n" + JSON.stringify(options, null, "\t"));
this.dstHost = options.dstHost;
this.dstPort = options.dstPort;
Expand All @@ -43,14 +48,15 @@ wst_server_reverse = function(options) {
else
console.log("[SYSTEM] - " + (new Date()) + " - WS Reverse Tunnel Server starting...");


if(https_flag == "true"){

//HTTPS
console.log("[SYSTEM] - " + (new Date()) + " WS Reverse Tunnel Server over HTTPS.");
var https = require('https');
var fs = require('fs');

require("../lib/https_override");
require("../lib/https_override"); //add parameters overriding each https request

https_flag = options.ssl;

Expand Down Expand Up @@ -96,7 +102,7 @@ wst_server_reverse = function(options) {

wst_server_reverse.prototype.start = function(port) {

if (https_flag == true)
if (https_flag == "true")
console.log("[SYSTEM] - " + (new Date()) + " WS Reverse Tunnel Server starting on: wss://localhost:" + port + " - CERT: \n" + this.s4t_cert);
else
console.log("[SYSTEM] - " + (new Date()) + " WS Reverse Tunnel Server starting on: ws://localhost:" + port);
Expand All @@ -105,7 +111,8 @@ wst_server_reverse.prototype.start = function(port) {
this.httpServer.listen(port, function() {
console.log("[SYSTEM] - " + (new Date()) + " WS Reverse Tunnel Server is listening...");
});



this.wsServerForControll.on('request', (function(_this){
return function(request){

Expand All @@ -114,30 +121,32 @@ wst_server_reverse.prototype.start = function(port) {

var uri = url.parse(request.httpRequest.url, true);

var src_address = request.httpRequest.client._peername.address.split(":")[3];

if (uri.query.dst != undefined){

var remoteAddr = uri.query.dst;
var portTcp;
ref1 = remoteAddr.split(":"); portTcp = ref1[1];
ref1 = remoteAddr.split(":");
var portTcp = ref1[1];

console.log("[SYSTEM] - " + (new Date()) + " New request:");
console.log("[SYSTEM] - " + (new Date()) + " WebSocket creation towards " + src_address + " on port " + portTcp );

request.tcpServer.listen(portTcp);
console.log("[SYSTEM] - " + (new Date()) + " --> TCP server is listening on port " + portTcp);
console.log("[SYSTEM] - " + (new Date()) + " --> TCP server is listening on port" + portTcp);

request.wsConnectionForControll = request.accept('tunnel-protocol', request.origin);
console.log("[SYSTEM] - " + (new Date()) + " --> WS connection created");

request.wsConnectionForControll.on('close', function(reasonCode, description) {
console.log('[SYSTEM] - ' + (new Date()) + ' WebSocket Controll Peer ' + request.wsConnectionForControll.remoteAddress + ' disconnected reason: \"'+description+'\"');
console.log("[SYSTEM] - " + (new Date()) + " Close TCP server on port " + portTcp);
console.log('[SYSTEM] - ' + (new Date()) + ' WebSocket Controll Peer ' + request.wsConnectionForControll.remoteAddress.split(":")[3] + ' disconnected reason: \"'+description+'\"');
console.log("[SYSTEM] - " + (new Date()) + " Close TCP server on port" + portTcp);
request.tcpServer.close();
});

}
else{
//REQUEST FOR WS USED FOR DATA
console.log("[SYSTEM] - " + (new Date()) + " --> Request for Data WS Socket");
console.log("[SYSTEM] - " + (new Date()) + " --> WebSocket Request for Data");
newWSTCP_DATA.emit('created', request);
}

Expand All @@ -150,8 +159,7 @@ wst_server_reverse.prototype.start = function(port) {

//Putting in pause the tcp connection waiting the new socket WS Socket for data
tcpConn.pause();

var idConnection = randomIntInc(1,1000);
var idConnection = uuid.v4(); //randomIntInc(1,100000);
var msgForNewConnection = "NC:"+idConnection;

request.wsConnectionForControll.sendUTF(msgForNewConnection);
Expand Down Expand Up @@ -180,6 +188,7 @@ wst_server_reverse.prototype.start = function(port) {
}catch (err) {
// handle the error
console.log("[SYSTEM] - " + (new Date()) + " --> ERROR: " + err);
request.tcpServer.close();

}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@mdslab/wstun",
"version": "1.0.1",
"version": "1.0.2",
"description": "A set of tools to establish TCP tunnels (or TCP reverse tunnels) over WebSocket connections for circumventing the problem of directly connect to hosts behind a strict firewall or without public IP. It also supports WebSocket Secure (wss) connections.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4b4a5d5

Please sign in to comment.