Created
February 22, 2016 09:54
-
-
Save fangdingjun/31c61a348ba17c6f9095 to your computer and use it in GitHub Desktop.
check network connection and re-dial when connection is down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
//"net" | |
//"net/http" | |
"fmt" | |
"log" | |
//"os" | |
"flag" | |
"os/exec" | |
"strings" | |
"time" | |
) | |
var time_wait int = 10 | |
type output struct { | |
} | |
func (o *output) Write(b []byte) (int, error) { | |
log.Printf("%s", b) | |
return len(b), nil | |
} | |
var myout = &output{} | |
func check(u string, max int, errch chan int) { | |
log.Printf("checking %s...", u) | |
var count int | |
for { | |
time.Sleep(time.Duration(time_wait) * time.Second) | |
if dialing { | |
count = 0 | |
continue | |
} | |
if checknet(u) { | |
count = 0 | |
} else { | |
count += 1 | |
} | |
if count >= max { | |
errch <- 1 | |
count = 0 | |
} | |
} | |
} | |
func checknet(u string) bool { | |
args := fmt.Sprintf("--connect-timeout %d --speed-limit %d --speed-time %d -s -S -k -L %s", | |
3, 10*1024, 5, | |
u, | |
) | |
cmd := exec.Command("curl", strings.Fields(args)...) | |
//cmd.Stdout = myout | |
cmd.Stderr = myout | |
err := cmd.Run() | |
if err != nil { | |
return false | |
} | |
return true | |
} | |
func redial(c chan int) { | |
log.Println("redialing...") | |
cmd := exec.Command("sudo", "poff", "dsl-provider") | |
cmd.Stdout = myout | |
cmd.Stderr = myout | |
cmd.Run() | |
time.Sleep(time.Duration(dial_wait) * time.Second) | |
cmd = exec.Command("sudo", "pon", "dsl-provider") | |
cmd.Stdout = myout | |
cmd.Stderr = myout | |
cmd.Run() | |
time.Sleep(time.Duration(dial_wait) * time.Second) | |
c <- 1 | |
log.Println("done") | |
} | |
var dialing = false | |
var max_err = 3 | |
var idle_wait, busy_wait, dial_wait int | |
func main() { | |
flag.IntVar(&max_err, "max_error", 3, "max error on check to trigger re-dial") | |
flag.IntVar(&idle_wait, "idle_wait", 60, "seconds to wait when network not busy") | |
flag.IntVar(&busy_wait, "busy_wait", 10, "seconds to wait when network busy") | |
flag.IntVar(&dial_wait, "dial_wait", 5, "seconds to wait during re-dial") | |
flag.Parse() | |
domains := []string{ | |
//"https://www.ratafee.nl/stream-bytes/10240", | |
"https://www.httpbin.org/ip", | |
"https://www.baidu.com/", | |
"http://www.ip.cn/", | |
"https://www.taobao.com/", | |
} | |
args := flag.Args() | |
if len(args) > 0 { | |
domains = args | |
} | |
errch := make(chan int, len(domains)) | |
for _, dn := range domains { | |
go check(dn, max_err, errch) | |
} | |
dial_done := make(chan int) | |
for { | |
h := time.Now().Hour() | |
if h >= 19 && h < 24 { | |
time_wait = busy_wait | |
} else { | |
time_wait = idle_wait | |
} | |
select { | |
case <-errch: | |
if !dialing { | |
dialing = true | |
go redial(dial_done) | |
} | |
case <-dial_done: | |
dialing = false | |
case <-time.After(30 * time.Minute): | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment