Add: SetStaleTime, GetStaleTime, GetMaxWorkers

This commit is contained in:
kayos@tcp.direct 2021-09-13 09:32:07 -07:00
parent 39c93cf73a
commit d6cc41d957
5 changed files with 75 additions and 43 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) [year] [fullname]
Copyright (c) 2021 yung innanet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

14
defs.go
View File

@ -34,16 +34,16 @@ type Swamp struct {
var (
defaultStaleTime = 1 * time.Hour
defWorkers = 100
defaultChecks = []string{"https://wtfismyip.com/text", "https://myexternalip.com/raw", "https://ipinfo.io/ip"}
defaultChecks = []string{"https://wtfismyip.com/text", "https://myexternalip.com/raw", "https://ipinfo.io/ip"}
)
func defOpt() *SwampOptions {
return &SwampOptions{
UserAgents: DefaultUserAgents,
UserAgents: DefaultUserAgents,
CheckEndpoints: defaultChecks,
Stale: defaultStaleTime,
MaxWorkers: defWorkers,
Debug: false,
Stale: defaultStaleTime,
MaxWorkers: defWorkers,
Debug: false,
}
}
@ -63,8 +63,8 @@ type SwampOptions struct {
}
var (
useProx *rl.Limiter
badProx *rl.Limiter
useProx *rl.Limiter
badProx *rl.Limiter
)
// Proxy represents an individual proxy

View File

@ -1,8 +1,11 @@
package pxndscvm
import (
"fmt"
"context"
"net"
"time"
"h12.io/socks"
)
// Socks5Str gets a SOCKS5 proxy that we have fully verified (dialed and then retrieved our IP address from a what-is-my-ip endpoint.
@ -41,6 +44,7 @@ func (s *Swamp) Socks4aStr() string {
if !s.stillGood(sock) {
continue
}
s.Stats.dispense()
return sock.Endpoint
}
@ -73,7 +77,6 @@ func (s *Swamp) GetAnySOCKS() Proxy {
func (s *Swamp) stillGood(candidate Proxy) bool {
if time.Since(candidate.Verified) > s.swampopt.Stale {
s.dbgPrint("proxy stale: " + candidate.Endpoint)
fmt.Println("time since: ", time.Since(candidate.Verified))
return false
}
return true
@ -86,12 +89,62 @@ func (s *Swamp) RandomUserAgent() string {
return randStrChoice(s.swampopt.UserAgents)
}
// GetMysteryDialer will return a dialer that will use a different proxy for every request.
func (s *Swamp) GetMysteryDialer(ctx context.Context, network, addr string) (net.Conn, error) {
var sock Proxy
sock = Proxy{Endpoint: ""}
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
for {
if err := ctx.Err(); err != nil {
return nil, err
}
time.Sleep(10 * time.Millisecond)
candidate := s.GetAnySOCKS()
if !s.stillGood(candidate) {
continue
}
sock = candidate
if sock.Endpoint != "" {
break
}
}
if err := ctx.Err(); err != nil {
return nil, err
}
var dialSocks = socks.Dial("socks" + sock.Proto + "://" + sock.Endpoint + "?timeout=3s")
return dialSocks(network, addr)
}
// GetRandomEndpoint returns a random whatismyip style endpoint from our Swamp's options
func (s *Swamp) GetRandomEndpoint() string {
s.mu.RLock()
defer s.mu.RUnlock()
return randStrChoice(s.swampopt.CheckEndpoints)
}
// DebugEnabled will return the current state of our Debug switch
// GetStaleTime returns the duration of time after which a proxy will be considered "stale".
func (s *Swamp) GetStaleTime() time.Duration {
s.mu.RLock()
defer s.mu.RUnlock()
return s.swampopt.Stale
}
// DebugEnabled returns the current state of our Debug switch
func (s *Swamp) DebugEnabled() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.swampopt.Debug
}
// GetMaxWorkers returns maximum amount of workers that validate proxies concurrently. Note this is read-only during runtime.
func (s *Swamp) GetMaxWorkers() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.swampopt.MaxWorkers
}
// TODO: Implement ways to access worker pool (pond) statistics

31
main.go
View File

@ -3,7 +3,6 @@ package pxndscvm
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"errors"
"io"
@ -71,36 +70,6 @@ func (s *Swamp) feed() {
}
}
// MysteryDial will return a dialer that will use a different proxy for every request.
func (s *Swamp) MysteryDial(ctx context.Context, network, addr string) (net.Conn, error) {
var sock Proxy
sock = Proxy{Endpoint: ""}
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
for {
if err := ctx.Err(); err != nil {
return nil, err
}
time.Sleep(10 * time.Millisecond)
candidate := s.GetAnySOCKS()
if !s.stillGood(candidate) {
continue
}
sock = candidate
if sock.Endpoint != "" {
break
}
}
if err := ctx.Err(); err != nil {
return nil, err
}
var dialSocks = socks.Dial("socks" + sock.Proto + "://" + sock.Endpoint + "?timeout=3s")
return dialSocks(network, addr)
}
func (s *Swamp) checkHTTP(sock Proxy) (string, error) {
req, err := http.NewRequest("GET", s.GetRandomEndpoint(), bytes.NewBuffer([]byte("")))
if err != nil {

View File

@ -1,6 +1,9 @@
package pxndscvm
import "errors"
import (
"errors"
"time"
)
// AddUserAgents appends to the list of useragents we randomly choose from during proxied requests
func (s *Swamp) AddUserAgents(uagents []string) {
@ -31,6 +34,13 @@ func (s *Swamp) AddCheckEndpoints(newendpoints []string) {
s.swampopt.CheckEndpoints = append(s.swampopt.CheckEndpoints, newendpoints...)
}
// SetStaleTime replaces the duration of time after which a proxy will be considered "stale". stale proxies will be skipped upon retrieval.
func (s *Swamp) SetStaleTime(newtime time.Duration) {
s.mu.Lock()
defer s.mu.Unlock()
s.swampopt.Stale = newtime
}
// EnableDebug enables printing of verbose messages during operation
func (s *Swamp) EnableDebug() {
s.mu.Lock()