forked from zengzhengkevin/clashr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysproxy.go
83 lines (74 loc) · 2.03 KB
/
sysproxy.go
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
package route
import (
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/whojave/clashr/hub/executor"
"net/http"
"os/exec"
"strconv"
)
const (
Gsettings = "gsettings"
Set = "set"
SystemProxy = "org.gnome.system.proxy"
SystemProxyHttp = "org.gnome.system.proxy.http"
SystemProxyHttps = "org.gnome.system.proxy.https"
SystemProxySocks = "org.gnome.system.proxy.socks"
ProxyHost = "host"
ProxyPort = "port"
ProxyMode = "mode"
None = "none"
Manual = "manual"
)
func systemProxySettingRouter() http.Handler {
r := chi.NewRouter()
r.Get("/http", setSystemProxyHttp)
r.Get("/socks", setSystemProxySocks)
r.Get("/none", closeSystemProxy)
return r
}
func setSystemProxyHttp(w http.ResponseWriter, r *http.Request) {
config := executor.GetGeneral()
err := exec.Command(Gsettings, Set, SystemProxyHttp, ProxyHost, config.BindAddress).Run()
err = exec.Command(Gsettings, Set, SystemProxyHttp, ProxyPort, strconv.Itoa(config.Port)).Run()
err = exec.Command(Gsettings, Set, SystemProxy, ProxyMode, Manual).Run()
if err != nil {
render.JSON(w, r, render.M{
"success": false,
"err": err.Error(),
})
} else {
render.JSON(w, r, render.M{
"success": true,
})
}
}
func setSystemProxySocks(w http.ResponseWriter, r *http.Request) {
config := executor.GetGeneral()
err := exec.Command(Gsettings, Set, SystemProxySocks, ProxyHost, config.BindAddress).Run()
err = exec.Command(Gsettings, Set, SystemProxySocks, ProxyPort, strconv.Itoa(config.SocksPort)).Run()
err = exec.Command(Gsettings, Set, SystemProxy, ProxyMode, Manual).Run()
if err != nil {
render.JSON(w, r, render.M{
"success": false,
"err": err.Error(),
})
} else {
render.JSON(w, r, render.M{
"success": true,
})
}
}
func closeSystemProxy(w http.ResponseWriter, r *http.Request) {
err := exec.Command(Gsettings, Set, SystemProxy, ProxyMode, None).Run()
if err != nil {
render.JSON(w, r, render.M{
"success": false,
"err": err.Error(),
})
} else {
render.JSON(w, r, render.M{
"success": true,
})
}
}