crawler_go/socket_health_crawler.go
jackdaw@loafle.com a1a8d24728 .
2017-04-11 17:42:02 +09:00

110 lines
2.1 KiB
Go

package crawler
import (
"crypto/tls"
"loafle.com/overflow/commons_go/matcher"
"loafle.com/overflow/commons_go/matcher/packet"
"net"
"loafle.com/overflow/commons_go/model/scaninfo"
)
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,
ClientAuth: tls.RequestClientCert,
},
)
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()
info := scaninfo.NewScanInfoImpl(params["ip"].(string),params["port"].(string))
if s.m.IsPrePacket() == true {
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := packet.NewPacket(bytes, n)
if s.m.Match(0, p, info) == 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+1) == true { // empty last response
break
}
p := packet.NewPacket(bytes, n)
if s.m.Match(i+1, p, info) == 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, info) == false {
return false, nil
}
}
}
return true, nil
}