@itd2902/auggw 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/api.js +124 -74
package/package.json
CHANGED
package/src/utils/api.js
CHANGED
|
@@ -1,74 +1,124 @@
|
|
|
1
|
-
const os = require("os");
|
|
2
|
-
const {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
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
|
-
if (
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
1
|
+
const os = require("os");
|
|
2
|
+
const { execSync } = require("child_process");
|
|
3
|
+
const { loadToken } = require("./token");
|
|
4
|
+
|
|
5
|
+
const DEFAULT_API_URL = "https://auggw.quangit.site";
|
|
6
|
+
// const DEFAULT_API_URL = "http://localhost:3000";
|
|
7
|
+
|
|
8
|
+
function getBaseURL() {
|
|
9
|
+
return process.env.auggw_API_URL || DEFAULT_API_URL;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getIPv4FromInterface(interfaceName) {
|
|
13
|
+
const interfaces = os.networkInterfaces();
|
|
14
|
+
const addrs = interfaces[interfaceName] || [];
|
|
15
|
+
|
|
16
|
+
for (const iface of addrs) {
|
|
17
|
+
if ((iface.family === "IPv4" || iface.family === 4) && !iface.internal) {
|
|
18
|
+
return iface.address;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getDefaultInterfaceWindows() {
|
|
26
|
+
try {
|
|
27
|
+
const cmd = `powershell -NoProfile -Command "Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Sort-Object RouteMetric, InterfaceMetric | Select-Object -First 1 -ExpandProperty InterfaceAlias"`;
|
|
28
|
+
return execSync(cmd, { encoding: "utf8" }).trim();
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getDefaultInterfaceMac() {
|
|
35
|
+
try {
|
|
36
|
+
const output = execSync("route -n get default", { encoding: "utf8" });
|
|
37
|
+
const match = output.match(/interface:\s+(.+)/);
|
|
38
|
+
return match ? match[1].trim() : null;
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getLocalIP() {
|
|
45
|
+
const platform = process.platform;
|
|
46
|
+
|
|
47
|
+
// Try default interface first (most accurate)
|
|
48
|
+
let defaultInterface = null;
|
|
49
|
+
if (platform === "win32") {
|
|
50
|
+
defaultInterface = getDefaultInterfaceWindows();
|
|
51
|
+
} else if (platform === "darwin") {
|
|
52
|
+
defaultInterface = getDefaultInterfaceMac();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (defaultInterface) {
|
|
56
|
+
const ip = getIPv4FromInterface(defaultInterface);
|
|
57
|
+
if (ip) return ip;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Fallback: skip virtual/VPN adapters, prefer real Ethernet/Wi-Fi
|
|
61
|
+
const skipPatterns =
|
|
62
|
+
/tailscale|vpn|virtual|vethernet|bluetooth|loopback|docker|vbox|vmware|wsl/i;
|
|
63
|
+
|
|
64
|
+
const interfaces = os.networkInterfaces();
|
|
65
|
+
|
|
66
|
+
for (const [name, addrs] of Object.entries(interfaces)) {
|
|
67
|
+
if (skipPatterns.test(name)) continue;
|
|
68
|
+
for (const iface of addrs) {
|
|
69
|
+
if ((iface.family === "IPv4" || iface.family === 4) && !iface.internal) {
|
|
70
|
+
return iface.address;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Last resort: any non-internal IPv4
|
|
76
|
+
for (const addrs of Object.values(interfaces)) {
|
|
77
|
+
for (const iface of addrs) {
|
|
78
|
+
if ((iface.family === "IPv4" || iface.family === 4) && !iface.internal) {
|
|
79
|
+
return iface.address;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return "127.0.0.1";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function apiRequest(method, path, body = null) {
|
|
88
|
+
const url = `${getBaseURL()}${path}`;
|
|
89
|
+
const token = loadToken();
|
|
90
|
+
|
|
91
|
+
const headers = {
|
|
92
|
+
"Content-Type": "application/json",
|
|
93
|
+
"X-Client-IP": getLocalIP(),
|
|
94
|
+
"X-Client-Hostname": os.hostname(),
|
|
95
|
+
};
|
|
96
|
+
if (token) {
|
|
97
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const options = { method, headers };
|
|
101
|
+
if (body) {
|
|
102
|
+
options.body = JSON.stringify(body);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const res = await fetch(url, options);
|
|
107
|
+
const data = await res.json();
|
|
108
|
+
|
|
109
|
+
if (!res.ok) {
|
|
110
|
+
const err = new Error(data.error || `HTTP ${res.status}`);
|
|
111
|
+
err.status = res.status;
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return data;
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if (err.cause && err.cause.code === "ECONNREFUSED") {
|
|
118
|
+
throw new Error(`Could not connect to server at ${getBaseURL()}`);
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = { apiRequest, getBaseURL };
|