-
Notifications
You must be signed in to change notification settings - Fork 85
/
chinaip_gen.go
104 lines (91 loc) · 2.08 KB
/
chinaip_gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// +build generate
// go run chinaip_gen.go
package main
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
// use china ip list database by ipip.net
const (
chinaIPListFile = "https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt"
)
func main() {
resp, err := http.Get(chinaIPListFile)
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
panic(fmt.Errorf("Unexpected status %d", resp.StatusCode))
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
startList := []string{}
countList := []string{}
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
parts := strings.Split(line, "/")
if len(parts) != 2 {
panic(errors.New("Invalid CIDR"))
}
ip := parts[0]
mask := parts[1]
count, err := cidrCalc(mask)
if err != nil {
panic(err)
}
ipLong, err := ipToUint32(ip)
if err != nil {
panic(err)
}
startList = append(startList, strconv.FormatUint(uint64(ipLong), 10))
countList = append(countList, strconv.FormatUint(uint64(count), 10))
}
file, err := os.OpenFile("chinaip_data.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to generate chinaip_data.go: %v", err)
}
defer file.Close()
fmt.Fprintln(file, "package main")
fmt.Fprint(file, "var CNIPDataStart = []uint32 {\n ")
fmt.Fprint(file, strings.Join(startList, ",\n "))
fmt.Fprintln(file, ",\n }")
fmt.Fprint(file, "var CNIPDataNum = []uint{\n ")
fmt.Fprint(file, strings.Join(countList, ",\n "))
fmt.Fprintln(file, ",\n }")
}
func cidrCalc(mask string) (uint, error) {
i, err := strconv.Atoi(mask)
if err != nil || i > 32 {
return 0, errors.New("Invalid Mask")
}
p := 32 - i
res := uint(intPow2(p))
return res, nil
}
func intPow2(p int) int {
r := 1
for i := 0; i < p; i++ {
r *= 2
}
return r
}
func ipToUint32(ipstr string) (uint32, error) {
ip := net.ParseIP(ipstr)
if ip == nil {
return 0, errors.New("Invalid IP")
}
ip = ip.To4()
if ip == nil {
return 0, errors.New("Not IPv4")
}
return binary.BigEndian.Uint32(ip), nil
}