-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.ts
50 lines (46 loc) · 1.41 KB
/
mod.ts
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
const decoder = new TextDecoder();
export const decode = (x: Uint8Array) => decoder.decode(x);
export const getNetworkAddr = async () => {
const isWin = Deno.build.os === 'windows';
const command = isWin ? 'ipconfig' : 'ifconfig';
try {
let ifconfig = await Deno.run({
cmd: [command],
stdout: 'piped',
});
const { success } = await ifconfig.status();
if (!success) {
throw new Error(`Subprocess ${command} failed to run`);
}
const raw = await ifconfig.output();
const text = decode(raw);
if (isWin) {
const addrs = text.match(new RegExp('ipv4.+([0-9]+.){3}[0-9]+', 'gi'));
let temp = addrs
? addrs[0].match(new RegExp('([0-9]+.){3}[0-9]+', 'g'))
: undefined;
const addr = temp ? temp[0] : undefined;
await Deno.close(ifconfig.rid);
if (!addr) {
throw new Error('Could not resolve your local adress.');
}
return addr;
} else {
const addrs = text.match(
new RegExp('inet (addr:)?([0-9]*.){3}[0-9]*', 'g')
);
await Deno.close(ifconfig.rid);
if (!addrs || !addrs.some((x) => !x.startsWith('inet 127'))) {
throw new Error('Could not resolve your local adress.');
}
return (
addrs &&
addrs
.find((addr: string) => !addr.startsWith('inet 127'))
?.split('inet ')[1]
);
}
} catch (err) {
console.log(err.message);
}
};