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 pathclient.js
64 lines (55 loc) · 1.5 KB
/
client.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
59
60
61
62
63
64
"use strict";
var args = process.argv.splice(2);
var http = require(args[0].substring(0, 5) == 'https' ? 'https' : 'http');
var sys = require('sys');
var net = require('net');
var url = require('url');
var options = url.parse(args[0]);
options.headers = {
'Connection': 'Upgrade',
'Upgrade': 'TCPoverHTTP'
};
options.rejectUnauthorized = false;
var req = http.request(options);
req.end();
if(args.length > 1) {
var listen = parseInt(args[1], 10);
req.on('upgrade', function(res, socket, head) {
console.log('Connection established. Listening on localhost:' + listen);
var server = net.createServer(function(c) {
c.on('data', function(chunk) {
socket.write(chunk);
}).on('end', function() {
socket.end();
});
socket.on('data', function(chunk) {
c.write(chunk);
}).on('end', function() {
// don't shut down the server on socket close
// c.end();
});
});
server.listen(listen, 'localhost');
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
} else {
req.on('upgrade', function(res, socket, head) {
var stdin = process.stdin;
var stdout = process.stdout;
stdin.resume();
socket.on('data', function(chunk) {
stdout.write(chunk);
}).on('end', function() {
// don't close Stdout, it does not make any sense
// stdout.end();
});
stdin.on('data', function(chunk) {
socket.write(chunk);
}).on('end', function() {
socket.end();
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
}