Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tcp based repeater #445

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
config.js
*.idea
112 changes: 81 additions & 31 deletions backends/repeater.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
/*jshint node:true, laxcomma:true */

var util = require('util')
, dgram = require('dgram')
, logger = require('../lib/logger');
, dgram = require('dgram')
, logger = require('../lib/logger');

var l;
var debug;
var net = require('net')
, generic_pool = require('generic-pool');

function RepeaterBackend(startupTime, config, emitter){
var self = this;
this.config = config.repeater || [];
this.sock = (config.repeaterProtocol == 'udp6') ?
function UDPRepeater(config, logger) {
var self = this;
this.debug = config.debug;
this.logger = logger
this.config = config;
this.sock = (config.repeaterProtocol == 'udp6') ?
dgram.createSocket('udp6') :
dgram.createSocket('udp4');
// Attach DNS error handler
this.sock.on('error', function (err) {
if (debug) {
l.log('Repeater error: ' + err);
// Attach DNS error handler
this.sock.on('error', function (err) {
if (self.debug) {
self.logger.log('UDP Repeater error: ' + err);
}
});
}

UDPRepeater.prototype.process = function (packet, rinfo) {
var self = this;
var hosts = self.config.hosts;
for (var i = 0; i < hosts.length; i++) {
self.sock.send(packet, 0, packet.length, hosts[i].port, hosts[i].host, function (err, bytes) {
if (err && self.debug) {
self.logger.log(err);
}
});
}
});
// attach
emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
};

function TCPRepeater(config, logger) {
var self = this;
this.debug = config.debug;
this.logger = logger
this.connectionPool = generic_pool.Pool({
name: 'connectionPool',
max: config.maxConnections,
create: function(callback) {
var conn = net.createConnection(config.port, config.host);
conn.addListener('error', function (connectionException) {
self.logger.log('TCP Repeater error: ' + connectionException);
self.connectionPool.destroy(this)
callback(connectionException, null);
});
conn.on('connect', function () {
if (self.debug) {
self.logger.log("connected to " + config.host + ":" + config.port);
}
callback(null, this);
});
},
destroy: function(conn) {
if (self.debug) {
self.logger.log("connection is destroyed");
}
conn.destroy();
}
});
}

RepeaterBackend.prototype.process = function(packet, rinfo) {
var self = this;
hosts = self.config;
for(var i=0; i<hosts.length; i++) {
self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
function(err,bytes) {
if (err && debug) {
l.log(err);
}
TCPRepeater.prototype.process = function (packet, rinfo) {
var self = this;
self.connectionPool.acquire(function(err, conn) {
if (err) {
self.logger.log("connection error. data is lost: "+packet);
}
else {
conn.write(packet + "\n");
}
self.connectionPool.release(conn);
});
}

exports.init = function (startupTime, config, events, logger) {
var instance = {};
if (config.repeater.udp && config.repeater.udp.enabled) {
instance = new UDPRepeater(config.repeater.udp, logger);
}
else if (config.repeater.tcp && config.repeater.tcp.enabled){
instance = new TCPRepeater(config.repeater.tcp, logger);
}
events.on('packet', function (packet, rinfo) {
instance.process(packet, rinfo);
});
}
};

exports.init = function(startupTime, config, events, logger) {
var instance = new RepeaterBackend(startupTime, config, events);
debug = config.debug;
l = logger;
return true;
return true;
};
59 changes: 59 additions & 0 deletions backends/repeaterTcp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*jshint node:true, laxcomma:true */

var util = require('util');
var logger = require('../lib/logger');
var net = require('net');
var generic_pool = require('generic-pool');

function TCPRepeater(config, logger) {
var self = this;
this.debug = config.debug;
this.logger = logger
this.connectionPool = generic_pool.Pool({
name: 'connectionPool',
max: config.maxConnections,
create: function(callback) {
var conn = net.createConnection(config.port, config.host);
conn.addListener('error', function (connectionException) {
self.logger.log('TCP Repeater error: ' + connectionException);
self.connectionPool.destroy(this)
callback(connectionException, null);
});
conn.on('connect', function () {
if (self.debug) {
self.logger.log("connected to " + config.host + ":" + config.port);
}
callback(null, this);
});
},
destroy: function(conn) {
if (self.debug) {
self.logger.log("connection is destroyed");
}
conn.destroy();
}
});
}

TCPRepeater.prototype.process = function (packet, rinfo) {
var self = this;
self.connectionPool.acquire(function(err, conn) {
if (err) {
self.logger.log("connection error. data is lost: "+packet);
}
else {
conn.write(packet + "\n");
}
self.connectionPool.release(conn);
});
}

exports.init = function (startupTime, config, events, logger) {
if (config.repeater.tcp && config.repeater.tcp.enabled){
instance = new TCPRepeater(config.repeater.tcp, logger);
events.on('packet', function (packet, rinfo) {
instance.process(packet, rinfo);
});
}
return true;
};