container_network/crawler/health/SocketHeahthCrawler.go

192 lines
4.0 KiB
Go
Raw Normal View History

2018-04-19 15:46:38 +00:00
package health
import (
"crypto/tls"
"encoding/base64"
2018-07-04 12:28:28 +00:00
"fmt"
2018-04-19 15:46:38 +00:00
"net"
2018-07-04 09:33:58 +00:00
"os"
"syscall"
2018-04-19 15:46:38 +00:00
cnsm "git.loafle.net/commons/service_matcher-go"
2018-04-28 15:01:45 +00:00
cuej "git.loafle.net/commons/util-go/encoding/json"
2018-07-03 15:07:12 +00:00
ocmm "git.loafle.net/overflow/commons-go/model/meta"
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"
)
2018-07-02 11:53:06 +00:00
type SocketHealthCrawler struct {
2018-04-19 15:46:38 +00:00
crawler.Crawler
m cnsm.Matcher
}
2018-07-02 11:53:06 +00:00
func (s *SocketHealthCrawler) SetMatcher(m cnsm.Matcher) {
2018-04-19 15:46:38 +00:00
s.m = m
}
2018-07-04 09:31:45 +00:00
func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Conn, error) {
2018-07-02 11:53:06 +00:00
connection := config.Connection
metaIPTypeKey := connection.MetaIPTypeKey
2018-04-19 15:46:38 +00:00
ip := connection.IP
port := connection.Port
2018-07-02 11:53:06 +00:00
metaPortTypeKey := connection.MetaPortTypeKey
metaCryptoTypeKey := connection.MetaCryptoTypeKey
2018-04-19 15:46:38 +00:00
2018-07-04 03:14:25 +00:00
addr := net.JoinHostPort(ip, port.String())
2018-04-19 15:46:38 +00:00
2018-06-13 11:22:23 +00:00
network := ""
2018-07-02 11:53:06 +00:00
switch metaIPTypeKey {
2018-07-04 12:28:28 +00:00
case ocmm.MetaIPTypeEnumV6.String():
2018-07-02 11:53:06 +00:00
switch metaPortTypeKey {
2018-07-03 15:07:12 +00:00
case ocmm.MetaPortTypeEnumUDP.String():
2018-06-13 11:22:23 +00:00
network = "udp6"
default:
network = "tcp6"
}
default:
2018-07-02 11:53:06 +00:00
switch metaPortTypeKey {
2018-07-03 15:07:12 +00:00
case ocmm.MetaPortTypeEnumUDP.String():
2018-06-13 11:22:23 +00:00
network = "udp"
default:
network = "tcp"
}
}
2018-07-05 03:06:41 +00:00
conn, err := net.Dial(network, addr)
2018-04-19 15:46:38 +00:00
if err != nil {
2018-07-05 03:06:41 +00:00
ee := ToSocketErrorEnum(err)
return nil, fmt.Errorf(ee.String())
2018-04-19 15:46:38 +00:00
}
2018-07-04 03:14:25 +00:00
switch metaCryptoTypeKey {
case ocmm.MetaCryptoTypeEnumTLS.String():
2018-04-19 15:46:38 +00:00
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
2018-07-04 03:14:25 +00:00
default:
2018-04-19 15:46:38 +00:00
}
return conn, nil
2018-07-04 09:33:58 +00:00
}
func ToSocketErrorEnum(err error) SocketErrorEnum {
2018-07-05 06:16:53 +00:00
switch err.(type) {
case *net.OpError:
ne := err.(*net.OpError)
if ne.Timeout() {
return SocketErrorEnumTIMOUT
}
switch ne.Err.(type) {
case *os.SyscallError:
sce := ne.Err.(*os.SyscallError)
switch sce.Err.(type) {
case syscall.Errno:
n := sce.Err.(syscall.Errno)
switch n {
case syscall.EHOSTUNREACH:
return SocketErrorEnumHOSTUNREACH
case syscall.ECONNREFUSED:
return SocketErrorEnumCONNREFUSED
default:
return SocketErrorEnumUNKNOWN
}
default:
return SocketErrorEnumUNKNOWN
}
default:
return SocketErrorEnumUNKNOWN
}
2018-07-04 09:33:58 +00:00
default:
return SocketErrorEnumUNKNOWN
}
2018-07-05 03:06:41 +00:00
2018-07-05 06:16:53 +00:00
// e, ok := err.(net.Error)
// ; ok && e.Timeout() {
// return SocketErrorEnumTIMOUT
// }
// 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
// }
2018-04-19 15:46:38 +00:00
}
2018-07-04 12:28:28 +00:00
func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig, conn net.Conn) error {
2018-07-04 09:31:45 +00:00
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) {
2018-07-04 12:28:28 +00:00
return fmt.Errorf("Not Matched")
2018-04-19 15:46:38 +00:00
}
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)
2018-07-04 12:28:28 +00:00
if !s.m.Match(info, i+1, p) {
return fmt.Errorf("Not Matched")
2018-04-19 15:46:38 +00:00
}
}
2018-07-04 12:28:28 +00:00
return nil
2018-04-19 15:46:38 +00:00
2018-07-04 12:28:28 +00:00
} else {
2018-04-19 15:46:38 +00:00
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)
2018-07-04 12:28:28 +00:00
if !s.m.Match(info, i, p) {
return fmt.Errorf("Not Matched")
2018-04-19 15:46:38 +00:00
}
}
}
2018-07-04 12:28:28 +00:00
return nil
2018-04-19 15:46:38 +00:00
}
func convertBase64(buf []byte) string {
return base64.StdEncoding.EncodeToString(buf)
}