container_discovery/internal/discoverer/ipv4/port_tcp.go

201 lines
4.3 KiB
Go
Raw Permalink Normal View History

2018-04-19 11:36:56 +00:00
package ipv4
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
2018-04-27 15:33:22 +00:00
"sync/atomic"
2018-04-19 11:36:56 +00:00
"time"
"git.loafle.net/commons/logging-go"
2018-05-11 02:44:47 +00:00
occu "git.loafle.net/overflow/commons-go/core/util"
2018-04-26 09:00:24 +00:00
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
2018-06-13 10:20:14 +00:00
ocmm "git.loafle.net/overflow/commons-go/model/meta"
2018-04-19 11:36:56 +00:00
"git.loafle.net/overflow/container_discovery/internal/pcap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
2018-04-27 16:20:01 +00:00
func scanPortTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
2018-04-19 11:36:56 +00:00
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() {
2018-04-27 15:33:22 +00:00
go pcap.ReleaseScanner(host.Zone)
2018-04-19 11:36:56 +00:00
}()
2018-06-13 10:20:14 +00:00
tcpChan := ps.OpenTCP(host.Address)
2018-04-19 11:36:56 +00:00
defer func() {
2018-06-13 10:20:14 +00:00
go ps.CloseTCP(host.Address, tcpChan)
2018-04-19 11:36:56 +00:00
}()
2018-04-27 15:33:22 +00:00
timerStopped := make(chan struct{})
2018-04-19 11:36:56 +00:00
go func() {
2018-04-26 09:00:24 +00:00
ports := make(map[int]*ocmd.Port)
2018-04-19 11:36:56 +00:00
2018-04-27 15:33:22 +00:00
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Millisecond * 500)
2018-04-19 11:36:56 +00:00
for {
select {
case packet, ok := <-tcpChan:
if !ok {
logging.Logger().Debugf("Discovery: tcp channel is closed")
return
}
2018-04-27 15:33:22 +00:00
delay.Store(true)
2018-04-19 11:36:56 +00:00
if p := handlePacketTCP(host, dp, ports, packet); nil != p {
resultChan <- p
}
2018-04-27 15:33:22 +00:00
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
2018-04-19 11:36:56 +00:00
case <-stopChan:
return
}
}
}()
if err := sendTCP(host, dp, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
2018-04-27 15:33:22 +00:00
case <-timerStopped:
2018-04-19 11:36:56 +00:00
return
}
}
2018-04-27 16:20:01 +00:00
func sendTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, stopChan chan struct{}) error {
2018-04-19 11:36:56 +00:00
tcpPacket, err := makePacketPortTCP(host)
if nil != err {
return err
}
defer func() {
tcpPacket.PacketConn.Close()
}()
buf := gopacket.NewSerializeBuffer()
Loop:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue Loop
}
}
}
tcpPacket.TCP.DstPort = layers.TCPPort(portNumber)
tcpPacket.TCP.SetNetworkLayerForChecksum(tcpPacket.IP)
if err := gopacket.SerializeLayers(buf, tcpPacket.Opts, tcpPacket.TCP); err != nil {
return err
}
if _, err := tcpPacket.PacketConn.WriteTo(buf.Bytes(), &net.IPAddr{IP: tcpPacket.IP.DstIP}); err != nil {
return err
}
timer := time.NewTimer(time.Microsecond * 100)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
return nil
}
2018-04-27 16:20:01 +00:00
func handlePacketTCP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd.Port, packet *layers.TCP) *ocmd.Port {
2018-04-19 11:36:56 +00:00
if nil == packet || packet.DstPort != 60000 {
return nil
}
if packet.RST {
return nil
}
port := int(packet.SrcPort)
2018-06-13 10:20:14 +00:00
logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.Address)
2018-04-19 11:36:56 +00:00
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
2018-04-26 09:00:24 +00:00
p := &ocmd.Port{
2018-06-13 10:20:14 +00:00
MetaPortType: ocmm.ToMetaPortType(ocmm.MetaPortTypeEnumTCP),
2018-05-11 02:44:47 +00:00
PortNumber: json.Number(strconv.Itoa(port)),
DiscoveredDate: occu.NowPtr(),
2018-04-19 11:36:56 +00:00
}
p.Host = host
ports[port] = p
return p
}
type PortPacketTCP struct {
IP *layers.IPv4
TCP *layers.TCP
Opts gopacket.SerializeOptions
PacketConn net.PacketConn
}
2018-04-26 09:00:24 +00:00
func makePacketPortTCP(host *ocmd.Host) (*PortPacketTCP, error) {
2018-04-19 11:36:56 +00:00
packetTCP := &PortPacketTCP{}
2018-06-13 10:20:14 +00:00
srcIP := net.ParseIP(host.Zone.Address)
2018-04-19 11:36:56 +00:00
if nil == srcIP {
2018-06-13 10:20:14 +00:00
return nil, fmt.Errorf("Discovery: IP(%s) of zone is not valid", host.Zone.Address)
2018-04-19 11:36:56 +00:00
}
2018-06-13 10:20:14 +00:00
dstIP := net.ParseIP(host.Address)
2018-04-19 11:36:56 +00:00
if nil == dstIP {
2018-06-13 10:20:14 +00:00
return nil, fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address)
2018-04-19 11:36:56 +00:00
}
packetTCP.IP = &layers.IPv4{
SrcIP: srcIP.To4(),
DstIP: dstIP.To4(),
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
packetTCP.TCP = &layers.TCP{
SrcPort: 60000,
DstPort: 0, // will be incremented during the scan
SYN: true,
Seq: 0,
}
packetTCP.Opts = gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
if err != nil {
return nil, fmt.Errorf("Discovery: SYN create socket error %v", err)
}
packetTCP.PacketConn = conn
return packetTCP, nil
}