/
config.go
265 lines (221 loc) · 5.29 KB
/
config.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
package query
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/popstk/subserver/helper"
"github.com/tidwall/gjson"
)
// Config -
type Config struct {
Address string `json:"addr"`
Valid map[string][]Source `json:"valid"`
}
// Fmt -
type Fmt struct {
Vmess string `json:"vmess"`
SS string `json:"ss"`
}
// LoadConfig load config from config file
func LoadConfig() error {
data, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
configMutex.Lock()
defer configMutex.Unlock()
if err = json.Unmarshal(data, &config); err != nil {
return err
}
log.Println("config updated")
return nil
}
// ListenConfig watch config file
func ListenConfig(ctx context.Context) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
if err = watcher.Add(configFile); err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case ev := <-watcher.Events:
if ev.Op == fsnotify.Write {
_ = LoadConfig()
}
}
}
}
// Source subserver source
type Source struct {
Type string `json:"type"`
File string `json:"file"`
Host string `json:"host"`
Addr string `json:"addr"`
Fmt Fmt `json:"fmt"`
Rewrite map[string]string `json:"rewrite"`
}
const (
defaultVmessName = "{protocol}-{network}"
defaultSSName = "{protocol}"
)
// URLToEndpoint parse uri array to endpoint array
func URLToEndpoint(uris ...string) []helper.Endpoint {
var nodes []helper.Endpoint
for _, u := range uris {
u = strings.TrimSpace(u)
if u == "" {
continue
}
node, err := helper.ParseEndpoint(u)
if err != nil {
log.Println(err, " => ", u)
continue
}
nodes = append(nodes, node)
}
return nodes
}
// V2rayNodeHandler parse v2ray source
func V2rayNodeHandler(s *Source) ([]helper.Endpoint, error) {
data, err := ioutil.ReadFile(s.File)
if err != nil {
return nil, err
}
if s.Fmt.Vmess == "" {
s.Fmt.Vmess = defaultVmessName
}
if s.Fmt.SS == "" {
s.Fmt.SS = defaultSSName
}
value := gjson.Get(string(data), "inbounds")
if !value.IsArray() {
return nil, errors.New("unknown format")
}
ret := make([]helper.Endpoint, 0)
for _, inbound := range value.Array() {
host := s.Host
port := inbound.Get("port").String()
listen := inbound.Get("listen").String()
add, exist := s.Rewrite[port]
if exist {
host, port, err = net.SplitHostPort(add)
if err != nil {
return nil, err
}
} else if listen == "127.0.0.1" || listen == "localhost" {
return nil, errors.New(fmt.Sprint("loopback inbound without rewrite: ", port))
}
protocol := inbound.Get("protocol").String()
if protocol == "vmess" {
helper.VmessParser.TagFmt = s.Fmt.Vmess
helper.VmessParser.DefaultField["add"] = host
helper.VmessParser.DefaultField["port"] = port
u, err := helper.VmessParser.Parse(inbound)
if err != nil {
return nil, err
}
ret = append(ret, u)
} else if protocol == "shadowsocks" {
helper.SSParser.TagFmt = s.Fmt.SS
helper.SSParser.DefaultField["host"] = host
helper.SSParser.DefaultField["port"] = port
u, err := helper.SSParser.Parse(inbound)
if err != nil {
return nil, err
}
ret = append(ret, u)
}
}
return ret, nil
}
// RawNodeHandler parse raw source
func RawNodeHandler(s *Source) ([]helper.Endpoint, error) {
uris, err := helper.ReadLines(s.File)
if err != nil {
return nil, err
}
return URLToEndpoint(uris...), nil
}
// URLNodeHandler parse url source
func URLNodeHandler(s *Source) ([]helper.Endpoint, error) {
if s.Addr == "" {
return nil, errors.New("addr: url is empty")
}
rsp, err := http.Get(s.Addr)
if err != nil {
return nil, errors.Wrap(err, "UrlNodeHandler")
}
data, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, errors.Wrap(err, "UrlNodeHandler")
}
_ = rsp.Body.Close()
nodes, err := helper.Base64Decode(string(data))
if err != nil {
return nil, errors.Wrap(err, "UrlNodeHandler")
}
urls := strings.Split(nodes, "\n")
return URLToEndpoint(urls...), nil
}
func ProxyNodeHandler(s *Source) ([]helper.Endpoint, error) {
val, err := helper.ParseProxy(s.Addr)
if err != nil {
return nil, errors.Wrap(err, "helper.ParseProxy")
}
return []helper.Endpoint{val}, nil
}
var handlerMap = map[string]func(s *Source) ([]helper.Endpoint, error){
"raw": RawNodeHandler,
"v2ray": V2rayNodeHandler,
"url": URLNodeHandler,
"proxy": ProxyNodeHandler,
}
// ParseConfig parse config to endpoint by uuid
func ParseConfig(uuid string) ([]helper.Endpoint, error) {
nodes := make([]helper.Endpoint, 0)
m := make(map[string]bool)
q := []string{uuid}
configMutex.Lock()
defer configMutex.Unlock()
for len(q) > 0 {
uuid := q[0]
q = q[1:]
m[uuid] = true
array, exist := config.Valid[uuid]
if !exist {
return nil, errors.New("Invalid uuid: " + uuid)
}
for _, s := range array {
if s.Type == "sub" {
_, exist := m[s.Addr]
if exist {
q = append(q, s.Addr)
}
continue
}
handler, exist := handlerMap[s.Type]
if !exist {
log.Println("invalid type: ", s.Type)
}
eps, err := handler(&s)
if err != nil {
log.Println(s.Type, " => ", err)
continue
}
nodes = append(nodes, eps...)
}
}
return nodes, nil
}