2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-13 08:30:49 +00:00
|
|
|
|
2021-09-13 16:01:19 +00:00
|
|
|
import (
|
2021-09-24 21:06:08 +00:00
|
|
|
"strconv"
|
2022-05-23 01:20:40 +00:00
|
|
|
"sync/atomic"
|
2021-09-13 16:01:19 +00:00
|
|
|
"time"
|
2022-05-23 01:20:40 +00:00
|
|
|
|
|
|
|
"git.tcp.direct/kayos/common/entropy"
|
2021-09-13 16:01:19 +00:00
|
|
|
)
|
2021-09-13 09:37:59 +00:00
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
// GetProto retrieves the known protocol value of the Proxy.
|
|
|
|
func (sock *Proxy) GetProto() ProxyProtocol {
|
|
|
|
return sock.proto
|
2021-09-24 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 15:46:15 +00:00
|
|
|
// GetStatistics returns all current statistics.
|
|
|
|
// * This is a pointer, do not modify it!
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetStatistics() *statistics {
|
|
|
|
return pe.stats
|
2021-10-27 15:46:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:08 +00:00
|
|
|
// RandomUserAgent retrieves a random user agent from our list in string form.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) RandomUserAgent() string {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
return entropy.RandomStrChoice(pe.swampopt.userAgents)
|
2021-09-13 09:37:59 +00:00
|
|
|
}
|
2021-09-13 11:26:18 +00:00
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
// GetRandomEndpoint returns a random whatismyip style endpoint from our ProxyEngine's options
|
|
|
|
func (pe *ProxyEngine) GetRandomEndpoint() string {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
return entropy.RandomStrChoice(pe.swampopt.checkEndpoints)
|
2021-09-13 15:36:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:32:07 +00:00
|
|
|
// GetStaleTime returns the duration of time after which a proxy will be considered "stale".
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetStaleTime() time.Duration {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
return pe.swampopt.stale
|
2021-09-13 16:32:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:08 +00:00
|
|
|
// GetValidationTimeout returns the current value of validationTimeout.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetValidationTimeout() time.Duration {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
return pe.swampopt.validationTimeout
|
2021-09-13 17:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:46:09 +00:00
|
|
|
// GetValidationTimeoutStr returns the current value of validationTimeout (in seconds string).
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetValidationTimeoutStr() string {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
timeout := pe.swampopt.validationTimeout
|
2021-09-28 06:25:00 +00:00
|
|
|
return strconv.Itoa(int(timeout / time.Second))
|
2021-09-24 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:46:09 +00:00
|
|
|
// GetServerTimeout returns the current value of serverTimeout.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetServerTimeout() time.Duration {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
return pe.swampopt.serverTimeout
|
2021-10-27 16:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerTimeoutStr returns the current value of serverTimeout (in seconds string).
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetServerTimeoutStr() string {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
timeout := pe.swampopt.serverTimeout
|
2021-10-28 15:51:10 +00:00
|
|
|
if timeout == time.Duration(0) {
|
|
|
|
return "-1"
|
|
|
|
}
|
2021-10-27 16:46:09 +00:00
|
|
|
return strconv.Itoa(int(timeout / time.Second))
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:32:07 +00:00
|
|
|
// GetMaxWorkers returns maximum amount of workers that validate proxies concurrently. Note this is read-only during runtime.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetMaxWorkers() int {
|
|
|
|
return pe.pool.Cap()
|
2021-09-13 16:32:07 +00:00
|
|
|
}
|
2021-09-20 05:57:42 +00:00
|
|
|
|
2021-09-20 01:23:18 +00:00
|
|
|
// IsRunning returns true if our background goroutines defined in daemons.go are currently operational
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) IsRunning() bool {
|
|
|
|
return atomic.LoadInt32(&pe.runningdaemons) == 2
|
2021-09-20 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 07:05:21 +00:00
|
|
|
// GetRecyclingStatus retrieves the current recycling status, see EnableRecycling.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetRecyclingStatus() bool {
|
|
|
|
pe.swampopt.RLock()
|
|
|
|
defer pe.swampopt.RLock()
|
|
|
|
return pe.swampopt.recycle
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 07:38:00 +00:00
|
|
|
// GetWorkers retrieves pond worker statistics:
|
|
|
|
// * return MaxWorkers, RunningWorkers, IdleWorkers
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetWorkers() (maxWorkers, runningWorkers, idleWorkers int) {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
return pe.pool.Cap(), pe.pool.Running(), pe.pool.Free()
|
2021-09-23 07:38:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 08:49:06 +00:00
|
|
|
// GetRemoveAfter retrieves the removeafter policy, the amount of times a recycled proxy is marked as bad until it is removed entirely.
|
|
|
|
// * returns -1 if recycling is disabled.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetRemoveAfter() int {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
if !pe.swampopt.recycle {
|
2021-09-20 08:49:06 +00:00
|
|
|
return -1
|
|
|
|
}
|
2022-06-26 02:51:42 +00:00
|
|
|
return pe.swampopt.removeafter
|
2021-09-20 08:49:06 +00:00
|
|
|
}
|
2021-09-28 06:25:00 +00:00
|
|
|
|
|
|
|
// GetDialerBailout retrieves the dialer bailout policy. See SetDialerBailout for more info.
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetDialerBailout() int {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
return pe.swampopt.dialerBailout
|
2022-05-23 01:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: More docs
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) GetDispenseMiddleware() func(*Proxy) (*Proxy, bool) {
|
|
|
|
pe.mu.RLock()
|
|
|
|
defer pe.mu.RUnlock()
|
|
|
|
return pe.dispenseMiddleware
|
2021-09-28 06:25:00 +00:00
|
|
|
}
|