telebit-relay.js/snippets/tcp-spy.js

34 lines
665 B
JavaScript
Raw Normal View History

2016-09-30 22:03:41 +00:00
'use strict';
var net = require('net');
var http = require('http');
var http80 = http.createServer(function (req, res) {
res.end('Hello, World!');
});
var tcp80 = net.createServer(function (socket) {
socket.once('data', function (chunk) {
if (/http\/1/i.test(chunk.toString())) {
console.log("looks like http, continue");
http80.emit('connection', socket);
} else {
console.log("looks like tcp, die");
socket.end();
}
socket.pause();
process.nextTick(function () {
socket.emit('data', chunk);
socket.resume();
});
});
});
tcp80.listen(80, function () {
console.log('listening on 80');
});