2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-12 02:59:05 +00:00
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
import (
|
2022-05-30 10:42:18 +00:00
|
|
|
"context"
|
2022-05-23 01:20:40 +00:00
|
|
|
"errors"
|
2022-06-26 02:51:42 +00:00
|
|
|
"sync/atomic"
|
2022-05-23 01:20:40 +00:00
|
|
|
)
|
2021-09-18 03:47:11 +00:00
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
// SwampStatus represents the current state of our ProxyEngine.
|
2021-09-23 07:38:00 +00:00
|
|
|
type SwampStatus uint32
|
|
|
|
|
|
|
|
const (
|
2022-05-23 01:20:40 +00:00
|
|
|
// StateRunning means the proxy pool is currently taking in proxys and validating them, and is available to dispense proxies.
|
|
|
|
StateRunning SwampStatus = iota
|
2022-06-26 02:51:42 +00:00
|
|
|
// StatePaused means the proxy pool has been with ProxyEngine.Pause() and may be resumed with Swamp.Resume()
|
2022-05-23 01:20:40 +00:00
|
|
|
StatePaused
|
|
|
|
// StateNew means the proxy pool has never been started.
|
|
|
|
StateNew
|
2021-09-23 07:38:00 +00:00
|
|
|
)
|
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
// Start starts our proxy pool operations. Trying to start a running ProxyEngine will return an error.
|
|
|
|
func (pe *ProxyEngine) Start() error {
|
|
|
|
if atomic.LoadUint32(&pe.Status) != uint32(StateNew) {
|
|
|
|
return pe.Resume()
|
2021-09-18 03:47:11 +00:00
|
|
|
}
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.startDaemons()
|
2021-09-20 01:23:18 +00:00
|
|
|
return nil
|
2021-09-14 07:35:04 +00:00
|
|
|
}
|
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-06-26 02:51:42 +00:00
|
|
|
* You will be able to start the proxy pool again with Swamp.Resume(), it will have the same Statistics, options, and ratelimits.
|
2021-09-13 19:18:18 +00:00
|
|
|
* During pause you are still able to dispense proxies.
|
|
|
|
* Options may be changed and proxy lists may be loaded when paused.
|
2022-06-26 02:51:42 +00:00
|
|
|
* Pausing an already paused ProxyEngine is a nonop.
|
2021-09-13 19:18:18 +00:00
|
|
|
*/
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) Pause() error {
|
|
|
|
if !pe.IsRunning() {
|
|
|
|
return errors.New("not running")
|
2021-09-13 19:16:26 +00:00
|
|
|
}
|
2021-10-23 14:26:23 +00:00
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.dbgPrint("pausing...")
|
2021-10-23 14:26:23 +00:00
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.quit()
|
2021-09-23 07:38:00 +00:00
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
atomic.StoreUint32(&pe.Status, uint32(StatePaused))
|
2021-09-20 12:29:28 +00:00
|
|
|
return nil
|
2021-09-13 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
func (pe *ProxyEngine) startDaemons() {
|
|
|
|
go pe.mapBuilder()
|
|
|
|
<-pe.conductor
|
|
|
|
pe.svcUp()
|
|
|
|
go pe.jobSpawner()
|
2021-10-23 17:25:28 +00:00
|
|
|
|
|
|
|
for {
|
2022-06-26 02:51:42 +00:00
|
|
|
if pe.IsRunning() {
|
|
|
|
atomic.StoreUint32(&pe.Status, uint32(StateRunning))
|
2021-10-23 17:25:28 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 02:51:42 +00:00
|
|
|
// Resume will resume pause proxy pool operations, attempting to resume a running ProxyEngine is returns an error.
|
|
|
|
func (pe *ProxyEngine) Resume() error {
|
|
|
|
if pe.IsRunning() {
|
2022-05-30 10:42:18 +00:00
|
|
|
return errors.New("already running")
|
2021-09-13 19:16:26 +00:00
|
|
|
}
|
2022-06-26 02:51:42 +00:00
|
|
|
pe.ctx, pe.quit = context.WithCancel(context.Background())
|
|
|
|
pe.startDaemons()
|
2021-09-18 03:47:11 +00:00
|
|
|
return nil
|
2021-09-13 19:08:41 +00:00
|
|
|
}
|