149 lines
3.1 KiB
Go
149 lines
3.1 KiB
Go
package health
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
"net"
|
|
|
|
cnsm "git.loafle.net/commons/service_matcher-go"
|
|
cuej "git.loafle.net/commons/util-go/encoding/json"
|
|
ocmm "git.loafle.net/overflow/commons-go/model/meta"
|
|
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
|
"git.loafle.net/overflow/crawler-go"
|
|
)
|
|
|
|
type SocketHealthCrawler struct {
|
|
crawler.Crawler
|
|
m cnsm.Matcher
|
|
}
|
|
|
|
func (s *SocketHealthCrawler) SetMatcher(m cnsm.Matcher) {
|
|
s.m = m
|
|
}
|
|
|
|
func (s *SocketHealthCrawler) getConnection(config *ocmsc.SensorConfig) (net.Conn, error) {
|
|
connection := config.Connection
|
|
metaIPTypeKey := connection.MetaIPTypeKey
|
|
ip := connection.IP
|
|
port := connection.Port
|
|
metaPortTypeKey := connection.MetaPortTypeKey
|
|
metaCryptoTypeKey := connection.MetaCryptoTypeKey
|
|
|
|
addr := net.JoinHostPort(ip, port.String())
|
|
|
|
network := ""
|
|
switch metaIPTypeKey {
|
|
case ocmm.MetaIPTypeEnumV4.String():
|
|
switch metaPortTypeKey {
|
|
case ocmm.MetaPortTypeEnumUDP.String():
|
|
network = "udp6"
|
|
default:
|
|
network = "tcp6"
|
|
}
|
|
default:
|
|
switch metaPortTypeKey {
|
|
case ocmm.MetaPortTypeEnumUDP.String():
|
|
network = "udp"
|
|
default:
|
|
network = "tcp"
|
|
}
|
|
}
|
|
|
|
conn, err := net.Dial(network, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch metaCryptoTypeKey {
|
|
case ocmm.MetaCryptoTypeEnumTLS.String():
|
|
cfg := &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
ServerName: ip,
|
|
ClientAuth: tls.RequestClientCert,
|
|
}
|
|
tlsConn := tls.Client(conn, cfg)
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
conn = tlsConn
|
|
default:
|
|
}
|
|
|
|
return conn, nil
|
|
}
|
|
|
|
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (result map[string]string, err error) {
|
|
result = make(map[string]string, 0)
|
|
|
|
conn, cErr := s.getConnection(config)
|
|
if cErr != nil {
|
|
result["Error"] = cErr.Error()
|
|
err = cErr
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
connection := config.Connection
|
|
port, _ := cuej.NumberToInt(connection.Port)
|
|
info := cnsm.NewMatchInfo(connection.IP, port)
|
|
|
|
if s.m.IsPrePacket() {
|
|
result["PacketType"] = "Pre"
|
|
|
|
buf := make([]byte, 1024)
|
|
n, _ := conn.Read(buf)
|
|
p := cnsm.NewPacket(buf, n)
|
|
if !s.m.Match(info, 0, p) {
|
|
result["Packet"] = convertBase64(buf)
|
|
result["Error"] = "Not Matched"
|
|
return
|
|
}
|
|
|
|
for i := 0; i < s.m.PacketCount(); i++ {
|
|
pack := s.m.Packet(i)
|
|
conn.Write(pack.Buffer)
|
|
buf := make([]byte, 1024)
|
|
n, _ := conn.Read(buf)
|
|
|
|
if !s.m.HasResponse(i + 1) { // empty last response
|
|
break
|
|
}
|
|
|
|
p := cnsm.NewPacket(buf, n)
|
|
if s.m.Match(info, i+1, p) == false {
|
|
result["Packet"] = convertBase64(buf)
|
|
result["Error"] = "Not Matched"
|
|
return
|
|
}
|
|
}
|
|
|
|
} else {
|
|
result["PacketType"] = "Post"
|
|
|
|
for i := 0; i < s.m.PacketCount(); i++ {
|
|
pack := s.m.Packet(i)
|
|
conn.Write(pack.Buffer)
|
|
buf := make([]byte, 1024)
|
|
n, _ := conn.Read(buf)
|
|
|
|
if !s.m.HasResponse(i) { // empty last response
|
|
break
|
|
}
|
|
|
|
p := cnsm.NewPacket(buf, n)
|
|
if s.m.Match(info, i, p) == false {
|
|
result["Packet"] = convertBase64(buf)
|
|
result["Error"] = "Not Matched"
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func convertBase64(buf []byte) string {
|
|
return base64.StdEncoding.EncodeToString(buf)
|
|
}
|