-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
184 lines (163 loc) · 5.92 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***
* Built-in modules
***/
var tls = require('tls');
var net = require('net');
var fs = require('fs');
var spawn = require('child_process').spawn
var readline = require('readline');
/***
* External modules
***/
var pty = require('pty.js');
/***
* Local modules
***/
var sslopts = require('./ssloptions');
var jsonfuncs = require('./jsonfuncs');
/***
* Configuration
**/
// once config is read, remove comments, parse it, then kick off server.
fs.readFile('conf.client.json', function(err,data) {
if (err) throw err;
run_client(JSON.parse(jsonfuncs.remove_comments(data)));
});
/***
* Program execution
***/
function run_client(cfg) {
// Establish the sockets and main loop of the client.
// setup some if/then stuff as dictionaries
// TODO this executes more code than is needed. make it lazy
var pcl = cfg.remote.ssl; // true or false
var prot = { true: tls, false: net };
var opts = { true: sslopts.filter_options(cfg.ssl), false: {} }
// update the options to contain host and port
opts[pcl].host = cfg.remote.address;
opts[pcl].port = cfg.remote.port;
// connect the socket.
var socket = prot[pcl].connect(opts[pcl], function() {
// check to see if secure connection failed.
if (pcl && socket.authorized === false) {
console.log('Secure connection failed: ' + socket.authorizationError);
socket.end();
} else {
console.log('Connected!');
// begin handling protocol
buffer = readline.createInterface(socket, socket);
buffer.my_socket = socket;
socket.my_buffer = buffer;
buffer.on('line', handle_protocol);
}
});
}
/***
* Protocol Handler
***/
function handle_protocol(data) {
var buffer = this;
socket = buffer.my_socket;
// manage client/server protocol communications.
data = data.toString();
// fix some kind of problem with readline or telnet in charmode
data = data.replace('\u0000','');
// first word of data is a command, the rest is arguments.
var command = data.split(' ', 1)[0];
// clean up any excess whitespace at the end of command and normalize case.
command = command.trimRight().toUpperCase();
// remove the command from the rest of the string
data = data.substring(command.length);
try {
resolve[command](buffer, data);
} catch (err) {
socket.write('command(' + command + '): ' + err.toString() + '\n');
}
}
var resolve = {
"PING": function(buffer, args) {
// Server sent PING commmand.
// Respond with PONG.
buffer.my_socket.write('PONG\n');
},
"MAC": function(buffer, args) {
// Server sent the MAC command.
// Respond with the MAC address of the connected socket.
// Unfortunately node.js does not support MAC address retrieval
// natively, so this will be captured through bash.
socket = buffer.my_socket;
// Connected socket local IP address.
var localIP = socket.address().address;
// Grab the hardware address from ifconfig for the given IP address.
// Assumes Linux base system, but so does 'bash' below.
console.log('ip',localIP.toString());
subproc = spawn('bash', ['-c','ifconfig | grep -B1 ' + localIP.toString()]);
subproc.stdout.on('data', function(data) {
data = data.toString();
// Extract MAC from ifconfig and send it to server.
// Assumes IPv4 for now ('HWaddr ##:##:##:##:##:##')
var start = data.search('HWaddr ') + 7;
// not found is -1, -1 + 7 = 6
if (start == 6) {
socket.write('ERR No HWaddr found, IP address: ' +
localIP.toString() + '\n');
return;
}
var end = start + 17;
socket.write(data.slice(start, end));
socket.write('\n');
});
subproc.stderr.on('data', function(data) {
socket.write('ERROR' + data.toString() + '\n');
});
},
"BYE": function(buffer, args) {
// Server sent BYE command.
// Close socket.
console.log('Server requests a disconnect. Complying with request...');
buffer.my_socket.end();
},
"TTY": function(buffer, args) {
// Server sent "TTY" command.
// Crack open a shell.
socket = buffer.my_socket;
console.log('Server requests a TTY.');
var tty = new pty.Terminal('bash', [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
// Stop processing protocol.
buffer.removeListener('line', handle_protocol);
// Connect the shell and socket together.
tty.pipe(socket); // bash.stdout | clientsocket.write
socket.pipe(tty); // clientsocket.read | bash.stdin
console.log('Terminal established.');
// Disconnect pipes when appropriate.
events = ['end','timeout','close','destroy','unpipe'];
var reset = function() {
console.log('Terminal reset indicated.');
// Turn off reset listeners, they won't be needed now.
for(var i = 0; i < events.length; i++) {
the_event = events[i];
tty.removeListener(the_event, reset);
socket.removeListener(the_event, reset);
}
// Disconnect shell from socket. (maybe this is redundant?)
tty.socket.unpipe(socket);
socket.unpipe(tty);
// Object cleanup.
delete tty;
// Start processing protocol again.
buffer.on('line', handle_protocol);
console.log('Terminal reset completed.');
}
for(var i = 0; i < events.length; i++) {
the_event = events[i];
tty.on(the_event, reset);
socket.on(the_event, reset);
}
}
}