2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-13 19:48:42 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-14 01:50:39 +00:00
|
|
|
"context"
|
2021-09-28 06:25:00 +00:00
|
|
|
"errors"
|
2021-09-13 19:48:42 +00:00
|
|
|
"net"
|
2021-09-29 08:31:19 +00:00
|
|
|
"strconv"
|
2021-10-23 14:58:03 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"h12.io/socks"
|
2021-09-13 19:48:42 +00:00
|
|
|
)
|
|
|
|
|
2022-07-09 18:36:45 +00:00
|
|
|
// DialContext is a simple stub adapter to implement a net.Dialer.
|
2021-09-28 06:25:00 +00:00
|
|
|
func (s *Swamp) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return s.MysteryDialer(ctx, network, addr)
|
|
|
|
}
|
|
|
|
|
2022-07-09 18:36:45 +00:00
|
|
|
// DialContext is a simple stub adapter to implement a net.Dialer.
|
|
|
|
func (s *Swamp) Dial(network, addr string) (net.Conn, error) {
|
|
|
|
return s.DialContext(context.Background(), network, addr)
|
2021-09-28 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 19:48:42 +00:00
|
|
|
// MysteryDialer is a dialer function that will use a different proxy for every request.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) MysteryDialer(ctx context.Context, network, addr string) (net.Conn, error) {
|
2022-05-23 01:20:40 +00:00
|
|
|
var (
|
|
|
|
socksString string
|
|
|
|
count int
|
|
|
|
)
|
2021-09-13 19:48:42 +00:00
|
|
|
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
|
|
|
|
for {
|
2022-06-26 02:51:42 +00:00
|
|
|
max := pe.GetDialerBailout()
|
2021-09-29 08:31:19 +00:00
|
|
|
if count > max {
|
|
|
|
return nil, errors.New("giving up after " + strconv.Itoa(max) + " tries")
|
2021-09-28 06:25:00 +00:00
|
|
|
}
|
2021-09-13 19:48:42 +00:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-23 01:20:40 +00:00
|
|
|
var sock *Proxy
|
|
|
|
for {
|
2022-06-26 02:51:42 +00:00
|
|
|
sock = pe.GetAnySOCKS(false)
|
2022-05-23 01:20:40 +00:00
|
|
|
if !atomic.CompareAndSwapUint32(&sock.lock, stateUnlocked, stateLocked) {
|
|
|
|
continue
|
2021-10-23 14:58:03 +00:00
|
|
|
}
|
2022-05-23 01:20:40 +00:00
|
|
|
break
|
2021-10-23 14:58:03 +00:00
|
|
|
}
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.dbgPrint("dialer trying: " + sock.Endpoint + "...")
|
2021-10-28 15:51:10 +00:00
|
|
|
tout := ""
|
2022-06-26 02:51:42 +00:00
|
|
|
if pe.GetServerTimeoutStr() != "-1" {
|
|
|
|
tout = "?timeout=" + pe.GetServerTimeoutStr() + "s"
|
2022-05-23 01:20:40 +00:00
|
|
|
}
|
|
|
|
socksString = "socks" + getProtoStr(sock.proto) + "://" + sock.Endpoint + tout
|
|
|
|
var ok bool
|
2022-06-26 02:51:42 +00:00
|
|
|
if sock, ok = pe.dispenseMiddleware(sock); !ok {
|
|
|
|
pe.dbgPrint("failed middleware check, " + socksString + ", cycling...")
|
2022-05-23 01:20:40 +00:00
|
|
|
continue
|
2021-10-28 15:51:10 +00:00
|
|
|
}
|
2021-10-23 14:58:03 +00:00
|
|
|
atomic.StoreUint32(&sock.lock, stateUnlocked)
|
2021-09-24 16:38:57 +00:00
|
|
|
dialSocks := socks.Dial(socksString)
|
2022-05-23 01:20:40 +00:00
|
|
|
conn, err := dialSocks(network, addr)
|
|
|
|
if err != nil {
|
2021-09-28 06:25:00 +00:00
|
|
|
count++
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.dbgPrint("unable to reach [redacted] with " + socksString + ", cycling...")
|
2021-09-24 16:38:57 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.dbgPrint("MysteryDialer using socks: " + socksString)
|
2022-05-23 01:20:40 +00:00
|
|
|
return conn, nil
|
2021-09-13 19:48:42 +00:00
|
|
|
}
|
|
|
|
}
|