Add errors for Start and Stop. Add ants pool with submitting. Add more options e.g timeout, workers
This commit is contained in:
parent
f1b5ba900e
commit
9e7f881026
71
falcon.go
71
falcon.go
|
@ -1,29 +1,40 @@
|
|||
package falcon
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
// Falcon represents a proxy checker
|
||||
type Falcon struct {
|
||||
// Proxies to verify
|
||||
Proxies []string
|
||||
// Proxy timeout (default: 5 seconds)
|
||||
// Pool workers
|
||||
Workers int
|
||||
// Proxy timeout (default: 10)
|
||||
Timeout time.Duration
|
||||
// Checking status
|
||||
// Proxy list
|
||||
Proxies []string
|
||||
// Running status
|
||||
Running bool
|
||||
// Incoming proxies
|
||||
// Proxy channels
|
||||
*Channels
|
||||
|
||||
wg *sync.WaitGroup
|
||||
pool *ants.Pool
|
||||
}
|
||||
|
||||
// New will return a default Falcon
|
||||
func New(proxies []string) *Falcon {
|
||||
func New(workers int) *Falcon {
|
||||
pool, err := ants.NewPool(workers)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &Falcon{
|
||||
Proxies: proxies,
|
||||
Timeout: time.Second * 5,
|
||||
Workers: workers,
|
||||
Timeout: time.Second * 10,
|
||||
Proxies: []string{},
|
||||
Running: false,
|
||||
Channels: &Channels{
|
||||
SOCKS5: make(chan *Lookup),
|
||||
|
@ -32,27 +43,41 @@ func New(proxies []string) *Falcon {
|
|||
HTTP: make(chan *Lookup),
|
||||
Error: make(chan error),
|
||||
},
|
||||
wg: &sync.WaitGroup{},
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
// Start will start the proxy checker
|
||||
func (f *Falcon) Start() {
|
||||
f.Running = true
|
||||
|
||||
for _, proxy := range f.Proxies {
|
||||
if f.Running {
|
||||
f.wg.Add(1)
|
||||
go f.Verify(proxy, f.Timeout)
|
||||
f.wg.Done()
|
||||
}
|
||||
func (f *Falcon) Start() error {
|
||||
if f.Running {
|
||||
return errors.New("proxy checker is already running")
|
||||
}
|
||||
|
||||
f.wg.Wait()
|
||||
f.Running = false
|
||||
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() {
|
||||
func (f *Falcon) Stop() error {
|
||||
if !f.Running {
|
||||
return errors.New("proxy checker is not running")
|
||||
}
|
||||
|
||||
f.Running = false
|
||||
return nil
|
||||
}
|
||||
|
|
7
go.mod
7
go.mod
|
@ -2,4 +2,9 @@ module git.supernets.org/perp/falcon
|
|||
|
||||
go 1.22.4
|
||||
|
||||
require h12.io/socks v1.0.3
|
||||
require (
|
||||
github.com/panjf2000/ants/v2 v2.10.0
|
||||
h12.io/socks v1.0.3
|
||||
)
|
||||
|
||||
require golang.org/x/sync v0.3.0 // indirect
|
||||
|
|
20
go.sum
20
go.sum
|
@ -1,6 +1,26 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364 h1:5XxdakFhqd9dnXoAZy1Mb2R/DZ6D1e+0bGC/JhucGYI=
|
||||
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364/go.mod h1:eDJQioIyy4Yn3MVivT7rv/39gAJTrA7lgmYr8EW950c=
|
||||
github.com/panjf2000/ants/v2 v2.10.0 h1:zhRg1pQUtkyRiOFo2Sbqwjp0GfBNo9cUY2/Grpx1p+8=
|
||||
github.com/panjf2000/ants/v2 v2.10.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
|
||||
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
|
||||
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
h12.io/socks v1.0.3 h1:Ka3qaQewws4j4/eDQnOdpr4wXsC//dXtWvftlIcCQUo=
|
||||
h12.io/socks v1.0.3/go.mod h1:AIhxy1jOId/XCz9BO+EIgNL2rQiPTBNnOfnVnQ+3Eck=
|
||||
|
|
Loading…
Reference in New Issue