Skip to content

Commit

Permalink
Extensive code redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
tewarid committed May 26, 2017
1 parent 5d197e8 commit 275d3c6
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 284 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "node-tcp-relay",
"description": "A simple TCP relay mechanism for NAT traversal built using Node.js",
"license": "MIT",
"version": "0.0.6",
"version": "0.0.7",
"main": "index.js",
"repository": {
"type": "git",
Expand Down
2 changes: 0 additions & 2 deletions relay-client-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var argv = require("optimist")
.demand(['host', 'port', 'relayHost', 'relayPort'])
.argv;

console.log(argv);

var relayClient = require("./relay-client.js")

var newRelayClient = relayClient.createRelayClient(argv.host, argv.port, argv.relayHost, argv.relayPort, argv.numConn);
Expand Down
157 changes: 65 additions & 92 deletions relay-client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

var util = require("util");
var EventEmitter = require("events").EventEmitter;
var net = require("net");

function uniqueKey(socket) {
var key = socket.localAddress + ':' + socket.localPort;
return key;
module.exports = {
createRelayClient: function createRelayClient(host, port, relayHost, relayPort, numConn) {
return new RelayClient(host, port, relayHost, relayPort, numConn);
}
}

function RelayClient(host, port, relayHost, relayPort, numConn) {
Expand All @@ -12,110 +14,81 @@ function RelayClient(host, port, relayHost, relayPort, numConn) {
this.relayHost = relayHost;
this.relayPort = relayPort;
this.numConn = numConn;
this.relaySockets = {};
this.clients = new Array();

if (this.numConn == undefined) {
this.numConn = 1;
}

for (var i = 0; i < this.numConn; i++) {
this.newSocket();
}
this.clients[this.clients.length] = this.newClient(host, port, relayHost, relayPort);
}
}

RelayClient.prototype.newSocket = function () {
RelayClient.prototype.newClient = function(host, port, relayHost, relayPort) {
var relayClient = this;
var client = new Client(host, port, relayHost, relayPort);
client.on("inUse", function() {
relayClient.clients[relayClient.clients.length] =
relayClient.newClient(host, port, relayHost, relayPort);
});
return client;
}

var socket = undefined;
var connected = false;
var buffers = new Array();

var relaySocket = new net.Socket();

relaySocket.connect(relayClient.relayPort, relayClient.relayHost,
function () {
console.log("relay socket established");

relayClient.relaySockets[uniqueKey(relaySocket)] = relaySocket;

relaySocket.on("data", function (data) {
if (socket == undefined) {
buffers[buffers.length] = data;

socket = new net.Socket();
socket.connect(relayClient.port, relayClient.host,
function () {
console.log("service socket established");

connected = true;
if (buffers.length > 0) {
for (var i = 0; i < buffers.length; i++) {
socket.write(buffers[i]);
}
buffers == undefined;
}

socket.on("data", function (data) {
try {
relaySocket.write(data);
} catch (ex) {
console.log(ex);
}
});

socket.on("close", function (had_error) {
console.log("service socket closed");
console.log(had_error);
socket == undefined;
console.log(" ending relay socket");
relaySocket.destroy();
});
});

socket.on("error", function (e) {
console.log("service socket error");
console.log(e);
console.log(" ending relay socket");
relaySocket.destroy();
});
RelayClient.prototype.end = function () {
for (var i = 0; i < this.clients.length; i++) {
this.clients[i].relaySocket.end();
}
}

relayClient.newSocket();
console.log("next relay socket established");
util.inherits(Client, EventEmitter);

function Client(host, port, relayHost, relayPort) {
this.serviceSocket = undefined;
this.bufferData = true;
this.buffer = new Array();

var client = this;
client.relaySocket = new net.Socket();
client.relaySocket.connect(relayPort, relayHost, function () {
client.relaySocket.on("data", function (data) {
if (client.serviceSocket == undefined) {
client.emit("inUse");
client.createServiceSocket(host, port);
}
if (client.bufferData) {
client.buffer[client.buffer.length] = data;
} else {
if (!connected) {
buffers[buffers.length] = data;
} else {
try {
socket.write(data);
} catch (ex) {
console.log(ex);
}
}
client.serviceSocket.write(data);
}
});

relaySocket.on("close", function (had_error) {
console.log("relay socket closed");
delete relayClient.relaySockets[uniqueKey(relaySocket)];
if (socket != undefined)
socket.destroy();
});

relaySocket.on("error", function(e) {
console.log("relay socket error");
console.log(e);
client.relaySocket.on("close", function (had_error) {
if (client.serviceSocket != undefined) {
client.serviceSocket.destroy();
}
});
});
}

RelayClient.prototype.end = function () {
console.log("Terminating relay client");
for (var key in this.relaySockets) {
this.relaySockets[key].end();
}
}

exports.createRelayClient = function createRelayClient(host, port, relayHost, relayPort, numConn) {
return new RelayClient(host, port, relayHost, relayPort, numConn);
}
Client.prototype.createServiceSocket = function (host, port) {
var client = this;
client.serviceSocket = new net.Socket();
client.serviceSocket.connect(port, host, function () {
client.bufferData = false;
if (client.buffer.length > 0) {
for (var i = 0; i < client.buffer.length; i++) {
client.serviceSocket.write(client.buffer[i]);
}
client.buffer.length = 0;
}
client.serviceSocket.on("data", function (data) {
try {
client.relaySocket.write(data);
} catch (ex) {
}
});
});
client.serviceSocket.on("error", function (had_error) {
client.relaySocket.end();
});
}
9 changes: 6 additions & 3 deletions relay-server-cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env node
var argv = require("optimist")
.usage('Usage: $0 --relayPort [port] --servicePort [port]')
.usage('Usage: $0 --relayPort [port] --servicePort [port] [--auth]')
.demand(['relayPort', 'servicePort'])
.boolean('auth')
.argv;

console.info(argv);
var options = {
auth: argv.auth
};

var relayServer = require("./relay-server.js");

var newRelayServer = relayServer.createRelayServer(argv.relayPort, argv.servicePort);
var newRelayServer = relayServer.createRelayServer(argv.relayPort, argv.servicePort, options);

process.on("uncaughtException", function (err) {
console.info(err);
Expand Down
Loading

0 comments on commit 275d3c6

Please sign in to comment.