147 lines
2.9 KiB
Go
147 lines
2.9 KiB
Go
package health
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net"
|
|
|
|
cnsm "git.loafle.net/commons/service_matcher-go"
|
|
cuej "git.loafle.net/commons/util-go/encoding/json"
|
|
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 := fmt.Sprintf("%s:%s", ip, port)
|
|
|
|
network := ""
|
|
switch metaIPTypeKey {
|
|
case "V6":
|
|
switch metaPortTypeKey {
|
|
case "UDP":
|
|
network = "udp6"
|
|
default:
|
|
network = "tcp6"
|
|
}
|
|
default:
|
|
switch metaPortTypeKey {
|
|
case "UDP":
|
|
network = "udp"
|
|
default:
|
|
network = "tcp"
|
|
}
|
|
}
|
|
|
|
conn, err := net.Dial(network, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if metaCryptoTypeKey == "TLS" {
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|