crawler_go/socket_health_crawler.go

114 lines
2.2 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"
2017-04-11 08:42:02 +00:00
"loafle.com/overflow/commons_go/model/scaninfo"
2017-04-27 10:48:52 +00:00
config "loafle.com/overflow/agent_api/config_manager"
2017-04-10 09:03:31 +00:00
)
type SocketHeahthCrawler struct {
CrawlerImpl
2017-04-11 03:20:05 +00:00
m matcher.Matcher
}
func (s *SocketHeahthCrawler) SetMatcher(m matcher.Matcher) {
s.m = m
2017-04-10 09:03:31 +00:00
}
2017-04-13 07:09:16 +00:00
func (s *SocketHeahthCrawler) getConnection(params config.Config) (net.Conn, error) {
connection := params.Target.Connection
ip := connection.Ip
port := connection.Port
portType := connection.PortType
ssl := connection.SSL
2017-04-10 09:03:31 +00:00
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,
2017-04-11 08:42:02 +00:00
ClientAuth: tls.RequestClientCert,
2017-04-10 09:03:31 +00:00
},
)
if err != nil {
return nil, err
}
return conn, nil
}
}
2017-04-13 07:09:16 +00:00
func (s *SocketHeahthCrawler) CheckHeahth(params config.Config) (bool, error) {
2017-04-10 09:03:31 +00:00
conn, err := s.getConnection(params)
if err != nil {
return false, err
}
defer conn.Close()
2017-04-11 08:42:02 +00:00
2017-04-13 07:09:16 +00:00
connection := params.Target.Connection
info := scaninfo.NewScanInfoImpl(connection.Ip,connection.Port)
2017-04-11 08:42:02 +00:00
2017-04-11 03:20:05 +00:00
if s.m.IsPrePacket() == true {
2017-04-10 09:03:31 +00:00
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := packet.NewPacket(bytes, n)
2017-04-11 08:42:02 +00:00
if s.m.Match(0, p, info) == false {
2017-04-10 09:03:31 +00:00
return false, nil
2017-04-11 03:20:05 +00:00
} else {
2017-04-11 03:20:14 +00:00
2017-04-11 08:42:02 +00:00
for i := 0; i < s.m.PacketCount(); i++ {
pack := s.m.Packet(i)
2017-04-11 03:20:05 +00:00
conn.Write(pack.Buffer)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
2017-04-11 03:20:14 +00:00
2017-04-11 08:42:02 +00:00
if s.m.IsNoResponse(i+1) == true { // empty last response
2017-04-11 03:20:05 +00:00
break
}
2017-04-11 03:20:14 +00:00
2017-04-11 03:20:05 +00:00
p := packet.NewPacket(bytes, n)
2017-04-11 08:42:02 +00:00
if s.m.Match(i+1, p, info) == false {
2017-04-11 03:20:05 +00:00
return false, nil
}
}
2017-04-11 03:20:14 +00:00
2017-04-10 09:03:31 +00:00
}
2017-04-11 03:20:14 +00:00
2017-04-11 03:20:05 +00:00
} else {
2017-04-11 03:20:14 +00:00
for i := 0; i < s.m.PacketCount(); i++ {
2017-04-11 03:20:05 +00:00
pack := s.m.Packet(i)
conn.Write(pack.Buffer)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
2017-04-11 03:20:14 +00:00
2017-04-11 03:20:05 +00:00
if s.m.IsNoResponse(i) == true { // empty last response
break
}
2017-04-11 03:20:14 +00:00
2017-04-11 03:20:05 +00:00
p := packet.NewPacket(bytes, n)
2017-04-11 08:42:02 +00:00
if s.m.Match(i, p, info) == false {
2017-04-11 03:20:05 +00:00
return false, nil
}
2017-04-10 09:03:31 +00:00
}
}
2017-04-11 03:20:14 +00:00
2017-04-10 09:03:31 +00:00
return true, nil
}