prox5/dispense.go

107 lines
2.3 KiB
Go
Raw Normal View History

2021-10-09 17:23:13 +00:00
package Prox5
import (
"sync/atomic"
"time"
)
// 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:
if !s.stillGood(sock) {
continue
}
s.Stats.dispense()
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 {
defer s.Stats.dispense()
for {
select {
2021-09-21 12:24:30 +00:00
case sock := <-s.ValidSocks4:
if !s.stillGood(sock) {
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 {
defer s.Stats.dispense()
for {
select {
2021-09-21 12:24:30 +00:00
case sock := <-s.ValidSocks4a:
if !s.stillGood(sock) {
continue
}
return sock.Endpoint
}
}
}
// GetAnySOCKS retrieves any version SOCKS proxy as a Proxy type
// Will block if one is not available!
func (s *Swamp) GetAnySOCKS() *Proxy {
defer s.Stats.dispense()
for {
select {
2021-09-21 12:24:30 +00:00
case sock := <-s.ValidSocks4:
if s.stillGood(sock) {
return sock
}
continue
2021-09-21 12:24:30 +00:00
case sock := <-s.ValidSocks4a:
if s.stillGood(sock) {
return sock
}
continue
2021-09-21 12:24:30 +00:00
case sock := <-s.ValidSocks5:
if s.stillGood(sock) {
return sock
}
continue
default:
2021-09-28 06:25:00 +00:00
s.dbgPrint(red + "no valid proxies in channels, sleeping" + rst)
time.Sleep(10 * time.Second)
}
}
}
func (s *Swamp) stillGood(sock *Proxy) bool {
for !atomic.CompareAndSwapUint32(&sock.lock, stateUnlocked, stateLocked) {
randSleep()
}
defer atomic.StoreUint32(&sock.lock, stateUnlocked)
2021-09-24 19:07:56 +00:00
if sock.timesBad.Load().(int) > s.GetRemoveAfter() {
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
}
}
if s.badProx.Peek(sock) {
s.dbgPrint(ylw + "badProx dial ratelimited: " + sock.Endpoint + rst)
return false
}
if time.Since(sock.lastValidated.Load().(time.Time)) > s.swampopt.stale.Load().(time.Duration) {
s.dbgPrint("proxy stale: " + sock.Endpoint)
go s.Stats.stale()
return false
}
return true
}