-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtongshe.go
284 lines (253 loc) · 6.73 KB
/
tongshe.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/getlantern/systray"
"github.com/gorilla/mux"
"github.com/skratchdot/open-golang/open"
)
var Token string
func getConfigFunc(name string) func(string, string) {
if name == "is_global" {
return func(name, value string) {
refreshGlobalToggle(value == "on")
SetPac()
}
}
if name == "child_lock" {
return func(name, value string) {
SetPac()
}
}
return func(s, v string) {}
}
type JsonResponse struct {
Succeed bool `json:"ok"`
Data *json.RawMessage `json:"data"`
Message string `json:"message"`
}
func renderJson(w http.ResponseWriter, res *JsonResponse) {
buf, err := json.Marshal(res)
if err != nil {
fmt.Fprintf(w, "json marshal failed with error", err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
}
func set(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
value := r.FormValue("value")
if err := effectSetting(name, value); err != nil {
res := &JsonResponse{Succeed: false, Data: nil, Message: "设置文件有问题"}
renderJson(w, res)
return
}
settings(w, r)
}
func effectSetting(name, value string) error {
log.Printf("Get command to set %s to %s", name, value)
config, err := LoadConfig()
if err != nil {
return err
}
config.Set(name, value)
f := getConfigFunc(name)
f(name, value)
return nil
}
func settings(w http.ResponseWriter, r *http.Request) {
config, _ := LoadConfig()
bt, _ := json.Marshal(config.Config)
data := (*json.RawMessage)(&bt)
res := &JsonResponse{
Succeed: true, Data: data, Message: ""}
renderJson(w, res)
}
func getPac(w http.ResponseWriter, r *http.Request) {
bt := GetRes("pac.tpl")
var proxy string
if runtime.GOOS == "windows" {
proxy = fmt.Sprintf("PROXY %s; DIRECT;", GetHttpProxy())
} else {
proxy = fmt.Sprintf("SOCKS5 %s; SOCKS %s; DIRECT;", GetSocksProxy(), GetSocksProxy())
}
config, _ := LoadConfig()
dds := config.Get("diy_domains")
dms := []string{}
if len(dds) > 0 {
dms = strings.Split(dds, ",")
}
dmsJson, _ := json.Marshal(dms)
s := strings.Replace(string(bt), "__PROXY__", proxy, -1)
s = strings.Replace(s, "__DOMAINS__", string(dmsJson), -1)
isGs := "false"
global := config.Get("is_global") == "on"
if global {
isGs = "true"
}
s = strings.Replace(s, "__IS_GLOBAL__", isGs, -1)
fmt.Fprintf(w, s)
}
func tokenRequired(f func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("token")
if err != nil || cookie.Value != Token {
log.Printf("token is not correct, get:%s ", cookie)
res := &JsonResponse{Succeed: false, Data: nil, Message: "Unknown source"}
renderJson(w, res)
} else {
f(w, r)
}
}
}
func static(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
if p == "/index" || p == "/" || p == "/index.html" {
http.SetCookie(w, &http.Cookie{Name: "token", Value: Token, Path: "/"})
p = "/index.html"
}
f := "ui" + p
fmt.Println(f)
arr := strings.Split(f, ".")
switch arr[len(arr)-1] {
case "css":
w.Header().Set("Content-Type", "text/css; charset=utf-8")
case "html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
case "js":
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
}
w.Write(GetRes(f))
}
func RandomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func shadowsocks(w http.ResponseWriter, r *http.Request) {
config, err := LoadConfig()
if err != nil {
}
switch r.Method {
case "POST":
ss := r.FormValue("ss")
log.Printf("Post ss with value: %s", ss)
err = config.AddTunnel(ss)
case "DELETE":
ss := r.URL.Query().Get("ss")
log.Printf("Delete ss: %s", ss)
err = config.DeleteTunnel(ss)
case "PUT":
ss := r.FormValue("ss")
old := r.URL.Query().Get("ss")
err = config.UpdateTunnel(old, ss)
}
log.Printf("ss tunnels count is %d now", len(config.GetSSTunnels()))
SetTunnels(config.GetSSTunnels())
if len(config.GetSSTunnels()) == 0 {
UnsetPac()
}
if r.Method == "POST" && len(config.GetSSTunnels()) == 1 {
SetPac()
}
bt, _ := json.Marshal(config.SSTunnels)
data := (*json.RawMessage)(&bt)
if err == nil {
res := &JsonResponse{Succeed: true, Data: data, Message: ""}
renderJson(w, res)
} else {
res := &JsonResponse{Succeed: false, Data: data, Message: err.Error()}
renderJson(w, res)
}
}
func StartWeb() {
Token = RandomString(32)
rtr := mux.NewRouter()
rtr.HandleFunc("/pac", getPac)
rtr.HandleFunc("/set", tokenRequired(set))
rtr.HandleFunc("/settings", tokenRequired(settings))
rtr.HandleFunc("/shadowsocks", tokenRequired(shadowsocks))
rtr.PathPrefix("/").HandlerFunc(static)
http.Handle("/", rtr)
srv := &http.Server{
Handler: rtr,
Addr: GetManagementAddr(),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("start web at : %s", GetManagementAddr())
log.Fatal(srv.ListenAndServe())
}
func onTrayReady() {
// iconBytes should be the []byte content of .ico for windows and .ico/.jpg/.png
// for other platforms.
setrlimit()
systray.SetIcon(GetRes("icon22x22.ico"))
systray.SetTitle("")
systray.SetTooltip("铜蛇")
go StartSS()
go StartHttpProxy()
config, _ := LoadConfig()
SetTunnels(config.GetSSTunnels())
SetPac()
go traceTray()
StartWeb()
}
var mToggleGlobal *systray.MenuItem
func refreshGlobalToggle(global bool) {
if global {
mToggleGlobal.SetTitle("关闭全局模式")
} else {
mToggleGlobal.SetTitle("开启全局模式")
}
}
func traceTray() {
mConfig := systray.AddMenuItem("账号与设置", "账号与设置")
mToggleGlobal = systray.AddMenuItem("开启全局模式", "全局模式开关")
mQuit := systray.AddMenuItem("退出", "退出铜蛇")
murl := fmt.Sprintf("http://%s", GetManagementAddr())
open.Run(murl)
config, _ := LoadConfig()
var global bool = config.Get("is_global") == "on"
refreshGlobalToggle(global)
for {
select {
case <-mConfig.ClickedCh:
open.Run(murl)
case <-mQuit.ClickedCh:
log.Println("clear pac settings...")
UnsetPac()
log.Println("sync rest traffic ...")
TrafficCounter.Sync()
log.Println("shut tray...")
systray.Quit()
log.Println("Quit...")
os.Exit(0)
return
case <-mToggleGlobal.ClickedCh:
config, _ := LoadConfig()
global := !(config.Get("is_global") == "on")
if global {
effectSetting("is_global", "on")
} else {
effectSetting("is_global", "off")
}
refreshGlobalToggle(global)
log.Printf("global mode is %v ", global)
}
}
}
func main() {
systray.Run(onTrayReady)
}