This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
58 lines (51 loc) · 1.66 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
var http = require('http');
var net = require('net');
var args = process.argv.splice(2);
var port = 1194;
var host = 'localhost';
var listenPort = 8800;
if(args.length > 0) {
var connectTo = args[0].split(':');
host = connectTo[0]
if(connectTo.length > 1) { port = parseInt(connectTo[1], 10); }
}
if(args.length > 1) {
listenPort = parseInt(args[1], 10);
}
var srv = http.createServer(function(req, res) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not found');
});
srv.on('upgrade', function(req, socket, head) {
if('TCPoverHTTP' != req.headers.upgrade) {
socket.end(
'HTTP/1.1 404 Not Found\r\n' +
'Content-Type: text/plain\r\n' +
'Connection: close\r\n' +
'Content-Length: 9\r\n' +
'\r\n' +
'Not found'
);
}
// request is the arguments for the http request, as it is in the request event.
// socket is the network socket between the server and client.
// head is an instance of Buffer, the first packet of the upgraded stream, this may be empty.
socket.write(
'HTTP/1.1 101 Switching Protocols\r\n' +
'Upgrade: TCPoverHTTP\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');
var remote = net.connect(port, host);
socket.pipe(remote);
remote.pipe(socket);
});
console.log('To change listening port or target connection, start like this:');
console.log(' nodejs server.js <host>:<port> [<listeningPort>]');
console.log('');
console.log('For example:');
console.log(' nodejs server.js www.google.com:80');
console.log('');
console.log('Listening on port %d, will forward connections to %s:%d...', listenPort, host, port);
console.log('Now connect with "nodejs client.js http(s)://<host>:<port>/"');
srv.listen(listenPort);