-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (138 loc) · 2.79 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"sync"
"fmt"
"time"
"io/ioutil"
"strings"
"net/http"
"net"
"strconv"
"context"
)
var HttpClientPool sync.Pool
var ipChan chan string
var wg sync.WaitGroup
var Stop = false
/**
- 103.21.244.0/22
- 103.22.200.0/22
- 103.31.4.0/22
- 104.16.0.0/12
- 108.162.192.0/18
131.0.72.0/22
- 141.101.64.0/18
- 162.158.0.0/15
- 172.64.0.0/13
- 173.245.48.0/20
- 188.114.96.0/20
- 190.93.240.0/20
197.234.240.0/22
- 198.41.128.0/17
*/
func main() {
ipChan = make(chan string, 32)
fmt.Println(ipToInt("192.168.1.1"))
fmt.Println(IntToIp(2147483648))
fmt.Println(ipWithMask("199.27.132.0/24"))
start, length := ipWithMask("199.27.132.0/24")
var i uint32
for j := 0; j < 32; j++ {
go routine1()
wg.Add(1)
}
for i = 0; i < length; i += 4 {
ipChan <- IntToIp(start + i)
}
Stop = true
wg.Wait()
fmt.Println("All done.")
}
func routine1() {
for {
select {
case ipStr := <-ipChan:
fmt.Println(ipStr, " ", getCdnTrace(ipStr))
default:
if Stop {
goto FlagEnd
}
}
}
FlagEnd:
wg.Done()
}
func getCdnTrace(ipStr string) (colo string) {
c := acquireHttpClient(3 * time.Second)
resp, err := c.Get("http://" + ipStr + "/cdn-cgi/trace")
if err != nil {
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
bodyStr := string(b)
s := strings.Index(bodyStr, "colo=")
if s == -1 {
return
}
colo = bodyStr[s : s+8]
if colo != "colo=LAX" && colo != "colo=SJC" {
colo += "FIND IT!!!"
}
return
}
func acquireHttpClient(timeout time.Duration) (http.Client) {
c := HttpClientPool.Get()
if c == nil {
c = http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, time.Second*3)
if err != nil {
return nil, err
}
deadline := time.Now().Add(timeout)
c.SetDeadline(deadline)
return c, nil
},
},
}
}
return c.(http.Client)
}
func putHttpClient(client http.Client) {
HttpClientPool.Put(client)
}
func ipWithMask(ipMaskStr string) (start uint32, length uint32) {
s := strings.Split(ipMaskStr, "/")
if len(s) != 2 {
return 0, 0
}
maskLen, _ := strconv.Atoi(s[1])
return ipToInt(s[0]), 0xffffffff>>uint32(maskLen) + 1
}
func ipToInt(ipStr string) (ipInt uint32) {
s := strings.Split(ipStr, ".")
if len(s) != 4 {
return 0
}
b0, _ := strconv.Atoi(s[0])
b1, _ := strconv.Atoi(s[1])
b2, _ := strconv.Atoi(s[2])
b3, _ := strconv.Atoi(s[3])
ipInt += (uint32)(b0 << 24)
ipInt += (uint32)(b1 << 16)
ipInt += (uint32)(b2 << 8)
ipInt += (uint32)(b3 << 0)
return
}
func IntToIp(ipInt uint32) (ipStr string) {
b0 := byte(ipInt & 0xff000000 >> 24)
b1 := byte(ipInt & 0xff0000 >> 16)
b2 := byte(ipInt & 0xff00 >> 8)
b3 := byte(ipInt & 0xff)
return net.IPv4(b0, b1, b2, b3).String()
}