This commit is contained in:
crusader 2017-11-15 21:09:38 +09:00
parent f1fac395d5
commit e07d0717a2
12 changed files with 147 additions and 185 deletions

125
discovery/discovery.go Normal file
View File

@ -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() {
}

6
discovery/host.go Normal file
View File

@ -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{}) {
}

View File

@ -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{}{}
}

View File

@ -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 {
}

View File

@ -1 +0,0 @@
package net

View File

@ -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"`
}

View File

@ -1,4 +0,0 @@
package model
type DiscoveryNetwork struct {
}

View File

@ -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"`
}

View File

@ -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() {
}

View File

@ -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
}
}
}

View File

@ -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
}

View File

@ -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() {