commit 3d6d1bf5ab6d11450994e4b08b07a4b6b9e3ccea Author: snoop Date: Tue Nov 7 16:55:07 2017 +0900 added discovery / timestampe model diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25d2bcf --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Created by .ignore support plugin (hsz.mobi) +### Go template +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + +.idea/ +.vscode/ + + diff --git a/modules/discovery/model/net/config.go b/modules/discovery/model/net/config.go new file mode 100644 index 0000000..6d7f16b --- /dev/null +++ b/modules/discovery/model/net/config.go @@ -0,0 +1,25 @@ +package net + +type DiscoveryConfig struct { + ZoneConfig *DiscoveryZoneConfig + HostConfig *DiscoveryHostConfig + PortConfig *DiscoveryPortConfig + ServiceConfig *DiscoveryServiceConfig +} + +type DiscoveryZoneConfig struct { + ExcludePatterns []string `json:"excludePatterns"` +} + +type DiscoveryHostConfig struct { + DiscoveryZone DiscoveryZone + FirstScanRange int `json:"firstScanRange"` + LastScanRange int `json:"lastScanRange"` + ExcludeHosts []int `json:"excludeHosts"` +} + +type DiscoveryPortConfig struct { +} + +type DiscoveryServiceConfig struct { +} diff --git a/modules/discovery/model/net/host.go b/modules/discovery/model/net/host.go new file mode 100644 index 0000000..4511e14 --- /dev/null +++ b/modules/discovery/model/net/host.go @@ -0,0 +1,16 @@ +package net + +import "git.loafle.net/overflow/overflow_commons_go/modules/timestamp/model" + +type DiscoveryHost struct { + Zone *DiscoveryZone `json:"-"` + ID int `json:"id,omitempty"` + IP string `json:"ip"` + Mac string `json:"mac"` + + Os string `json:"os,omitempty"` + Target bool `json:"target,omitempty"` + + CreateDate timestamp.Timestamp `json:"createDate,omitempty"` + UpdateDate timestamp.Timestamp `json:"updateDate,omitempty"` +} diff --git a/modules/discovery/model/net/network.go b/modules/discovery/model/net/network.go new file mode 100644 index 0000000..8c6a963 --- /dev/null +++ b/modules/discovery/model/net/network.go @@ -0,0 +1,4 @@ +package net + +type DiscoveryNetwork struct { +} diff --git a/modules/discovery/model/net/zone.go b/modules/discovery/model/net/zone.go new file mode 100644 index 0000000..c0f0492 --- /dev/null +++ b/modules/discovery/model/net/zone.go @@ -0,0 +1,11 @@ +package net + +type DiscoveryZone struct { + ID int `json:"id,omitempty"` + Network string `json:"network"` + IP string `json:"ip"` + Iface string `json:"iface"` + Mac string `json:"mac"` + FirstScanRange int64 `json:"firstScanRange"` + LastScanRange int64 `json:"lastScanRange"` +} diff --git a/modules/discovery/model/types/discovery.go b/modules/discovery/model/types/discovery.go new file mode 100644 index 0000000..afc1192 --- /dev/null +++ b/modules/discovery/model/types/discovery.go @@ -0,0 +1,28 @@ +package types + +import ( + "sync" + + "git.loafle.net/overflow/overflow_commons_go/modules/timestamp/model" +) + +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 +} diff --git a/modules/discovery/model/types/discoveryhost.go b/modules/discovery/model/types/discoveryhost.go new file mode 100644 index 0000000..c9e0da3 --- /dev/null +++ b/modules/discovery/model/types/discoveryhost.go @@ -0,0 +1,92 @@ +package types + +import ( + "net" + "sync" + + "git.loafle.net/overflow/overflow_commons_go/modules/timestamp/model" +) + +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 +} diff --git a/modules/discovery/model/types/discoveryport.go b/modules/discovery/model/types/discoveryport.go new file mode 100644 index 0000000..dc8e03e --- /dev/null +++ b/modules/discovery/model/types/discoveryport.go @@ -0,0 +1,65 @@ +package types + +import ( + "strconv" + + "git.loafle.net/overflow/overflow_commons_go/modules/timestamp/model" +) + +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 +} diff --git a/modules/discovery/model/types/discoveryservice.go b/modules/discovery/model/types/discoveryservice.go new file mode 100644 index 0000000..925e995 --- /dev/null +++ b/modules/discovery/model/types/discoveryservice.go @@ -0,0 +1,45 @@ +package types + +import ( + "git.loafle.net/overflow/overflow_commons_go/modules/timestamp/model" +) + +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, + } +} diff --git a/modules/discovery/model/types/discoveryzone.go b/modules/discovery/model/types/discoveryzone.go new file mode 100644 index 0000000..2e86661 --- /dev/null +++ b/modules/discovery/model/types/discoveryzone.go @@ -0,0 +1,53 @@ +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() +} diff --git a/modules/discovery/model/types/scaninfo.go b/modules/discovery/model/types/scaninfo.go new file mode 100644 index 0000000..146f8d9 --- /dev/null +++ b/modules/discovery/model/types/scaninfo.go @@ -0,0 +1,12 @@ +package types + +type ServiceScanInfo struct { + History []*ServiceScanHistory + Port *DiscoveryPort +} + +func NewServiceScanInfo(port *DiscoveryPort) *ServiceScanInfo { + s := &ServiceScanInfo{} + s.Port = port + return s +} diff --git a/modules/timestamp/model/timestamp.go b/modules/timestamp/model/timestamp.go new file mode 100644 index 0000000..6a465d0 --- /dev/null +++ b/modules/timestamp/model/timestamp.go @@ -0,0 +1,37 @@ +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)) +}