ing
This commit is contained in:
parent
30075ad015
commit
b099b2756e
48
Gopkg.toml
48
Gopkg.toml
|
@ -25,6 +25,54 @@
|
|||
# unused-packages = true
|
||||
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/annotation-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/di-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/log-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/model"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/rpc-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/server-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/service_matcher-go"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "git.loafle.net/overflow/util-go"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/google/gopacket"
|
||||
version = "1.1.14"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/k-sone/snmpgo"
|
||||
version = "3.2.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/valyala/fasthttp"
|
||||
version = "20180529.0.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "go.uber.org/zap"
|
||||
version = "1.9.1"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
unused-packages = true
|
||||
|
|
17
annotation/service.go
Normal file
17
annotation/service.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
oa "git.loafle.net/overflow/annotation-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
oa.Register(ServiceAnnotationType)
|
||||
}
|
||||
|
||||
var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil))
|
||||
|
||||
type ServiceAnnotation struct {
|
||||
oa.Annotation `@name:"@Service"`
|
||||
}
|
|
@ -3,9 +3,9 @@ package discoverer
|
|||
import (
|
||||
"sync"
|
||||
|
||||
"git.loafle.net/commons/util-go/net/cidr"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
"git.loafle.net/overflow/model/util"
|
||||
"git.loafle.net/overflow/util-go/net/cidr"
|
||||
)
|
||||
|
||||
type DiscoveryDataType int
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strconv"
|
||||
"testing"
|
||||
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
olog "git.loafle.net/overflow/log-go"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
|
@ -70,7 +70,7 @@ var (
|
|||
|
||||
func init() {
|
||||
// runtime.LockOSThread()
|
||||
logging.InitializeLogger("")
|
||||
olog.InitializeLogger("")
|
||||
}
|
||||
|
||||
func TestDiscoverZone(t *testing.T) {
|
||||
|
|
|
@ -7,10 +7,10 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.loafle.net/commons/util-go/net/cidr"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
"git.loafle.net/overflow/util-go/net/cidr"
|
||||
"git.loafle.net/overflow_scanner/probe/pcap"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
|
|
|
@ -9,12 +9,13 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
olog "git.loafle.net/overflow/log-go"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
"git.loafle.net/overflow_scanner/probe/pcap"
|
||||
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
)
|
||||
|
@ -49,7 +50,7 @@ func scanPortTCP(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface
|
|||
select {
|
||||
case packet, ok := <-tcpChan:
|
||||
if !ok {
|
||||
logging.Logger().Debugf("Discovery: tcp channel is closed")
|
||||
olog.Logger().Debug("Discovery: tcp channel is closed")
|
||||
return
|
||||
}
|
||||
delay.Store(true)
|
||||
|
@ -134,7 +135,7 @@ func handlePacketTCP(host *omd.Host, dp *omd.DiscoverPort, ports map[int]*omd.Po
|
|||
if _, ok := ports[port]; ok || !dp.Contains(port) {
|
||||
return nil
|
||||
}
|
||||
logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.Address)
|
||||
olog.Logger().Debug("Discovery", zap.String("ip", host.Address), zap.Int("port", port))
|
||||
|
||||
p := &omd.Port{
|
||||
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
|
||||
|
|
|
@ -9,13 +9,14 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
olog "git.loafle.net/overflow/log-go"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
"git.loafle.net/overflow_scanner/probe/matcher"
|
||||
"git.loafle.net/overflow_scanner/probe/pcap"
|
||||
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
)
|
||||
|
@ -51,7 +52,7 @@ func scanPortUDP(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface
|
|||
select {
|
||||
case packet, ok := <-udpChan:
|
||||
if !ok {
|
||||
logging.Logger().Debugf("Discovery: udp channel is closed")
|
||||
olog.Logger().Debug("Discovery: udp channel is closed")
|
||||
return
|
||||
}
|
||||
delay.Store(true)
|
||||
|
@ -121,7 +122,7 @@ func sendUDP(host *omd.Host, dp *omd.DiscoverPort, stopChan chan struct{}) error
|
|||
for i := 0; i < m.PacketCount(); i++ {
|
||||
p := m.Packet(i)
|
||||
if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil {
|
||||
logging.Logger().Errorf("Discovery: UDP write error %v", err)
|
||||
olog.Logger().Error("Discovery: UDP write error", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,7 +152,7 @@ func handlePacketUDP(host *omd.Host, dp *omd.DiscoverPort, ports map[int]*omd.Po
|
|||
|
||||
srcIP := ipLayer.(*layers.IPv4).SrcIP
|
||||
port := int(udp.SrcPort)
|
||||
logging.Logger().Debugf("Discovery: IP of UDP(%d) src %v", port, srcIP)
|
||||
olog.Logger().Debug("Discovery", zap.String("ip", srcIP.String()), zap.Int("port", port))
|
||||
if _, ok := ports[port]; ok || !dp.Contains(port) {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,15 +3,15 @@ package ipv4
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
cuej "git.loafle.net/commons/util-go/encoding/json"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
ouej "git.loafle.net/overflow/util-go/encoding/json"
|
||||
"github.com/google/gopacket/layers"
|
||||
)
|
||||
|
||||
func ScanService(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
|
||||
portNumber, err := cuej.NumberToInt(port.PortNumber)
|
||||
portNumber, err := ouej.NumberToInt(port.PortNumber)
|
||||
if err != nil {
|
||||
errChan <- fmt.Errorf("Discovery: Service scan port[%s] error %v ", port.PortNumber, err)
|
||||
return
|
||||
|
|
|
@ -5,12 +5,11 @@ import (
|
|||
"net"
|
||||
"time"
|
||||
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
cuej "git.loafle.net/commons/util-go/encoding/json"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
osm "git.loafle.net/overflow/service_matcher-go"
|
||||
cuej "git.loafle.net/overflow/util-go/encoding/json"
|
||||
"git.loafle.net/overflow_scanner/probe/matcher"
|
||||
)
|
||||
|
||||
|
@ -43,12 +42,12 @@ LOOP:
|
|||
errChan <- fmt.Errorf("Discovery: Service scan[%s] on %s:%d error has occurred %v ", sc.MetaCryptoType().Key, hostIP, portNumber, err)
|
||||
return false
|
||||
}
|
||||
logging.Logger().Debugf("Discovery: Service scan connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key)
|
||||
// olog.Logger().Debug("Discovery: Service scan connected", zap.String("hostIP", hostIP), zap.Int("portNumber", portNumber), zap.String("MetaCryptoType", sc.MetaCryptoType().Key))
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
|
||||
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
||||
logging.Logger().Debugf("Discovery: cannot set readdeadline connected[%s:%d] %s", hostIP, portNumber, sc.MetaCryptoType().Key)
|
||||
// olog.Logger().Debug("Discovery: cannot set readdeadline connected", zap.String("hostIP", hostIP), zap.Int("portNumber", portNumber), zap.String("MetaCryptoType", sc.MetaCryptoType().Key))
|
||||
return false
|
||||
}
|
||||
rn, err := conn.Read(buf)
|
||||
|
@ -93,8 +92,8 @@ func hadlePrePacket(info osm.MatchInfo, sc serviceConnector, conn net.Conn, pack
|
|||
conn.Close()
|
||||
}()
|
||||
|
||||
// logging.Logger().Debugf("Discovery: Service scan pre packet length[%d], buf[%v]", packet.Len, packet.Buffer)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d pre packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), packet.Len)
|
||||
// olog.Logger().Debugf("Discovery: Service scan pre packet length[%d], buf[%v]", packet.Len, packet.Buffer)
|
||||
// olog.Logger().Debug("Discovery: Service scan[%s] on %s:%d pre packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), packet.Len)
|
||||
|
||||
ms := matcher.GetTCPMatchers(true)
|
||||
buf := make([]byte, 1024)
|
||||
|
@ -116,40 +115,40 @@ LOOP:
|
|||
|
||||
for j := 0; j < packetCount; j++ {
|
||||
tPacket := m.Packet(j)
|
||||
// logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
|
||||
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
||||
logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
break LOOP
|
||||
}
|
||||
wn, err := conn.Write(tPacket.Buffer)
|
||||
if nil != err {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
continue LOOP
|
||||
}
|
||||
if wn != tPacket.Len {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
|
||||
continue LOOP
|
||||
}
|
||||
|
||||
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
||||
logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
break LOOP
|
||||
}
|
||||
rn, err := conn.Read(buf)
|
||||
if nil != err {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
break LOOP
|
||||
}
|
||||
|
||||
if err := m.Match(info, j+1, osm.NewPacket(buf, rn)); err == nil {
|
||||
// logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
// olog.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
found = true
|
||||
} else {
|
||||
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
// olog.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
found = false
|
||||
continue LOOP
|
||||
}
|
||||
|
@ -183,32 +182,32 @@ LOOP:
|
|||
|
||||
conn, err := sc.Dial(info.IP(), info.Port())
|
||||
if err != nil {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d socket dial error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d socket dial error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
break LOOP
|
||||
}
|
||||
|
||||
packetCount := m.PacketCount()
|
||||
for j := 0; j < packetCount; j++ {
|
||||
tPacket := m.Packet(j)
|
||||
// logging.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: Service scan send packet length[%d], buf[%v]", tPacket.Len, tPacket.Buffer)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
|
||||
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
||||
logging.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: cannot set writeDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
break
|
||||
}
|
||||
wn, err := conn.Write(tPacket.Buffer)
|
||||
if nil != err {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
break
|
||||
}
|
||||
if wn != tPacket.Len {
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d send packet length[%d] not same with %d", sc.MetaCryptoType().Key, info.IP(), info.Port(), wn, tPacket.Len)
|
||||
break
|
||||
}
|
||||
|
||||
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
||||
logging.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
// olog.Logger().Debugf("Discovery: cannot set readDeadLine Service scan[%s] on %s:%d send packet length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), tPacket.Len)
|
||||
break
|
||||
}
|
||||
rn, err := conn.Read(buf)
|
||||
|
@ -218,7 +217,7 @@ LOOP:
|
|||
break
|
||||
}
|
||||
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive packet error %v", sc.MetaCryptoType().Key, info.IP(), info.Port(), err)
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -228,12 +227,12 @@ LOOP:
|
|||
break
|
||||
}
|
||||
|
||||
// logging.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
// olog.Logger().Debugf("Discovery: Service scan receive match length[%d], buf[%v]", rn, buf)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
continue
|
||||
} else {
|
||||
// logging.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
|
||||
logging.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
// olog.Logger().Debugf("Discovery: Service scan receive not match length[%d], buf[%v]", rn, buf)
|
||||
// olog.Logger().Debugf("Discovery: Service scan[%s] on %s:%d receive not match length[%d]", sc.MetaCryptoType().Key, info.IP(), info.Port(), rn)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,16 +3,16 @@ package ipv4
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
cuej "git.loafle.net/commons/util-go/encoding/json"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
osm "git.loafle.net/overflow/service_matcher-go"
|
||||
ouej "git.loafle.net/overflow/util-go/encoding/json"
|
||||
"git.loafle.net/overflow_scanner/probe/matcher"
|
||||
)
|
||||
|
||||
func scanServiceUDP(port *omd.Port, ds *omd.DiscoverService, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) bool {
|
||||
portNumber, err := cuej.NumberToInt(port.PortNumber)
|
||||
portNumber, err := ouej.NumberToInt(port.PortNumber)
|
||||
if err != nil {
|
||||
errChan <- fmt.Errorf("Discovery: Service scan port[%s] error %v ", port.PortNumber, err)
|
||||
return false
|
||||
|
|
|
@ -4,10 +4,10 @@ import (
|
|||
"net"
|
||||
"regexp"
|
||||
|
||||
cun "git.loafle.net/commons/util-go/net"
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
omu "git.loafle.net/overflow/model/util"
|
||||
oun "git.loafle.net/overflow/util-go/net"
|
||||
)
|
||||
|
||||
func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
|
||||
|
@ -18,8 +18,8 @@ func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan er
|
|||
var err error
|
||||
var ifaces []net.Interface
|
||||
var addrs []net.Addr
|
||||
var ipnet *cun.IPNet
|
||||
var zones []*cun.IPNet
|
||||
var ipnet *oun.IPNet
|
||||
var zones []*oun.IPNet
|
||||
// var gwIP net.IP
|
||||
// var gwIFace string
|
||||
|
||||
|
@ -33,7 +33,7 @@ func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan er
|
|||
return
|
||||
}
|
||||
|
||||
zones = make([]*cun.IPNet, 0)
|
||||
zones = make([]*oun.IPNet, 0)
|
||||
|
||||
for _, i := range ifaces {
|
||||
|
||||
|
@ -44,7 +44,7 @@ func scanZone(dz *omd.DiscoverZone, resultChan chan interface{}, errChan chan er
|
|||
|
||||
for _, addr := range addrs {
|
||||
|
||||
if _, ipnet, err = cun.ParseCIDR(addr.String()); nil != err {
|
||||
if _, ipnet, err = oun.ParseCIDR(addr.String()); nil != err {
|
||||
errChan <- err
|
||||
continue
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ func checkExclude(ep []string, iface string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func checkSameZone(zones []*cun.IPNet, ipnet *cun.IPNet) bool {
|
||||
func checkSameZone(zones []*oun.IPNet, ipnet *oun.IPNet) bool {
|
||||
for _, i := range zones {
|
||||
if i.Contains(ipnet.IP) {
|
||||
return true
|
||||
|
|
39
main.go
39
main.go
|
@ -1,5 +1,42 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.loafle.net/overflow_scanner/probe/server"
|
||||
)
|
||||
|
||||
func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := server.New()
|
||||
|
||||
go func() {
|
||||
err := s.ListenAndServe()
|
||||
if nil != err {
|
||||
log.Printf("err: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt,
|
||||
syscall.SIGKILL,
|
||||
syscall.SIGSTOP,
|
||||
syscall.SIGHUP,
|
||||
syscall.SIGINT,
|
||||
syscall.SIGTERM,
|
||||
syscall.SIGQUIT)
|
||||
|
||||
<-interrupt
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := s.Shutdown(ctx); err != nil {
|
||||
log.Printf("error: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
138
server/scanner-servlet.go
Normal file
138
server/scanner-servlet.go
Normal file
|
@ -0,0 +1,138 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
olog "git.loafle.net/overflow/log-go"
|
||||
orp "git.loafle.net/overflow/rpc-go/protocol"
|
||||
orr "git.loafle.net/overflow/rpc-go/registry"
|
||||
"git.loafle.net/overflow/server-go"
|
||||
oss "git.loafle.net/overflow/server-go/socket"
|
||||
ossw "git.loafle.net/overflow/server-go/socket/web"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type ScannerServlet interface {
|
||||
ossw.Servlet
|
||||
}
|
||||
|
||||
type ScannerServlets struct {
|
||||
ossw.Servlets
|
||||
|
||||
RPCInvoker orr.RPCInvoker
|
||||
RPCServerCodec orp.ServerCodec
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) Init(serverCtx server.ServerCtx) error {
|
||||
if err := s.Servlets.Init(serverCtx); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) OnStart(serverCtx server.ServerCtx) error {
|
||||
if err := s.Servlets.OnStart(serverCtx); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) OnStop(serverCtx server.ServerCtx) {
|
||||
|
||||
s.Servlets.OnStop(serverCtx)
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) Destroy(serverCtx server.ServerCtx) {
|
||||
|
||||
s.Servlets.Destroy(serverCtx)
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) OnConnect(servletCtx server.ServletCtx, conn oss.Conn) {
|
||||
s.Servlets.OnConnect(servletCtx, conn)
|
||||
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) OnDisconnect(servletCtx server.ServletCtx) {
|
||||
s.Servlets.OnDisconnect(servletCtx)
|
||||
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) Handle(servletCtx server.ServletCtx,
|
||||
stopChan <-chan struct{}, doneChan chan<- struct{},
|
||||
readChan <-chan oss.SocketMessage, writeChan chan<- oss.SocketMessage) {
|
||||
|
||||
var (
|
||||
src orp.ServerRequestCodec
|
||||
messageType int
|
||||
message []byte
|
||||
result interface{}
|
||||
resMessageType int
|
||||
resMessage []byte
|
||||
err error
|
||||
)
|
||||
|
||||
for {
|
||||
select {
|
||||
case socketMessage, ok := <-readChan:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
messageType, message = socketMessage()
|
||||
// grpc exec method call
|
||||
src, err = s.RPCServerCodec.NewRequest(messageType, message)
|
||||
if nil != err {
|
||||
olog.Logger().Error(err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
if !s.RPCInvoker.HasMethod(src.Method()) {
|
||||
olog.Logger().Error(err.Error())
|
||||
s.writeError(src, writeChan, orp.E_NO_METHOD, "", err)
|
||||
break
|
||||
}
|
||||
|
||||
result, err = s.RPCInvoker.Invoke(src)
|
||||
if nil != err {
|
||||
olog.Logger().Error(err.Error())
|
||||
}
|
||||
|
||||
if !src.HasResponse() {
|
||||
break
|
||||
}
|
||||
resMessageType, resMessage, err = src.NewResponse(result, err)
|
||||
if nil != err {
|
||||
olog.Logger().Error(err.Error())
|
||||
s.writeError(src, writeChan, orp.E_INTERNAL, "", err)
|
||||
break
|
||||
}
|
||||
|
||||
writeChan <- oss.MakeSocketMessage(resMessageType, resMessage)
|
||||
case <-stopChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ScannerServlets) writeError(src orp.ServerRequestCodec, writeChan chan<- oss.SocketMessage, code orp.ErrorCode, message string, data interface{}) {
|
||||
if !src.HasResponse() {
|
||||
return
|
||||
}
|
||||
|
||||
pErr := &orp.Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
resMessageType, resMessage, err := src.NewResponse(nil, pErr)
|
||||
if nil != err {
|
||||
olog.Logger().Error(err.Error())
|
||||
return
|
||||
}
|
||||
writeChan <- oss.MakeSocketMessage(resMessageType, resMessage)
|
||||
}
|
76
server/server-handler.go
Normal file
76
server/server-handler.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
"git.loafle.net/overflow/server-go"
|
||||
ossw "git.loafle.net/overflow/server-go/socket/web"
|
||||
)
|
||||
|
||||
type ServerHandler interface {
|
||||
ossw.ServerHandler
|
||||
}
|
||||
|
||||
type ServerHandlers struct {
|
||||
ossw.ServerHandlers
|
||||
|
||||
Services []interface{}
|
||||
OrderedServices []reflect.Type
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
|
||||
if err := sh.ServerHandlers.Init(serverCtx); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) OnStart(serverCtx server.ServerCtx) error {
|
||||
if err := sh.ServerHandlers.OnStart(serverCtx); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) OnStop(serverCtx server.ServerCtx) {
|
||||
|
||||
sh.ServerHandlers.OnStop(serverCtx)
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
|
||||
|
||||
sh.ServerHandlers.Destroy(serverCtx)
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) {
|
||||
for i := 60000; i < 61000; i++ {
|
||||
addr := fmt.Sprintf("localhost:%d", i)
|
||||
l, err := net.Listen("tcp", addr)
|
||||
if nil == err {
|
||||
// config.ProbePortNumber = int64(i)
|
||||
return l, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Cannot find availrable port")
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Validate() error {
|
||||
if err := sh.ServerHandlers.Validate(); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
if nil == sh.Services {
|
||||
return fmt.Errorf("Services must be specified")
|
||||
}
|
||||
|
||||
if nil == sh.OrderedServices {
|
||||
return fmt.Errorf("OrderedServices must be specified")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
42
server/server.go
Normal file
42
server/server.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
od "git.loafle.net/overflow/di-go"
|
||||
olog "git.loafle.net/overflow/log-go"
|
||||
orpj "git.loafle.net/overflow/rpc-go/protocol/json"
|
||||
orr "git.loafle.net/overflow/rpc-go/registry"
|
||||
ossw "git.loafle.net/overflow/server-go/socket/web"
|
||||
|
||||
"git.loafle.net/overflow_scanner/probe/annotation"
|
||||
|
||||
// For service
|
||||
_ "git.loafle.net/overflow_scanner/probe/service"
|
||||
)
|
||||
|
||||
func New() *ossw.Server {
|
||||
rpcServerCodec := orpj.NewServerCodec()
|
||||
|
||||
services, err := od.GetInstancesByAnnotationType(annotation.ServiceAnnotationType)
|
||||
if nil != err {
|
||||
olog.Logger().Panic(err.Error())
|
||||
}
|
||||
|
||||
rpcRegistry := orr.NewRPCRegistry()
|
||||
rpcRegistry.RegisterServices(services...)
|
||||
|
||||
ss := &ScannerServlets{}
|
||||
ss.RPCInvoker = rpcRegistry
|
||||
ss.RPCServerCodec = rpcServerCodec
|
||||
|
||||
sh := &ServerHandlers{}
|
||||
sh.Name = "Probe"
|
||||
sh.Services = services
|
||||
|
||||
sh.RegisterServlet("/", ss)
|
||||
|
||||
s := &ossw.Server{
|
||||
ServerHandler: sh,
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
26
service/DiscoveryService.go
Normal file
26
service/DiscoveryService.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
oa "git.loafle.net/overflow/annotation-go"
|
||||
od "git.loafle.net/overflow/di-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
od.RegisterType(DiscoveryServiceType)
|
||||
}
|
||||
|
||||
var DiscoveryServiceType = reflect.TypeOf((*DiscoveryService)(nil))
|
||||
|
||||
type DiscoveryService struct {
|
||||
oa.TypeAnnotation `annotation:"@Injectable('name': 'DiscoveryService') @Service()"`
|
||||
}
|
||||
|
||||
func (s *DiscoveryService) StartDiscover() {
|
||||
|
||||
}
|
||||
|
||||
func (s *DiscoveryService) StopDiscover() {
|
||||
|
||||
}
|
22
service/MachineService.go
Normal file
22
service/MachineService.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
oa "git.loafle.net/overflow/annotation-go"
|
||||
od "git.loafle.net/overflow/di-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
od.RegisterType(MachineServiceType)
|
||||
}
|
||||
|
||||
var MachineServiceType = reflect.TypeOf((*MachineService)(nil))
|
||||
|
||||
type MachineService struct {
|
||||
oa.TypeAnnotation `annotation:"@Injectable('name': 'MachineService') @Service()"`
|
||||
}
|
||||
|
||||
func (s *MachineService) Interfaces() {
|
||||
|
||||
}
|
30
service/PingService.go
Normal file
30
service/PingService.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
oa "git.loafle.net/overflow/annotation-go"
|
||||
od "git.loafle.net/overflow/di-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
od.RegisterType(PingServiceType)
|
||||
}
|
||||
|
||||
var PingServiceType = reflect.TypeOf((*PingService)(nil))
|
||||
|
||||
type PingService struct {
|
||||
oa.TypeAnnotation `annotation:"@Injectable('name': 'PingService') @Service()"`
|
||||
}
|
||||
|
||||
func (s *PingService) PingHost() {
|
||||
|
||||
}
|
||||
|
||||
func (s *PingService) PingService() {
|
||||
|
||||
}
|
||||
|
||||
func (s *PingService) PingAll() {
|
||||
|
||||
}
|
26
service/SNMPService.go
Normal file
26
service/SNMPService.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
oa "git.loafle.net/overflow/annotation-go"
|
||||
od "git.loafle.net/overflow/di-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
od.RegisterType(SNMPServiceType)
|
||||
}
|
||||
|
||||
var SNMPServiceType = reflect.TypeOf((*SNMPService)(nil))
|
||||
|
||||
type SNMPService struct {
|
||||
oa.TypeAnnotation `annotation:"@Injectable('name': 'SNMPService') @Service()"`
|
||||
}
|
||||
|
||||
func (s *SNMPService) ScanSNMP() {
|
||||
|
||||
}
|
||||
|
||||
func (s *SNMPService) PingSNMP() {
|
||||
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package service
|
||||
|
||||
/*
|
||||
StartProcess()
|
||||
StopProcess()
|
||||
|
||||
Interfaces()
|
||||
StartDiscover()
|
||||
StopDiscover()
|
||||
|
||||
PingHost()
|
||||
PingService()
|
||||
PingAll() // hosts, services, snmp
|
||||
|
||||
ScanSNMP() // by a host
|
||||
PingSNMP()
|
||||
*/
|
Loading…
Reference in New Issue
Block a user