(port?: number)
| 273 | |
| 274 | if (targetPort < 1 || targetPort > 65535 || isNaN(targetPort)) { |
| 275 | return `❌ 无效端口: ${targetPort}`; |
| 276 | } |
| 277 | |
| 278 | // 检查并安装二进制文件 |
| 279 | const binCheck = await SystemExecutor.run(`test -f ${WIREPROXY_BINARY} && echo exists || echo missing`); |
| 280 | if (binCheck.output.trim() !== "exists") { |
| 281 | await this.installBinary(); |
| 282 | } |
| 283 | |
| 284 | // 获取账户信息 |
| 285 | const account = await AccountManager.getOrCreate(); |
| 286 | |
| 287 | // 生成配置文件 |
| 288 | const config = this.generateConfig(account, targetPort); |
| 289 | await execAsync(`sudo bash -lc 'cat > ${WIREPROXY_CONFIG_FILE} <<"EOF"\n${config}\nEOF'`); |
| 290 | |
| 291 | // 创建系统服务 |
| 292 | const service = this.generateService(); |
| 293 | await execAsync(`sudo bash -lc 'cat > ${WIREPROXY_SERVICE_FILE} <<"EOF"\n${service}\nEOF'`); |
| 294 | |
| 295 | // 启动服务 |
| 296 | const startResult = await SystemExecutor.runSudo("systemctl daemon-reload && systemctl enable wireproxy && systemctl restart wireproxy"); |
| 297 | if (!startResult.success) { |
| 298 | throw new Error(`服务启动失败: ${startResult.error}`); |
| 299 | } |
| 300 | |
| 301 | return `✅ WireProxy 已启动,SOCKS5 代理: 127.0.0.1:${targetPort}`; |
| 302 | } catch (error: any) { |
| 303 | return `❌ 启动失败: ${htmlEscape(error.message)}`; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | private static generateConfig(account: { privateKey: string; address6: string }, port: number): string { |
| 308 | const address4 = "172.16.0.2/32"; |
| 309 | const address6 = `${account.address6}/128`; |
| 310 | const dns = "1.1.1.1,8.8.8.8,8.8.4.4,2606:4700:4700::1111,2001:4860:4860::8888,2001:4860:4860::8844"; |
| 311 | |
| 312 | return `[Interface] |
| 313 | PrivateKey = ${account.privateKey} |
no test coverage detected