added discovery / timestampe model
This commit is contained in:
commit
3d6d1bf5ab
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
|
@ -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/
|
||||
|
||||
|
25
modules/discovery/model/net/config.go
Normal file
25
modules/discovery/model/net/config.go
Normal file
|
@ -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 {
|
||||
}
|
16
modules/discovery/model/net/host.go
Normal file
16
modules/discovery/model/net/host.go
Normal file
|
@ -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"`
|
||||
}
|
4
modules/discovery/model/net/network.go
Normal file
4
modules/discovery/model/net/network.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package net
|
||||
|
||||
type DiscoveryNetwork struct {
|
||||
}
|
11
modules/discovery/model/net/zone.go
Normal file
11
modules/discovery/model/net/zone.go
Normal file
|
@ -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"`
|
||||
}
|
28
modules/discovery/model/types/discovery.go
Normal file
28
modules/discovery/model/types/discovery.go
Normal file
|
@ -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
|
||||
}
|
92
modules/discovery/model/types/discoveryhost.go
Normal file
92
modules/discovery/model/types/discoveryhost.go
Normal file
|
@ -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
|
||||
}
|
65
modules/discovery/model/types/discoveryport.go
Normal file
65
modules/discovery/model/types/discoveryport.go
Normal file
|
@ -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
|
||||
}
|
45
modules/discovery/model/types/discoveryservice.go
Normal file
45
modules/discovery/model/types/discoveryservice.go
Normal file
|
@ -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,
|
||||
}
|
||||
}
|
53
modules/discovery/model/types/discoveryzone.go
Normal file
53
modules/discovery/model/types/discoveryzone.go
Normal file
|
@ -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()
|
||||
}
|
12
modules/discovery/model/types/scaninfo.go
Normal file
12
modules/discovery/model/types/scaninfo.go
Normal file
|
@ -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
|
||||
}
|
37
modules/timestamp/model/timestamp.go
Normal file
37
modules/timestamp/model/timestamp.go
Normal file
|
@ -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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user