Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix new listen handler initialization #1233

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v2/pkg/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func NewScanner(options *Options) (*Scanner, error) {
acquire:
if handler, err := Acquire(options); err != nil {
// automatically fallback to connect scan
if err != nil && options.ScanType == "s" {
if options.ScanType == "s" {
gologger.Info().Msgf("syn scan is not possible, falling back to connect scan")
options.ScanType = "c"
goto acquire
Expand Down
5 changes: 4 additions & 1 deletion v2/pkg/scan/scan_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func NewListenHandler() *ListenHandler {
func Acquire(options *Options) (*ListenHandler, error) {
// always grant to unprivileged scans or connect scan
if PkgRouter == nil || !privileges.IsPrivileged || options.ScanType == "c" {
return NewListenHandler(), nil
if listenHandler, err := buildListenHandler(); err == nil {
ListenHandlers = append(ListenHandlers, listenHandler)
return listenHandler, nil
}
}

for _, listenHandler := range ListenHandlers {
Expand Down
74 changes: 37 additions & 37 deletions v2/pkg/scan/scan_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,11 @@ func init() {

// pre-reserve up to 10 ports
for i := 0; i < NumberOfHandlers; i++ {
listenHandler := NewListenHandler()
if port, err := freeport.GetFreeTCPPort(""); err != nil {
gologger.Error().Msgf("could not setup get free port: %s", err)
return
} else {
listenHandler.Port = port.Port
}

listenHandler.TcpChan = make(chan *PkgResult, chanSize)
listenHandler.UdpChan = make(chan *PkgResult, chanSize)
listenHandler.HostDiscoveryChan = make(chan *PkgResult, chanSize)

var err error
listenHandler.TcpConn4, err = net.ListenIP("ip4:tcp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf("0.0.0.0:%d", listenHandler.Port))})
listenHandler, err := buildListenHandler()
if err != nil {
gologger.Error().Msgf("could not setup ip4:tcp: %s", err)
return
}
listenHandler.UdpConn4, err = net.ListenIP("ip4:udp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf("0.0.0.0:%d", listenHandler.Port))})
if err != nil {
gologger.Error().Msgf("could not setup ip4:udp: %s", err)
return
}

listenHandler.TcpConn6, err = net.ListenIP("ip6:tcp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf(":::%d", listenHandler.Port))})
if err != nil {
gologger.Error().Msgf("could not setup ip6:tcp: %s\n", err)
}

listenHandler.UdpConn6, err = net.ListenIP("ip6:udp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf(":::%d", listenHandler.Port))})
if err != nil {
gologger.Error().Msgf("could not setup ip6:udp: %s\n", err)
}

go listenHandler.ICMPReadWorker4()
go listenHandler.ICMPReadWorker6()
go listenHandler.TcpReadWorker4()
go listenHandler.TcpReadWorker6()
go listenHandler.UdpReadWorker4()
go listenHandler.UdpReadWorker6()

ListenHandlers = append(ListenHandlers, listenHandler)
}

Expand All @@ -120,6 +84,42 @@ func init() {
go ICMPWriteWorker()
}

func buildListenHandler() (*ListenHandler, error) {
listenHandler := NewListenHandler()
if port, err := freeport.GetFreeTCPPort(""); err != nil {

return nil, fmt.Errorf("could not setup get free port: %s", err)
} else {
listenHandler.Port = port.Port
}

listenHandler.TcpChan = make(chan *PkgResult, chanSize)
listenHandler.UdpChan = make(chan *PkgResult, chanSize)
listenHandler.HostDiscoveryChan = make(chan *PkgResult, chanSize)

var err error
listenHandler.TcpConn4, err = net.ListenIP("ip4:tcp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf("0.0.0.0:%d", listenHandler.Port))})
if err != nil {
return nil, fmt.Errorf("could not setup ip4:tcp: %s", err)
}
listenHandler.UdpConn4, err = net.ListenIP("ip4:udp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf("0.0.0.0:%d", listenHandler.Port))})
if err != nil {
return nil, fmt.Errorf("could not setup ip4:udp: %s", err)
}

listenHandler.TcpConn6, _ = net.ListenIP("ip6:tcp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf(":::%d", listenHandler.Port))})

listenHandler.UdpConn6, _ = net.ListenIP("ip6:udp", &net.IPAddr{IP: net.ParseIP(fmt.Sprintf(":::%d", listenHandler.Port))})

go listenHandler.ICMPReadWorker4()
go listenHandler.ICMPReadWorker6()
go listenHandler.TcpReadWorker4()
go listenHandler.TcpReadWorker6()
go listenHandler.UdpReadWorker4()
go listenHandler.UdpReadWorker6()
return listenHandler, nil
}

// ICMPWriteWorker writes packet to the network layer
func ICMPWriteWorker() {
for pkg := range icmpPacketSend {
Expand Down
Loading