prox5/getters.go

129 lines
3.6 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
2021-09-13 08:30:49 +00:00
import (
"strconv"
"sync/atomic"
"time"
"git.tcp.direct/kayos/common/entropy"
)
2021-10-27 15:46:15 +00:00
// GetStatistics returns all current statistics.
// * This is a pointer, do not modify it!
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetStatistics() *statistics {
2022-09-29 06:20:53 +00:00
p5.mu.RLock()
defer p5.mu.RUnlock()
2022-09-22 23:48:08 +00:00
return p5.stats
2021-10-27 15:46:15 +00:00
}
// RandomUserAgent retrieves a random user agent from our list in string form.
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) RandomUserAgent() string {
p5.mu.RLock()
defer p5.mu.RUnlock()
return entropy.RandomStrChoice(p5.swampopt.userAgents)
}
2022-09-22 23:28:28 +00:00
// GetRandomEndpoint returns a random whatismyip style endpoint from our Swamp's options
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetRandomEndpoint() string {
p5.mu.RLock()
defer p5.mu.RUnlock()
return entropy.RandomStrChoice(p5.swampopt.checkEndpoints)
}
// GetStaleTime returns the duration of time after which a proxy will be considered "stale".
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetStaleTime() time.Duration {
p5.swampopt.RLock()
2022-09-29 06:31:33 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
return p5.swampopt.stale
}
// GetValidationTimeout returns the current value of validationTimeout.
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetValidationTimeout() time.Duration {
p5.swampopt.RLock()
2022-09-29 06:20:53 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
return p5.swampopt.validationTimeout
}
// GetValidationTimeoutStr returns the current value of validationTimeout (in seconds string).
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetValidationTimeoutStr() string {
p5.swampopt.RLock()
2022-09-29 06:20:53 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
timeout := p5.swampopt.validationTimeout
2021-09-28 06:25:00 +00:00
return strconv.Itoa(int(timeout / time.Second))
}
// GetServerTimeout returns the current value of serverTimeout.
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetServerTimeout() time.Duration {
p5.swampopt.RLock()
2022-09-29 06:20:53 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
return p5.swampopt.serverTimeout
}
// GetServerTimeoutStr returns the current value of serverTimeout (in seconds string).
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetServerTimeoutStr() string {
p5.swampopt.RLock()
2022-09-29 06:20:53 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
timeout := p5.swampopt.serverTimeout
2021-10-28 15:51:10 +00:00
if timeout == time.Duration(0) {
return "-1"
}
return strconv.Itoa(int(timeout / time.Second))
}
// GetMaxWorkers returns maximum amount of workers that validate proxies concurrently. Note this is read-only during runtime.
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetMaxWorkers() int {
return p5.pool.Cap()
}
2021-09-20 01:23:18 +00:00
// IsRunning returns true if our background goroutines defined in daemons.go are currently operational
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) IsRunning() bool {
2022-09-29 04:26:10 +00:00
return atomic.LoadUint32(&p5.Status) == 0
2021-09-20 01:23:18 +00:00
}
// GetRecyclingStatus retrieves the current recycling status, see EnableRecycling.
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetRecyclingStatus() bool {
p5.swampopt.RLock()
2022-09-29 06:31:33 +00:00
defer p5.swampopt.RUnlock()
2022-09-22 23:48:08 +00:00
return p5.swampopt.recycle
}
// GetWorkers retrieves pond worker statistics:
// - return MaxWorkers, RunningWorkers, IdleWorkers
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetWorkers() (maxWorkers, runningWorkers, idleWorkers int) {
p5.mu.RLock()
defer p5.mu.RUnlock()
return p5.pool.Cap(), p5.pool.Running(), p5.pool.Free()
}
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-09-22 23:48:08 +00:00
func (p5 *Swamp) GetRemoveAfter() int {
p5.mu.RLock()
defer p5.mu.RUnlock()
if !p5.swampopt.recycle {
2021-09-20 08:49:06 +00:00
return -1
}
2022-09-22 23:48:08 +00:00
return p5.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-09-22 23:48:08 +00:00
func (p5 *Swamp) GetDialerBailout() int {
p5.mu.RLock()
defer p5.mu.RUnlock()
return p5.swampopt.dialerBailout
}
2022-07-25 07:14:26 +00:00
// TODO: Document middleware concept
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetDispenseMiddleware() func(*Proxy) (*Proxy, bool) {
p5.mu.RLock()
defer p5.mu.RUnlock()
return p5.dispenseMiddleware
2021-09-28 06:25:00 +00:00
}
2022-09-22 23:48:08 +00:00
func (p5 *Swamp) GetShuffleStatus() bool {
p5.mu.RLock()
defer p5.mu.RUnlock()
return p5.swampopt.shuffle
}