prox5/mystery_dialer.go

46 lines
1.3 KiB
Go
Raw Normal View History

2021-10-09 17:23:13 +00:00
package Prox5
import (
2021-09-14 01:50:39 +00:00
"context"
2021-09-28 06:25:00 +00:00
"errors"
"fmt"
2021-10-09 20:45:30 +00:00
"h12.io/socks"
"net"
2021-09-29 08:31:19 +00:00
"strconv"
)
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)
}
// MysteryDialer is a dialer function that will use a different proxy for every request.
func (s *Swamp) MysteryDialer(ctx context.Context, network, addr string) (net.Conn, error) {
var sock Proxy
2021-09-24 16:38:57 +00:00
var socksString string
var conn net.Conn
2021-09-28 06:25:00 +00:00
var count int
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
for {
2021-09-29 08:31:19 +00:00
max := s.GetDialerBailout()
if count > max {
return nil, errors.New("giving up after " + strconv.Itoa(max) + " tries")
2021-09-28 06:25:00 +00:00
}
if err := ctx.Err(); err != nil {
return nil, err
}
sock = s.GetAnySOCKS()
2021-09-24 16:38:57 +00:00
s.dbgPrint("dialer trying: " + sock.Endpoint + "...")
var err error
socksString = fmt.Sprintf("socks%s://%s?timeout=%ss", sock.GetProto(), sock.Endpoint, s.GetTimeoutSecondsStr())
2021-09-24 16:38:57 +00:00
dialSocks := socks.Dial(socksString)
if conn, err = dialSocks(network, addr); err != nil {
2021-09-28 06:25:00 +00:00
count++
s.dbgPrint(ylw + "unable to reach [redacted] with " + socksString + ", cycling..." + rst)
2021-09-24 16:38:57 +00:00
continue
}
break
}
2021-09-24 19:07:56 +00:00
s.dbgPrint(grn + "MysteryDialer using socks: " + socksString + rst)
return conn, nil
}