Update chinaip generator

pull/62/head
v2ray 2015-12-08 23:12:12 +01:00
parent 9d51bd2985
commit 6afeaccc5a
4 changed files with 6337 additions and 12647 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +0,0 @@
#!/bin/bash
APNIC_FILE="http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest"
curl "${APNIC_FILE}" | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > ipv4.txt

File diff suppressed because it is too large Load Diff

View File

@ -3,31 +3,49 @@ package main
import (
"bufio"
"fmt"
"math"
"net"
"os"
"path/filepath"
"net/http"
"strconv"
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
)
const (
apnicFile = "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest"
)
func main() {
GOPATH := os.Getenv("GOPATH")
src := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "tools", "chinaip", "ipv4.txt")
reader, err := os.Open(src)
resp, err := http.Get(apnicFile)
if err != nil {
panic(err)
}
ipNet := v2net.NewIPNet()
if resp.StatusCode != 200 {
panic(fmt.Errorf("Unexpected status %d", resp.StatusCode))
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
scanner := bufio.NewScanner(reader)
ipNet := v2net.NewIPNet()
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if len(line) == 0 {
break
parts := strings.Split(line, "|")
if len(parts) < 5 {
continue
}
_, t, err := net.ParseCIDR(line)
if strings.ToLower(parts[1]) != "cn" || strings.ToLower(parts[2]) != "ipv4" {
continue
}
ip := parts[3]
count, err := strconv.Atoi(parts[4])
if err != nil {
continue
}
mask := 32 - int(math.Floor(math.Log2(float64(count))+0.5))
cidr := fmt.Sprintf("%s/%d", ip, mask)
_, t, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}