Skip to content

Commit

Permalink
fix: critical unlimited goroutines bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fagci committed Apr 12, 2022
1 parent a34496a commit 56b0382
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func init() {
flag.Int64Var(&randomIPsCount, "n", -1, "generate N random WAN IPs")
flag.StringVar(&cidrNetwork, "net", "", "Network in CIDR notation to scan in random order")
flag.StringVar(&ipList, "list", "", "IP/networks list (CIDR) to scan in random order")
flag.IntVar(&scanWorkers, "w", 64, "workers count")
flag.IntVar(&scanWorkers, "workers", 64, "workers count")
flag.IntVar(&scanWorkers, "w", 4096, "workers count")
flag.IntVar(&scanWorkers, "workers", 4096, "workers count")
flag.DurationVar(&connTimeout, "t", 700*time.Millisecond, "scan connect timeout")
flag.DurationVar(&connTimeout, "timeout", 700*time.Millisecond, "scan connect timeout")

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

utils.EPrint("[i] callback set")
if !callbackE {
utils.EPrint(", err")
utils.EPrint(" [err]")
cbFlags = cbFlags.Set(utils.ERR)
}
if !callbackW {
utils.EPrint(", warn")
utils.EPrint(" [warn]")
cbFlags = cbFlags.Set(utils.WARN)
}
if !callbackI {
utils.EPrint(", info")
utils.EPrint(" [info]")
cbFlags = cbFlags.Set(utils.INFO)
}
utils.EPrintln()
utils.EPrintln(" cb concurrency:", callbackConcurrency)
utils.EPrintln(" cb timeout:", callbackTimeout)

utils.EPrintln("=========================")
sp.Start()
Expand Down
5 changes: 1 addition & 4 deletions services/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ func (p *Processor) Process() <-chan HostResult {

func (p *Processor) work(ipGeneratorChannel <-chan net.IP, wg *sync.WaitGroup) {
defer wg.Done()
var swg sync.WaitGroup
for ip := range ipGeneratorChannel {
for _, svc := range p.services {
swg.Add(1)
go svc.Check(ip, p.ch, &swg)
svc.Check(ip, p.ch)
}
}
swg.Wait()
}
3 changes: 2 additions & 1 deletion services/rtsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func NewRTSPService(ports []int, timeout time.Duration, paths []string) *RTSPSer
func (s *RTSPService) ScanAddr(addr net.TCPAddr, ch chan<- HostResult, wg *sync.WaitGroup) {
defer wg.Done()
r := protocol.NewRTSP(&addr, s.paths, s.fakePath, s.timeout)
if res, err := r.Check(); err == nil {
res, err := r.Check()
if err == nil {
ch <- HostResult{
Addr: &addr,
Details: &RTSPResult{Url: res},
Expand Down
19 changes: 9 additions & 10 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type ServiceInterface interface {
ScanAddr(net.TCPAddr, chan<- HostResult, *sync.WaitGroup)
Check(net.IP, chan<- HostResult, *sync.WaitGroup)
Check(net.IP, chan<- HostResult)
}

type Service struct {
Expand All @@ -17,11 +17,11 @@ type Service struct {

var _ ServiceInterface = &Service{}

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

// coz we are netstalkers, not DoSers
portRateLimiter := make(chan struct{}, 8)
// coz we are netstalkers, not DoSers
portRateLimiter := make(chan struct{}, 8)

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

for _, port := range s.Ports {
addr := net.TCPAddr{IP: ip, Port: port}
portRateLimiter <- struct{}{}
go func(){
s.ServiceInterface.ScanAddr(addr, ch, &wg)
<-portRateLimiter
}()
portRateLimiter <- struct{}{}
go func() {
s.ServiceInterface.ScanAddr(addr, ch, &wg)
<-portRateLimiter
}()
}

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

0 comments on commit 56b0382

Please sign in to comment.