/
ds_server.go
172 lines (163 loc) · 4.62 KB
/
ds_server.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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/darksocks/darksocks/ds"
"golang.org/x/net/websocket"
)
//ServerConf is pojo for server configure
type ServerConf struct {
HTTPListenAddr string `json:"http_listen_addr"`
HTTPSListenAddr string `json:"https_listen_addr"`
HTTPSCert string `json:"https_cert"`
HTTPSKey string `json:"https_key"`
Manager map[string]string `json:"manager"`
UserFile string `json:"user_file"`
LogLevel int `json:"log"`
}
var serverConf string
var serverConfDir string
var httpServer = map[string]*http.Server{}
var httpServerLck = sync.RWMutex{}
func startServer(c string) (err error) {
conf := &ServerConf{}
err = ds.ReadJSON(c, &conf)
if err != nil {
ds.ErrorLog("Server read configure from %v fail with %v", c, err)
exitf(1)
return
}
serverConf = c
serverConfDir = filepath.Dir(serverConf)
ds.SetLogLevel(conf.LogLevel)
userFile := conf.UserFile
if len(userFile) > 0 && !filepath.IsAbs(userFile) {
userFile, _ = filepath.Abs(filepath.Join(serverConfDir, userFile))
}
auth := ds.NewJSONFileAuth(conf.Manager, userFile)
server := ds.NewServer(ds.DefaultBufferSize, ds.NetDialer("tcp"))
mux := http.NewServeMux()
mux.Handle("/ds", websocket.Handler(func(ws *websocket.Conn) {
ok, err := auth.BasicAuth(ws.Request())
if ok && err == nil {
server.ProcConn(ds.NewBaseConn(ws, server.BufferSize))
} else {
ds.WarnLog("Server receive auth fail connection from %v", ws.RemoteAddr())
}
ws.Close()
}))
mux.HandleFunc("/manager/", auth.ListUser)
mux.HandleFunc("/manager/addUser", auth.AddUser)
mux.HandleFunc("/manager/removeUser", auth.RemoveUser)
wait := sync.WaitGroup{}
if len(conf.HTTPListenAddr) > 0 {
ds.InfoLog("Server start http server on %v", conf.HTTPListenAddr)
var addrs []string
addrs, err = parseListenAddr(conf.HTTPListenAddr)
if err != nil {
ds.ErrorLog("Server start http server on %v fail with %v", conf.HTTPListenAddr, err)
exitf(1)
return
}
wait.Add(len(addrs))
for _, a := range addrs {
go func(addr string) {
s := &http.Server{Addr: addr, Handler: mux}
httpServerLck.Lock()
httpServer[fmt.Sprintf("%p", s)] = s
httpServerLck.Unlock()
rerr := s.ListenAndServe()
if rerr != nil {
ds.ErrorLog("Server http server on %v is stopped fail with %v", addr, rerr)
}
httpServerLck.Lock()
delete(httpServer, fmt.Sprintf("%p", s))
httpServerLck.Unlock()
wait.Done()
}(a)
}
}
if len(conf.HTTPSListenAddr) > 0 {
ds.InfoLog("Server start https server on %v", conf.HTTPSListenAddr)
var addrs []string
addrs, err = parseListenAddr(conf.HTTPSListenAddr)
if err != nil {
ds.ErrorLog("Server start https server on %v fail with %v", conf.HTTPSListenAddr, err)
exitf(1)
return
}
certFile, certKey := conf.HTTPSCert, conf.HTTPSKey
if !filepath.IsAbs(certFile) {
certFile, _ = filepath.Abs(filepath.Join(serverConfDir, certFile))
}
if !filepath.IsAbs(certKey) {
certKey, _ = filepath.Abs(filepath.Join(serverConfDir, certKey))
}
wait.Add(len(addrs))
for _, a := range addrs {
go func(addr string) {
s := &http.Server{Addr: addr, Handler: mux}
httpServerLck.Lock()
httpServer[fmt.Sprintf("%p", s)] = s
httpServerLck.Unlock()
rerr := s.ListenAndServeTLS(certFile, certKey)
if rerr != nil {
ds.ErrorLog("Server https server on %v is stopped fail with %v", addr, rerr)
}
httpServerLck.Lock()
delete(httpServer, fmt.Sprintf("%p", s))
httpServerLck.Unlock()
wait.Done()
}(a)
}
}
go handlerServerKill()
wait.Wait()
ds.InfoLog("Server all listener is stopped")
return
}
func stopServer() {
ds.InfoLog("Server stopping client listener")
httpServerLck.Lock()
for _, s := range httpServer {
s.Close()
}
httpServerLck.Unlock()
}
func parseListenAddr(addr string) (addrs []string, err error) {
parts := strings.SplitN(addr, ":", 2)
if len(parts) < 2 {
err = fmt.Errorf("invalid uri")
return
}
ports := strings.SplitN(parts[1], "-", 2)
start, err := strconv.ParseInt(ports[0], 10, 32)
if err != nil {
return
}
end := start
if len(ports) > 1 {
end, err = strconv.ParseInt(ports[1], 10, 32)
if err != nil {
return
}
}
for i := start; i <= end; i++ {
addrs = append(addrs, fmt.Sprintf("%v:%v", parts[0], i))
}
return
}
var serverKillSignal chan os.Signal
func handlerServerKill() {
serverKillSignal = make(chan os.Signal, 1000)
signal.Notify(serverKillSignal, os.Kill, os.Interrupt)
v := <-serverKillSignal
ds.WarnLog("Server receive kill signal:%v", v)
stopServer()
}