Repository navigation
Expand file tree
/
Copy pathproxy.go
More file actions
83 lines (74 loc) · 1.48 KB
/
Copy pathproxy.go
File metadata and controls
83 lines (74 loc) · 1.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
package main
import (
"bufio"
"os"
"strings"
)
type ProxyPool struct {
proxies []string
tokens chan string
}
func LoadProxies(path string) *ProxyPool {
p := &ProxyPool{}
f, err := os.Open(path)
if err != nil {
return p
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
line = strings.TrimPrefix(line, "\ufeff")
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}
if !strings.Contains(line, "://") {
line = "http://" + line
}
p.proxies = append(p.proxies, line)
}
if len(p.proxies) > 0 {
p.tokens = make(chan string, len(p.proxies))
for _, px := range p.proxies {
p.tokens <- px
}
}
return p
}
func (p *ProxyPool) Len() int {
return len(p.proxies)
}
// Acquire blocks sampai ada proxy bebas (1 koneksi per proxy).
// Return "" kalau mode direct (tanpa proxy).
func (p *ProxyPool) Acquire() string {
if p.tokens == nil {
return ""
}
return <-p.tokens
}
func (p *ProxyPool) Release(proxy string) {
if proxy == "" || p.tokens == nil {
return
}
select {
case p.tokens <- proxy:
default:
}
}
func proxyKey(proxy string) string {
if proxy == "" {
return "direct"
}
if i := strings.LastIndex(proxy, "@"); i >= 0 && i+1 < len(proxy) {
return proxy[i+1:]
}
return proxy
}
func proxyVia(proxy string) string {
k := proxyKey(proxy)
if strings.Contains(k, " ") || strings.HasPrefix(k, "#") {
return "direct"
}
return k
}