This commit is contained in:
crusader 2017-12-15 14:58:30 +09:00
parent 8239f564f7
commit c59c3ed31b
17 changed files with 6 additions and 833 deletions

View File

@ -1,82 +0,0 @@
package discovery
import (
"context"
"git.loafle.net/overflow/overflow_discovery/discovery/types"
)
type Discoverer interface {
Discover(endded chan<- error) error
DiscoverZone(endded chan<- error) error
DiscoverHost(endded chan<- error) error
DiscoverPort(endded chan<- error) error
DiscoverService(endded chan<- error) error
DiscoverUDPService(endded chan<- error) error
}
type discovery struct {
zd zoneDiscoverer
hd hostDiscoverer
pd portDiscoverer
endded chan<- error
zoneEndded chan error
hostEndded chan error
portEndded chan error
zoneFound chan *types.DiscoveryZone
}
func New() Discoverer {
d := &discovery{}
d.zd = newZoneDiscoverer()
d.hd = newHostDiscoverer()
d.pd = newPortDiscoverer()
return d
}
func (d *discovery) Discover(endded chan<- error) error {
d.endded = endded
return nil
}
func (d *discovery) DiscoverZone(endded chan<- error) error {
d.endded = endded
// 1. When discoverd zone
// 2. When occurred error
return nil
}
func (d *discovery) DiscoverHost(endded chan<- error) error {
d.endded = endded
return nil
}
func (d *discovery) DiscoverPort(endded chan<- error) error {
d.endded = endded
return nil
}
func (d *discovery) DiscoverService(endded chan<- error) error {
d.endded = endded
return nil
}
func (d *discovery) DiscoverUDPService(endded chan<- error) error {
d.endded = endded
return nil
}
func (d *discovery) Shutdown(ctx context.Context) error {
return nil
}

View File

@ -1,31 +0,0 @@
package discovery
import (
"context"
"git.loafle.net/overflow/overflow_discovery/commons"
)
type hostDiscoverer interface {
commons.EndableStarter
commons.Shutdowner
}
type hostDiscovery struct {
}
func newHostDiscoverer() hostDiscoverer {
hd := &hostDiscovery{}
return hd
}
func (hd *hostDiscovery) EndableStart(endded chan<- error) error {
return nil
}
func (hd *hostDiscovery) Shutdown(ctx context.Context) error {
return nil
}

View File

@ -1,31 +0,0 @@
package discovery
import (
"context"
"git.loafle.net/overflow/overflow_discovery/commons"
)
type portDiscoverer interface {
commons.EndableStarter
commons.Shutdowner
}
type portDiscovery struct {
}
func newPortDiscoverer() portDiscoverer {
pd := &portDiscovery{}
return pd
}
func (pd *portDiscovery) EndableStart(endded chan<- error) error {
return nil
}
func (pd *portDiscovery) Shutdown(ctx context.Context) error {
return nil
}

View File

@ -1,31 +0,0 @@
package discovery
import (
"context"
"git.loafle.net/overflow/overflow_discovery/commons"
)
type serviceDiscoverer interface {
commons.EndableStarter
commons.Shutdowner
}
type serviceDiscovery struct {
}
func newServiceDiscoverer() portDiscoverer {
pd := &serviceDiscovery{}
return pd
}
func (sd *serviceDiscovery) EndableStart(endded chan<- error) error {
return nil
}
func (sd *serviceDiscovery) Shutdown(ctx context.Context) error {
return nil
}

View File

@ -1,21 +0,0 @@
package types
const (
TYPE_TCP = "TCP"
TYPE_UDP = "UDP"
TYPE_TLS = "TLS"
SEND = "Send"
RECV = "Recv"
TIMEOUT = "Timeout"
RECV_RST = "Closed"
SUCCESS = "Success"
TYPE_DISCOVERY = "DISCOVERY"
TYPE_DETECTNETWORK = "DETECTNETWORK"
TYPE_SENSOR = "SENSOR"
RANGE_TYPE_RANGE = "RANGE"
RANGE_TYPE_TARGET = "TARGET"
)

View File

@ -1,28 +0,0 @@
package types
import (
"sync"
"git.loafle.net/overflow/overflow_discovery/discovery/types/timestamp"
)
type DiscoveryHistory struct {
ID int `json:"id,omitempty"`
StartDate timestamp.Timestamp `json:"startDate"`
EndDate timestamp.Timestamp `json:"endDate"`
Result bool `json:"result"`
Zone *DiscoveryZone `json:"zone"`
}
func NewDiscoveryHistory(zone *DiscoveryZone) DiscoveryHistory {
return DiscoveryHistory{
Zone: zone,
StartDate: timestamp.Now(),
}
}
type DiscoveryMgr struct {
Stop chan bool
DiscoveryWg *sync.WaitGroup
Zone *DiscoveryZone
}

View File

@ -1,92 +0,0 @@
package types
import (
"net"
"sync"
"git.loafle.net/overflow/overflow_discovery/discovery/types/timestamp"
)
type DiscoveryHost struct {
M *sync.RWMutex `json:"-"`
Ports_ map[string]*DiscoveryPort `json:"-"`
HostName string `json:"-"`
PortDiscoveryTime timestamp.Timestamp `json:"-"`
Zone *DiscoveryZone `json:"-"`
Histories []*PortScanHistory `json:"-"`
FirstScanRange uint16 `json:"firstScanRange"`
LastScanRange uint16 `json:"lastScanRange"`
Name string `json:"name"`
ID int `json:"id,omitempty"`
IP string `json:"ip"`
Mac string `json:"mac"`
Ports []*DiscoveryPort `json:"ports"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"`
Os string `json:"os,omitempty"`
Target bool `json:"target,omitempty"`
}
func (h *DiscoveryHost) SetPort(key string, value *DiscoveryPort) {
h.M.Lock()
defer h.M.Unlock()
h.Ports_[key] = value
}
func (h *DiscoveryHost) GetPort(key string) *DiscoveryPort {
h.M.RLock()
defer h.M.RUnlock()
return h.Ports_[key]
}
type HostScanHistory struct {
ID int `json:"id,omitempty"`
IP string `json:"ip"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
SendDate timestamp.Timestamp `json:"sendDate"`
ResultDate timestamp.Timestamp `json:"resultDate"`
ResultType string `json:"resultType"`
Description string `json:"description"`
Zone *DiscoveryZone `json:"-"`
}
type HostMgr struct {
DiscoveryMgr
ToPort chan *DiscoveryHost
}
func NewHost(ip net.IP, macAddr net.HardwareAddr) *DiscoveryHost {
host := &DiscoveryHost{
M: new(sync.RWMutex),
Ports_: make(map[string]*DiscoveryPort, 100),
IP: ip.String(),
Mac: macAddr.String(),
CreateDate: timestamp.Now(),
UpdateDate: timestamp.Now(),
}
return host
}
func NewHostHistory(ip net.IP, resultType string, des string) *HostScanHistory {
hh := &HostScanHistory{
IP: ip.String(),
SendDate: timestamp.Now(),
ResultType: resultType,
Description: des,
}
return hh
}
func (p *DiscoveryHost) AddHistory(h *PortScanHistory) {
p.Histories = append(p.Histories, h)
}
type PortRange struct {
Types string
First int
Second int
}

View File

@ -1,65 +0,0 @@
package types
import (
"strconv"
"git.loafle.net/overflow/overflow_discovery/discovery/types/timestamp"
)
type DiscoveryPort struct {
Port_ string `json:"-"`
ID int `json:"id,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"`
Host *DiscoveryHost `json:"-"`
Services []*DiscoveryService `json:"services"`
Histories []*ServiceScanHistory `json:"-"`
PortType string `json:"portType"`
Number uint16 `json:"portNumber"`
Target bool `json:"target,omitempty"`
}
type PortScanHistory struct {
ID int `json:"id,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
Host *DiscoveryHost `json:"-"`
PortNumber uint16 `json:"portNumber"`
PortType string `json:"portType"`
DirectionType string `json:"directionType"`
Description string `json:"description"`
StartDate timestamp.Timestamp `json:"startDate"`
EndDate timestamp.Timestamp `json:"endDate"`
}
type PortMgr struct {
DiscoveryMgr
FromHost chan *DiscoveryHost
ToService chan *DiscoveryPort
}
func NewPort(port string, host *DiscoveryHost, portType string) *DiscoveryPort {
rPort := &DiscoveryPort{Port_: port, Host: host, PortType: portType}
i, _ := strconv.ParseUint(port, 10, 16)
rPort.Number = uint16(i)
return rPort
}
func NewPortScanHistory(host *DiscoveryHost, port uint16, portType string, directionType string, description string) *PortScanHistory {
rPortSH := &PortScanHistory{
CreateDate: timestamp.Now(),
Host: host,
PortNumber: port,
PortType: portType,
DirectionType: directionType,
Description: description,
}
host.AddHistory(rPortSH)
return rPortSH
}
func (p *DiscoveryPort) SetHistory(h []*ServiceScanHistory) {
p.Histories = h
}

View File

@ -1,45 +0,0 @@
package types
import (
"git.loafle.net/overflow/overflow_discovery/discovery/types/timestamp"
)
type DiscoveryService struct {
ID int `json:"id,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"`
Port *DiscoveryPort `json:"-"`
PortType string `json:"portType"` /*tls or normal*/
ServiceName string `json:"serviceName"`
Target bool `json:"target,omitempty"`
}
func NewService(ptype string, serviceName string, port *DiscoveryPort) *DiscoveryService {
service := &DiscoveryService{PortType: ptype, ServiceName: serviceName, Port: port}
return service
}
const (
DIRECTION_SEND = "Send"
DIRECTION_RECV = "Recv"
)
type ServiceScanHistory struct {
ID int `json:"id,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
Port *DiscoveryPort `json:"-"`
ServiceName string `json:"serviceName"`
Direction string `json:"direction"`
Packet []byte `json:"packet"`
}
func NewServiceScanHistory(port *DiscoveryPort, serviceName string, direction string, packet []byte) *ServiceScanHistory {
return &ServiceScanHistory{
CreateDate: timestamp.Now(),
Port: port,
ServiceName: serviceName,
Direction: direction,
Packet: packet,
}
}

View File

@ -1,53 +0,0 @@
package types
import (
"net"
"sync"
)
type discoverTestZoneCallback func(host *DiscoveryZone)
type DiscoveryZone struct {
ID int `json:"id,omitempty"`
IP string `json:"ip"`
Netmask string `json:"mask"`
Iface string `json:"iface"`
Mac string `json:"mac"`
FirstScanRange int64 `json:"firstScanRange"`
LastScanRange int64 `json:"lastScanRange"`
Histories []*HostScanHistory `json:"-"`
Hosts []*DiscoveryHost `json:"hosts"`
CurrentTypes string `json:"-"`
Hosts_ map[string]*DiscoveryHost `json:"-"`
M *sync.RWMutex `json:"-"`
}
func (z *DiscoveryZone) SetHost(ip string, value *DiscoveryHost) {
z.M.Lock()
defer z.M.Unlock()
z.Hosts_[ip] = value
}
func (z *DiscoveryZone) GetHost(ip string) (*DiscoveryHost, bool) {
z.M.RLock()
defer z.M.RUnlock()
h, ok := z.Hosts_[ip]
return h, ok
}
func (p *DiscoveryZone) AddHistory(h *HostScanHistory) {
p.Histories = append(p.Histories, h)
}
func (z *DiscoveryZone) CidrString() string {
netmask := net.IPMask(net.ParseIP(z.Netmask))
str := z.IP + "/" + netmask.String()
_, ipnet, err := net.ParseCIDR(str)
if nil != err {
//logging.Logger.Error(fmt.Sprintf("ParseCIDR: %v", err))
}
return ipnet.String()
}

View File

@ -1,272 +0,0 @@
package types
import (
"encoding/json"
"fmt"
"net"
//"strconv"
//"strings"
"testing"
//"bytes"
//"encoding/binary"
//"bytes"
//"io/ioutil"
"git.loafle.net/overflow/overflow_discovery/discovery/types/timestamp"
//"net/http"
)
type DiscoveryZone222 struct {
MinIp string `json:"-"`
MaxIp string `json:"-"`
LocalIp string `json:"ip"`
Cidr string `json:"cidr"`
Inter net.Interface `json:"-"`
//Hosts map[string]*Host `json:"-"`
}
func TestZone222(t *testing.T) {
zz := DiscoveryZone222{}
zz.MaxIp = "192.168.1.254"
zz.MinIp = "192.168.1.1"
zz.LocalIp = "192.168.1.207"
zz.Cidr = "192.168.1.0/24"
jb, _ := json.Marshal(zz)
fmt.Println(string(jb))
}
type DiscoveryZone1 struct {
MinIp string `json:"-"`
MaxIp string `json:"-"`
Ip_ string `json:"-"`
Cidr_ string `json:"-"`
Inter_ net.Interface `json:"-"`
//Hosts map[string]*Host `json:"-"`
Ip int64 `json:"ip"`
Cidr int64 `json:"cidr"`
HostScanHistories []*DiscoveryHostHistory `json:"hostScanHistories"`
}
type DiscoveryHostHistory struct {
ID int `json:"id"`
Ip int64 `json:"ip"`
CreateDate timestamp.Timestamp `json:"createDate"`
SendDate timestamp.Timestamp `json:"sendDate"`
ResultDate timestamp.Timestamp `json:"resultDate"`
ResultType string `json:"resultType"`
Description string `json:"description"`
}
func TestDiscoveryZone(t *testing.T) {
zones := NewZones()
fmt.Println(len(zones))
fmt.Println(zones[0].LocalIp_)
fmt.Println(zones[0].Cidr)
var zone DiscoveryZone = DiscoveryZone{
MinIp_: "192.168.0.1",
MaxIp_: "192.168.0.254",
Cidr: "192.168.0.0/24",
LocalIp_: "192.168.0.203",
Inter_: net.Interface{},
Hosts_: make(map[string]*DiscoveryHost, 255),
Ip: 233312321,
Cidr_: 123123321,
}
var zones1 []*DiscoveryZone
zones1 = append(zones1, &zone)
z := NewZonesByDiscoveryZone(zones1)
fmt.Println(len(z))
//for _, zone := range zones {
// //a := inet_strton(zone.LocalIp)
// //fmt.Println(a)
// //
// //str := inet_ntoa(a)
// //
// //fmt.Println(str)
//
// //aa := CidrToInt64(zone.Cidr)
// //
// //fmt.Println(aa)
// //
// //str := Int64ToCidr(aa)
// //
// //fmt.Println(str)
//
//}
}
//func CreateDHH(ip string) *DiscoveryHostHistory {
// dhh := &DiscoveryHostHistory{}
//
// dhh.CreateDate = timestamp.Now()
// dhh.SendDate = timestamp.Now()
// dhh.ResultDate = timestamp.Now()
// dhh.Description = "testest"
//
// strIp := "192.168.1."
// strIp += ip
// dhh.Ip = inet_strton(strIp)
//
// return dhh
//}
//
//func TestInsertDZone(t *testing.T) {
//
// dz := DiscoveryZone{}
//
// dz.Cidr_ = "192.168.1.0/24"
// dz.Ip_ = "192.168.1.207"
//
// dz.Cidr = CidrToInt64(dz.Cidr_)
// dz.Ip = inet_strton(dz.Ip_)
//
// dz.HostScanHistories = append(dz.HostScanHistories, CreateDHH("103"), CreateDHH("104"), CreateDHH("105"), CreateDHH("106"))
//
// fmt.Println(inet_ntoa(dz.Ip))
// fmt.Println(Int64ToCidr(dz.Cidr))
//
// jb, _ := json.Marshal(dz)
//
// fmt.Println(string(jb))
//
// resp, _ := http.Post("http://192.168.1.207:8080/discoveryZone", "application/json", bytes.NewBuffer(jb))
// bb, _ := ioutil.ReadAll(resp.Body)
//
// ddzz := DiscoveryZone{}
//
// json.Unmarshal(bb, &ddzz)
//
// fmt.Println(ddzz)
//
// resp.Body.Close()
//
//}
//
//func TestSelectDZone(t *testing.T) {
//
// resp, _ := http.Get("http://192.168.1.207:8080/discoveryZone/1")
// bb, _ := ioutil.ReadAll(resp.Body)
// ddzz := DiscoveryZone{}
//
// fmt.Println(string(bb))
// json.Unmarshal(bb, &ddzz)
//
// fmt.Println(ddzz)
//
// fmt.Println(Int64ToCidr(ddzz.Cidr))
// fmt.Println(inet_ntoa(ddzz.Ip))
//
// resp.Body.Close()
//}
//
//func inet_ntoa(ipnr int64) net.IP {
// var bytes [4]byte
// bytes[0] = byte(ipnr & 0xFF)
// bytes[1] = byte((ipnr >> 8) & 0xFF)
// bytes[2] = byte((ipnr >> 16) & 0xFF)
// bytes[3] = byte((ipnr >> 24) & 0xFF)
//
// return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
//}
//
//// Convert net.IP to int64 , http://www.outofmemory.cn
//func inet_aton(ipnr net.IP) int64 {
// bits := strings.Split(ipnr.String(), ".")
//
// b0, _ := strconv.Atoi(bits[0])
// b1, _ := strconv.Atoi(bits[1])
// b2, _ := strconv.Atoi(bits[2])
// b3, _ := strconv.Atoi(bits[3])
//
// var sum int64
//
// sum += int64(b0) << 24
// sum += int64(b1) << 16
// sum += int64(b2) << 8
// sum += int64(b3)
//
// return sum
//}
//
//func inet_strton(ipnr string) int64 {
// bits := strings.Split(ipnr, ".")
//
// b0, _ := strconv.Atoi(bits[0])
// b1, _ := strconv.Atoi(bits[1])
// b2, _ := strconv.Atoi(bits[2])
// b3, _ := strconv.Atoi(bits[3])
//
// var sum int64
//
// sum += int64(b0) << 24
// sum += int64(b1) << 16
// sum += int64(b2) << 8
// sum += int64(b3)
//
// return sum
//}
//
//func CidrToInt64(cidr string) int64 {
//
// bits := strings.Split(cidr, ".")
//
// b0, _ := strconv.Atoi(bits[0])
// b1, _ := strconv.Atoi(bits[1])
// b2, _ := strconv.Atoi(bits[2])
// //b3, _ := strconv.Atoi(bits[3])
//
// masks := strings.Split(bits[3], "/")
//
// bb3 := masks[0]
// bb4 := masks[1]
//
// b3, _ := strconv.Atoi(bb3)
// b4, _ := strconv.Atoi(bb4)
//
// var sum int64
//
// sum += int64(b0) << 32
// sum += int64(b1) << 24
// sum += int64(b2) << 16
// sum += int64(b3) << 8
// sum += int64(b4)
//
// return sum
//
//}
//
//func Int64ToCidr(cidrInt int64) string {
//
// c1 := cidrInt & 0xFF
// c2 := (cidrInt >> 8) & 0xFF
// c3 := (cidrInt >> 16) & 0xFF
// c4 := (cidrInt >> 24) & 0xFF
// c5 := (cidrInt >> 32) & 0xFF
//
// var cidr string
//
// cidr += strconv.FormatInt(c5, 10)
// cidr += "."
// cidr += strconv.FormatInt(c4, 10)
// cidr += "."
// cidr += strconv.FormatInt(c3, 10)
// cidr += "."
// cidr += strconv.FormatInt(c2, 10)
// cidr += "/"
// cidr += strconv.FormatInt(c1, 10)
//
// return cidr
//}

View File

@ -1,12 +0,0 @@
package types
type ServiceScanInfo struct {
History []*ServiceScanHistory
Port *DiscoveryPort
}
func NewServiceScanInfo(port *DiscoveryPort) *ServiceScanInfo {
s := &ServiceScanInfo{}
s.Port = port
return s
}

View File

@ -1,37 +0,0 @@
package timestamp
import (
"fmt"
"strconv"
"time"
)
type Timestamp time.Time
func (t Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(t).Unix()
stamp := fmt.Sprint(ts * 1000)
return []byte(stamp), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*t = Timestamp(time.Unix(int64(ts)/1000, 0))
return nil
}
func (t Timestamp) String() string {
return time.Time(t).String()
}
func Now() Timestamp {
return Timestamp(time.Now())
}
func Date(year int, month time.Month, day int) Timestamp {
return Timestamp(time.Date(year, month, day, 0, 0, 0, 0, time.UTC))
}

View File

@ -1,31 +0,0 @@
package discovery
import (
"context"
"git.loafle.net/overflow/overflow_discovery/commons"
)
type zoneDiscoverer interface {
commons.EndableStarter
commons.Shutdowner
}
type zoneDiscovery struct {
}
func newZoneDiscoverer() zoneDiscoverer {
zd := &zoneDiscovery{}
return zd
}
func (zd *zoneDiscovery) EndableStart(endded chan<- error) error {
return nil
}
func (zd *zoneDiscovery) Shutdown(ctx context.Context) error {
return nil
}

5
build.sh Executable file
View File

@ -0,0 +1,5 @@
rm ./dist/*
CGO_ENABLED=1 go build -a --installsuffix cgo --ldflags="-s" -o ./dist/overflow_probe_container_discovery
chmod u+s ./dist/overflow_probe_container_discovery
cp ./dist/overflow_probe_container_discovery /project/overFlow/

View File

@ -39,8 +39,8 @@ type PCapScanner interface {
} }
type pCapScan struct { type pCapScan struct {
zone *discoveryM.Zone
pCapHandle *pcap.Handle pCapHandle *pcap.Handle
zone *discoveryM.Zone
arpListenerChanMtx sync.RWMutex arpListenerChanMtx sync.RWMutex
arpListenerChans []chan *layers.ARP arpListenerChans []chan *layers.ARP

View File

@ -19,7 +19,6 @@ var (
func init() { func init() {
pidPath = flag.String("pid-path", "/tmp/discovery-container.pid", "The path of pid file") pidPath = flag.String("pid-path", "/tmp/discovery-container.pid", "The path of pid file")
flag.Parse() flag.Parse()
} }
func main() { func main() {