155 lines
3.3 KiB
Go
155 lines
3.3 KiB
Go
package ipv4
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
|
|
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
|
|
"git.loafle.net/overflow/overflow_discovery/service"
|
|
"github.com/google/gopacket"
|
|
"github.com/google/gopacket/layers"
|
|
)
|
|
|
|
func scanPortUDP(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
|
|
defer func() {
|
|
wg.Done()
|
|
}()
|
|
|
|
ps, err := pcap.RetainScanner(host.Zone)
|
|
if nil != err {
|
|
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
|
|
return
|
|
}
|
|
defer func() {
|
|
pcap.ReleaseScanner(host.Zone)
|
|
}()
|
|
|
|
udpChan := ps.OpenUDP(host.IP)
|
|
defer func() {
|
|
ps.CloseUDP(host.IP, udpChan)
|
|
}()
|
|
|
|
go func() {
|
|
ports := make(map[int]*model.Port)
|
|
|
|
for {
|
|
select {
|
|
case packet, ok := <-udpChan:
|
|
if !ok {
|
|
logging.Logger().Debug(fmt.Sprintf("Discovery: udp channel is closed"))
|
|
return
|
|
}
|
|
if p := handlePacketUDP(host, dp, ports, packet); nil != p {
|
|
resultChan <- p
|
|
}
|
|
case <-stopChan:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err := sendUDP(host, dp, stopChan); nil != err {
|
|
errChan <- err
|
|
return
|
|
}
|
|
|
|
timer := time.NewTimer(3 * time.Second)
|
|
|
|
select {
|
|
case <-stopChan:
|
|
return
|
|
case <-timer.C:
|
|
return
|
|
}
|
|
}
|
|
|
|
func sendUDP(host *model.Host, dp *model.DiscoveryPort, stopChan chan struct{}) error {
|
|
ip := net.ParseIP(host.IP)
|
|
if nil == ip {
|
|
return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IP)
|
|
}
|
|
|
|
ms := service.GetUDPMatchers()
|
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
conn.Close()
|
|
}()
|
|
|
|
for indexI := 0; indexI < len(ms); indexI++ {
|
|
m := ms[indexI]
|
|
|
|
Loop:
|
|
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
|
|
if nil != dp.ExcludePorts {
|
|
for _, exPortNumber := range dp.ExcludePorts {
|
|
if portNumber == exPortNumber {
|
|
continue Loop
|
|
}
|
|
}
|
|
}
|
|
|
|
if !m.IsSend(portNumber) {
|
|
continue
|
|
}
|
|
|
|
addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber}
|
|
for i := 0; i < m.PacketCount(); i++ {
|
|
p := m.Packet(i)
|
|
if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil {
|
|
logging.Logger().Error(fmt.Sprintf("Discovery: UDP write error %v", err))
|
|
}
|
|
}
|
|
|
|
timer := time.NewTimer(time.Microsecond * 200)
|
|
|
|
select {
|
|
case <-stopChan:
|
|
return nil
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func handlePacketUDP(host *model.Host, dp *model.DiscoveryPort, ports map[int]*model.Port, packet gopacket.Packet) *model.Port {
|
|
ipLayer := packet.Layer(layers.LayerTypeIPv4)
|
|
|
|
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.IP {
|
|
return nil
|
|
}
|
|
|
|
if net := packet.NetworkLayer(); net == nil {
|
|
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer == nil {
|
|
} else if udp, ok := udpLayer.(*layers.UDP); ok {
|
|
|
|
srcIP := ipLayer.(*layers.IPv4).SrcIP
|
|
port := int(udp.SrcPort)
|
|
logging.Logger().Debug(fmt.Sprintf("Discovery: IP of UDP(%d) src %v", port, srcIP))
|
|
if _, ok := ports[port]; ok || !dp.Contains(port) {
|
|
return nil
|
|
}
|
|
|
|
p := &model.Port{
|
|
PortType: model.PortTypeUDP,
|
|
PortNumber: port,
|
|
UDPLayer: udpLayer,
|
|
}
|
|
p.Host = host
|
|
ports[port] = p
|
|
|
|
return p
|
|
}
|
|
|
|
return nil
|
|
}
|