-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
221 lines (192 loc) · 4.66 KB
/
main.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
package main
import (
"C"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/hub/route"
"github.com/Dreamacro/clash/log"
"github.com/oschwald/geoip2-golang"
"github.com/phayes/freeport"
)
func isAddrValid(addr string) bool {
if addr != "" {
comps := strings.Split(addr, ":")
v := comps[len(comps)-1]
if port, err := strconv.Atoi(v); err == nil {
if port > 0 && port < 65535 {
return checkPortAvailable(port)
}
}
}
return false
}
func checkPortAvailable(port int) bool {
if port < 1 || port > 65534 {
return false
}
addr := ":"
l, err := net.Listen("tcp", addr+strconv.Itoa(port))
if err != nil {
log.Warnln("check port fail 0.0.0.0:%d", port)
return false
}
_ = l.Close()
addr = "127.0.0.1:"
l, err = net.Listen("tcp", addr+strconv.Itoa(port))
if err != nil {
log.Warnln("check port fail 127.0.0.1:%d", port)
return false
}
_ = l.Close()
log.Infoln("check port %d success", port)
return true
}
//export initClashCore
func initClashCore() {
configFile := filepath.Join(constant.Path.HomeDir(), constant.Path.Config())
constant.SetConfig(configFile)
log.Warnln("initClashCore-homeDir:%s", constant.Path.HomeDir())
log.Warnln("initClashCore-configFile:%s", constant.Path.Config())
}
//读配置文件
func readConfig(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("Configuration file %s is empty", path)
}
return data, err
}
//解析默认配置启动
func parseDefaultConfigThenStart(checkPort, allowLan bool) (*config.Config, error) {
buf, err := readConfig(constant.Path.Config())
if err != nil {
return nil, err
}
rawCfg, err := config.UnmarshalRawConfig(buf)
if err != nil {
return nil, err
}
if rawCfg.MixedPort == 0 {
if rawCfg.Port > 0 {
rawCfg.MixedPort = rawCfg.Port
rawCfg.Port = 0
} else if rawCfg.SocksPort > 0 {
rawCfg.MixedPort = rawCfg.SocksPort
rawCfg.SocksPort = 0
} else {
rawCfg.MixedPort = 7890
}
if rawCfg.SocksPort == rawCfg.MixedPort {
rawCfg.SocksPort = 0
}
if rawCfg.Port == rawCfg.MixedPort {
rawCfg.Port = 0
}
}
rawCfg.ExternalUI = ""
rawCfg.Profile.StoreSelected = false
if checkPort {
if !isAddrValid(rawCfg.ExternalController) {
port, err := freeport.GetFreePort()
if err != nil {
return nil, err
}
rawCfg.ExternalController = "127.0.0.1:" + strconv.Itoa(port)
rawCfg.Secret = ""
}
rawCfg.AllowLan = allowLan
if !checkPortAvailable(rawCfg.MixedPort) {
if port, err := freeport.GetFreePort(); err == nil {
rawCfg.MixedPort = port
}
}
}
cfg, err := config.ParseRawConfig(rawCfg)
if err != nil {
return nil, err
}
go route.Start(cfg.General.ExternalController, cfg.General.Secret)
executor.ApplyConfig(cfg, true)
return cfg, nil
}
//export verifyClashConfig
func verifyClashConfig(content *C.char) *C.char {
b := []byte(C.GoString(content))
cfg, err := executor.ParseWithBytes(b)
if err != nil {
return C.CString(err.Error())
}
if len(cfg.Proxies) < 1 {
return C.CString("No proxy found in config")
}
return C.CString("success")
}
//export run
func run(checkConfig, allowLan bool) *C.char {
cfg, err := parseDefaultConfigThenStart(checkConfig, allowLan)
if err != nil {
return C.CString(err.Error())
}
portInfo := map[string]string{
"externalController": cfg.General.ExternalController,
"secret": cfg.General.Secret,
}
jsonString, err := json.Marshal(portInfo)
if err != nil {
return C.CString(err.Error())
}
return C.CString(string(jsonString))
}
//export setUIPath
func setUIPath(path *C.char) {
route.SetUIPath(C.GoString(path))
}
//export clashUpdateConfig
func clashUpdateConfig(path *C.char) *C.char {
cfg, err := executor.ParseWithPath(C.GoString(path))
if err != nil {
return C.CString(err.Error())
}
executor.ApplyConfig(cfg, false)
return C.CString("success")
}
//export clashGetConfigs
func clashGetConfigs() *C.char {
general := executor.GetGeneral()
jsonString, err := json.Marshal(general)
if err != nil {
return C.CString(err.Error())
}
return C.CString(string(jsonString))
}
//export verifyGEOIPDataBase
func verifyGEOIPDataBase() bool {
mmdb, err := geoip2.Open(constant.Path.MMDB())
if err != nil {
log.Warnln("mmdb fail:%s", err.Error())
return false
}
_, err = mmdb.Country(net.ParseIP("114.114.114.114"))
if err != nil {
log.Warnln("mmdb lookup fail:%s", err.Error())
return false
}
return true
}
func main() {
}