probe/ping/ping.go

100 lines
1.8 KiB
Go
Raw Normal View History

2018-08-12 10:24:23 +00:00
package ping
import (
"crypto/tls"
"fmt"
"net"
"strings"
"time"
csm "git.loafle.net/commons/service_matcher-go"
opm "git.loafle.net/overflow_scanner/probe/matcher"
)
type Res struct {
Matcher csm.Matcher
Error error
}
2018-08-15 06:18:40 +00:00
func Ping(ch chan *Res, ip string, port int, tls bool, portType, key string) {
2018-08-12 10:24:23 +00:00
go func() {
conn, err := getConnection(ip, port, portType, tls)
if err != nil {
2018-08-15 06:18:40 +00:00
ch <- &Res{nil, err}
2018-08-12 10:24:23 +00:00
close(ch)
}
defer conn.Close()
m := opm.GetMatcherByKey(key)
if m.IsPrePacket() {
processPrepacket(ch, conn, m)
}
processPostpacket(ch, conn, m)
}()
}
2018-08-15 06:18:40 +00:00
func processPrepacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
2018-08-12 10:24:23 +00:00
for i := 0; i < m.PacketCount(); i++ {
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := csm.NewPacket(bytes, n)
_, err := conn.Write(m.Packet(i).Buffer)
if err != nil {
2018-08-15 06:18:40 +00:00
ch <- &Res{nil, err}
2018-08-12 10:24:23 +00:00
close(ch)
}
if err := m.Match(nil, i, p); err != nil {
2018-08-15 06:18:40 +00:00
ch <- &Res{nil, err}
2018-08-12 10:24:23 +00:00
close(ch)
}
}
2018-08-15 06:18:40 +00:00
ch <- &Res{m, nil}
2018-08-12 10:24:23 +00:00
close(ch)
}
2018-08-15 06:18:40 +00:00
func processPostpacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
2018-08-12 10:24:23 +00:00
for i := 0; i < m.PacketCount(); i++ {
_, err := conn.Write(m.Packet(i).Buffer)
if err != nil {
2018-08-15 06:18:40 +00:00
ch <- &Res{nil, err}
2018-08-12 10:24:23 +00:00
close(ch)
}
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := csm.NewPacket(bytes, n)
if err := m.Match(nil, i, p); err != nil {
2018-08-15 06:18:40 +00:00
ch <- &Res{nil, err}
2018-08-12 10:24:23 +00:00
close(ch)
}
}
2018-08-15 06:18:40 +00:00
ch <- &Res{m, nil}
2018-08-12 10:24:23 +00:00
close(ch)
}
func getConnection(ip string, port int, portType string, isTLS bool) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
portType = strings.ToLower(portType)
if isTLS {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
}
return tls.DialWithDialer(
dialer,
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: ip,
},
)
}
return net.Dial(portType, addr)
}