crawler_go/socket_health_crawler.go
jackdaw@loafle.com 55682d7d39 .
2017-04-11 14:07:34 +09:00

106 lines
1.9 KiB
Go

package crawler
import (
"crypto/tls"
"loafle.com/overflow/commons_go/matcher"
"loafle.com/overflow/commons_go/matcher/packet"
"net"
)
type SocketHeahthCrawler struct {
CrawlerImpl
m matcher.Matcher
}
func (s *SocketHeahthCrawler) SetMatcher(m matcher.Matcher) {
s.m = m
}
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{}) (bool, error) {
conn, err := s.getConnection(params)
if err != nil {
return false, err
}
defer conn.Close()
if s.m.IsPrePacket() == true {
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := packet.NewPacket(bytes, n)
if s.m.Match(0, p, nil) == false {
return false, nil
} else {
for i := 1; i < s.m.PacketCount(); i++ {
pack := s.m.Packet(i-1)
conn.Write(pack.Buffer)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
if s.m.IsNoResponse(i) == true { // empty last response
break
}
p := packet.NewPacket(bytes, n)
if s.m.Match(i, p, nil) == false {
return false, nil
}
}
}
} else {
for i := 0; i < s.m.PacketCount(); i++ {
pack := s.m.Packet(i)
conn.Write(pack.Buffer)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
if s.m.IsNoResponse(i) == true { // empty last response
break
}
p := packet.NewPacket(bytes, n)
if s.m.Match(i, p, nil) == false {
return false, nil
}
}
}
return true, nil
}