-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
106 lines (106 loc) · 3.86 KB
/
index.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
import { spawn, exec } from 'node:child_process';
import got from 'got';
import path from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
export class VpnGot {
configFile;
loginFile;
tableId;
interface;
constructor(configFile, loginFile) {
this.configFile = configFile;
this.loginFile = loginFile;
}
async getNewTableId() {
return new Promise((resolve, reject) => {
const command = `ip route show table all | \\
grep "table" | \\
sed 's/.*\\(table.*\\)/\\1/g' | \\
awk '{print $2}' | \\
sort | \\
uniq | \\
grep -e "[0-9]"`;
exec(command, (err, stdout) => {
if (err) {
console.log('err', err);
resolve('10');
}
console.log('stdout', stdout);
const existingIds = stdout
.toString()
.trim()
.split('\n')
.sort((a, b) => Number(a) - Number(b));
const nextId = Number(existingIds[existingIds.length - 1]) + 10;
resolve(nextId.toString());
});
});
}
async connect() {
const __filename = fileURLToPath(import.meta.url);
// 👇️ "/home/john/Desktop/javascript"
const __dirname = path.dirname(__filename);
console.log('directory-name 👉️', __dirname);
// 👇️ "/home/borislav/Desktop/javascript/dist/index.html"
console.log(path.join(__dirname, '/dist', 'index.html'));
return new Promise(async (resolve, reject) => {
this.tableId = await this.getNewTableId();
const ovpnClient = spawn('sudo', [
'openvpn',
'--script-security',
'2',
'--route-noexec',
`--route-up`,
`'${__dirname}/route_up.sh`,
`${this.tableId}'`,
`--config`,
`${this.configFile}`,
`--auth-user-pass`,
`${this.loginFile}`,
], { env: { TABLE_ID: this.tableId.toString() }, shell: true });
ovpnClient.stdout.on('data', (chunk) => {
chunk
.toString()
.split('\n')
.forEach((line) => {
const data = line.trim();
console.log('data', data);
if (data.toString().includes('[[network interface]]')) {
this.interface = data.toString().split(':')[1]?.trim();
console.log('data.toString()', data.toString());
console.log(chalk.green(`Sucessfully created VPN interface on ${this.interface}`));
}
if (data.toString().includes('AUTH_FAILED')) {
reject(new Error(`Auth failed for ${this.configFile}`));
}
if (data.toString().includes('Initialization Sequence Completed')) {
console.log(chalk.green(data.toString().trim()));
resolve(this);
}
});
});
ovpnClient.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ovpnClient.on('error', (err) => {
console.error('Error spawning ovpn:', err);
});
ovpnClient.on('close', (code) => {
console.log('openvpn exited with code', code);
});
});
}
async get(url, opts) {
if (!opts) {
opts = {
localAddress: this.interface,
};
}
else {
opts.localAddress = this.interface;
}
console.log('opts', opts);
return await got.get(url, opts);
}
}