forked from GreenteaOS/Tofita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.js
50 lines (42 loc) · 1.57 KB
/
listen.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
// The Tofita Kernel
// Copyright (C) 2019 Oleg Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Simple tool to read COM port data into TCP socket
// Exits when connection lost, so it may be useful from terminal
const net = require('net')
// Configuration
const command = '"C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage" --nologo startvm Tofita'
const host = 'localhost'
const port = 1234
// Create server
const server = net.createServer(onClientConnected)
server.listen(port, host, function() {
console.log('Server listening on %j', server.address())
require('child_process').exec(command)
})
function onClientConnected(sock) {
const client = sock.remoteAddress + ':' + sock.remotePort
console.log('VirtualBox connected:', client)
sock.on('data', (data) => {
// Print
process.stdout.write(data)
})
sock.on('close', () => {
console.log('Connection from %s closed, exit', client)
process.exit(0)
})
sock.on('error', (err) => {
console.log('Connection %s error: %s', client, err.message)
})
}