From e07d0717a24e5939dec71085920703bab7362a64 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 15 Nov 2017 21:09:38 +0900 Subject: [PATCH] ing --- discovery/discovery.go | 125 +++++++++++++++++++++++++++++++++++++ discovery/host.go | 6 ++ {net => discovery}/zone.go | 47 +++----------- net/config.go | 29 --------- net/host.go | 1 - net/model/host.go | 16 ----- net/model/network.go | 4 -- net/model/zone.go | 11 ---- net/net.go | 53 ---------------- net/zone_test.go | 32 ---------- rpc/discovery_service.go | 3 + server/server_handlers.go | 5 +- 12 files changed, 147 insertions(+), 185 deletions(-) create mode 100644 discovery/discovery.go create mode 100644 discovery/host.go rename {net => discovery}/zone.go (62%) delete mode 100644 net/config.go delete mode 100644 net/host.go delete mode 100644 net/model/host.go delete mode 100644 net/model/network.go delete mode 100644 net/model/zone.go delete mode 100644 net/net.go delete mode 100644 net/zone_test.go diff --git a/discovery/discovery.go b/discovery/discovery.go new file mode 100644 index 0000000..7d44db0 --- /dev/null +++ b/discovery/discovery.go @@ -0,0 +1,125 @@ +package discovery + +import ( + "fmt" + "sync" + + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" +) + +var discoverer *discovery + +func DiscoveryInit() { + discoverer = &discovery{} + discoverer.start() +} + +func DiscoveryDestroy() { + discoverer.stop() + discoverer = nil +} + +func DiscoverZone(dz *model.DiscoveryZone) { + discoverer.discoverZone(dz) +} + +func DiscoverHost(dh *model.DiscoveryHost) { + discoverer.discoverHost(dh) +} + +func DiscoverPort(dp *model.DiscoveryPort) { + discoverer.discoverPort(dp) +} + +func DiscoverService(ds *model.DiscoveryService) { + discoverer.discoverService(ds) +} + +func Stop() { + +} + +type discovery struct { + sendChan chan interface{} + errChan chan error + + stopChan chan struct{} + stopWg sync.WaitGroup +} + +func (d *discovery) start() { + if d.stopChan != nil { + panic("Discovery: discovery is already running. Stop it before starting it again") + } + + d.stopChan = make(chan struct{}) + d.sendChan = make(chan interface{}) + d.errChan = make(chan error) + logging.Logger().Info(fmt.Sprintf("Discovery: discovery is started")) + + d.stopWg.Add(1) + go handle(d) +} + +func (d *discovery) stop() { + if d.stopChan == nil { + panic("Discovery: discovery must be started before stopping it") + } + close(d.stopChan) + d.stopWg.Wait() + d.stopChan = nil + + logging.Logger().Info(fmt.Sprintf("Discovery: discovery is stopped")) +} + +func handle(d *discovery) { + defer d.stopWg.Done() + + for { + select { + case <-d.stopChan: + return + case data := <-d.sendChan: + logging.Logger().Info(fmt.Sprintf("chan: %v", data)) + } + } +} + +func (d *discovery) discoverZone(dz *model.DiscoveryZone) { + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start")) + + doneChan := make(chan struct{}) + + d.stopWg.Add(1) + defer d.stopWg.Done() + + go scanZone(d, dz, doneChan) + + select { + case <-d.stopChan: + <-doneChan + case <-doneChan: + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete")) + } +} + +func (d *discovery) discoverHost(dh *model.DiscoveryHost) { + logging.Logger().Debug(fmt.Sprintf("DiscoverHost")) +} + +func (d *discovery) discoverPort(dp *model.DiscoveryPort) { + logging.Logger().Debug(fmt.Sprintf("DiscoverPort")) +} + +func (d *discovery) discoverService(ds *model.DiscoveryService) { + logging.Logger().Debug(fmt.Sprintf("DiscoverService")) +} + +func (d *discovery) sendResult() { + +} + +func (d *discovery) sendError() { + +} diff --git a/discovery/host.go b/discovery/host.go new file mode 100644 index 0000000..e682b76 --- /dev/null +++ b/discovery/host.go @@ -0,0 +1,6 @@ +package discovery + +import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" + +func scanHost(d *discovery, dh *model.DiscoveryHost, doneChan chan<- struct{}) { +} diff --git a/net/zone.go b/discovery/zone.go similarity index 62% rename from net/zone.go rename to discovery/zone.go index dcd4935..a3e52f2 100644 --- a/net/zone.go +++ b/discovery/zone.go @@ -1,41 +1,14 @@ -package net +package discovery import ( "net" "regexp" "strings" - "git.loafle.net/overflow/overflow_discovery/net/model" + "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" ) -type ZoneScanner interface { - -} - -type zoneScan struct { - config *DiscoveryZoneConfig - endChan chan bool - zoneChan chan *model.DiscoveryZone - logChan chan error -} - -func newZoneScan(dzc *DiscoveryZoneConfig) *zoneScan { - zs := &zoneScan{ - config: dzc, - endChan: make(chan bool), - zoneChan: make(chan *model.DiscoveryZone, 4), - logChan: make(chan error, 4), - } - return zs -} - -func (zs *zoneScan) Close() { - close(zs.endChan) - close(zs.zoneChan) - close(zs.logChan) -} - -func scanZone(zs *zoneScan) { +func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { var err error var ifaces []net.Interface var addrs []net.Addr @@ -50,7 +23,7 @@ func scanZone(zs *zoneScan) { // } if ifaces, err = net.Interfaces(); nil != err { - zs.logChan <- err + d.errChan <- err return } @@ -59,34 +32,34 @@ func scanZone(zs *zoneScan) { for _, i := range ifaces { if addrs, err = i.Addrs(); nil != err { - zs.logChan <- err + d.errChan <- err continue } for _, addr := range addrs { if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { - zs.logChan <- err + d.errChan <- err continue } - if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(zs.config.ExcludePatterns, i.Name) { + if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) { continue } zones = append(zones, ipnet) - dz := &model.DiscoveryZone{ + z := &model.Zone{ Network: ipnet.String(), Iface: i.Name, Mac: i.HardwareAddr.String(), IP: strings.Split(addr.String(), "/")[0], } - zs.zoneChan <- dz + d.sendChan <- z } } - zs.endChan <- true + doneChan <- struct{}{} } diff --git a/net/config.go b/net/config.go deleted file mode 100644 index d333f75..0000000 --- a/net/config.go +++ /dev/null @@ -1,29 +0,0 @@ -package net - -import ( - "git.loafle.net/overflow/overflow_discovery/net/model" -) - -type DiscoveryConfig struct { - ZoneConfig *DiscoveryZoneConfig - HostConfig *DiscoveryHostConfig - PortConfig *DiscoveryPortConfig - ServiceConfig *DiscoveryServiceConfig -} - -type DiscoveryZoneConfig struct { - ExcludePatterns []string `json:"excludePatterns"` -} - -type DiscoveryHostConfig struct { - DiscoveryZone model.DiscoveryZone - FirstScanRange int `json:"firstScanRange"` - LastScanRange int `json:"lastScanRange"` - ExcludeHosts []int `json:"excludeHosts"` -} - -type DiscoveryPortConfig struct { -} - -type DiscoveryServiceConfig struct { -} diff --git a/net/host.go b/net/host.go deleted file mode 100644 index 9d9f1a1..0000000 --- a/net/host.go +++ /dev/null @@ -1 +0,0 @@ -package net diff --git a/net/model/host.go b/net/model/host.go deleted file mode 100644 index 09c3fa2..0000000 --- a/net/model/host.go +++ /dev/null @@ -1,16 +0,0 @@ -package model - -import "git.loafle.net/overflow/overflow_probe/model/timestamp" - -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/net/model/network.go b/net/model/network.go deleted file mode 100644 index 5cb1c85..0000000 --- a/net/model/network.go +++ /dev/null @@ -1,4 +0,0 @@ -package model - -type DiscoveryNetwork struct { -} diff --git a/net/model/zone.go b/net/model/zone.go deleted file mode 100644 index 958b441..0000000 --- a/net/model/zone.go +++ /dev/null @@ -1,11 +0,0 @@ -package model - -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/net/net.go b/net/net.go deleted file mode 100644 index 53f3d4e..0000000 --- a/net/net.go +++ /dev/null @@ -1,53 +0,0 @@ -package net - -type Discoverer interface { -} - -type discovery struct { -} - -func NewDiscoverer() Discoverer { - d := &discovery{} - - return d -} - -func (d *discovery) Discover() { - -} - -func (d *discovery) DiscoverZone(dzc *DiscoveryZoneConfig) { - zs := newZoneScan(dzc) - - defer func() { - zs.Close() - }() - - go scanZone(zs) - -Loop: - for { - select { - case zone := <-zs.zoneChan: - - case err := <-zs.logChan: - - case <-zs.endChan: - break Loop - } - - } - -} - -func (d *discovery) DiscoverHost() { - -} - -func (d *discovery) DiscoverPort() { - -} - -func (d *discovery) DiscoverService() { - -} diff --git a/net/zone_test.go b/net/zone_test.go deleted file mode 100644 index f744a8d..0000000 --- a/net/zone_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package net - -import ( - "testing" -) - -func TestScanZone(t *testing.T) { - dzc := &DiscoveryZoneConfig{ - ExcludePatterns: []string{`^br-`, `^docker`}, - } - zs := newZoneScan(dzc) - - defer func() { - zs.Close() - }() - - go scanZone(zs) - -Loop: - for { - select { - case zone := <-zs.zoneChan: - - t.Logf("zone: %v", zone) - case err := <-zs.logChan: - t.Logf("log: %v", err) - case <-zs.endChan: - break Loop - } - - } -} diff --git a/rpc/discovery_service.go b/rpc/discovery_service.go index 0b26d33..ac302b4 100644 --- a/rpc/discovery_service.go +++ b/rpc/discovery_service.go @@ -2,6 +2,7 @@ package rpc import ( "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" + "git.loafle.net/overflow/overflow_discovery/discovery" ) type DiscoveryService struct { @@ -9,6 +10,8 @@ type DiscoveryService struct { func (ds *DiscoveryService) DiscoverZone(dz *model.DiscoveryZone) error { + discovery.DiscoverZone(dz) + return nil } diff --git a/server/server_handlers.go b/server/server_handlers.go index 02d9ae6..d16bbbd 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -6,6 +6,7 @@ import ( "net" "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_discovery/discovery" crs "git.loafle.net/commons_go/rpc/server" "git.loafle.net/commons_go/server" @@ -34,7 +35,7 @@ func (sh *ServerHandlers) Init() error { } func (sh *ServerHandlers) OnStart() { - // no op + discovery.DiscoveryInit() } func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) { @@ -73,7 +74,7 @@ func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneCh } func (sh *ServerHandlers) OnStop() { - // no op + discovery.DiscoveryDestroy() } func (sh *ServerHandlers) Validate() {