forked from Spedoske/CloudflareScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcping.go
159 lines (147 loc) · 4.08 KB
/
tcping.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
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"github.com/VividCortex/ewma"
"io"
"net"
"net/http"
"strconv"
"sync"
"time"
)
//bool connectionSucceed float32 time
func tcping(ip net.IPAddr) (bool, float32) {
startTime := time.Now()
conn, err := net.DialTimeout("tcp", ip.String()+":"+strconv.Itoa(defaultTcpPort), tcpConnectTimeout)
if err != nil {
return false, 0
} else {
var endTime = time.Since(startTime)
var duration = float32(endTime.Microseconds()) / 1000.0
_ = conn.Close()
return true, duration
}
}
//pingReceived pingTotalTime
func checkConnection(ip net.IPAddr) (int, float32) {
pingRecv := 0
var pingTime float32 = 0.0
for i := 1; i <= failTime; i++ {
pingSucceed, pingTimeCurrent := tcping(ip)
if pingSucceed {
pingRecv++
pingTime += pingTimeCurrent
}
}
return pingRecv, pingTime
}
//return Success packetRecv averagePingTime specificIPAddr
func tcpingHandler(ip net.IPAddr, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) {
ipCanConnect := false
pingRecv := 0
var pingTime float32 = 0.0
for !ipCanConnect {
pingRecvCurrent, pingTimeCurrent := checkConnection(ip)
if pingRecvCurrent != 0 {
ipCanConnect = true
pingRecv = pingRecvCurrent
pingTime = pingTimeCurrent
} else {
ip.IP[15]++
if ip.IP[15] == 0 {
break
}
break
}
}
if ipCanConnect {
progressHandler(AvailableIPFound)
for i := failTime; i < pingCount; i++ {
pingSuccess, pingTimeCurrent := tcping(ip)
progressHandler(NormalPing)
if pingSuccess {
pingRecv++
pingTime += pingTimeCurrent
}
}
return true, pingRecv, pingTime / float32(pingRecv), ip
} else {
progressHandler(NoAvailableIPFound)
return false, 0, 0, net.IPAddr{}
}
}
func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) {
defer wg.Done()
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, pingCount, progressHandler)
if success {
mutex.Lock()
var cfdata CloudflareIPData
cfdata.ip = currentIP
cfdata.pingReceived = pingRecv
cfdata.pingTime = pingTimeAvg
cfdata.pingCount = pingCount
*csv = append(*csv, cfdata)
mutex.Unlock()
}
<-control
}
func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) {
return func(ctx context.Context, network, address string) (net.Conn, error) {
c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
return c, e
}
}
//bool : can download,float32 downloadSpeed
func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
var client = http.Client{
Transport: nil,
CheckRedirect: nil,
Jar: nil,
Timeout: 0,
}
client.Transport = &http.Transport{
DialContext: GetDialContextByAddr(ip.String() + ":443"),
}
response, err := client.Get(url)
if err != nil {
return false, 0
} else {
defer func() { _ = response.Body.Close() }()
if response.StatusCode == 200 {
timeStart := time.Now()
timeEnd := timeStart.Add(downloadTestTime)
contentLength := response.ContentLength
buffer := make([]byte, downloadBufferSize)
var contentRead int64 = 0
var timeSlice = downloadTestTime / 100
var timeCounter = 1
var lastContentRead int64 = 0
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
e := ewma.NewMovingAverage()
for ; contentLength != contentRead; {
var currentTime = time.Now()
if currentTime.After(nextTime) {
timeCounter += 1
nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
e.Add(float64(contentRead - lastContentRead))
lastContentRead = contentRead
}
if currentTime.After(timeEnd) {
break
}
bufferRead, err := response.Body.Read(buffer)
contentRead += int64(bufferRead)
if err != nil {
if err != io.EOF {
break
} else {
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
}
}
}
return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 100)
} else {
return false, 0
}
}
}