prox5/conductor.go

100 lines
2.7 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
2021-09-12 02:59:05 +00:00
import (
"errors"
2022-06-26 02:51:42 +00:00
"sync/atomic"
"time"
)
2022-10-16 10:53:04 +00:00
// engineState represents the current state of our ProxyEngine.
type engineState uint32
const (
2023-01-31 09:21:29 +00:00
// stateRunning means the proxy pool is currently taking in proxys and validating them, and is available to dispense proxies.
stateRunning engineState = iota
// statePaused means the proxy pool has been with ProxyEngine.Pause() and may be resumed with ProxyEngine.Resume()
statePaused
// stateNew means the proxy pool has never been started.
stateNew
)
2022-10-16 10:53:04 +00:00
// Start starts our proxy pool operations. Trying to start a running ProxyEngine will return an error.
func (p5 *ProxyEngine) Start() error {
2023-01-31 09:21:29 +00:00
if atomic.LoadUint32(&p5.Status) != uint32(stateNew) {
p5.DebugLogger.Printf("proxy pool has been started before, resuming instead")
2022-09-22 23:48:08 +00:00
return p5.Resume()
}
p5.DebugLogger.Printf("starting prox5")
2022-09-22 23:48:08 +00:00
p5.startDaemons()
2021-09-20 01:23:18 +00:00
return nil
}
2021-09-15 05:09:43 +00:00
2021-09-13 19:18:18 +00:00
/*
Pause will cease the creation of any new proxy validation operations.
2022-10-16 10:53:04 +00:00
- You will be able to start the proxy pool again with ProxyEngine.Resume(), it will have the same Statistics, options, and ratelimits.
- During pause you are still able to dispense proxies.
- Options may be changed and proxy lists may be loaded when paused.
2022-10-16 10:53:04 +00:00
- Pausing an already paused ProxyEngine is a nonop.
2021-09-13 19:18:18 +00:00
*/
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) Pause() error {
2022-09-22 23:48:08 +00:00
if !p5.IsRunning() {
2022-06-26 02:51:42 +00:00
return errors.New("not running")
}
2022-09-22 23:48:08 +00:00
p5.dbgPrint(simpleString("pausing proxy pool"))
2023-02-02 05:46:47 +00:00
// p5.quit()
2023-01-31 09:21:29 +00:00
atomic.StoreUint32(&p5.Status, uint32(statePaused))
2023-02-02 05:46:47 +00:00
2021-09-20 12:29:28 +00:00
return nil
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) startDaemons() {
2022-09-22 23:48:08 +00:00
go p5.jobSpawner()
2023-01-31 09:21:29 +00:00
atomic.StoreUint32(&p5.Status, uint32(stateRunning))
p5.DebugLogger.Printf("prox5 started")
}
2022-10-16 10:53:04 +00:00
// Resume will resume pause proxy pool operations, attempting to resume a running ProxyEngine is returns an error.
func (p5 *ProxyEngine) Resume() error {
2022-09-22 23:48:08 +00:00
if p5.IsRunning() {
2022-05-30 10:42:18 +00:00
return errors.New("already running")
}
2023-02-02 05:46:47 +00:00
// p5.ctx, p5.quit = context.WithCancel(context.Background())
2022-09-22 23:48:08 +00:00
p5.startDaemons()
return nil
}
2023-01-31 09:21:29 +00:00
// CloseAllConns will (maybe) close all connections in progress by the dialers (including the SOCKS server if in use).
2023-01-31 09:21:29 +00:00
// Note this does not effect the proxy pool, it will continue to operate as normal.
// this is hacky FIXME
2023-01-31 09:21:29 +00:00
func (p5 *ProxyEngine) CloseAllConns() {
timeout := time.NewTicker(5 * time.Second)
defer func() {
timeout.Stop()
select {
case p5.conKiller <- struct{}{}:
default:
}
}()
for {
select {
case p5.conKiller <- struct{}{}:
timeout.Reset(1 * time.Second)
p5.DebugLogger.Printf("killed a connection")
case <-p5.ctx.Done():
return
case <-timeout.C:
return
}
}
2023-01-31 09:21:29 +00:00
}
2023-09-27 05:12:53 +00:00
func (p5 *ProxyEngine) Close() error {
p5.mu.Lock()
defer p5.mu.Unlock()
p5.quit()
2023-09-27 05:12:53 +00:00
return p5.Pause()
}