This commit is contained in:
crusader 2018-09-18 01:25:56 +09:00
parent b3e58106a9
commit 419c5cb6ac
12 changed files with 587 additions and 140 deletions

View File

@ -7,12 +7,12 @@ import (
omm "git.loafle.net/overflow/model/meta" omm "git.loafle.net/overflow/model/meta"
) )
// const ( const (
// ZONE_NETWORK = "192.168.1" ZONE_NETWORK = "192.168.1"
// ZONE_IFACE = "enp3s0" ZONE_IFACE = "enp3s0"
// ZONE_ADDRESS = "101" ZONE_ADDRESS = "101"
// ZONE_MAC = "44:8a:5b:f1:f1:f3" ZONE_MAC = "44:8a:5b:f1:f1:f3"
// ) )
// const ( // const (
// ZONE_NETWORK = "192.168.1" // ZONE_NETWORK = "192.168.1"
@ -21,12 +21,12 @@ import (
// ZONE_MAC = "30:9C:23:15:A3:09" // ZONE_MAC = "30:9C:23:15:A3:09"
// ) // )
const ( // const (
ZONE_NETWORK = "192.168.35" // ZONE_NETWORK = "192.168.35"
ZONE_IFACE = "wlp5s0" // ZONE_IFACE = "wlp5s0"
ZONE_ADDRESS = "234" // ZONE_ADDRESS = "234"
ZONE_MAC = "d0:7e:35:da:26:68" // ZONE_MAC = "d0:7e:35:da:26:68"
) // )
func Zone() *omd.Zone { func Zone() *omd.Zone {
return omd.NewZone( return omd.NewZone(

View File

@ -116,7 +116,7 @@ func hadlePrePacket(matchCtx *osm.MatchCtx, _connector connector, conn net.Conn,
LOOP: LOOP:
for _, _matcher := range matchers { for _, _matcher := range matchers {
matchCtx.InitAttribute() matchCtx.InitAttribute()
if err := _matcher.Match(matchCtx, 0, packet); err != nil { if err := _matcher.Match(matchCtx, 0, packet); nil != err {
continue LOOP continue LOOP
} }

View File

@ -1,12 +1,11 @@
package host package host
import ( import (
omcp "git.loafle.net/overflow/model/config/ping"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
ounp "git.loafle.net/overflow/util-go/net/ping" ounp "git.loafle.net/overflow/util-go/net/ping"
) )
func Ping(host *omd.Host, pingOption omcp.Option) (omcp.Result, error) { func Ping(host *omd.Host, pingOption ounp.Option) (ounp.Result, error) {
return ounp.Ping(host.Address, pingOption) return ounp.Ping(host.Address, pingOption)
} }

51
ping/host/host_test.go Normal file
View File

@ -0,0 +1,51 @@
package host
import (
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
ounp "git.loafle.net/overflow/util-go/net/ping"
"git.loafle.net/overflow_scanner/probe/__test"
)
func TestPing(t *testing.T) {
type args struct {
host *omd.Host
pingOption ounp.Option
}
tests := []struct {
name string
args args
want ounp.Result
wantErr bool
}{
{
name: "192.168.1.1",
args: args{
host: __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
),
pingOption: &ounp.PingOption{
Retry: 3,
Interval: 1,
Deadline: 1,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Ping(tt.args.host, tt.args.pingOption)
if (err != nil) != tt.wantErr {
t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ping() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,99 +1,16 @@
package ping package ping
import ( import (
"crypto/tls" omd "git.loafle.net/overflow/model/discovery"
"fmt" ounp "git.loafle.net/overflow/util-go/net/ping"
"net" "git.loafle.net/overflow_scanner/probe/ping/host"
"strings" "git.loafle.net/overflow_scanner/probe/ping/service"
"time"
csm "git.loafle.net/overflow/service_matcher-go"
opm "git.loafle.net/overflow_scanner/probe/matcher"
) )
type Res struct { func PingHost(_host *omd.Host, pingOption ounp.Option) (ounp.Result, error) {
Matcher csm.Matcher return host.Ping(host, pingOption)
Error error
} }
func Ping(ch chan *Res, ip string, port int, tls bool, portType, key string) { func PingService(_service *omd.Service, pingOption *ounp.PingOption) (ounp.Result, error) {
go func() { return service.Ping(_service, pingOption)
conn, err := getConnection(ip, port, portType, tls)
if err != nil {
ch <- &Res{nil, err}
close(ch)
}
defer conn.Close()
m := opm.GetMatcherByKey(key)
if m.IsPrePacket() {
processPrepacket(ch, conn, m)
}
processPostpacket(ch, conn, m)
}()
}
func processPrepacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
for i := 0; i < m.PacketCount(); i++ {
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := csm.NewPacket(bytes, n)
_, err := conn.Write(m.Packet(i).Buffer)
if err != nil {
ch <- &Res{nil, err}
close(ch)
}
if err := m.Match(nil, i, p); err != nil {
ch <- &Res{nil, err}
close(ch)
}
}
ch <- &Res{m, nil}
close(ch)
}
func processPostpacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
for i := 0; i < m.PacketCount(); i++ {
_, err := conn.Write(m.Packet(i).Buffer)
if err != nil {
ch <- &Res{nil, err}
close(ch)
}
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := csm.NewPacket(bytes, n)
if err := m.Match(nil, i, p); err != nil {
ch <- &Res{nil, err}
close(ch)
}
}
ch <- &Res{m, nil}
close(ch)
}
func getConnection(ip string, port int, portType string, isTLS bool) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
portType = strings.ToLower(portType)
if isTLS {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
}
return tls.DialWithDialer(
dialer,
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: ip,
},
)
}
return net.Dial(portType, addr)
} }

View File

@ -1,26 +0,0 @@
package ping
import (
"testing"
)
func TestPing(t *testing.T) {
ip := "192.168.1.229"
port := 6379
portType := "TCP"
key := "REDIS"
tls := false
ch := make(chan *Res)
Ping(ch, ip, port, tls, portType, key)
for res := range ch {
if res.Error != nil {
t.Error(res.Error)
} else {
t.Log(res.Matcher.Name())
t.Log(res.Matcher.Meta())
}
}
}

View File

@ -0,0 +1,63 @@
package connection
import (
"crypto/tls"
"net"
"strings"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
ounp "git.loafle.net/overflow/util-go/net/ping"
)
func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0),
Summary: &ounp.PingSummary{},
}
LOOP:
for indexR := 0; indexR < pingOption.GetRetry(); indexR++ {
conn, err := getConnection(service, pingOption)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
continue LOOP
}
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
}
conn.Close()
}
return pingResult, nil
}
func getConnection(service *omd.Service, pingOption ounp.Option) (net.Conn, error) {
addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String())
portType := strings.ToLower(service.Port.MetaPortType.Key)
switch omm.ToMetaCryptoTypeEnum(service.MetaCryptoType) {
case omm.MetaCryptoTypeEnumTLS:
dialer := &net.Dialer{
Timeout: time.Duration(pingOption.GetDeadline()) * time.Second,
}
return tls.DialWithDialer(
dialer,
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: service.Port.Host.Address,
},
)
default:
return net.DialTimeout(portType, addr, time.Duration(pingOption.GetDeadline())*time.Second)
}
}

View File

@ -0,0 +1,64 @@
package connection
import (
"net"
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
ounp "git.loafle.net/overflow/util-go/net/ping"
)
func TestPing(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
}
tests := []struct {
name string
args args
want ounp.Result
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Ping(tt.args.service, tt.args.pingOption)
if (err != nil) != tt.wantErr {
t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ping() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getConnection(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
}
tests := []struct {
name string
args args
want net.Conn
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getConnection(tt.args.service, tt.args.pingOption)
if (err != nil) != tt.wantErr {
t.Errorf("getConnection() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getConnection() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,250 @@
package matcher
import (
"crypto/tls"
"net"
"strings"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
osm "git.loafle.net/overflow/service_matcher-go"
ouej "git.loafle.net/overflow/util-go/encoding/json"
ounp "git.loafle.net/overflow/util-go/net/ping"
"git.loafle.net/overflow_scanner/probe/internal/matcher"
)
func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
portNumber, err := ouej.NumberToInt(service.Port.PortNumber)
if nil != err {
return nil, err
}
matchCtx := osm.NewMatchCtx(service.Port.Host.Address, portNumber)
_matcher := matcher.GetMatcherByKey(service.Key)
if _matcher.IsPrePacket() {
return processPrepacket(service, pingOption, matchCtx, _matcher), nil
}
return processPostpacket(service, pingOption, matchCtx, _matcher), nil
}
func processPrepacket(service *omd.Service, pingOption ounp.Option, matchCtx *osm.MatchCtx, _matcher osm.Matcher) ounp.Result {
pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0),
Summary: &ounp.PingSummary{},
}
buf := make([]byte, 1024)
LOOP:
for indexR := 0; indexR < pingOption.GetRetry(); indexR++ {
matchCtx.InitAttribute()
conn, err := getConnection(service, pingOption)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
continue LOOP
}
if err := conn.SetReadDeadline(time.Now().Add(time.Duration(pingOption.GetDeadline()) * time.Second)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
conn.Close()
continue LOOP
}
n, err := conn.Read(buf)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
conn.Close()
continue LOOP
}
if err := _matcher.Match(matchCtx, 0, osm.NewPacket(buf, n)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
conn.Close()
continue LOOP
}
packetCount := _matcher.PacketCount(matchCtx)
if 0 == packetCount {
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
}
conn.Close()
continue LOOP
}
isMatched := false
INNER_LOOP:
for indexM := 0; indexM < packetCount; indexM++ {
_packet := _matcher.Packet(matchCtx, indexM)
if err := conn.SetWriteDeadline(time.Now().Add(time.Duration(pingOption.GetDeadline()) * time.Second)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
_, err := conn.Write(_packet.Buffer)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
if err := conn.SetReadDeadline(time.Now().Add(time.Duration(pingOption.GetDeadline()) * time.Second)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
n, err := conn.Read(buf)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
if err := _matcher.Match(matchCtx, indexM+1, osm.NewPacket(buf, n)); nil == err {
isMatched = true
} else {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: "Protocol not match",
}
break INNER_LOOP
}
}
conn.Close()
if isMatched {
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
}
}
}
return pingResult
}
func processPostpacket(service *omd.Service, pingOption ounp.Option, matchCtx *osm.MatchCtx, _matcher osm.Matcher) ounp.Result {
pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0),
Summary: &ounp.PingSummary{},
}
buf := make([]byte, 1024)
LOOP:
for indexR := 0; indexR < pingOption.GetRetry(); indexR++ {
matchCtx.InitAttribute()
packetCount := _matcher.PacketCount(matchCtx)
if 0 == packetCount {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: "Protocol not match",
}
continue LOOP
}
conn, err := getConnection(service, pingOption)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
continue LOOP
}
isMatched := false
INNER_LOOP:
for indexM := 0; indexM < packetCount; indexM++ {
_packet := _matcher.Packet(matchCtx, indexM)
if err := conn.SetWriteDeadline(time.Now().Add(time.Duration(pingOption.GetDeadline()) * time.Second)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
_, err := conn.Write(_packet.Buffer)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
if err := conn.SetReadDeadline(time.Now().Add(time.Duration(pingOption.GetDeadline()) * time.Second)); nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
n, err := conn.Read(buf)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
break INNER_LOOP
}
if err := _matcher.Match(matchCtx, indexM+1, osm.NewPacket(buf, n)); nil == err {
if packetCount-1 == indexM {
isMatched = true
break INNER_LOOP
}
} else {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: "Protocol not match",
}
break INNER_LOOP
}
}
conn.Close()
if isMatched {
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
}
}
}
return pingResult
}
func getConnection(service *omd.Service, pingOption ounp.Option) (net.Conn, error) {
addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String())
portType := strings.ToLower(service.Port.MetaPortType.Key)
switch omm.ToMetaCryptoTypeEnum(service.MetaCryptoType) {
case omm.MetaCryptoTypeEnumTLS:
dialer := &net.Dialer{
Timeout: time.Duration(pingOption.GetDeadline()) * time.Second,
}
return tls.DialWithDialer(
dialer,
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: service.Port.Host.Address,
},
)
default:
return net.DialTimeout(portType, addr, time.Duration(pingOption.GetDeadline())*time.Second)
}
}

View File

@ -0,0 +1,111 @@
package matcher
import (
"net"
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
osm "git.loafle.net/overflow/service_matcher-go"
ounp "git.loafle.net/overflow/util-go/net/ping"
)
func TestPing(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
}
tests := []struct {
name string
args args
want ounp.Result
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Ping(tt.args.service, tt.args.pingOption)
if (err != nil) != tt.wantErr {
t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ping() = %v, want %v", got, tt.want)
}
})
}
}
func Test_processPrepacket(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
matchCtx *osm.MatchCtx
_matcher osm.Matcher
}
tests := []struct {
name string
args args
want ounp.Result
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := processPrepacket(tt.args.service, tt.args.pingOption, tt.args.matchCtx, tt.args._matcher); !reflect.DeepEqual(got, tt.want) {
t.Errorf("processPrepacket() = %v, want %v", got, tt.want)
}
})
}
}
func Test_processPostpacket(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
matchCtx *osm.MatchCtx
_matcher osm.Matcher
}
tests := []struct {
name string
args args
want ounp.Result
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := processPostpacket(tt.args.service, tt.args.pingOption, tt.args.matchCtx, tt.args._matcher); !reflect.DeepEqual(got, tt.want) {
t.Errorf("processPostpacket() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getConnection(t *testing.T) {
type args struct {
service *omd.Service
pingOption ounp.Option
}
tests := []struct {
name string
args args
want net.Conn
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getConnection(tt.args.service, tt.args.pingOption)
if (err != nil) != tt.wantErr {
t.Errorf("getConnection() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getConnection() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,11 +1,28 @@
package service package service
import ( import (
omcp "git.loafle.net/overflow/model/config/ping"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
ounp "git.loafle.net/overflow/util-go/net/ping"
"git.loafle.net/overflow_scanner/probe/ping/service/connection"
"git.loafle.net/overflow_scanner/probe/ping/service/matcher"
) )
func Ping(service *omd.Service, pingOption *omcp.PingOption) (*omcp.PingResult, error) { func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
if isDiscoveredByMatcher(service) {
return nil, nil return matcher.Ping(service, pingOption)
}
return connection.Ping(service, pingOption)
}
func isDiscoveredByMatcher(service *omd.Service) bool {
for _, discoveredBy := range service.DiscoveredBy {
switch omm.ToMetaDiscovererTypeEnum(discoveredBy) {
case omm.MetaDiscovererTypeEnumUDPMatcher, omm.MetaDiscovererTypeEnumTCPMatcher:
return true
}
}
return false
} }

View File

@ -5,8 +5,9 @@ import (
oa "git.loafle.net/overflow/annotation-go" oa "git.loafle.net/overflow/annotation-go"
od "git.loafle.net/overflow/di-go" od "git.loafle.net/overflow/di-go"
omcp "git.loafle.net/overflow/model/config/ping"
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
ounp "git.loafle.net/overflow/util-go/net/ping"
"git.loafle.net/overflow_scanner/probe/ping"
) )
func init() { func init() {
@ -30,12 +31,12 @@ func (s *PingService) DestroyService() {
} }
func (s *PingService) PingHost(host *omd.Host, pingOption *omcp.PingOption) error { func (s *PingService) PingHost(host *omd.Host, pingOption *ounp.PingOption) (*ounp.PingResult, error) {
return nil return ping.PingHost(host, pingOption)
} }
func (s *PingService) PingService(host *omd.Service, pingOption *omcp.PingOption) error { func (s *PingService) PingService(service *omd.Service, pingOption *ounp.PingOption) (*ounp.PingResult, error) {
return nil return ping.PingService(service, pingOption)
} }
func (s *PingService) PingAll() error { func (s *PingService) PingAll() error {