-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_checker.go
255 lines (212 loc) · 6.76 KB
/
proxy_checker.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/schollz/progressbar/v3"
"gopkg.in/ini.v1"
)
// Config struct to store settings from proxy.config.ini
type Config struct {
ProxyList string
Protocol string
URL string
ValidStr string
Timeout time.Duration
Threads int
}
// Load configuration from proxy.config.ini
func loadConfig() (*Config, error) {
cfg, err := ini.Load("proxy.config.ini")
if err != nil {
return nil, fmt.Errorf("failed to read proxy.config.ini: %v", err)
}
config := &Config{
ProxyList: cfg.Section("config").Key("proxy_list").MustString("proxies.txt"),
Protocol: cfg.Section("config").Key("protocol").MustString("socks5"),
URL: cfg.Section("config").Key("url").MustString("https://example.com"),
ValidStr: cfg.Section("config").Key("valid_string").MustString("Success"),
Timeout: time.Duration(cfg.Section("config").Key("timeout").MustInt(5000)) * time.Millisecond,
Threads: cfg.Section("config").Key("threads").MustInt(10),
}
return config, nil
}
// Prompt user for input with a default value
func promptInput(prompt, defaultValue string) string {
fmt.Printf("%s (Press Enter for default: %s): ", prompt, defaultValue)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := strings.TrimSpace(scanner.Text())
if input == "" {
return defaultValue
}
return input
}
// Prompt user to choose a protocol (Silently includes 4 & 5 as Socks4 and Socks5)
func promptProtocol(defaultProtocol string) string {
fmt.Println("\nChoose Proxy Protocol:")
fmt.Println("1. HTTPS")
fmt.Println("2. SOCKS4")
fmt.Println("3. SOCKS5")
protocols := map[string]string{
"1": "https",
"2": "socks4",
"3": "socks5",
"4": "socks4",
"5": "socks5",
}
choice := promptInput("Enter choice (1-3)", defaultProtocol)
if val, exists := protocols[choice]; exists {
return val
}
fmt.Println("Invalid choice, using default:", defaultProtocol)
return defaultProtocol
}
// Fetch proxies from a URL if a URL is given
func fetchProxiesFromURL(proxyURL string) ([]string, error) {
resp, err := http.Get(proxyURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return extractProxies(string(body)), nil
}
// Extract proxies or URLs from a file or string content
func extractProxies(content string) []string {
lines := strings.Split(content, "\n")
proxySet := make(map[string]struct{})
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
proxySet[line] = struct{}{}
}
}
uniqueProxies := make([]string, 0, len(proxySet))
for proxy := range proxySet {
uniqueProxies = append(uniqueProxies, proxy)
}
return uniqueProxies
}
// Read proxies from a file or fetch from URL
func readProxies(filePath string) ([]string, error) {
if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") {
return fetchProxiesFromURL(filePath)
}
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
proxies := extractProxies(string(data))
return proxies, nil
}
// Validate proxy by sending request through it
func checkProxy(proxy, protocol, targetURL, validString string, timeout time.Duration, results chan<- string, validCount *int, wg *sync.WaitGroup) {
defer wg.Done()
proxyURL := fmt.Sprintf("%s://%s", protocol, proxy)
proxyParsed, err := url.Parse(proxyURL)
if err != nil {
return
}
transport := &http.Transport{Proxy: http.ProxyURL(proxyParsed)}
client := &http.Client{Transport: transport, Timeout: timeout}
start := time.Now() // Start measuring response time
resp, err := client.Get(targetURL)
if err != nil {
return
}
defer resp.Body.Close()
responseTime := time.Since(start).Milliseconds() // Calculate response time
body := make([]byte, 1024)
n, _ := resp.Body.Read(body)
responseBody := string(body[:n])
if strings.Contains(responseBody, validString) {
results <- fmt.Sprintf("%s|%dms", proxy, responseTime) // Send proxy with response time
*validCount++
}
}
// Save valid proxies to file (without response time)
func saveValidProxy(protocol, proxy string) error {
os.MkdirAll("results", os.ModePerm)
file, err := os.OpenFile(fmt.Sprintf("results/%s.valid.txt", protocol), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(proxy + "\n")
return err
}
// Display valid proxy in green and save it
func handleValidProxy(protocol, proxyWithTime string) {
parts := strings.Split(proxyWithTime, "|")
proxy := parts[0]
responseTime := parts[1]
fmt.Printf("\033[32m[%s] %s\033[0m\n", responseTime, proxy) // Green text with response time
saveValidProxy(protocol, proxy)
}
// Function to handle graceful exit
func handleExit(results chan string, validCount *int) {
go func() {
for proxy := range results {
handleValidProxy("socks5", proxy)
}
fmt.Printf("\nSaved %d valid proxies to file\n", *validCount)
}()
}
// Main function
func main() {
// Print bright blue startup message
fmt.Println("\033[94mProxy Checker | Fast and Efficient | Written in GO\033[0m")
fmt.Println("\033[94mBuild Date: Feburary 16 2025...\033[0m\n")
// Load configuration
config, err := loadConfig()
if err != nil {
fmt.Println("Error loading config:", err)
return
}
// Get user inputs with defaults
proxyFile := promptInput("Enter proxy list file path", config.ProxyList)
protocol := promptProtocol(config.Protocol)
threadCount := promptInput("Enter number of threads", fmt.Sprintf("%d", config.Threads))
// Convert thread count to int
threads := config.Threads
if t, err := fmt.Sscanf(threadCount, "%d", &threads); err != nil || t != 1 {
fmt.Println("Invalid thread count, using default:", config.Threads)
threads = config.Threads
}
// Read proxy list
proxies, err := readProxies(proxyFile)
if err != nil {
fmt.Println("Error reading proxy list:", err)
return
}
// Setup worker pool and progress bar
var wg sync.WaitGroup
results := make(chan string, len(proxies))
bar := progressbar.NewOptions(len(proxies), progressbar.OptionSetWidth(10))
validCount := 0
handleExit(results, &validCount)
sem := make(chan struct{}, threads)
for _, proxy := range proxies {
wg.Add(1)
sem <- struct{}{}
go func(proxy string) {
defer func() { <-sem }()
checkProxy(proxy, protocol, config.URL, config.ValidStr, config.Timeout, results, &validCount, &wg)
bar.Add(1)
}(proxy)
}
wg.Wait()
close(results)
bar.Finish()
fmt.Printf("\nTotal Online proxies found: %d\n", validCount)
}