Skip to content

Instantly share code, notes, and snippets.

@Filirom1
Created October 26, 2016 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Filirom1/7ef6615db3a6c42f79e83867ee60ff7d to your computer and use it in GitHub Desktop.
Save Filirom1/7ef6615db3a6c42f79e83867ee60ff7d to your computer and use it in GitHub Desktop.
OpenTSDB proxy for debugging
#!/usr/bin/env node
var clusterMaster = require("cluster-master")
clusterMaster({
exec: "proxy.js",
// exec: "proxy-simple.js",
size: 2,
silent: false,
signals: true,
repl: '/root/cluster-master.socket'
})
/*jshint node:true*/
'use strict';
var net = require('net');
var fs = require('fs');
function readConfig(cb){
fs.readFile('/root/proxy.json', function(err, data){
if(err) return cb(err);
var json = JSON.parse(data.toString());
cb(err, json);
})
}
var proxy=null;
readConfig(function(err, config){
if(err) return console.error(err);
// proxy server
proxy = net.createServer(function (socket) {
var client;
// Create a new connection to the TCP server
client = net.connect(config.remotePort, config.remoteHost);
client.on('end', function(){
process.stdout.write('.');
});
// 2-way pipe between client and TCP server
socket.pipe(client).pipe(socket);
socket.setTimeout(60000);
socket.on('timeout', function() {
process.stdout.write('X');
socket.end();
});
socket.on('error', function (err) {
console.log('Error: ' + err);
});
});
proxy.maxConnections = config.maxConnections
proxy.listen(config.port, config.host);
})
process.on('SIGHUP', function () {
readConfig(function(err, config){
if(err) return console.error(err);
console.log('maxConnections = ' + config.maxConnections)
proxy.maxConnections = config.maxConnections
})
})
/*jshint node:true*/
'use strict';
var net = require('net');
var fs = require('fs');
function readConfig(cb){
fs.readFile('/root/proxy.json', function(err, data){
if(err) return cb(err);
var json = JSON.parse(data.toString());
cb(err, json);
})
}
var proxy=null;
readConfig(function(err, config){
if(err) return console.error(err);
// proxy server
proxy = net.createServer(function (socket) {
var client;
// Create a new connection to the TCP server
client = net.connect(config.remotePort, config.remoteHost);
var connectTime = null;
var socketSize = 0;
socket.on('data', function(data){
socketSize += data.length
});
var clientSize = 0;
client.on('data', function(data){
clientSize += data.length
});
client.on('connect', function(){
connectTime = new Date();
});
client.on('end', function(){
var diffTime = (new Date() - connectTime) /1000;
console.log(new Date() + ' - ' + socketSize + ' - ' + clientSize + ' - ' + diffTime);
//process.stdout.write('.');
});
// 2-way pipe between client and TCP server
socket.pipe(client).pipe(socket);
//client.setTimeout(60000);
//client.on('timeout', function() {
// console.log('timeout');
// //process.stdout.write('X');
// client.end();
//});
socket.on('error', function (err) {
console.log('Error: ' + err);
});
});
proxy.maxConnections = config.maxConnections
proxy.listen(config.port, config.host);
})
process.on('SIGHUP', function () {
readConfig(function(err, config){
if(err) return console.error(err);
console.log('maxConnections = ' + config.maxConnections)
proxy.maxConnections = config.maxConnections
})
})
{
"port": 4242,
"host": "0.0.0.0",
"remotePort": 4241,
"remoteHost": "127.0.0.1",
"maxConnections": 600
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment