-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
90 lines (77 loc) · 2.48 KB
/
Copy pathproxy.go
File metadata and controls
90 lines (77 loc) · 2.48 KB
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
// Package proxy fetches free HTTP, SOCKS4, and SOCKS5 proxy lists
// from the ProxyScrape API with automatic gzip decompression.
package proxy
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/klauspost/compress/gzhttp"
)
// Proxy describes a single proxy server with its connection details and health metrics.
type Proxy struct {
Host string `json:"host"`
Port int `json:"port"`
Country string `json:"country"`
Timeout float64 `json:"timeout"`
LastSeen float64 `json:"last_seen"`
Uptime float64 `json:"uptime"`
AliveSince float64 `json:"alive_since"`
}
// Address returns the host:port string for this proxy.
func (p *Proxy) Address() string {
return fmt.Sprintf("%s:%d", p.Host, p.Port)
}
type entry map[string]*Proxy
// Proxies holds proxy lists grouped by protocol type.
type Proxies struct {
HTTP entry `json:"http"`
SOCKS4 entry `json:"socks4"`
SOCKS5 entry `json:"socks5"`
}
// Len returns the total number of proxies across all protocols.
func (receiver *Proxies) Len() int {
return len(receiver.HTTP) + len(receiver.SOCKS4) + len(receiver.SOCKS5)
}
// List returns all HTTP proxies as a flat slice with host and port populated.
func (receiver *Proxies) List() (l []*Proxy, err error) {
for k, v := range receiver.HTTP {
a := strings.Split(k, ":")
v.Port, err = strconv.Atoi(a[1])
if err != nil {
return
}
v.Host = a[0]
l = append(l, v)
}
return
}
// Fetch retrieves the current proxy list from the ProxyScrape API.
func Fetch() (l *Proxies, err error) {
client := &http.Client{
Transport: gzhttp.Transport(http.DefaultTransport),
}
req, err := http.NewRequest("GET", "https://api.proxyscrape.com/proxytable.php?nf=true&country=all", nil)
if err != nil {
return nil, fmt.Errorf("proxy: failed to create request: %w", err)
}
req.Header.Set("accept", "*/*")
req.Header.Set("origin", "https://www.proxyscrape.com")
req.Header.Set("referer", "https://www.proxyscrape.com/")
req.Header.Set("user-agent", "Mozilla/5.0 (compatible; go-proxy/1.0)")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("proxy: request failed: %w", err)
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&l); err != nil {
return nil, fmt.Errorf("proxy: failed to decode response: %w", err)
}
return
}
// List is a convenience function that fetches and returns all HTTP proxies.
// Deprecated: Use Fetch() and Proxies.List() for more control.
func List() (l *Proxies, err error) {
return Fetch()
}