forked from XTLS/Xray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
48 lines (45 loc) · 1.31 KB
/
json.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
package json
import (
"io"
"github.com/gurmeherchawla/xray-core/common"
"github.com/gurmeherchawla/xray-core/common/cmdarg"
"github.com/gurmeherchawla/xray-core/core"
"github.com/gurmeherchawla/xray-core/infra/conf"
"github.com/gurmeherchawla/xray-core/infra/conf/serial"
"github.com/gurmeherchawla/xray-core/vpn/confloader"
)
func init() {
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
Name: "JSON",
Extension: []string{"json"},
Loader: func(input interface{}) (*core.Config, error) {
switch v := input.(type) {
case cmdarg.Arg:
cf := &conf.Config{}
for i, arg := range v {
newError("Reading config: ", arg).AtInfo().WriteToLog()
r, err := confloader.LoadConfig(arg)
if err != nil {
return nil, newError("failed to read config: ", arg).Base(err)
}
c, err := serial.DecodeJSONConfig(r)
if err != nil {
return nil, newError("failed to decode config: ", arg).Base(err)
}
if i == 0 {
// This ensure even if the muti-json parser do not support a setting,
// It is still respected automatically for the first configure file
*cf = *c
continue
}
cf.Override(c, arg)
}
return cf.Build()
case io.Reader:
return serial.LoadJSONConfig(v)
default:
return nil, newError("unknow type")
}
},
}))
}