package health import ( "crypto/tls" "encoding/base64" "fmt" "net" "os" "syscall" 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.MetaIPTypeEnumV6.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 { ee := ToSocketErrorEnum(err) return nil, fmt.Errorf(ee.String()) } 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 ToSocketErrorEnum(err error) SocketErrorEnum { if e, ok := err.(net.Error); ok && e.Timeout() { return SocketErrorEnumTIMOUT } n, ok := err.(*net.OpError).Err.(*os.SyscallError).Err.(syscall.Errno) if !ok { return SocketErrorEnumUNKNOWN } switch n { case syscall.EHOSTUNREACH: return SocketErrorEnumHOSTUNREACH case syscall.ECONNREFUSED: return SocketErrorEnumCONNREFUSED default: return SocketErrorEnumUNKNOWN } } func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.Conn) error { connection := config.Connection port, _ := cuej.NumberToInt(connection.Port) info := cnsm.NewMatchInfo(connection.IP, port) if s.m.IsPrePacket() { buf := make([]byte, 1024) n, _ := conn.Read(buf) p := cnsm.NewPacket(buf, n) if !s.m.Match(info, 0, p) { return fmt.Errorf("Not Matched") } 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) { return fmt.Errorf("Not Matched") } } return nil } else { 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) { return fmt.Errorf("Not Matched") } } } return nil } func convertBase64(buf []byte) string { return base64.StdEncoding.EncodeToString(buf) }