-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchecker.go
48 lines (42 loc) · 955 Bytes
/
checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"encoding/json"
"github.com/geNAZt/minecraft-status/data"
"github.com/geNAZt/minecraft-status/protocol"
"net"
"log"
"time"
)
func isMinecraft(ip string) bool {
if !portOpen(ip + ":25565") {
return false
}
conn, err := protocol.NewNetClient(ip)
if err != nil {
return false
}
defer conn.Close()
conn.SendHandshake()
conn.State = protocol.Status
conn.SendStatusRequest()
statusPacket, err := conn.ReadPacket()
if err != nil {
return false
}
status := &data.Status{}
errJson := json.Unmarshal([]byte(statusPacket.(protocol.StatusResponse).Data), status)
if errJson != nil {
return false
}
// ScanRes := fmt.Sprintf("%s:%s\n", ip.String(), status.Description)
log.Printf("%s:%s\n", ip, status.Description)
return true
}
func portOpen(addr string) bool {
conn, err := net.DialTimeout("tcp", addr, time.Duration(500*time.Millisecond))
if err != nil {
return false
}
conn.Close()
return true
}