crawler_go/socket_health_crawler.go

82 lines
1.5 KiB
Go
Raw Normal View History

2017-04-10 09:03:31 +00:00
package crawler
import (
"crypto/tls"
2017-04-10 11:15:47 +00:00
"loafle.com/overflow/commons_go/matcher"
"loafle.com/overflow/commons_go/matcher/packet"
2017-04-10 09:03:31 +00:00
"net"
)
type SocketHeahthCrawler struct {
CrawlerImpl
}
func (s *SocketHeahthCrawler) getConnection(params map[string]interface{}) (net.Conn, error) {
ip := params["ip"].(string)
port := params["port"].(string)
portType := params["portType"].(string)
ssl := params["ssl"].(bool)
var addr string = ip
addr += ":"
addr += port
if ssl == false {
conn, err := net.Dial(portType, addr)
if err != nil {
return nil, err
}
return conn, nil
} else {
conn, err := tls.Dial(
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: ip,
},
)
if err != nil {
return nil, err
}
return conn, nil
}
}
func (s *SocketHeahthCrawler) CheckHeahth(params map[string]interface{}, mat matcher.Matcher) (bool, error) {
conn, err := s.getConnection(params)
if err != nil {
return false, err
}
defer conn.Close()
if mat.IsPrePacket() == true {
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := packet.NewPacket(bytes, n)
if mat.Match(0, p, nil) == false {
return false, nil
}
}
for i := 0; i < mat.PacketCount(); i++ {
pack := mat.Packet(i)
conn.Write(pack.Buffer)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
if mat.IsNoResponse(i) == true { // empty last response
break
}
p := packet.NewPacket(bytes, n)
if mat.Match(i, p, nil) == false {
return false, nil
}
}
return true, nil
}