-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
292 lines (265 loc) · 7.03 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/robfig/cron/v3"
"github.com/ztc1997/ikuai-bypass/api"
"github.com/ztc1997/ikuai-bypass/router"
"gopkg.in/yaml.v3"
)
var confPath = flag.String("c", "./config.yml", "配置文件路径")
var conf struct {
IkuaiURL string `yaml:"ikuai-url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Cron string `yaml:"cron"`
CustomIsp []struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
} `yaml:"custom-isp"`
IpGroup []struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
} `yaml:"ip-group"`
StreamDomain []struct {
Interface string `yaml:"interface"`
SrcAddr string `yaml:"src-addr"`
URL string `yaml:"url"`
} `yaml:"stream-domain"`
StreamIpPort []struct {
Type string `yaml:"type"`
Interface string `yaml:"interface"`
Nexthop string `yaml:"nexthop"`
SrcAddr string `yaml:"src-addr"`
IpGroup string `yaml:"ip-group"`
} `yaml:"stream-ipport"`
}
func main() {
flag.Parse()
err := readConf(*confPath)
if err != nil {
log.Println("读取配置文件失败:", err)
return
}
update()
if conf.Cron == "" {
return
}
c := cron.New()
_, err = c.AddFunc(conf.Cron, update)
if err != nil {
log.Println("启动计划任务失败:", err)
return
} else {
log.Println("已启动计划任务")
}
c.Start()
{
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals
}
}
func readConf(filename string) error {
buf, err := os.ReadFile(filename)
if err != nil {
return err
}
err = yaml.Unmarshal(buf, &conf)
if err != nil {
return fmt.Errorf("in file %q: %v", filename, err)
}
return nil
}
func update() {
err := readConf(*confPath)
if err != nil {
log.Println("更新配置文件失败:", err)
return
}
baseurl := conf.IkuaiURL
if baseurl == "" {
gateway, err := router.GetGateway()
if err != nil {
log.Println("获取默认网关失败:", err)
return
}
baseurl = "http://" + gateway
log.Println("使用默认网关地址:", baseurl)
}
iKuai := api.NewIKuai(baseurl)
err = iKuai.Login(conf.Username, conf.Password)
if err != nil {
log.Println("登陆失败:", err)
return
} else {
log.Println("登录成功")
}
err = iKuai.DelIKuaiBypassCustomIsp()
if err != nil {
log.Println("移除旧的自定义运营商失败:", err)
} else {
log.Println("移除旧的自定义运营商成功")
}
for _, customIsp := range conf.CustomIsp {
err = updateCustomIsp(iKuai, customIsp.Name, customIsp.URL)
if err != nil {
log.Printf("添加自定义运营商'%s@%s'失败:%s\n", customIsp.Name, customIsp.URL, err)
} else {
log.Printf("添加自定义运营商'%s@%s'成功\n", customIsp.Name, customIsp.URL)
}
}
err = iKuai.DelIKuaiBypassIpGroup()
if err != nil {
log.Println("移除旧的IP分组失败:", err)
} else {
log.Println("移除旧的IP分组成功")
}
for _, ipGroup := range conf.IpGroup {
err = updateIpGroup(iKuai, ipGroup.Name, ipGroup.URL)
if err != nil {
log.Printf("添加IP分组'%s@%s'失败:%s\n", ipGroup.Name, ipGroup.URL, err)
} else {
log.Printf("添加IP分组'%s@%s'成功\n", ipGroup.Name, ipGroup.URL)
}
}
err = iKuai.DelIKuaiBypassStreamDomain()
if err != nil {
log.Println("移除旧的域名分流失败:", err)
} else {
log.Println("移除旧的域名分流成功")
}
for _, streamDomain := range conf.StreamDomain {
err = updateStreamDomain(iKuai, streamDomain.Interface, streamDomain.SrcAddr, streamDomain.URL)
if err != nil {
log.Printf("添加域名分流 '%s@%s' 失败:%s\n", streamDomain.Interface, streamDomain.URL, err)
} else {
log.Printf("添加域名分流 '%s@%s' 成功\n", streamDomain.Interface, streamDomain.URL)
}
}
err = iKuai.DelIKuaiBypassStreamIpPort()
if err != nil {
log.Println("移除旧的端口分流失败:", err)
} else {
log.Println("移除旧的端口分流成功")
}
for _, streamIpPort := range conf.StreamIpPort {
err = updateStreamIpPort(iKuai, streamIpPort.Type, streamIpPort.Interface, streamIpPort.Nexthop, streamIpPort.SrcAddr, streamIpPort.IpGroup)
if err != nil {
log.Printf("添加端口分流 '%s@%s' 失败:%s\n", streamIpPort.Interface+streamIpPort.Nexthop, streamIpPort.IpGroup, err)
} else {
log.Printf("添加端口分流 '%s@%s' 成功\n", streamIpPort.Interface+streamIpPort.Nexthop, streamIpPort.IpGroup)
}
}
}
func updateCustomIsp(iKuai *api.IKuai, name, url string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New(resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
ips := strings.Split(string(body), "\n")
ips = removeIpv6(ips)
ipGroups := group(ips, 5000)
for _, ig := range ipGroups {
ipGroup := strings.Join(ig, ",")
iKuai.AddCustomIsp(name, ipGroup)
}
return
}
func updateIpGroup(iKuai *api.IKuai, name, url string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New(resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
ips := strings.Split(string(body), "\n")
ips = removeIpv6(ips)
ipGroups := group(ips, 1000)
for index, ig := range ipGroups {
ipGroup := strings.Join(ig, ",")
iKuai.AddIpGroup(name+"_"+strconv.Itoa(index), ipGroup)
}
return
}
func updateStreamDomain(iKuai *api.IKuai, iface, srcAddr, url string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New(resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
domains := strings.Split(string(body), "\n")
domainGroup := group(domains, 1000)
for _, d := range domainGroup {
domain := strings.Join(d, ",")
iKuai.AddStreamDomain(iface, srcAddr, domain)
}
return
}
func updateStreamIpPort(iKuai *api.IKuai, forwardType string, iface string, nexthop string, srcAddr string, ipGroup string) (err error) {
var ipGroupList []string
for _, ipGroupItem := range strings.Split(ipGroup, ",") {
var data []string
data, err = iKuai.GetAllIKuaiBypassIpGroupNamesByName(ipGroupItem)
if err != nil {
return
}
ipGroupList = append(ipGroupList, data...)
}
iKuai.AddStreamIpPort(forwardType, iface, strings.Join(ipGroupList, ","), srcAddr, nexthop)
return
}
func removeIpv6(ips []string) []string {
i := 0
for _, ip := range ips {
if !strings.Contains(ip, ":") {
ips[i] = ip
i++
}
}
return ips[:i]
}
func group(arr []string, subGroupLength int64) [][]string {
max := int64(len(arr))
var segmens = make([][]string, 0)
quantity := max / subGroupLength
remainder := max % subGroupLength
i := int64(0)
for i = int64(0); i < quantity; i++ {
segmens = append(segmens, arr[i*subGroupLength:(i+1)*subGroupLength])
}
if quantity == 0 || remainder != 0 {
segmens = append(segmens, arr[i*subGroupLength:i*subGroupLength+remainder])
}
return segmens
}