-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverws.js
48 lines (44 loc) · 1.16 KB
/
serverws.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
import { Tun, Tap} from 'tuntap2';
import { WebSocketServer } from 'ws';
import {config} from 'dotenv';
import fs from 'fs';
config();
const tap = new Tap();
//let passphrase= process.env.passphrase;
/* let pubkeyobj = readArmored(fs.readFileSync('pub.asc', 'utf8'));
let privkeyobj = readArmored(fs.readFileSync('priv.asc', 'utf8')); */
try{
tap.mtu = 1400;
//tap.ipv4 = "10.11.12.1/24";
/* tap.on('data', (buf) => {
console.log(`received: ${buf}`);
}); */
tap.isUp = true;
console.log(`created tap: ${tap.name}, ip: ${tap.ipv4}, mtu: ${tap.mtu}`);
//console.log(tap);
//tap.release();
}catch(e){
console.log(`error: ${e}`);
process.exit(0);
}
var webs = new WebSocketServer({port: 8124});
webs.on('connection', function(c) {
console.log('client connected');
if(tap) {
tap.on('data', (buf) => {
//console.log(`sent: ${buf}`);
c.send(buf);
}
);
c.on('message', (buf) => {
//console.log(`received: ${buf}`);
tap.write(buf);
}
);
}
});
webs.on('error', function(e) {
console.log(`error: ${e}`);
process.exit(0);
}
);