2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-20 07:05:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2022-05-23 01:20:40 +00:00
|
|
|
|
|
|
|
"git.tcp.direct/kayos/common/entropy"
|
2021-09-20 07:05:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Socks5Str gets a SOCKS5 proxy that we have fully verified (dialed and then retrieved our IP address from a what-is-my-ip endpoint.
|
|
|
|
// Will block if one is not available!
|
|
|
|
func (s *Swamp) Socks5Str() string {
|
|
|
|
for {
|
|
|
|
select {
|
2021-09-21 12:24:30 +00:00
|
|
|
case sock := <-s.ValidSocks5:
|
2021-09-25 03:26:57 +00:00
|
|
|
if !s.stillGood(sock) {
|
2021-09-20 07:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-05-23 01:20:40 +00:00
|
|
|
s.stats.dispense()
|
2021-09-20 07:05:21 +00:00
|
|
|
return sock.Endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socks4Str gets a SOCKS4 proxy that we have fully verified.
|
|
|
|
// Will block if one is not available!
|
|
|
|
func (s *Swamp) Socks4Str() string {
|
2022-05-23 01:20:40 +00:00
|
|
|
defer s.stats.dispense()
|
2021-09-20 07:05:21 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-09-21 12:24:30 +00:00
|
|
|
case sock := <-s.ValidSocks4:
|
2021-09-25 03:26:57 +00:00
|
|
|
if !s.stillGood(sock) {
|
2021-09-20 07:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return sock.Endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socks4aStr gets a SOCKS4 proxy that we have fully verified.
|
|
|
|
// Will block if one is not available!
|
|
|
|
func (s *Swamp) Socks4aStr() string {
|
2022-05-23 01:20:40 +00:00
|
|
|
defer s.stats.dispense()
|
2021-09-20 07:05:21 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-09-21 12:24:30 +00:00
|
|
|
case sock := <-s.ValidSocks4a:
|
2021-09-25 03:26:57 +00:00
|
|
|
if !s.stillGood(sock) {
|
2021-09-20 07:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return sock.Endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
// GetHTTPTunnel checks for an available HTTP CONNECT proxy in our pool.
|
|
|
|
// For now, this function does not loop forever like the GetAnySOCKS does.
|
|
|
|
// Alternatively it can be included within the for loop by passing true to GetAnySOCKS.
|
|
|
|
// If there is an HTTP proxy available, ok will be true. If not, it will return false without delay.
|
|
|
|
func (s *Swamp) GetHTTPTunnel() (p *Proxy, ok bool) {
|
|
|
|
select {
|
|
|
|
case httptunnel := <-s.ValidHTTP:
|
|
|
|
return httptunnel, true
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:05:21 +00:00
|
|
|
// GetAnySOCKS retrieves any version SOCKS proxy as a Proxy type
|
|
|
|
// Will block if one is not available!
|
2022-05-23 01:20:40 +00:00
|
|
|
// StateNew/Temporary: Pass a true boolean to this to also receive HTTP proxies.
|
|
|
|
func (s *Swamp) GetAnySOCKS(AcceptHTTP bool) *Proxy {
|
|
|
|
defer s.stats.dispense()
|
2021-09-20 07:05:21 +00:00
|
|
|
for {
|
2022-05-23 01:20:40 +00:00
|
|
|
var sock *Proxy
|
2021-09-20 07:05:21 +00:00
|
|
|
select {
|
2022-05-23 01:20:40 +00:00
|
|
|
case sock = <-s.ValidSocks4:
|
|
|
|
break
|
|
|
|
case sock = <-s.ValidSocks4a:
|
|
|
|
break
|
|
|
|
case sock = <-s.ValidSocks5:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
if !AcceptHTTP {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
continue
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
2022-05-23 01:20:40 +00:00
|
|
|
if httptun, htok := s.GetHTTPTunnel(); htok {
|
|
|
|
sock = httptun
|
|
|
|
break
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-23 01:20:40 +00:00
|
|
|
if s.stillGood(sock) {
|
|
|
|
return sock
|
|
|
|
}
|
|
|
|
continue
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:26:57 +00:00
|
|
|
func (s *Swamp) stillGood(sock *Proxy) bool {
|
|
|
|
for !atomic.CompareAndSwapUint32(&sock.lock, stateUnlocked, stateLocked) {
|
2022-05-23 01:20:40 +00:00
|
|
|
entropy.RandSleepMS(200)
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
defer atomic.StoreUint32(&sock.lock, stateUnlocked)
|
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
if atomic.LoadInt64(&sock.timesBad) > int64(s.GetRemoveAfter()) && s.GetRemoveAfter() != -1 {
|
2021-09-23 15:12:05 +00:00
|
|
|
s.dbgPrint(red + "deleting from map (too many failures): " + sock.Endpoint + rst)
|
2021-09-20 08:49:06 +00:00
|
|
|
if err := s.swampmap.delete(sock.Endpoint); err != nil {
|
2021-10-23 21:30:31 +00:00
|
|
|
s.dbgPrint(red + err.Error() + rst)
|
2021-09-20 08:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:05:21 +00:00
|
|
|
if s.badProx.Peek(sock) {
|
|
|
|
s.dbgPrint(ylw + "badProx dial ratelimited: " + sock.Endpoint + rst)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
if time.Since(sock.lastValidated) > s.swampopt.stale {
|
2021-09-20 07:05:21 +00:00
|
|
|
s.dbgPrint("proxy stale: " + sock.Endpoint)
|
2022-05-23 01:20:40 +00:00
|
|
|
go s.stats.stale()
|
2021-09-20 07:05:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|