/
main.go
196 lines (158 loc) · 5.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"io/ioutil"
"strings"
"net/http"
"net/url"
"runtime"
"sync"
"sort"
)
type ProxyStatus struct {
ok bool
proxy string
errorMessage string
downloadedUrl string
}
type ProxyStatuses []*ProxyStatus
func (s ProxyStatuses) Len() int { return len(s) }
func (s ProxyStatuses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByAddress struct{ ProxyStatuses }
func (s ByAddress) Less(i, j int) bool { return s.ProxyStatuses[i].proxy < s.ProxyStatuses[j].proxy }
var proxyCheckWaitGroup sync.WaitGroup
var proxyStatuses ProxyStatuses
var getsInProgress = make(chan int, 15) // Max. number of simultaneous requests
func (status ProxyStatus) String() string {
var output string
if (status.ok) { output += "OK" } else { output += "ERROR" }
output += " " + status.proxy
output += " " + status.downloadedUrl
if (!status.ok) { output += " " + status.errorMessage }
return output
}
func checkProxy(proxy string, downloadedUrl string) (success bool, errorMessage string) {
getsInProgress <- 1
defer func() { <- getsInProgress }()
if !strings.HasPrefix(proxy, "http") { proxy = "http://" + proxy }
proxyUrl, err := url.Parse(proxy)
httpClient := &http.Client { Transport: &http.Transport { Proxy: http.ProxyURL(proxyUrl) } }
response, err := httpClient.Get(downloadedUrl)
if err != nil { return false, err.Error() }
body, err := ioutil.ReadAll(response.Body)
if err != nil { return false, err.Error() }
bodyString := strings.ToLower(strings.Trim(string(body), " \n\t\r"))
if strings.Index(bodyString, "<body") < 0 && strings.Index(bodyString, "<head") < 0 {
if strings.Index(bodyString, "<title>invalid request</title>") >= 0 {
return false, "Tracker responsed 'Invalid request' - might be dead"
} else {
return false, "Reveived page is not HTML: " + bodyString
}
}
return true, ""
}
func asyncCheckProxy(proxyInfoChan chan ProxyStatus, proxy string, downloadedUrl string) {
success, errorMessage := checkProxy(proxy, downloadedUrl)
var info ProxyStatus
info.proxy = proxy
info.ok = success
info.errorMessage = errorMessage
info.downloadedUrl = downloadedUrl
proxyInfoChan <- info
}
func checkResults(proxyInfoChan chan ProxyStatus) {
for {
status := <- proxyInfoChan
proxyStatuses = append(proxyStatuses, &status)
if status.ok { fmt.Println(status) }
proxyCheckWaitGroup.Done()
}
}
func parseSamairProxies() []string {
content, err := ioutil.ReadFile("proxy.txt")
if err != nil {
panic(err.Error())
}
lines := strings.Split(string(content), "\n")
var proxies[] string
for i := 0; i < len(lines); i++ {
var line = strings.Trim(lines[i], " \t\n\r")
if len(line) == 0 { continue }
if line[0] == '#' { continue }
var tokens = strings.Split(line, " ")
if len(tokens) <= 0 { continue }
var proxy = strings.Trim(tokens[0], " \t\n\r")
if len(proxy) < 7 { continue }
proxies = append(proxies, proxy)
}
return proxies
}
func parseHmaProxies() []string {
content, err := ioutil.ReadFile("hma_proxies.txt")
if err != nil {
panic(err.Error())
}
lines := strings.Split(string(content), "\n")
var proxies[] string
proxy := ""
for i := 0; i < len(lines); i++ {
var line = strings.Trim(lines[i], " \t\n\r")
if i % 2 == 1 {
var tokens = strings.Split(line, "\t")
var protocol string
if len(tokens) >= 1 {
protocol = strings.ToLower(tokens[0])
} else {
protocol = "http"
}
if protocol != "http" && protocol != "https" { continue }
proxy = protocol + "://" + proxy
proxies = append(proxies, proxy)
continue
}
if len(line) == 0 { continue }
var tokens = strings.Split(line, "\t")
if len(tokens) < 3 { continue }
proxy = strings.Trim(tokens[1], " \t\n\r")
proxy += ":" + strings.Trim(tokens[2], " \t\n\r")
if len(proxy) < 7 { continue }
}
return proxies
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
proxies := parseSamairProxies()
fmt.Println("# Checking", len(proxies), "proxies")
content, err := ioutil.ReadFile("trackers.all.txt")
if err != nil {
panic(err.Error())
}
lines := strings.Split(string(content), "\n")
var trackers[] string
for i := 0; i < len(lines); i++ {
var line = lines[i]
var tokens = strings.Split(line, " ")
if len(tokens) <= 0 { continue }
var tracker = strings.Trim(tokens[0], " \t\n\r")
if len(tracker) < 7 { continue }
if strings.Index(tracker, "udp://") >= 0 { continue; } // UDP not supported by HTTP client
trackers = append(trackers, tracker)
}
proxyInfoChan := make(chan ProxyStatus, 10)
for i := 0; i < len(proxies); i++ {
proxy := proxies[i]
for j := 0; j < len(trackers); j++ {
proxyCheckWaitGroup.Add(1)
go asyncCheckProxy(proxyInfoChan, proxy, trackers[j])
}
}
go checkResults(proxyInfoChan)
proxyCheckWaitGroup.Wait()
sort.Sort(ByAddress{proxyStatuses})
fmt.Println("==================================================")
for i := 0; i < len(proxyStatuses); i++ {
status := proxyStatuses[i]
if !status.ok { continue }
fmt.Println(status)
}
}