xtunnel 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
package/index.js ADDED
@@ -0,0 +1,76 @@
1
+ const program = require('commander')
2
+ const axios = require('axios')
3
+ const config = require('config')
4
+ const path = require('path')
5
+ const fs = require('fs')
6
+ const child_process = require('child_process')
7
+
8
+ let rayProcess = null
9
+
10
+ process.on('SIGINT', () => stop())
11
+
12
+ program
13
+ .version('1.0.0')
14
+ .description('Tunnel CLI')
15
+ program
16
+ .command('* <url>')
17
+ .action(url => url && init(url))
18
+ program.parse(process.argv)
19
+
20
+
21
+ async function init(url) {
22
+ try {
23
+ let res = await axios.get(url)
24
+ if (res.data) {
25
+ subItems = Buffer.from(res.data, 'base64').toString().split("vmess://").map(o => Buffer.from(o, 'base64').toString()).filter(o => o)
26
+ for (let subItem of subItems) {
27
+ subItem = JSON.parse(subItem)
28
+ if (subItem.verify_cert) {
29
+ switchServer(subItem)
30
+ break
31
+ }
32
+ }
33
+ }
34
+ } catch (error) {
35
+ console.error(error)
36
+ }
37
+ }
38
+
39
+ function switchServer(server) {
40
+ // 切换服务器
41
+ config.tpl.inbounds[1].settings.port = config.setting.localHttpPort
42
+ config.tpl.routing = config.routingGlobal
43
+ config.tpl.outbounds[0].settings.vnext[0].address = server.add
44
+ config.tpl.outbounds[0].settings.vnext[0].port = server.port
45
+ config.tpl.outbounds[0].settings.vnext[0].users[0].id = server.id
46
+ fs.writeFile(path.join(__dirname, './core/config.json'), JSON.stringify(config.tpl), err => !err && start())
47
+ }
48
+
49
+ function start() {
50
+ // 关闭上个进程
51
+ rayProcess && rayProcess.kill('SIGINT')
52
+ // 设置系统代理
53
+ setSystemProxy(config.setting.localHttpPort)
54
+ // 根据操作系统平台-启动新进程
55
+ rayProcess = child_process.exec(path.join(__dirname, './core/xray-linux-x64'), (error, stdout, stderr) => error && stop() && console.error('启动失败,请检查权限/端口占用'))
56
+ rayProcess.stdout.on('data', console.log)
57
+ rayProcess.stderr.on('data', console.error)
58
+ }
59
+
60
+ function stop() {
61
+ // 关闭上个进程
62
+ rayProcess && rayProcess.kill('SIGINT')
63
+ // 取消系统代理
64
+ setSystemProxy()
65
+ return true
66
+ }
67
+
68
+ function setSystemProxy(port) {
69
+ if (port) {
70
+ let command1 = `git config --global http.sslBackend "openssl" && git config --global http.proxy "http://127.0.0.1:${port}"`
71
+ child_process.exec(command1)
72
+ } else {
73
+ let command1 = `git config --global --unset http.sslBackend && git config --global --unset http.proxy`
74
+ child_process.exec(command1)
75
+ }
76
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "xtunnel",
3
+ "version": "1.0.0",
4
+ "description": "xtunnel",
5
+ "main": "index.js",
6
+ "scripts": {},
7
+ "bin": {
8
+ "tunnel": "./index.js"
9
+ },
10
+ "author": "cheney",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "axios": "^0.24.0",
14
+ "commander": "^8.3.0",
15
+ "config": "^3.3.6"
16
+ }
17
+ }