2018-04-19 15:46:38 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
cnsm "git.loafle.net/commons/service_matcher-go"
|
|
|
|
cuej "git.loafle.net/commons_go/util/encoding/json"
|
2018-04-26 08:50:26 +00:00
|
|
|
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
2018-04-19 15:46:38 +00:00
|
|
|
"git.loafle.net/overflow/crawler-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SocketHeahthCrawler struct {
|
|
|
|
crawler.Crawler
|
|
|
|
m cnsm.Matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SocketHeahthCrawler) SetMatcher(m cnsm.Matcher) {
|
|
|
|
s.m = m
|
|
|
|
}
|
|
|
|
|
2018-04-26 08:50:26 +00:00
|
|
|
func (s *SocketHeahthCrawler) getConnection(config *ocmsc.SensorConfig) (net.Conn, error) {
|
2018-04-19 15:46:38 +00:00
|
|
|
connection := config.Target.Connection
|
|
|
|
|
|
|
|
ip := connection.IP
|
|
|
|
port := connection.Port
|
|
|
|
portType := connection.PortType
|
|
|
|
ssl := connection.SSL
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", ip, port)
|
|
|
|
|
2018-04-28 13:42:26 +00:00
|
|
|
conn, err := net.Dial(portType.String(), addr)
|
2018-04-19 15:46:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ssl {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-26 08:50:26 +00:00
|
|
|
func (s *SocketHeahthCrawler) CheckHeahth(config *ocmsc.SensorConfig) (result map[string]string, err error) {
|
2018-04-19 15:46:38 +00:00
|
|
|
result = make(map[string]string, 0)
|
|
|
|
|
|
|
|
result["StartTime"] = time.Now().String()
|
|
|
|
|
|
|
|
conn, cErr := s.getConnection(config)
|
|
|
|
if cErr != nil {
|
|
|
|
result["Error"] = cErr.Error()
|
|
|
|
err = cErr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
connection := config.Target.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result["EndTime"] = time.Now().String()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertBase64(buf []byte) string {
|
|
|
|
return base64.StdEncoding.EncodeToString(buf)
|
|
|
|
}
|