-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (142 loc) · 3.53 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
package main
import (
"fmt"
"os"
"encoding/csv"
"io"
"net/url"
"net/http"
"strconv"
"time"
"flag"
"log"
"crypto/tls"
. "github.com/logrusorgru/aurora"
)
type Proxy struct {
ip string
port string
}
type CheckedProxy struct {
Proxy
httpCode int
responseDuration float32
err string
}
func main() {
start := time.Now()
var source string
flag.StringVar(&source, "source", "", "Path to CSV file with proxies to check")
var threads int
flag.IntVar(&threads, "threads", 5, "Number of threads")
var output string
flag.StringVar(&output, "output", "", "Folder, result file will be stored to")
var testUrl string
flag.StringVar(&testUrl, "url", "", "URL to test proxy")
var timeout int
flag.IntVar(&timeout, "timeout", 5, "Timeout per request, s")
var verbose bool
flag.BoolVar(&verbose, "verbose", false, "Log output")
flag.Parse()
fmt.Println("source:", source)
fmt.Println("threads:", threads)
fmt.Println("output:", output)
fmt.Println("url:", testUrl)
fmt.Println("timeout:", timeout)
fmt.Println("verbose:", verbose)
proxies, _ := loadProxies(source)
fmt.Println("Proxies to check", len(proxies))
toCheck := make(chan Proxy)
go func() {
for k := range proxies {
toCheck <- *proxies[k]
}
}()
checked := make(chan CheckedProxy)
for i := 0; i < threads; i++ {
go func() {
for p := range toCheck {
checkedP := checkProxy(p, timeout, testUrl, verbose)
checked <- checkedP
}
}()
}
checkedFile := output + "/" + "checked_proxies.csv"
f, err := os.Create(checkedFile)
if err != nil {
panic(err)
}
defer f.Close()
w := csv.NewWriter(f)
w.Write([]string{"IP", "Port", "HTTP Code", "Response Duration", "Error message"})
for i := 0; i < len(proxies); i++ {
cP := <-checked
record := []string{cP.ip, cP.port, strconv.Itoa(cP.httpCode), fmt.Sprintf("%f", cP.responseDuration), cP.err}
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
if err := w.Error(); err != nil {
log.Fatal(err)
}
w.Flush()
elapsed := time.Now().Sub(start)
fmt.Println("Checked file: " + checkedFile)
fmt.Println(Green("Finished in " + fmt.Sprintf("%f", elapsed.Seconds()) + " sec"))
}
func loadProxies(file string) (map[int]*Proxy, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
csvf := csv.NewReader(f)
proxies := map[int]*Proxy{}
idx := -1
for {
idx++
row, err := csvf.Read()
if idx == 0 {
continue
}
if err != nil {
if err == io.EOF {
err = nil
}
return proxies, err
}
p := &Proxy{}
p.ip = row[0]
p.port = row[1]
proxies[idx] = p
}
}
func checkProxy(p Proxy, timeout int, tUrl string, verbose bool) (CheckedProxy) {
start := time.Now()
testUrl, _ := url.Parse(tUrl)
proxyURL, _ := url.Parse("http://" + p.ip + ":" + p.port)
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: time.Duration(time.Duration(timeout) * time.Second),
}
request, _ := http.NewRequest("HEAD", testUrl.String(), nil)
response, err := client.Do(request)
elapsed := time.Now().Sub(start)
code := 0
errMsg := ""
if err != nil {
code = 0
errMsg = err.Error()
} else {
code = response.StatusCode
errMsg = ""
}
if verbose {
fmt.Println(p.ip+":"+p.port+", Code "+strconv.Itoa(code)+" elapsed "+fmt.Sprintf("%f", elapsed.Seconds())+" sec", Red(" "+errMsg))
}
return CheckedProxy{p, code, float32(elapsed.Seconds()), errMsg}
}