-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvironment.js
More file actions
255 lines (226 loc) · 8.19 KB
/
Copy pathenvironment.js
File metadata and controls
255 lines (226 loc) · 8.19 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Agent Connect — Environment Detection & Setup
*
* Detects OS, checks all dependencies, reports what's available.
* An AI agent calls setup() first to understand what it can do.
*/
import {
verifyDependencies,
IS_ADMIN,
WG_AVAILABLE,
V2RAY_VERSION,
preflight,
} from 'blue-js-sdk';
import { existsSync } from 'fs';
import { execSync } from 'child_process';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
const __dirname = dirname(fileURLToPath(import.meta.url));
// ─── V2Ray Detection (comprehensive) ────────────────────────────────────────
/**
* Find V2Ray binary by checking every known location.
* This is the authoritative detection — covers env var, SDK paths, system paths.
*/
function findV2Ray() {
const binary = process.platform === 'win32' ? 'v2ray.exe' : 'v2ray';
// 1. V2RAY_PATH env var (highest priority)
if (process.env.V2RAY_PATH && existsSync(process.env.V2RAY_PATH)) {
return process.env.V2RAY_PATH;
}
// 2. blue-js-sdk bin/ (resolve through npm package)
try {
const require = createRequire(import.meta.url);
const sdkPath = dirname(require.resolve('blue-js-sdk'));
const sdkBin = resolve(sdkPath, 'bin', binary);
if (existsSync(sdkBin)) return sdkBin;
} catch { /* blue-js-sdk not resolvable */ }
// 3. Local bin/
const localBin = resolve(__dirname, 'bin', binary);
if (existsSync(localBin)) return localBin;
// 4. Sibling bin/ (when SDK is installed as npm package)
const siblingBin = resolve(__dirname, '..', 'bin', binary);
if (existsSync(siblingBin)) return siblingBin;
// 5. System paths
const systemPaths = process.platform === 'win32'
? ['C:\\Program Files\\V2Ray\\v2ray.exe', 'C:\\Program Files (x86)\\V2Ray\\v2ray.exe']
: ['/usr/local/bin/v2ray', '/usr/bin/v2ray', '/opt/homebrew/bin/v2ray'];
for (const p of systemPaths) {
if (existsSync(p)) return p;
}
// 6. System PATH
try {
const cmd = process.platform === 'win32' ? 'where v2ray.exe' : 'which v2ray';
const result = execSync(cmd, { encoding: 'utf8', stdio: 'pipe' }).trim();
if (result) return result.split('\n')[0];
} catch { /* not in PATH */ }
return null;
}
// ─── WireGuard Detection (comprehensive) ─────────────────────────────────────
function findWireGuard() {
if (process.platform === 'win32') {
const paths = [
'C:\\Program Files\\WireGuard\\wireguard.exe',
'C:\\Program Files (x86)\\WireGuard\\wireguard.exe',
];
for (const p of paths) {
if (existsSync(p)) return p;
}
} else {
const paths = ['/usr/bin/wg', '/usr/local/bin/wg', '/opt/homebrew/bin/wg'];
for (const p of paths) {
if (existsSync(p)) return p;
}
}
try {
const cmd = process.platform === 'win32' ? 'where wireguard.exe' : 'which wg';
const result = execSync(cmd, { encoding: 'utf8', stdio: 'pipe' }).trim();
if (result) return result.split('\n')[0];
} catch { /* not in PATH */ }
return null;
}
// ─── getEnvironment() ────────────────────────────────────────────────────────
/**
* Detect the current environment without changing anything.
* Uses comprehensive detection — checks env vars, SDK paths, system paths, PATH.
*
* @returns {{
* os: string,
* arch: string,
* platform: string,
* nodeVersion: string,
* admin: boolean,
* v2ray: { available: boolean, version: string|null, path: string|null },
* wireguard: { available: boolean, path: string|null, requiresAdmin: true },
* capabilities: string[],
* recommended: string[],
* }}
*/
export function getEnvironment() {
const os = process.platform === 'win32' ? 'windows'
: process.platform === 'darwin' ? 'macos'
: process.platform === 'linux' ? 'linux'
: process.platform;
// V2Ray: our own comprehensive detection (not just SDK's verifyDependencies)
const v2rayPath = findV2Ray();
let v2rayVersion = null;
if (v2rayPath) {
try {
const out = execSync(`"${v2rayPath}" version`, { encoding: 'utf8', stdio: 'pipe', timeout: 5000 });
const match = out.match(/V2Ray\s+(\d+\.\d+\.\d+)/);
v2rayVersion = match ? match[1] : null;
} catch { /* version check optional */ }
}
const v2ray = {
available: !!v2rayPath,
version: v2rayVersion,
path: v2rayPath,
};
// WireGuard: our own comprehensive detection
const wgPath = findWireGuard();
const wireguard = {
available: !!wgPath,
path: wgPath,
requiresAdmin: true,
};
// What this environment can do
const capabilities = [];
if (v2ray.available) capabilities.push('v2ray');
if (wireguard.available && IS_ADMIN) capabilities.push('wireguard');
if (wireguard.available && !IS_ADMIN) capabilities.push('wireguard-needs-admin');
// What we recommend installing
const recommended = [];
if (!v2ray.available) recommended.push('v2ray — run: node setup.js');
if (!wireguard.available && os === 'windows') {
recommended.push('wireguard — run setup.js as admin for auto-install');
}
if (!wireguard.available && os === 'macos') {
recommended.push('wireguard — run: brew install wireguard-tools');
}
if (!wireguard.available && os === 'linux') {
recommended.push('wireguard — run: sudo apt install wireguard-tools');
}
if (wireguard.available && !IS_ADMIN) {
recommended.push('run as admin to use WireGuard nodes (faster, more reliable)');
}
return {
os,
arch: process.arch,
platform: `${os}-${process.arch}`,
nodeVersion: process.versions.node,
admin: IS_ADMIN,
v2ray,
wireguard,
capabilities,
recommended,
};
}
// ─── setup() ─────────────────────────────────────────────────────────────────
/**
* Full environment setup: check deps, install missing ones, report status.
* Runs preflight checks that verify everything needed for a VPN connection.
*
* Returns a FLAT structure — agents access .os, .v2ray, .wireguard directly.
* No nested .environment wrapper to misread.
*
* @returns {Promise<{
* ready: boolean,
* os: string,
* arch: string,
* platform: string,
* nodeVersion: string,
* admin: boolean,
* v2ray: boolean,
* v2rayVersion: string|null,
* v2rayPath: string|null,
* wireguard: boolean,
* wireguardPath: string|null,
* capabilities: string[],
* recommended: string[],
* preflight: object|null,
* issues: string[],
* }>}
*/
export async function setup() {
const env = getEnvironment();
const issues = [];
// Run preflight checks (network, chain reachability, etc.)
let preflightResult = null;
try {
preflightResult = await preflight();
} catch (err) {
issues.push(`Preflight failed: ${err.message}`);
}
// Check critical requirements
const nodeMajor = parseInt(process.versions.node.split('.')[0], 10);
if (nodeMajor < 20) {
issues.push(`Node.js ${process.versions.node} too old — need >= 20`);
}
if (!env.v2ray.available && !env.wireguard.available) {
issues.push('No tunnel protocol available — install V2Ray or WireGuard');
}
if (env.v2ray.available && env.v2ray.version && env.v2ray.version !== V2RAY_VERSION) {
issues.push(`V2Ray version ${env.v2ray.version} — need exactly ${V2RAY_VERSION} (5.44.1+ has bugs)`);
}
const ready = issues.length === 0 && env.capabilities.length > 0;
// Flat return — agent accesses .os, .v2ray, .admin directly
return {
ready,
os: env.os,
arch: env.arch,
platform: env.platform,
nodeVersion: env.nodeVersion,
admin: env.admin,
v2ray: env.v2ray.available,
v2rayVersion: env.v2ray.version,
v2rayPath: env.v2ray.path,
wireguard: env.wireguard.available,
wireguardPath: env.wireguard.path,
capabilities: env.capabilities,
recommended: env.recommended,
preflight: preflightResult,
issues,
// Backward compat — keep nested .environment for existing consumers
environment: env,
};
}