Skip to content

Commit 9fbf800

Browse files
committed
adding basic rate limiter
1 parent 7df9274 commit 9fbf800

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/projectdiscovery/fdmax v0.0.2
1010
github.com/projectdiscovery/gologger v1.0.1
1111
github.com/projectdiscovery/mapcidr v0.0.4
12-
github.com/remeh/sizedwaitgroup v1.0.0
1312
golang.org/x/net v0.0.0-20200904194848-62affa334b73
1413
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
1514
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ github.com/projectdiscovery/gologger v1.0.1 h1:FzoYQZnxz9DCvSi/eg5A6+ET4CQ0CDUs2
1414
github.com/projectdiscovery/gologger v1.0.1/go.mod h1:Ok+axMqK53bWNwDSU1nTNwITLYMXMdZtRc8/y1c7sWE=
1515
github.com/projectdiscovery/mapcidr v0.0.4 h1:2vBSjkmbQASAcO/m2L/dhdulMVu2y9HdyWOrWYJ74rU=
1616
github.com/projectdiscovery/mapcidr v0.0.4/go.mod h1:ALOIj6ptkWujNoX8RdQwB2mZ+kAmKuLJBq9T5gR5wG0=
17-
github.com/remeh/sizedwaitgroup v1.0.0 h1:VNGGFwNo/R5+MJBf6yrsr110p0m4/OX4S3DCy7Kyl5E=
18-
github.com/remeh/sizedwaitgroup v1.0.0/go.mod h1:3j2R4OIe/SeS6YDhICBy22RWjJC5eNCJ1V+9+NVNYlo=
1917
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2018
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2119
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=

pkg/runner/probe.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package runner
22

33
import (
44
"net"
5+
"sync"
56
"syscall"
67
"time"
78

89
"github.com/projectdiscovery/naabu/pkg/scan"
9-
"github.com/remeh/sizedwaitgroup"
1010
)
1111

1212
func (r *Runner) pingprobes(ip string) bool {
@@ -103,10 +103,11 @@ func (r *Runner) ProbeOrSkip() {
103103
return
104104
}
105105

106-
swg := sizedwaitgroup.New(r.options.Rate)
107-
106+
var swg sync.WaitGroup
107+
limiter := time.Tick(time.Second / time.Duration(r.options.Rate))
108108
for ip := range r.scanner.Targets {
109-
swg.Add()
109+
<-limiter
110+
swg.Add(1)
110111
go func(ip string) {
111112
defer swg.Done()
112113
r.pingprobesasync(ip)

pkg/runner/runner.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"sync"
1011
"time"
1112

1213
"github.com/projectdiscovery/gologger"
1314
"github.com/projectdiscovery/naabu/pkg/scan"
14-
"github.com/remeh/sizedwaitgroup"
1515
)
1616

1717
// Runner is an instance of the port enumeration
@@ -131,15 +131,17 @@ func (r *Runner) RunEnumeration() error {
131131

132132
func (r *Runner) ConnectVerification() {
133133
r.scanner.State = scan.Scan
134-
swg := sizedwaitgroup.New(r.options.Rate)
134+
var swg sync.WaitGroup
135+
limiter := time.Tick(time.Second / time.Duration(r.options.Rate))
135136

136137
for host, ports := range r.scanner.ScanResults.M {
137-
swg.Add()
138-
go func(swg *sizedwaitgroup.SizedWaitGroup, host string, ports map[int]struct{}) {
138+
<-limiter
139+
swg.Add(1)
140+
go func(host string, ports map[int]struct{}) {
139141
defer swg.Done()
140142
results := r.scanner.ConnectVerify(host, ports)
141143
r.scanner.ScanResults.SetPorts(host, results)
142-
}(&swg, host, ports)
144+
}(host, ports)
143145
}
144146

145147
swg.Wait()
@@ -151,12 +153,14 @@ func (r *Runner) BackgroundWorkers() {
151153

152154
func (r *Runner) RawSocketEnumeration() {
153155
r.scanner.State = scan.Scan
154-
swg := sizedwaitgroup.New(r.options.Rate)
156+
var swg sync.WaitGroup
157+
limiter := time.Tick(time.Second / time.Duration(r.options.Rate))
155158

156159
for retry := 0; retry < r.options.Retries; retry++ {
157160
for port := range r.scanner.Ports {
158161
for target := range r.scanner.Targets {
159-
swg.Add()
162+
<-limiter
163+
swg.Add(1)
160164
go r.handleHostPortSyn(&swg, target, port)
161165
}
162166
}
@@ -168,12 +172,14 @@ func (r *Runner) RawSocketEnumeration() {
168172
func (r *Runner) ConnectEnumeration() {
169173
r.scanner.State = scan.Scan
170174
// naive algorithm - ports spray
171-
swg := sizedwaitgroup.New(r.options.Rate)
175+
var swg sync.WaitGroup
176+
limiter := time.Tick(time.Second / time.Duration(r.options.Rate))
172177

173178
for retry := 0; retry < r.options.Retries; retry++ {
174179
for port := range r.scanner.Ports {
175180
for target := range r.scanner.Targets {
176-
swg.Add()
181+
<-limiter
182+
swg.Add(1)
177183
go r.handleHostPort(&swg, target, port)
178184
}
179185
}
@@ -198,7 +204,7 @@ func (r *Runner) canIScanIfCDN(host string, port int) bool {
198204
return port == 80 || port == 443
199205
}
200206

201-
func (r *Runner) handleHostPort(swg *sizedwaitgroup.SizedWaitGroup, host string, port int) {
207+
func (r *Runner) handleHostPort(swg *sync.WaitGroup, host string, port int) {
202208
defer swg.Done()
203209

204210
// performs cdn scan exclusions checks
@@ -217,7 +223,7 @@ func (r *Runner) handleHostPort(swg *sizedwaitgroup.SizedWaitGroup, host string,
217223
}
218224
}
219225

220-
func (r *Runner) handleHostPortSyn(swg *sizedwaitgroup.SizedWaitGroup, host string, port int) {
226+
func (r *Runner) handleHostPortSyn(swg *sync.WaitGroup, host string, port int) {
221227
defer swg.Done()
222228

223229
// performs cdn scan exclusions checks

0 commit comments

Comments
 (0)