Skip to content

Instantly share code, notes, and snippets.

@rlidwka
Created November 8, 2012 20:24
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 rlidwka/4041338 to your computer and use it in GitHub Desktop.
Save rlidwka/4041338 to your computer and use it in GitHub Desktop.
var net = require('net');
net.createServer(function (socket) {
socket.setEncoding("ascii");
socket.on("data", function (data) {
data = data.trim();
console.log('Serving: ', data);
if (data == 'img') {
socket.write('The reference images:\r\n');
socket.write('\r\n');
socket.write('I1\t/img/1.png\t10.10.0.190\t70\t+\r\n');
socket.write('I2\t/img/2.png\t10.10.0.190\t70\t+\r\n');
socket.write('I3\t/img/3.png\t10.10.0.190\t70\t+\r\n');
socket.end();
} else if (data == 'rfc') {
socket.write('0RFC 1436 Internet Gopher Protocol\t/rfc/rfc1436.txt\t10.10.0.190\t70\t+\r\n');
socket.write('hRFC 2068 HTTP/1.1 (html)\t/rfc/rfc2068.html\t10.10.0.190\t70\t+\r\n');
socket.write('0RFC 1034 Domain Names - Concepts and Facilities\t/rfc/rfc1034.txt\t10.10.0.190\t70\t+\r\n');
socket.write('0RFC 1035 Domain Names - Implementation and Specification\t/rfc/rfc1035.txt\t10.10.0.190\t70\t+\r\n');
socket.write('0Gother+\t/rfc/gopher+.txt\t10.10.0.190\t70\t+\r\n');
socket.write('0RFC 1533 DHCP Options\t/rfc/rfc1533.txt\t10.10.0.190\t70\t+\r\n');
socket.end();
} else if (data == '.' || data == '') {
socket.write('**************************\r\n');
socket.write('* Welcome to this server *\r\n');
socket.write('**************************\r\n');
socket.write('\r\n');
socket.write('1How it should look like\timg\t10.10.0.190\t70\t+\r\n');
socket.write('1Some RFC\trfc\t10.10.0.190\t70\t+\r\n');
socket.write('7Input\tinput\t10.10.0.190\t70\t+\r\n');
socket.end();
} else {
console.log('dont know about', data, 'proxying');
var upsock = new net.Socket();
upsock.connect(7000, 'localhost', function() {
upsock.write(data + "\r\n");
});
socket.on('close', function() {
upsock.end();
});
upsock.on('data', function(d) {
if (socket.writable) socket.write(d);
});
upsock.on('close', function() {
socket.end();
});
}
});
socket.on("end", function () {
console.log('Ending connection.');
socket.end();
});
}).listen(7001, "127.0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment