-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomegga.main.js
170 lines (147 loc) · 4.48 KB
/
omegga.main.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
const fs = require('fs');
const path = require('path');
const util = require('util');
const { exec: execNonPromise, spawn } = require('child_process');
const exec = util.promisify(execNonPromise);
const TOOL_IP = path.join(__dirname, 'tools/ip.sh');
const TOOL_PROXY = path.join(__dirname, 'tools/proxy.sh');
// log helper fns
const log = (...args) =>
(global.Omegga ?? global.Logger).log(
'wsl2binds'.underline,
'>>'.green,
...args
);
const info = (...args) =>
(global.Omegga ?? global.Logger).log(
'wsl2binds'.underline,
'?>'.blue,
...args
);
const error = (...args) =>
(global.Omegga ?? global.Logger).error(
'wsl2binds'.underline,
'!>'.red,
...args
);
module.exports = class Binder {
constructor(omegga, _config, _store) {
this.omegga = omegga;
}
setup() {
const { ip, port } = this;
if (this.child) {
error('Error - attempting to re-setup before process is closed');
return;
}
log(`UDP Proxy - Now forwarding to ${`${ip}:${port}`.yellow}`);
// create the child process
this.child = spawn(TOOL_PROXY, [ip, port], {
cwd: path.join(__dirname, 'tools'),
stdio: ['ignore', 'pipe', 'pipe'],
shell: true,
});
this.pid = null;
const handleLine = data => {
data = data.trim();
const isPid = data.match(/^pid = (\d+)$/);
const isClientOpen = data.match(
/^client (?<ip>\d+\.\d+\.\d+\.\d+:\d+) -> 0\.0\.0\.0:(?<port>\d+)$/
);
const isClientClose = data.match(
/^client (?<ip>\d+\.\d+\.\d+\.\d+:\d+) -> closed$/
);
const isStart = data.match(/^Listening on 0\.0\.0\.0:\d+$/);
if (isPid && !this.pid) {
this.pid = isPid[1];
info('UDP Proxy - PID is', (this.pid + '').yellow);
} else if (isClientOpen) {
log(
'Joining client',
isClientOpen.groups.ip.yellow,
'->',
isClientOpen.groups.port.yellow
);
} else if (isClientClose) {
// no need to log timed out clients
// log('Timed out client', isClientClose.groups.ip.yellow);
} else if (isStart) {
log('UDP Proxy - Listen server started');
} else if (data.length > 0) {
log(`stdout: ${data}`);
}
};
// handle output
this.child.stdout.on('data', data => {
data.toString().split('\n').forEach(handleLine);
});
// print error to console
this.child.stderr.on('data', data => {
error(`stderr: ${data}`);
});
this.child.on('close', code => {
log(`UDP Proxy - Process exited with code ${(code + '').yellow}`);
this.child = undefined;
if (!this.closed) {
info('Restarting proxy in', '5 seconds'.yellow);
setTimeout(() => {
this.setup();
}, 5000);
}
});
}
async init() {
this.closed = false;
// make sure this plugin can run
if (!this.omegga.config) {
return error(
'Omegga is outdated and missing required features for this plugin to operate'
);
}
// check if this is wsl2 (/run/WSL/ exists on wsl2 instances)
const isWSL2 = fs.existsSync('/run/WSL');
if (!isWSL2) {
return info('Not in WSL2 environment! This plugin is redundant!');
}
log('WSL2 detected');
// get the IP from a handy dandy smallguy one liner sh script
let { stdout: ip, stderr } = await exec(TOOL_IP, { shell: true });
// remove newline from IP
ip = ip.trim();
// make sure that the output is an IP
if (!ip || !ip.match(/^\d+\.\d+\.\d+\.\d+$/)) {
return error('Could not find IP', ip, stderr);
}
const { port: webPort } = this.omegga.config.omegga;
this.ip = ip;
info(
`Please run the following command in ${
'Windows Powershell'.brightYellow
} as ${'Administrator'.brightYellow} to access the ${'Web UI'.green}:`
);
info(
`netsh interface portproxy add v4tov4 listenport=${webPort} listenaddress=0.0.0.0 connectport=${webPort} connectaddress=${ip}`
.grey
);
info(
`Or simply connect to the Web UI on the same PC with ${
`https://${ip}:${webPort}`.green
}`
);
// get config from omegga config
const { port } = this.omegga.config.server;
this.port = port;
this.setup();
}
// cleanup by killing the proxy child process
async stop() {
this.closed = true;
if (this.pid) {
log('Killing proxy process', this.pid);
spawn('kill', ['-9', this.pid]);
}
if (this.child) {
this.child.kill('SIGINT');
}
}
};