Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e7dc3c73e3 | ||
|
dae7521e26 | ||
|
1f84d5ec79 | ||
|
5882bab2f3 | ||
|
b52adf23f3 | ||
|
9b61c8bae3 | ||
|
de5ee33866 | ||
|
7733db1744 | ||
|
aaa53b54e4 | ||
|
a00de486e4 | ||
|
abbfb5fbd9 | ||
|
627cca4dbf | ||
67cf4bc888 | |||
4149ea8d0f |
30
actives.yaml
30
actives.yaml
|
@ -23,4 +23,34 @@ switch:
|
|||
192.168.6.100:8091:
|
||||
group: ["10.10.10.92", "10.10.10.93"] # 三亚
|
||||
city: "4602"
|
||||
192.168.6.100:8092:
|
||||
group: ["10.10.21.26", "10.10.21.27"] # 昆明
|
||||
city: "5301"
|
||||
192.168.6.100:8093:
|
||||
group: ["10.10.21.28", "10.10.21.29"] # 福州
|
||||
city: "3501"
|
||||
192.168.6.100:8094:
|
||||
group: ["10.10.21.10", "10.10.21.11"] # 武汉
|
||||
city: "4201"
|
||||
192.168.6.100:8095:
|
||||
group: ["10.10.21.12", "10.10.21.14"] # 郑州
|
||||
city: "4101"
|
||||
192.168.6.100:8096:
|
||||
group: ["10.10.21.13", "10.10.21.15"] # 宁波
|
||||
city: "3302"
|
||||
192.168.6.100:8097:
|
||||
group: ["10.10.21.16", "10.10.21.17"] # 无锡
|
||||
city: "3202"
|
||||
192.168.6.100:8098:
|
||||
group: ["10.10.21.18", "10.10.21.19"] # 南京
|
||||
city: "3201"
|
||||
192.168.6.100:8099:
|
||||
group: ["10.10.21.20", "10.10.21.21"] # 石家庄
|
||||
city: "1301"
|
||||
192.168.6.100:8100:
|
||||
group: ["10.10.21.22", "10.10.21.23"] # 合肥
|
||||
city: "3401"
|
||||
192.168.6.100:8101:
|
||||
group: ["10.10.21.24", "10.10.21.25"] # 南昌
|
||||
city: "3601"
|
||||
restart: 65
|
||||
|
|
58
dip.go
58
dip.go
|
@ -34,8 +34,8 @@ func (worker *DIPWorker) ShowGroupInfo() string {
|
|||
overtimeLabel = "<☠?>"
|
||||
}
|
||||
tm := time.Unix(dhost.ActiveTime, 0)
|
||||
tm.Format("2006-01-02 15:04:05") //2018-07-11 15:10:19
|
||||
content += dhost.Host + "(" + tm.Format("2006-01-02 15:04:05") + overtimeLabel + "),"
|
||||
// tm.Format(time.RFC3339) //2018-07-11 15:10:19
|
||||
content += dhost.Host + "(" + tm.Format(time.RFC3339) + overtimeLabel + "),"
|
||||
}
|
||||
content = strings.TrimRight(content, ",") + "]\n"
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ func (worker *DIPWorker) ShowGroupInfo() string {
|
|||
return content
|
||||
}
|
||||
|
||||
// DIPGroup 动态ip组, 一个组指的是一条可动态切换的线
|
||||
type DIPGroup struct {
|
||||
Group map[string]*DHost // 原始参照
|
||||
Waitor map[string]*DHost
|
||||
|
@ -54,6 +55,7 @@ type DIPGroup struct {
|
|||
Mutex *sync.Mutex
|
||||
}
|
||||
|
||||
// NewDIPGroup 创建一个,动态ip组, 一个组指的是一条可动态切换的线
|
||||
func NewDIPGroup() *DIPGroup {
|
||||
group := DIPGroup{}
|
||||
group.Group = make(map[string]*DHost)
|
||||
|
@ -69,34 +71,59 @@ func (group *DIPGroup) Choose(addr string) {
|
|||
for ip, dhost := range group.Ready {
|
||||
// 设置转换的iptable
|
||||
if group.Current == nil {
|
||||
group.Current = dhost
|
||||
group.Current.ActiveTime = now
|
||||
SetAddrForward(group.IPTableNum, addr, ip)
|
||||
log.Println(addr, " Current is nil")
|
||||
} else {
|
||||
restartAddr := "http://" + group.Current.Host + ":8800/pppoe/restart"
|
||||
if _, err := requests.NewSession().Get(restartAddr).Execute(); err != nil {
|
||||
log.Println(err)
|
||||
group.Current.ActiveTime += 12
|
||||
} else {
|
||||
group.Current = dhost
|
||||
group.Current.ActiveTime = now
|
||||
// log.Println("new set addr:", group.Current.Host, "restartAddr:", restartAddr, "resp", resp.Content())
|
||||
SetAddrForward(group.IPTableNum, addr, ip)
|
||||
|
||||
i := 0
|
||||
for ; i < 2; i++ {
|
||||
|
||||
imokURL := "http://" + dhost.Host + ":8800/pppoe/imok"
|
||||
ses := requests.NewSession()
|
||||
ses.SetConfig(requests.CRequestTimeout, 4)
|
||||
|
||||
if _, err := ses.Get(imokURL).Execute(); err != nil {
|
||||
log.Println(err)
|
||||
if i == 2 {
|
||||
// 下个需要切换的DHost错误
|
||||
group.Current.ActiveTime = now + 15
|
||||
delete(group.Ready, ip)
|
||||
return // 不执行下面的 切换操作 for ip, dhost := range group.Ready
|
||||
}
|
||||
} else { // 如果ping 通下个切换的主机就切换
|
||||
for ii := 0; ii < 3; ii++ {
|
||||
restartURL := "http://" + group.Current.Host + ":8800/pppoe/restart"
|
||||
if _, err := ses.Get(restartURL).Execute(); err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
break // 可以切换 ii := 0; ii < 3; ii++
|
||||
}
|
||||
}
|
||||
break // for ; i < 3; i++ {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
group.Current = dhost
|
||||
group.Current.ActiveTime = now
|
||||
|
||||
SetAddrForward(group.IPTableNum, addr, ip)
|
||||
group.Waitor[ip] = group.Ready[ip]
|
||||
delete(group.Ready, ip)
|
||||
|
||||
break
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// DHost 动态IP的一个vps
|
||||
// Host vps的host属性 指定ip
|
||||
// ActiveTime ActiveTime活跃时间
|
||||
type DHost struct {
|
||||
Host string
|
||||
ActiveTime int64
|
||||
}
|
||||
|
||||
// NewDHost 创建一个DHost host 为 ip
|
||||
func NewDHost(host string) *DHost {
|
||||
dh := DHost{}
|
||||
dh.ActiveTime = time.Now().Unix()
|
||||
|
@ -104,6 +131,7 @@ func NewDHost(host string) *DHost {
|
|||
return &dh
|
||||
}
|
||||
|
||||
// OverTime dhost对比时间是否超过limit的限制, 超时判断
|
||||
func (dh *DHost) OverTime(now int64, limit int64) int64 {
|
||||
if now-dh.ActiveTime >= limit {
|
||||
return 1
|
||||
|
|
56
switch.go
56
switch.go
|
@ -26,7 +26,7 @@ type Switch struct {
|
|||
func SetAddrForward(num, addr, ip string) {
|
||||
port := strings.Split(addr, ":")[1]
|
||||
cmd := exec.Command("/bin/sh", "-c", "sudo iptables -t nat -R IPSWITCH "+num+" -p tcp --dport "+port+" -j DNAT --to "+ip+":8885")
|
||||
_, err := cmd.Output()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ func NewSwitch() *Switch {
|
|||
http.HandleFunc("/ippool/switch/imactive", swi.imActive)
|
||||
http.HandleFunc("/ippool/switch/update", swi.updateActives)
|
||||
http.HandleFunc("/ippool/switch/actives", swi.switchActives)
|
||||
http.HandleFunc("/ippool/switch/allproxies", swi.allProxies)
|
||||
|
||||
return &swi
|
||||
}
|
||||
|
@ -96,13 +97,28 @@ func (swi *Switch) timeToSwitch(now int64) {
|
|||
}
|
||||
}
|
||||
|
||||
func (swi *Switch) clearReady(now int64) {
|
||||
|
||||
for _, dipg := range swi.Worker.ForMatch {
|
||||
var delIPList []string
|
||||
|
||||
for ip, dhost := range dipg.Ready {
|
||||
if now >= dhost.ActiveTime+150 {
|
||||
delIPList = append(delIPList, ip)
|
||||
}
|
||||
}
|
||||
|
||||
for _, ip := range delIPList {
|
||||
delete(dipg.Ready, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkInReady 签到的IP(vps的vpn节点IP))
|
||||
func (swi *Switch) checkInReady(ip string, now int64) {
|
||||
|
||||
if dipg, ok := swi.Worker.ForMatch[ip]; ok {
|
||||
|
||||
dipg.Group[ip].ActiveTime = now
|
||||
|
||||
if dh, ok := dipg.Waitor[ip]; ok {
|
||||
if dh.OverTime(now, 20) > 0 {
|
||||
delete(dipg.Waitor, ip)
|
||||
|
@ -111,6 +127,8 @@ func (swi *Switch) checkInReady(ip string, now int64) {
|
|||
}
|
||||
}
|
||||
|
||||
dipg.Group[ip].ActiveTime = now
|
||||
|
||||
if dipg.Current == nil {
|
||||
dhost := NewDHost(ip)
|
||||
dhost.ActiveTime = now
|
||||
|
@ -146,6 +164,7 @@ func (swi *Switch) imActive(w http.ResponseWriter, req *http.Request) {
|
|||
|
||||
// 60秒show一次日志
|
||||
if now >= swi.lastShow+60 {
|
||||
swi.clearReady(now)
|
||||
swi.lastShow = now
|
||||
log.Println("\n" + swi.Worker.ShowGroupInfo())
|
||||
}
|
||||
|
@ -182,7 +201,6 @@ func (swi *Switch) switchActives(w http.ResponseWriter, req *http.Request) {
|
|||
for addr, group := range swi.Worker.ForLoop {
|
||||
|
||||
addrmap := make(map[string]interface{})
|
||||
content[addr] = addrmap
|
||||
|
||||
isappend := int64(0)
|
||||
var hosts []string
|
||||
|
@ -193,6 +211,7 @@ func (swi *Switch) switchActives(w http.ResponseWriter, req *http.Request) {
|
|||
hosts = append(hosts, dhost.Host)
|
||||
}
|
||||
if isappend > 1 {
|
||||
content[addr] = addrmap
|
||||
addrmap["group"] = hosts
|
||||
addrmap["city"] = group.City
|
||||
}
|
||||
|
@ -203,3 +222,32 @@ func (swi *Switch) switchActives(w http.ResponseWriter, req *http.Request) {
|
|||
ErrorLog(err)
|
||||
|
||||
}
|
||||
|
||||
func (swi *Switch) allProxies(w http.ResponseWriter, req *http.Request) {
|
||||
defer req.Body.Close()
|
||||
|
||||
swi.Worker.Mutex.Lock()
|
||||
defer swi.Worker.Mutex.Unlock()
|
||||
|
||||
now := time.Now().Unix()
|
||||
|
||||
content := make(map[string]map[string]interface{})
|
||||
for addr, group := range swi.Worker.ForLoop {
|
||||
|
||||
addrmap := make(map[string]interface{})
|
||||
content[addr] = addrmap
|
||||
|
||||
isappend := int64(0)
|
||||
var hosts []string
|
||||
for _, dhost := range group.Group {
|
||||
hosts = append(hosts, dhost.Host)
|
||||
}
|
||||
addrmap["group"] = hosts
|
||||
addrmap["city"] = group.City
|
||||
}
|
||||
jdata, err := json.Marshal(content)
|
||||
ErrorLog(err)
|
||||
_, err = w.Write(jdata)
|
||||
ErrorLog(err)
|
||||
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
)
|
||||
|
||||
func TestSwitchYaml(t *testing.T) {
|
||||
t.Error()
|
||||
|
||||
swi := NewSwitch()
|
||||
if swi == nil {
|
||||
|
|
Loading…
Reference in New Issue
Block a user