From 627cca4dbfcb2594cfdb985ce11adb266a88c0c9 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 24 Dec 2018 16:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=96=84=E5=88=87=E6=8D=A2=E7=9A=84?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dip.go | 52 +++++++++++++++++++++++++++++++++++++++------------- switch.go | 6 +++--- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/dip.go b/dip.go index 7fb1f03..07390a5 100644 --- a/dip.go +++ b/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,26 +71,45 @@ 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 { - for i := 0; i < 3; i++ { - restartAddr := "http://" + group.Current.Host + ":8800/pppoe/restart" - if _, err := requests.NewSession().Get(restartAddr).Execute(); err != nil { + i := 0 + for ; i < 3; i++ { + + imokURL := "http://" + dhost.Host + ":8800/pppoe/imok" + if _, err := requests.NewSession().Get(imokURL).Execute(); err != nil { + log.Println(err) - } else { - break + + } else { // 如果ping 通下个切换的主机就切换 + + for ii := 0; ii < 3; ii++ { + restartURL := "http://" + group.Current.Host + ":8800/pppoe/restart" + if _, err := requests.NewSession().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) + if i == 3 { + // 下个需要切换的DHost错误 + delete(group.Ready, ip) + continue // 不执行下面的 切换操作 for ip, dhost := range group.Ready + } } + group.Current = dhost + group.Current.ActiveTime = now + + SetAddrForward(group.IPTableNum, addr, ip) group.Waitor[ip] = group.Ready[ip] delete(group.Ready, ip) @@ -96,11 +117,15 @@ func (group *DIPGroup) Choose(addr string) { } } +// 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() @@ -108,6 +133,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 diff --git a/switch.go b/switch.go index 4ab4120..2aa12ec 100644 --- a/switch.go +++ b/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) } @@ -118,8 +118,6 @@ 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) @@ -128,6 +126,8 @@ func (swi *Switch) checkInReady(ip string, now int64) { } } + dipg.Group[ip].ActiveTime = now + if dipg.Current == nil { dhost := NewDHost(ip) dhost.ActiveTime = now