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 } // Duplication Method func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) { result := make(map[string]string, 0) conn, cErr := s.GetConnection(config) defer conn.Close() if cErr != nil { result["Error"] = cErr.Error() return result, cErr } 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 result, nil } 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 result, nil } } } 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, nil } } } return nil, nil } func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.Conn, result map[string]string) (map[string]string, error) { 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 result, nil } 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 result, nil } } } 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, nil } } } return nil, nil } func convertBase64(buf []byte) string { return base64.StdEncoding.EncodeToString(buf) }