-
Notifications
You must be signed in to change notification settings - Fork 20
/
runModeUpdateIspRule.go
121 lines (112 loc) · 3.98 KB
/
runModeUpdateIspRule.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
package main
import (
"fmt"
"log"
"time"
)
func updateIspRule() {
iKuai, err := loginToIkuai()
if err != nil {
log.Println("登录爱快失败:", err)
return
}
var GoroutineEnd1 bool = false
var GoroutineEnd2 bool = false
go func() {
for _, customIsp := range conf.CustomIsp {
//记录旧的自定义运营商
preIds, err := iKuai.GetCustomIspAll(customIsp.Tag)
if err != nil {
log.Println("运营商/IP分流== 获取准备更新的自定义运营商列表失败:", customIsp.Name, customIsp.Tag, err)
//return
break
} else {
log.Println("运营商/IP分流== 获取准备更新的自定义运营商列表成功", customIsp.Name, customIsp.Tag)
}
//是否要先删除旧规则
if *delOldRule == "before" {
//删除旧的自定义运营商
err = iKuai.DelCustomIspFromPreIds(preIds)
if err == nil {
log.Println("运营商/IP分流== 删除旧的运营商列表成功", customIsp.Name, customIsp.Tag)
log.Println("运营商/IP分流== 更新完成", customIsp.Name, customIsp.Tag)
} else {
log.Println("运营商/IP分流== 删除旧的运营商列表有错误", customIsp.Name, customIsp.Tag, err)
}
}
//更新自定义运营商
log.Println("运营商/IP分流== 正在更新", customIsp.Name, customIsp.Tag)
err = updateCustomIsp(iKuai, customIsp.Name, customIsp.Tag, customIsp.URL)
if err != nil {
log.Printf("运营商/IP分流== 添加自定义运营商'%s'失败:%s\n", customIsp.Name, err)
} else {
log.Printf("运营商/IP分流== 添加自定义运营商'%s'成功\n", customIsp.Name)
if err == nil {
if *delOldRule == "after" {
//删除旧的自定义运营商
err = iKuai.DelCustomIspFromPreIds(preIds)
if err == nil {
log.Println("运营商/IP分流== 删除旧的运营商列表成功", customIsp.Name, customIsp.Tag)
log.Println("运营商/IP分流== 更新完成", customIsp.Name, customIsp.Tag)
} else {
log.Println("运营商/IP分流== 删除旧的运营商列表有错误", customIsp.Name, customIsp.Tag, err)
}
}
} else {
log.Println("运营商/IP分流== 添加运营商的时候有错误", customIsp.Name, customIsp.Tag, err)
}
}
}
GoroutineEnd1 = true
}()
go func() {
for _, streamDomain := range conf.StreamDomain {
//记录旧的域名分流
preIds, err := iKuai.GetStreamDomainAll(streamDomain.Tag)
if err != nil {
log.Println("域名分流== 获取准备更新的域名列表失败:", streamDomain.Tag, err)
break
} else {
log.Println("域名分流== 获取准备更新的域名列表成功", streamDomain.Tag)
}
if *delOldRule == "before" {
//删除旧的域名分流
err = iKuai.DelStreamDomainFromPreIds(preIds)
if err == nil {
log.Println("域名分流== 删除旧的运营商列表成功")
} else {
log.Println("域名分流== 删除旧的运营商列表有错误", err)
}
}
//更新域名分流
log.Println("域名分流== 正在更新", streamDomain.Interface, streamDomain.Tag, streamDomain.SrcAddr)
err = updateStreamDomain(iKuai, streamDomain.Interface, streamDomain.Tag, streamDomain.SrcAddr, streamDomain.URL)
if err != nil {
log.Printf("域名分流== 添加域名分流 '%s' 失败:%s\n", streamDomain.Interface, err)
} else {
log.Printf("域名分流== 添加域名分流 '%s' 成功\n", streamDomain.Interface)
if err == nil {
if *delOldRule == "after" {
//删除旧的域名分流
err = iKuai.DelStreamDomainFromPreIds(preIds)
if err == nil {
log.Println("域名分流== 删除旧的运营商列表成功")
} else {
log.Println("域名分流== 删除旧的运营商列表有错误", err)
}
}
} else {
log.Println("域名分流== 添加运营商的时候有错误", err)
}
}
}
GoroutineEnd2 = true
}()
for { //等待两个协程结束
if GoroutineEnd1 && GoroutineEnd2 {
break
}
time.Sleep(1 * time.Second)
fmt.Printf(".")
}
}