forked from xiaokangwang/AndroidLibV2ray
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfigure.go
125 lines (109 loc) · 3.26 KB
/
configure.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
package libv2ray
import (
"encoding/json"
"errors"
"log"
simplejson "github.com/bitly/go-simplejson"
)
type libv2rayconf struct {
upscript string
downscript string
additionalEnv []string
esco []libv2rayconfEscortTarget
rend []libv2rayconfRenderCfgTarget
vpnConfig vpnserviceConfig
}
type libv2rayconfEscortTarget struct {
Target string `json:"Target"`
Args []string `json:"Args"`
Forgiveable bool `json:"Forgiveable"`
}
type libv2rayconfRenderCfgTarget struct {
Target string `json:"Target"`
Args []string `json:"Args"`
Source string `json:"Source"`
}
func (v *V2RayPoint) parseConf() error {
jsoncf, err := simplejson.NewFromReader(v.parseCfg())
if err != nil {
v.Callbacks.OnEmitStatus(-2, err.Error())
return err
}
libconf, isexist := jsoncf.CheckGet("#lib2ray")
if !isexist {
log.Print("No Vendor Conf found.")
return nil
}
enabled, err := libconf.GetPath("enabled").Bool()
if err != nil {
v.Callbacks.OnEmitStatus(-2, err.Error())
return err
}
if !enabled {
return nil
}
v.conf = &libv2rayconf{}
v.conf.upscript = jsonStringDefault(libconf.GetPath("listener", "onUp"), "#none")
v.conf.downscript = jsonStringDefault(libconf.GetPath("listener", "onDown"), "#none")
v.conf.additionalEnv, err = libconf.GetPath("env").StringArray()
if err != nil {
v.Callbacks.OnEmitStatus(-2, err.Error())
return err
}
escortconfjson, exist := libconf.CheckGet("escort")
if exist {
/*
var ok bool
v.conf.esco, ok = escortconfjson.Interface().([]libv2rayconfEscortTarget)
if !ok {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config escort")
return errors.New("Failed Type Assert: Config escort")
}*/
esco, ok := escortconfjson.MarshalJSON()
if ok != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config escort")
return errors.New("Failed Type Assert: Config escort")
}
err := json.Unmarshal(esco, &v.conf.esco)
if err != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config escortX")
return errors.New("Failed Type Assert: Config escortX")
}
}
renderconfjson, exist := libconf.CheckGet("render")
if exist {
rend, ok := renderconfjson.MarshalJSON()
if ok != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config render")
return errors.New("Failed Type Assert: Config render")
}
err := json.Unmarshal(rend, &v.conf.rend)
if err != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config renderX")
log.Println(err, "Failed Type Assert: Config renderX")
return errors.New("Failed Type Assert: Config renderX")
}
}
vpnConfigconfjson, exist := libconf.CheckGet("vpnservice")
if exist {
vpnConfig, ok := vpnConfigconfjson.MarshalJSON()
if ok != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config vpnConfig")
return errors.New("Failed Type Assert: Config vpnConfig")
}
err := json.Unmarshal(vpnConfig, &v.conf.vpnConfig)
if err != nil {
v.Callbacks.OnEmitStatus(-2, "Failed Type Assert: Config vpnConfigX")
log.Println(err, "Failed Type Assert: Config vpnConfigX")
return errors.New("Failed Type Assert: Config vpnConfigX")
}
}
return nil
}
func jsonStringDefault(jsons *simplejson.Json, def string) string {
s, err := jsons.String()
if err != nil {
return def
}
return s
}