-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
141 lines (111 loc) · 3.92 KB
/
main.js
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
const { app, BrowserWindow } = require('electron')
const path = require('path');
const electronIpcMain = require('electron').ipcMain;
const Store = require('electron-store');
const fs = require('fs');
const { dialog } = require('electron')
const store = new Store();
const { exec } = require('child_process');
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');
const createWindow = () => {
const win = new BrowserWindow({
width: 500,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true
}
})
win.setMenuBarVisibility(false)
win.loadFile('index.html')
return win
}
app.whenReady().then(() => {
let win = createWindow()
app.on('activate', () => {
if (win.getAllWindows().length === 0) createWindow()
})
win.on('close', async e => {
exec('pgrep --count tun2socks; echo $?', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
if (process.platform !== 'darwin') win.destroy()
return;
}
console.error(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
if(stdout.startsWith("0")){
if (process.platform !== 'darwin') win.destroy()
return
}
dialog.showErrorBox('Error', 'Stop Before Quit!')
})
e.preventDefault()
})
})
app.on('window-all-closed', (e) => {
exec('pgrep --count tun2socks; echo $?', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
if (process.platform !== 'darwin') app.quit()
return;
}
console.error(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
if(stdout.startsWith("0")){
if (process.platform !== 'darwin') app.quit()
return
}
dialog.showErrorBox('Error', 'Stop Before Quit!')
})
e.preventDefault();
})
var sudo = require('sudo-prompt');
electronIpcMain.on('runScript', () => {
//save config
let savePath = path.join(process.env.HOME, ".V2BOX")
exec('mkdir ' + savePath, (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
})
let configSavePath = path.join(process.env.HOME, ".V2BOX","config.json")
let configJson = JSON.parse(store.get('config'))
let customInbound = JSON.parse('[{"sniffing":{"enabled":false},"listen":"127.0.0.1","protocol":"socks","settings":{"udp":true,"auth":"noauth","userLevel":8},"tag":"socks","port":10808}]')
configJson["inbounds"] = customInbound
configJson = JSON.stringify(configJson)
try { fs.writeFileSync(configSavePath, configJson, 'utf-8'); }
catch(e) {
dialog.showErrorBox('Error', 'Failed to save the file ! ' + configSavePath + e)
e.preventDefault();
}
var options = {
name: 'V2Box',
};
let shellPath = path.join(__dirname, 'Tunnel/tunnel.sh')
sudo.exec('bash ' + shellPath + " " + store.get('serverAddress') + " " + path.join(__dirname, 'Tunnel') + " " + configSavePath, options,
function(error, stdout, stderr) {
if (error) throw error;
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
}
);
})
electronIpcMain.on('stopScript', stopScript)
function stopScript(){
// MacOS & Linux
var options = {
name: 'V2Box',
};
let shellPath = path.join(__dirname, 'Tunnel/stop.sh')
sudo.exec('bash ' + shellPath, options,
function(error, stdout, stderr) {
if (error) throw error;
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
}
);
}