From 44bc4f51d8826fd34514333943a89093137f2942 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 26 Oct 2017 21:55:55 +0900 Subject: [PATCH] ing --- discovery.go | 13 ----- glide.yaml | 3 ++ logging.json | 27 ++++++++++ main.go | 87 +++++++++++++++++++++++++++++++++ net/config.go | 13 +++++ net/host.go | 1 + net/net.go | 40 +++++++++++++++ net/zone.go | 72 +++++++++++++++++++++++---- net/zone_test.go | 27 +++++----- server/rpc/discovery_service.go | 12 +++++ server/server.go | 21 ++++++++ server/server_handlers.go | 32 ++++++++++++ 12 files changed, 310 insertions(+), 38 deletions(-) delete mode 100644 discovery.go create mode 100644 logging.json create mode 100644 main.go create mode 100644 net/host.go create mode 100644 server/rpc/discovery_service.go create mode 100644 server/server.go create mode 100644 server/server_handlers.go diff --git a/discovery.go b/discovery.go deleted file mode 100644 index 5f29b87..0000000 --- a/discovery.go +++ /dev/null @@ -1,13 +0,0 @@ -package discovery - -type Discoverer interface { -} - -type discovery struct { -} - -func New() Discoverer { - d := &discovery{} - - return d -} diff --git a/glide.yaml b/glide.yaml index c5a6e2f..d05527e 100644 --- a/glide.yaml +++ b/glide.yaml @@ -3,3 +3,6 @@ import: - package: git.loafle.net/commons_go/util - package: github.com/google/gopacket version: v1.1.14 +- package: git.loafle.net/commons_go/local_socket.git +- package: git.loafle.net/commons_go/server +- package: git.loafle.net/commons_go/rpc diff --git a/logging.json b/logging.json new file mode 100644 index 0000000..bfe748f --- /dev/null +++ b/logging.json @@ -0,0 +1,27 @@ +{ + "level": "debug", + "development": true, + "disableCaller": true, + "disableStacktrace": true, + "sampling": { + "initial": 100, + "thereafter": 100 + }, + "encoding": "console", + "encoderConfig": { + "messageKey": "message", + "levelKey": "level", + "timeKey": "time", + "nameKey": "name", + "callerKey": "caller", + "stacktraceKey": "stacktrace", + "lineEnding": "\n", + "levelEncoder": "color", + "timeEncoder": "ISO8601", + "durationEncoder": "string", + "callerEncoder": "full", + "nameEncoder": "full" + }, + "outputPaths": ["stdout", "/tmp/logs"], + "errorOutputPaths": ["stderr"] +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..4b1da6e --- /dev/null +++ b/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "log" + + "git.loafle.net/overflow/overflow_discovery/server" +) + +func main() { + // s := local_socket.NewLocalServer("discovery") + // s.SetOnConnectionCallback(func(socket net.Conn) { + // defer socket.Close() + // var wg sync.WaitGroup + // wg.Add(2) + // go func() { + // writer := bufio.NewWriter(socket) + // count := rand.Intn(20) + // fmt.Printf("send %d items\n", count) + // for i := 0; i < count; i++ { + // fmt.Printf("write %d\n", i) + // fmt.Fprintf(writer, "%d\n", i) + // } + // writer.WriteString("end\n") + // writer.Flush() + // fmt.Println("done write") + // wg.Done() + // }() + // go func() { + // reader := bufio.NewReader(socket) + // counter := 1 + // for { + // content, err := reader.ReadString('\n') + // if err != nil { + // fmt.Println(err) + // break + // } + // fmt.Printf("read %d: '%s'\n", counter, strings.TrimRight(content, "\n")) + // if content == "end\n" { + // break + // } + // counter++ + // } + // fmt.Println("done read") + // wg.Done() + // }() + // wg.Wait() + // }) + + // stop := make(chan os.Signal) + // signal.Notify(stop, syscall.SIGINT) + // var wg sync.WaitGroup + // wg.Add(1) + // go func() { + // defer wg.Done() + // s.ListenAndServe() + // }() + + // fmt.Printf("Serving Server at %s\n", s.Path()) + // select { + // case signal := <-stop: + // fmt.Printf("Got signal: %v\n", signal) + // } + // fmt.Printf("Stopping listener\n") + // s.Close() + // fmt.Printf("Waiting on server\n") + // wg.Wait() + + s := server.New("discovery") + + if err := s.Serve(); err != nil { + log.Fatalf("Cannot start rpc server: %s", err) + } +} + +/** +azure +consul +dns +ec2 +file +gce +kubernetes +marathon +openstack +triton +zookeeper +*/ diff --git a/net/config.go b/net/config.go index d81fb1c..d333f75 100644 --- a/net/config.go +++ b/net/config.go @@ -1,12 +1,25 @@ 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 { diff --git a/net/host.go b/net/host.go new file mode 100644 index 0000000..9d9f1a1 --- /dev/null +++ b/net/host.go @@ -0,0 +1 @@ +package net diff --git a/net/net.go b/net/net.go index 3bc6a17..53f3d4e 100644 --- a/net/net.go +++ b/net/net.go @@ -11,3 +11,43 @@ func NewDiscoverer() Discoverer { 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.go b/net/zone.go index efb6c4b..dcd4935 100644 --- a/net/zone.go +++ b/net/zone.go @@ -1,18 +1,56 @@ package net -import "net" -import "git.loafle.net/overflow/overflow_discovery/net/model" -import "strings" +import ( + "net" + "regexp" + "strings" -func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan chan<- error) { + "git.loafle.net/overflow/overflow_discovery/net/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) { var err error var ifaces []net.Interface var addrs []net.Addr var ipnet *net.IPNet var zones []*net.IPNet + // var gwIP net.IP + // var gwIFace string + + // if gwIP, gwIFace, err = gateway.DiscoverGateway(); nil != err { + // logChan <- err + // return + // } if ifaces, err = net.Interfaces(); nil != err { - logChan <- err + zs.logChan <- err return } @@ -21,17 +59,17 @@ func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan for _, i := range ifaces { if addrs, err = i.Addrs(); nil != err { - logChan <- err + zs.logChan <- err continue } for _, addr := range addrs { if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { - logChan <- err + zs.logChan <- err continue } - if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) { + if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(zs.config.ExcludePatterns, i.Name) { continue } @@ -44,14 +82,28 @@ func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan IP: strings.Split(addr.String(), "/")[0], } - zoneChan <- dz + zs.zoneChan <- dz } } - endChan <- true + zs.endChan <- true } +func checkExclude(ep []string, iface string) bool { + var r *regexp.Regexp + var err error + for _, p := range ep { + if r, err = regexp.Compile(p); nil != err { + return false + } + if r.MatchString(iface) { + return true + } + } + return false +} + func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool { for _, i := range zones { if i.Contains(ipnet.IP) { diff --git a/net/zone_test.go b/net/zone_test.go index 3f4d07f..f744a8d 100644 --- a/net/zone_test.go +++ b/net/zone_test.go @@ -1,33 +1,30 @@ package net import ( - "log" "testing" - - "git.loafle.net/overflow/overflow_discovery/net/model" ) func TestScanZone(t *testing.T) { - endChan := make(chan bool) - zoneChan := make(chan *model.DiscoveryZone, 4) - logChan := make(chan error, 4) + dzc := &DiscoveryZoneConfig{ + ExcludePatterns: []string{`^br-`, `^docker`}, + } + zs := newZoneScan(dzc) defer func() { - close(endChan) - close(zoneChan) - close(logChan) + zs.Close() }() - go ScanZone(endChan, zoneChan, logChan) + go scanZone(zs) Loop: for { select { - case zone := <-zoneChan: - log.Printf("zone: %v", zone) - case err := <-logChan: - log.Printf("log: %v", err) - case <-endChan: + 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/server/rpc/discovery_service.go b/server/rpc/discovery_service.go new file mode 100644 index 0000000..f6a009d --- /dev/null +++ b/server/rpc/discovery_service.go @@ -0,0 +1,12 @@ +package rpc + +import ( + "log" +) + +type DiscoveryService struct { +} + +func (ds *DiscoveryService) Start() { + log.Print("DiscoveryService.Start") +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..a4a93de --- /dev/null +++ b/server/server.go @@ -0,0 +1,21 @@ +package server + +import ( + "git.loafle.net/commons_go/rpc" + "git.loafle.net/commons_go/rpc/protocol/json" + "git.loafle.net/commons_go/server" + + dRPC "git.loafle.net/overflow/overflow_discovery/server/rpc" +) + +func New(addr string) server.Server { + registry := rpc.NewRegistry() + registry.RegisterCodec(json.NewCodec(), "json") + registry.RegisterService(new(dRPC.DiscoveryService), "") + + sh := NewServerHandler(addr, registry) + + s := server.NewServer(sh) + + return s +} diff --git a/server/server_handlers.go b/server/server_handlers.go new file mode 100644 index 0000000..6044cb7 --- /dev/null +++ b/server/server_handlers.go @@ -0,0 +1,32 @@ +package server + +import ( + "io" + + "git.loafle.net/commons_go/rpc" + "git.loafle.net/commons_go/server" + "git.loafle.net/commons_go/server/ipc" +) + +func NewServerHandler(addr string, registry rpc.Registry) ServerHandler { + sh := &ServerHandlers{ + registry: registry, + } + sh.Addr = addr + + return sh +} + +type ServerHandler interface { + server.ServerHandler +} + +type ServerHandlers struct { + ipc.ServerHandlers + + registry rpc.Registry +} + +func (sh *ServerHandlers) Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{}) { + +}