-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdaemon.js
151 lines (138 loc) · 5.28 KB
/
daemon.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
142
143
144
145
146
147
148
149
150
151
var gephDaemon = null
function getBinaryPath() {
const os = require('os')
if (os.platform() == 'linux') {
if (os.arch() == 'x64') {
return __dirname + '/assets/binaries/linux-x64/'
} else {
return __dirname + '/assets/binaries/linux-ia32/'
}
} else if (os.platform() == 'win32') {
return __dirname + '/assets/binaries/win-ia32/'
} else if (os.platform() == 'darwin') {
return __dirname + '/assets/binaries/mac-x64/'
}
throw 'UNKNOWN OS'
}
function binExt() {
const os = require('os')
if (os.platform() == 'win32') {
return ".exe"
} else {
return ""
}
}
function startDaemon() {
const os = require('os')
const spawn = require('child_process').spawn
let uname = localStorage.getItem("gephpref.uname")
let pword = localStorage.getItem("gephpref.pword")
console.log(uname)
console.log(pword)
let avoidChina = localStorage.getItem("gephpref.avoid-china") == "true"
gephDaemon = spawn(getBinaryPath() + 'geph' + binExt(),
['client', '--geodb', __dirname + '/assets/ip-mappings.csv',
'--uname', uname, '--pwd', pword,
'--whitelist', avoidChina ? "CN" : ""])
// PAC if we are setting the browser
if (localStorage.getItem("gephpref.autoconfig-browser") == "true") {
// Don't use the pac executable on Windoze!
if (require('os').platform() == "win32") {
console.log("Win32, using alternative proxy enable")
spawn(getBinaryPath() + 'ProxyToggle.exe', ["127.0.0.1:8780"])
} else {
spawn(getBinaryPath() + 'pac' + binExt(),
["on", "http://127.0.0.1:8790/proxy.pac"])
console.log("** PAC path is " + getBinaryPath() + "pac" + binExt(), ", trying to run **")
}
}
gephDaemon.stderr.on('data', (data) => console.log(`stderr: ${data}`))
}
function stopDaemon() {
if (gephDaemon != null) {
const spawn = require('child_process').spawn
gephDaemon.kill()
if (require('os').platform() == "win32") {
console.log("Win32, using alternative proxy disable")
spawn(getBinaryPath() + 'ProxyToggle.exe', [])
} else {
spawn(getBinaryPath() + 'pac' + binExt(), ["off"])
}
gephDaemon = null
}
}
// getCaptcha starts a proxbinder, gets a captcha, and calls cback with the result
function getCaptcha(cback) {
const spawn = require('child_process').spawn
let prox = spawn(getBinaryPath() + 'geph' + binExt(), ['proxbinder'])
prox.stdout.on('data', (data) => {
let burl = ("http://" + data).trim()
$.post(burl + "/fresh-captcha").done((data) => {
cback(data)
}).always(() => prox.kill())
})
}
// deriveKeys does the same thing for deriving keys
function deriveKeys(uname, pword, cback) {
const spawn = require('child_process').spawn
let prox = spawn(getBinaryPath() + 'geph' + binExt(), ['proxbinder'])
prox.stdout.on('data', (data) => {
let burl = ("http://" + data).trim()
$.getJSON(burl + "/derive-keys?uname=" + uname + "&pwd=" + pword).done((data) => {
cback(data)
}).always(() => prox.kill())
})
}
// registerAccount does the same thing for registering accounts
function registerAccount(uname, pubkey, captid, captsoln, cback, eback) {
const spawn = require('child_process').spawn
let prox = spawn(getBinaryPath() + 'geph' + binExt(), ['proxbinder'])
prox.stdout.on('data', (data) => {
let burl = ("http://" + data).trim()
let tosend = JSON.stringify({
"Username": uname,
"PubKey": pubkey,
"CaptchaID": captid,
"CaptchaSoln": captsoln
})
$.post(burl + "/register-account", tosend, "text")
.always(() => prox.kill())
.done(cback).fail(eback)
})
}
// set essential prefs if they don't exist
if (!localStorage.getItem("gephpref.autoconfig-browser")) {
localStorage.setItem("gephpref.autoconfig-browser", "true")
}
if (!localStorage.getItem("gephpref.avoid-china")) {
localStorage.setItem("gephpref.avoid-china", "true")
}
function arePermsCorrect() {
const fs = require('fs')
let stats = fs.statSync(getBinaryPath() + "pac")
console.log("UID of pac is", stats.uid, ", root is zero")
return (stats.uid == 0)
}
function forceElevatePerms() {
const spawn = require('child_process').spawn
let lol = spawn(getBinaryPath() + "cocoasudo",
["--prompt=" + l10n["macPacMsg"], getBinaryPath() + "pac", "setuid"])
console.log("** PAC path is " + getBinaryPath() + "pac" + ", trying to elevate **")
lol.stderr.on('data', (data) => console.log(`stderr: ${data}`))
}
function elevatePerms() {
const fs = require("fs")
let stats = fs.statSync(getBinaryPath() + "pac")
if (!arePermsCorrect()) {
console.log("We have to elevate perms for pac. But to prevent running into that infamous problem, we clear setuid bits first")
const spawnSync = require('child_process').spawnSync
spawnSync("/bin/chmod", ["ug-s", getBinaryPath() + "pac"])
console.log("Setuid cleared on pac, now we run cocoasudo!")
forceElevatePerms()
}
}
// on macOS, elevate pac permissions
const os = require('os')
if (os.platform() == 'darwin') {
elevatePerms()
}