falcon/falcon.go

84 lines
1.3 KiB
Go

package falcon
import (
"errors"
"slices"
"time"
"github.com/panjf2000/ants/v2"
)
// Falcon represents a proxy checker
type Falcon struct {
// Pool workers
Workers int
// Proxy timeout (default: 10)
Timeout time.Duration
// Proxy list
Proxies []string
// Running status
Running bool
// Proxy channels
*Channels
pool *ants.Pool
}
// New will return a default Falcon
func New(workers int) *Falcon {
pool, err := ants.NewPool(workers)
if err != nil {
panic(err)
}
return &Falcon{
Workers: workers,
Timeout: time.Second * 10,
Proxies: []string{},
Running: false,
Channels: &Channels{
SOCKS5: make(chan *Lookup),
SOCKS4: make(chan *Lookup),
SOCKS4A: make(chan *Lookup),
HTTP: make(chan *Lookup),
Error: make(chan error),
},
pool: pool,
}
}
// Start will start the proxy checker
func (f *Falcon) Start() error {
if f.Running {
return errors.New("proxy checker is already running")
}
f.Proxies = slices.Compact(f.Proxies)
go func() {
f.Running = true
for _, proxy := range f.Proxies {
if f.Running {
f.pool.Submit(func() {
f.Verify(proxy, f.Timeout)
})
}
}
f.Running = false
}()
return nil
}
// Stop will stop the proxy checker
func (f *Falcon) Stop() error {
if !f.Running {
return errors.New("proxy checker is not running")
}
f.Running = false
return nil
}