prox5/dispense.go

134 lines
3.1 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
import (
"strconv"
"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
default:
count := s.recycling()
if count > 0 {
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
}
time.Sleep(1 * time.Second)
}
}
}
// 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
default:
count := s.recycling()
if count > 0 {
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
}
time.Sleep(1 * time.Second)
}
}
}
// 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
default:
count := s.recycling()
if count > 0 {
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
}
time.Sleep(1 * time.Second)
}
}
}
// 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-11-23 12:08:22 +00:00
// s.dbgPrint(red + "no valid proxies in channels, sleeping" + rst)
count := s.recycling()
if count > 0 {
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
}
time.Sleep(1 * time.Second)
}
}
}
func (s *Swamp) stillGood(sock *Proxy) bool {
for !atomic.CompareAndSwapUint32(&sock.lock, stateUnlocked, stateLocked) {
randSleep()
}
defer atomic.StoreUint32(&sock.lock, stateUnlocked)
2021-10-27 03:08:23 +00:00
if sock.timesBad.Load().(int) > 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
}
}
if s.badProx.Peek(sock) {
s.dbgPrint(ylw + "badProx dial ratelimited: " + sock.Endpoint + rst)
return false
}
2022-05-23 01:05:50 +00:00
if s.swampopt.stale.Load().(time.Duration) == 0 {
return true
}
since := time.Since(sock.lastValidated.Load().(time.Time))
if since > s.swampopt.stale.Load().(time.Duration) {
s.dbgPrint("proxy stale: " + sock.Endpoint)
go s.Stats.stale()
return false
}
return true
}