This commit is contained in:
crusader 2017-10-26 21:55:55 +09:00
parent 1481ab4a7f
commit 44bc4f51d8
12 changed files with 310 additions and 38 deletions

View File

@ -1,13 +0,0 @@
package discovery
type Discoverer interface {
}
type discovery struct {
}
func New() Discoverer {
d := &discovery{}
return d
}

View File

@ -3,3 +3,6 @@ import:
- package: git.loafle.net/commons_go/util - package: git.loafle.net/commons_go/util
- package: github.com/google/gopacket - package: github.com/google/gopacket
version: v1.1.14 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

27
logging.json Normal file
View File

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

87
main.go Normal file
View File

@ -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
*/

View File

@ -1,12 +1,25 @@
package net package net
import (
"git.loafle.net/overflow/overflow_discovery/net/model"
)
type DiscoveryConfig struct { type DiscoveryConfig struct {
ZoneConfig *DiscoveryZoneConfig
HostConfig *DiscoveryHostConfig
PortConfig *DiscoveryPortConfig
ServiceConfig *DiscoveryServiceConfig
} }
type DiscoveryZoneConfig struct { type DiscoveryZoneConfig struct {
ExcludePatterns []string `json:"excludePatterns"`
} }
type DiscoveryHostConfig struct { type DiscoveryHostConfig struct {
DiscoveryZone model.DiscoveryZone
FirstScanRange int `json:"firstScanRange"`
LastScanRange int `json:"lastScanRange"`
ExcludeHosts []int `json:"excludeHosts"`
} }
type DiscoveryPortConfig struct { type DiscoveryPortConfig struct {

1
net/host.go Normal file
View File

@ -0,0 +1 @@
package net

View File

@ -11,3 +11,43 @@ func NewDiscoverer() Discoverer {
return d 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,18 +1,56 @@
package net package net
import "net" import (
import "git.loafle.net/overflow/overflow_discovery/net/model" "net"
import "strings" "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 err error
var ifaces []net.Interface var ifaces []net.Interface
var addrs []net.Addr var addrs []net.Addr
var ipnet *net.IPNet var ipnet *net.IPNet
var zones []*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 { if ifaces, err = net.Interfaces(); nil != err {
logChan <- err zs.logChan <- err
return return
} }
@ -21,17 +59,17 @@ func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan
for _, i := range ifaces { for _, i := range ifaces {
if addrs, err = i.Addrs(); nil != err { if addrs, err = i.Addrs(); nil != err {
logChan <- err zs.logChan <- err
continue continue
} }
for _, addr := range addrs { for _, addr := range addrs {
if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err {
logChan <- err zs.logChan <- err
continue continue
} }
if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) { if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(zs.config.ExcludePatterns, i.Name) {
continue continue
} }
@ -44,14 +82,28 @@ func ScanZone(endChan chan<- bool, zoneChan chan<- *model.DiscoveryZone, logChan
IP: strings.Split(addr.String(), "/")[0], 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 { func checkSameZone(zones []*net.IPNet, ipnet *net.IPNet) bool {
for _, i := range zones { for _, i := range zones {
if i.Contains(ipnet.IP) { if i.Contains(ipnet.IP) {

View File

@ -1,33 +1,30 @@
package net package net
import ( import (
"log"
"testing" "testing"
"git.loafle.net/overflow/overflow_discovery/net/model"
) )
func TestScanZone(t *testing.T) { func TestScanZone(t *testing.T) {
endChan := make(chan bool) dzc := &DiscoveryZoneConfig{
zoneChan := make(chan *model.DiscoveryZone, 4) ExcludePatterns: []string{`^br-`, `^docker`},
logChan := make(chan error, 4) }
zs := newZoneScan(dzc)
defer func() { defer func() {
close(endChan) zs.Close()
close(zoneChan)
close(logChan)
}() }()
go ScanZone(endChan, zoneChan, logChan) go scanZone(zs)
Loop: Loop:
for { for {
select { select {
case zone := <-zoneChan: case zone := <-zs.zoneChan:
log.Printf("zone: %v", zone)
case err := <-logChan: t.Logf("zone: %v", zone)
log.Printf("log: %v", err) case err := <-zs.logChan:
case <-endChan: t.Logf("log: %v", err)
case <-zs.endChan:
break Loop break Loop
} }

View File

@ -0,0 +1,12 @@
package rpc
import (
"log"
)
type DiscoveryService struct {
}
func (ds *DiscoveryService) Start() {
log.Print("DiscoveryService.Start")
}

21
server/server.go Normal file
View File

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

32
server/server_handlers.go Normal file
View File

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