-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmystery_dialer.go
186 lines (171 loc) · 4.65 KB
/
mystery_dialer.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
package prox5
import (
"context"
"fmt"
"io"
"net"
"os"
"sync/atomic"
"time"
"git.tcp.direct/kayos/socks"
)
// DialContext is a simple stub adapter to implement a net.Dialer.
func (p5 *ProxyEngine) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return p5.mysteryDialer(ctx, network, addr)
}
// Dial is a simple stub adapter to implement a net.Dialer.
func (p5 *ProxyEngine) Dial(network, addr string) (net.Conn, error) {
return p5.mysteryDialer(context.Background(), network, addr)
}
// DialTimeout is a simple stub adapter to implement a net.Dialer with a timeout.
func (p5 *ProxyEngine) DialTimeout(network, addr string, timeout time.Duration) (net.Conn, error) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(timeout))
defer cancel()
nc, err := p5.mysteryDialer(ctx, network, addr)
return nc, err
}
func (p5 *ProxyEngine) addTimeout(socksString string) string {
tout := strs.Get()
tout.MustWriteString(socksString)
tout.MustWriteString("?timeout=")
tout.MustWriteString(p5.GetServerTimeoutStr())
_, _ = tout.WriteRune('s')
socksString = tout.String()
strs.MustPut(tout)
return socksString
}
func (p5 *ProxyEngine) isEmpty() bool {
if p5.GetStatistics().Checked.Load() == 0 {
return true
}
// if stats.Valid5.Load()+stats.Valid4.Load()+stats.Valid4a.Load()+stats.ValidHTTP.Load() == 0 {
if p5.GetTotalValidated() == 0 {
return true
}
return false
}
var ErrNoProxies = fmt.Errorf("no proxies available")
func (p5 *ProxyEngine) popSockAndLockIt(ctx context.Context) (*Proxy, error) {
if p5.isEmpty() {
p5.scale()
return nil, ErrNoProxies
}
sock := p5.GetAnySOCKS()
select {
case <-ctx.Done():
return nil, fmt.Errorf("context done: %w", ctx.Err())
default:
//
}
if sock == nil {
return nil, nil
}
if atomic.CompareAndSwapUint32(&sock.lock, stateUnlocked, stateLocked) {
// p5.msgGotLock(socksString)
return sock, nil
}
switch sock.GetProto() {
case ProtoSOCKS5:
p5.Valids.SOCKS5.add(sock)
case ProtoSOCKS4:
p5.Valids.SOCKS4.add(sock)
case ProtoSOCKS4a:
p5.Valids.SOCKS4a.add(sock)
case ProtoHTTP:
p5.Valids.HTTP.add(sock)
default:
return nil, fmt.Errorf("unknown protocol: %s", sock.GetProto())
}
return nil, nil
}
func (p5 *ProxyEngine) announceDial(network, addr string) {
s := strs.Get()
s.MustWriteString("prox5 dialing: ")
s.MustWriteString(network)
s.MustWriteString("://")
if p5.opt.redact {
s.MustWriteString("[redacted]")
} else {
s.MustWriteString(addr)
}
s.MustWriteString(addr)
s.MustWriteString("...")
p5.dbgPrint(s)
}
// mysteryDialer is a dialer function that will use a different proxy for every request.
// If you're looking for this function, it has been unexported. Use Dial, DialTimeout, or DialContext instead.
func (p5 *ProxyEngine) mysteryDialer(ctx context.Context, network, addr string) (net.Conn, error) {
p5.announceDial(network, addr)
if p5.isEmpty() {
// p5.dbgPrint(simpleString("prox5: no proxies available"))
return nil, ErrNoProxies
}
timeout := time.NewTimer(p5.GetServerTimeout())
defer timeout.Stop()
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
var count = 0
for {
maxBail := p5.GetDialerBailout()
switch {
case count > maxBail:
return nil, fmt.Errorf("giving up after %d tries", maxBail)
case ctx.Err() != nil:
return nil, fmt.Errorf("context error: %w", ctx.Err())
default:
select {
case <-ctx.Done():
return nil, fmt.Errorf("context done: %w", ctx.Err())
case <-p5.ctx.Done():
return nil, fmt.Errorf("prox5 closed: %w", p5.ctx.Err())
case <-p5.conKiller:
return nil, fmt.Errorf("prox5 closed: %w", io.ErrClosedPipe)
case <-timeout.C:
return nil, fmt.Errorf("timeout: %w, %w", io.ErrClosedPipe, os.ErrDeadlineExceeded)
default:
}
}
var sock *Proxy
for {
if p5.scale() {
time.Sleep(5 * time.Millisecond)
}
var err error
sock, err = p5.popSockAndLockIt(ctx)
if err != nil {
// println(err.Error())
return nil, err
}
if sock != nil {
break
}
}
socksString := sock.String()
var ok bool
if sock, ok = p5.dispenseMiddleware(sock); !ok {
atomic.StoreUint32(&sock.lock, stateUnlocked)
p5.msgFailedMiddleware(socksString)
continue
}
p5.msgTry(socksString)
atomic.StoreUint32(&sock.lock, stateUnlocked)
dialSocks := socks.Dial(socksString)
conn, err := dialSocks(network, addr)
if err != nil {
count++
p5.msgUnableToReach(socksString, addr, err)
continue
}
p5.msgUsingProxy(socksString)
go func() {
select {
case <-ctx.Done():
_ = conn.Close()
case <-p5.conKiller:
_ = conn.Close()
case <-p5.ctx.Done():
_ = conn.Close()
}
}()
return conn, nil
}
}