2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-20 07:05:21 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-03 13:43:51 +00:00
|
|
|
"strconv"
|
2021-09-20 07:05:21 +00:00
|
|
|
"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:
|
2021-09-25 03:26:57 +00:00
|
|
|
if !s.stillGood(sock) {
|
2021-09-20 07:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.Stats.dispense()
|
|
|
|
return sock.Endpoint
|
2022-09-03 13:43:51 +00:00
|
|
|
default:
|
|
|
|
count := s.recycling()
|
|
|
|
if count > 0 {
|
|
|
|
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socks4Str gets a SOCKS4 proxy that we have fully verified.
|
|
|
|
// Will block if one is not available!
|
|
|
|
func (s *Swamp) Socks4Str() string {
|
2021-10-23 14:58:03 +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
|
2022-09-03 13:43:51 +00:00
|
|
|
default:
|
|
|
|
count := s.recycling()
|
|
|
|
if count > 0 {
|
|
|
|
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socks4aStr gets a SOCKS4 proxy that we have fully verified.
|
|
|
|
// Will block if one is not available!
|
|
|
|
func (s *Swamp) Socks4aStr() string {
|
2021-10-23 14:58:03 +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-09-03 13:43:51 +00:00
|
|
|
default:
|
|
|
|
count := s.recycling()
|
|
|
|
if count > 0 {
|
|
|
|
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
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!
|
2021-10-23 14:58:03 +00:00
|
|
|
func (s *Swamp) GetAnySOCKS() *Proxy {
|
|
|
|
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-10-23 14:58:03 +00:00
|
|
|
if s.stillGood(sock) {
|
|
|
|
return sock
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
continue
|
2021-09-21 12:24:30 +00:00
|
|
|
case sock := <-s.ValidSocks4a:
|
2021-10-23 14:58:03 +00:00
|
|
|
if s.stillGood(sock) {
|
|
|
|
return sock
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
continue
|
2021-09-21 12:24:30 +00:00
|
|
|
case sock := <-s.ValidSocks5:
|
2021-10-23 14:58:03 +00:00
|
|
|
if s.stillGood(sock) {
|
|
|
|
return sock
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
default:
|
2021-11-23 12:08:22 +00:00
|
|
|
// s.dbgPrint(red + "no valid proxies in channels, sleeping" + rst)
|
2022-09-03 13:43:51 +00:00
|
|
|
count := s.recycling()
|
|
|
|
if count > 0 {
|
|
|
|
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
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) {
|
2021-10-23 14:58:03 +00:00
|
|
|
randSleep()
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: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) {
|
2021-09-20 07:05:21 +00:00
|
|
|
s.dbgPrint("proxy stale: " + sock.Endpoint)
|
|
|
|
go s.Stats.stale()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|