2024-07-25 07:47:36 -04:00
|
|
|
package falcon
|
|
|
|
|
|
|
|
import (
|
2024-07-25 11:17:07 -04:00
|
|
|
"errors"
|
|
|
|
"slices"
|
2024-07-25 07:47:36 -04:00
|
|
|
"time"
|
2024-07-25 11:17:07 -04:00
|
|
|
|
|
|
|
"github.com/panjf2000/ants/v2"
|
2024-07-25 07:47:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Falcon represents a proxy checker
|
|
|
|
type Falcon struct {
|
2024-07-25 11:17:07 -04:00
|
|
|
// Pool workers
|
|
|
|
Workers int
|
|
|
|
// Proxy timeout (default: 10)
|
2024-07-25 07:47:36 -04:00
|
|
|
Timeout time.Duration
|
2024-07-25 11:17:07 -04:00
|
|
|
// Proxy list
|
|
|
|
Proxies []string
|
|
|
|
// Running status
|
2024-07-25 07:47:36 -04:00
|
|
|
Running bool
|
2024-07-25 11:17:07 -04:00
|
|
|
// Proxy channels
|
2024-07-25 07:47:36 -04:00
|
|
|
*Channels
|
|
|
|
|
2024-07-25 11:17:07 -04:00
|
|
|
pool *ants.Pool
|
2024-07-25 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New will return a default Falcon
|
2024-07-25 11:17:07 -04:00
|
|
|
func New(workers int) *Falcon {
|
|
|
|
pool, err := ants.NewPool(workers)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-07-25 07:47:36 -04:00
|
|
|
return &Falcon{
|
2024-07-25 11:17:07 -04:00
|
|
|
Workers: workers,
|
|
|
|
Timeout: time.Second * 10,
|
|
|
|
Proxies: []string{},
|
2024-07-25 07:47:36 -04:00
|
|
|
Running: false,
|
|
|
|
Channels: &Channels{
|
|
|
|
SOCKS5: make(chan *Lookup),
|
|
|
|
SOCKS4: make(chan *Lookup),
|
|
|
|
SOCKS4A: make(chan *Lookup),
|
|
|
|
HTTP: make(chan *Lookup),
|
|
|
|
Error: make(chan error),
|
|
|
|
},
|
2024-07-25 11:17:07 -04:00
|
|
|
pool: pool,
|
2024-07-25 07:47:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will start the proxy checker
|
2024-07-25 11:17:07 -04:00
|
|
|
func (f *Falcon) Start() error {
|
|
|
|
if f.Running {
|
|
|
|
return errors.New("proxy checker is already running")
|
2024-07-25 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
2024-07-25 11:17:07 -04:00
|
|
|
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
|
2024-07-25 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop will stop the proxy checker
|
2024-07-25 11:17:07 -04:00
|
|
|
func (f *Falcon) Stop() error {
|
|
|
|
if !f.Running {
|
|
|
|
return errors.New("proxy checker is not running")
|
|
|
|
}
|
|
|
|
|
2024-07-25 07:47:36 -04:00
|
|
|
f.Running = false
|
2024-07-25 11:17:07 -04:00
|
|
|
return nil
|
2024-07-25 07:47:36 -04:00
|
|
|
}
|