Skip to content

Commit 56b0382

Browse files
committed
fix: critical unlimited goroutines bug
1 parent a34496a commit 56b0382

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func init() {
4848
flag.Int64Var(&randomIPsCount, "n", -1, "generate N random WAN IPs")
4949
flag.StringVar(&cidrNetwork, "net", "", "Network in CIDR notation to scan in random order")
5050
flag.StringVar(&ipList, "list", "", "IP/networks list (CIDR) to scan in random order")
51-
flag.IntVar(&scanWorkers, "w", 64, "workers count")
52-
flag.IntVar(&scanWorkers, "workers", 64, "workers count")
51+
flag.IntVar(&scanWorkers, "w", 4096, "workers count")
52+
flag.IntVar(&scanWorkers, "workers", 4096, "workers count")
5353
flag.DurationVar(&connTimeout, "t", 700*time.Millisecond, "scan connect timeout")
5454
flag.DurationVar(&connTimeout, "timeout", 700*time.Millisecond, "scan connect timeout")
5555

@@ -145,18 +145,20 @@ func process(processor *services.Processor) {
145145

146146
utils.EPrint("[i] callback set")
147147
if !callbackE {
148-
utils.EPrint(", err")
148+
utils.EPrint(" [err]")
149149
cbFlags = cbFlags.Set(utils.ERR)
150150
}
151151
if !callbackW {
152-
utils.EPrint(", warn")
152+
utils.EPrint(" [warn]")
153153
cbFlags = cbFlags.Set(utils.WARN)
154154
}
155155
if !callbackI {
156-
utils.EPrint(", info")
156+
utils.EPrint(" [info]")
157157
cbFlags = cbFlags.Set(utils.INFO)
158158
}
159159
utils.EPrintln()
160+
utils.EPrintln(" cb concurrency:", callbackConcurrency)
161+
utils.EPrintln(" cb timeout:", callbackTimeout)
160162

161163
utils.EPrintln("=========================")
162164
sp.Start()

services/processor.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ func (p *Processor) Process() <-chan HostResult {
4646

4747
func (p *Processor) work(ipGeneratorChannel <-chan net.IP, wg *sync.WaitGroup) {
4848
defer wg.Done()
49-
var swg sync.WaitGroup
5049
for ip := range ipGeneratorChannel {
5150
for _, svc := range p.services {
52-
swg.Add(1)
53-
go svc.Check(ip, p.ch, &swg)
51+
svc.Check(ip, p.ch)
5452
}
5553
}
56-
swg.Wait()
5754
}

services/rtsp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func NewRTSPService(ports []int, timeout time.Duration, paths []string) *RTSPSer
3636
func (s *RTSPService) ScanAddr(addr net.TCPAddr, ch chan<- HostResult, wg *sync.WaitGroup) {
3737
defer wg.Done()
3838
r := protocol.NewRTSP(&addr, s.paths, s.fakePath, s.timeout)
39-
if res, err := r.Check(); err == nil {
39+
res, err := r.Check()
40+
if err == nil {
4041
ch <- HostResult{
4142
Addr: &addr,
4243
Details: &RTSPResult{Url: res},

services/service.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type ServiceInterface interface {
99
ScanAddr(net.TCPAddr, chan<- HostResult, *sync.WaitGroup)
10-
Check(net.IP, chan<- HostResult, *sync.WaitGroup)
10+
Check(net.IP, chan<- HostResult)
1111
}
1212

1313
type Service struct {
@@ -17,11 +17,11 @@ type Service struct {
1717

1818
var _ ServiceInterface = &Service{}
1919

20-
func (s *Service) Check(ip net.IP, ch chan<- HostResult, swg *sync.WaitGroup) {
20+
func (s *Service) Check(ip net.IP, ch chan<- HostResult) {
2121
var wg sync.WaitGroup
2222

23-
// coz we are netstalkers, not DoSers
24-
portRateLimiter := make(chan struct{}, 8)
23+
// coz we are netstalkers, not DoSers
24+
portRateLimiter := make(chan struct{}, 8)
2525

2626
if len(s.Ports) == 0 {
2727
s.Ports = []int{0}
@@ -31,13 +31,12 @@ func (s *Service) Check(ip net.IP, ch chan<- HostResult, swg *sync.WaitGroup) {
3131

3232
for _, port := range s.Ports {
3333
addr := net.TCPAddr{IP: ip, Port: port}
34-
portRateLimiter <- struct{}{}
35-
go func(){
36-
s.ServiceInterface.ScanAddr(addr, ch, &wg)
37-
<-portRateLimiter
38-
}()
34+
portRateLimiter <- struct{}{}
35+
go func() {
36+
s.ServiceInterface.ScanAddr(addr, ch, &wg)
37+
<-portRateLimiter
38+
}()
3939
}
4040

4141
wg.Wait()
42-
swg.Done() // w/o defer to speedup frequent calls
4342
}

0 commit comments

Comments
 (0)