package main import ( "bufio" "fmt" "github.com/EvilSuperstars/go-cidrman" "io" "log" "math" "net" "net/http" "os" "strconv" "strings" ) const ( ipdataurl = "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest" ) func getData(savefile string) error { resp, err := http.Get(ipdataurl) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("http error %d", resp.StatusCode) } fp, err := os.Create(savefile) if err != nil { return err } defer fp.Close() io.Copy(fp, resp.Body) return nil } func numToMask(n int) int { n1 := math.Log2(float64(n)) return 32 - int(n1) } func parseLine(l string) *net.IPNet { ll := strings.Split(l, "|") ipString := ll[3] numString := ll[4] ip := net.ParseIP(ipString) n, err := strconv.Atoi(numString) if ip == nil || err != nil { return nil } prefixLen := numToMask(n) mask := net.CIDRMask(prefixLen, 32) return &net.IPNet{IP: ip, Mask: mask} } func parseData(fn string) []*net.IPNet { fp, err := os.Open(fn) if err != nil { log.Println(err) return nil } defer fp.Close() ipnets := []*net.IPNet{} r := bufio.NewReader(fp) for { line, err := r.ReadString('\n') if err != nil { if err != io.EOF { log.Println(err) } break } if line[0] == '#' { continue } if strings.HasPrefix(line, "apnic|CN|ipv4") { ipnet := parseLine(line) if ipnet != nil { ipnets = append(ipnets, ipnet) } } } return ipnets } func writeRouteScriptLinux(ipnet []*net.IPNet) error { fp, err := os.Create("routes_add.sh") if err != nil { return err } defer fp.Close() fp1, err := os.Create("routes_del.sh") if err != nil { return err } defer fp1.Close() fmt.Fprintf(fp, `#!/bin/bash # route has two formats # 1.2.3.4 dev ppp0 src 10.150.16.112 # 1.2.3.4 via 192.168.1.1 dev eth0 src 192.168.1.160 gw=$(ip route get 1.2.3.4 | awk '/1.2.3.4/{gsub("1.2.3.4", "");print}') ip -batch - <