wip refactor

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-11-05 12:04:10 +00:00
parent 20edf2a7d5
commit 4a44bbe89d

133
main.go
View File

@ -6,8 +6,8 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"log"
"net" "net"
"os"
"strconv" "strconv"
"time" "time"
) )
@ -40,54 +40,84 @@ func handshake(addr string, port int, ver uint64) []byte {
return handshake.Bytes() return handshake.Bytes()
} }
func main() { func pingServer(conn net.Conn) time.Duration {
ping := make([]byte, 1)
start := time.Now()
conn.Write([]byte{0x01, 0x00})
_, _ = conn.Read(ping[:])
diff := time.Now().Sub(start)
return diff
}
func orig() error {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
addr := flag.String("addr", "127.0.0.1", "Server address") addr := flag.String("addr", "127.0.0.1", "Server address")
port := flag.Int("port", 25565, "Server Port") port := flag.Int("port", 25565, "Server Port")
ver := flag.Uint64("ver", 751, "Minecraft protocol version number") ver := flag.Uint64("ver", 751, "Minecraft protocol version number")
raw := flag.Bool("raw", false, "Prints raw json")
ping := flag.Bool("ping", false, "Pings the server")
flag.Parse() flag.Parse()
/*if len(os.Args) < 2 {
flag.Usage()
return
}*/
conn, err := net.Dial("tcp", *addr+":"+strconv.Itoa(*port)) conn, err := net.Dial("tcp", *addr+":"+strconv.Itoa(*port))
if err != nil { if err != nil {
log.Fatal(err) return err
} }
for { for {
err := conn.SetWriteDeadline(time.Now().Add(30 * time.Second)) err := conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil { if err != nil {
log.Println("WriteDealine failed:", err) conn.Close()
return return err
} }
conn.Write(handshake(*addr, *port, *ver)) conn.Write(handshake(*addr, *port, *ver))
conn.Write([]byte{0x01, 0x00}) conn.Write([]byte{0x01, 0x00})
recvBuf := make([]byte, 512)
var resp response
err = conn.SetReadDeadline(time.Now().Add(10 * time.Second)) err = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil { if err != nil {
log.Println("SetReadDealine failed:", err) conn.Close()
return return err
} }
recvBuf := make([]byte, 512)
var resp response
n, err := conn.Read(recvBuf) n, err := conn.Read(recvBuf)
if err != nil { if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() { if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Println("read timeout:", err)
} else {
log.Println("read error", err)
}
}
requestright := recvBuf[:n]
b := bytes.Split(requestright, []byte("{"))
ne := bytes.SplitAfterN(requestright, b[0], 2)
after := bytes.TrimSuffix(ne[1], []byte("\x00"))
if err := json.Unmarshal(after, &resp); err != nil {
log.Println(err)
conn.Close() conn.Close()
return return err
}
conn.Close()
return err
}
b := bytes.Split(recvBuf[:n], []byte("{"))
ne := bytes.SplitAfterN(recvBuf[:n], b[0], 2)
trim := bytes.TrimSuffix(ne[1], []byte("\x00"))
if *ping == false {
if err := json.Unmarshal(trim, &resp); err != nil {
conn.Close()
return err
}
if *raw == true {
json, err := json.MarshalIndent(resp, "", " ")
if err != nil {
conn.Close()
return err
}
fmt.Printf("%s\n", string(json))
conn.Close()
return err
} }
fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n", fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n",
resp.Description.Text, resp.Description.Text,
@ -100,21 +130,54 @@ func main() {
fmt.Printf("\t%s\n", player.Name) fmt.Printf("\t%s\n", player.Name)
} }
} }
}
recvBuf = make([]byte, 1) fmt.Printf("Ping: %+v\n", pingServer(conn))
start := time.Now()
conn.Write([]byte{0x01, 0x00})
_, _ = conn.Read(recvBuf[:])
diff := time.Now().Sub(start)
fmt.Printf("Ping: %+v\n", diff)
if err = conn.Close(); err != nil { if err = conn.Close(); err != nil {
log.Println(err) conn.Close()
return return err
} }
return return nil
} }
} }
func main() {
if err := orig(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run() error {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
addr := flag.String("addr", "127.0.0.1", "Server address")
port := flag.Int("port", 25565, "Server Port")
//ver := flag.Uint64("ver", 751, "Minecraft protocol version number")
flag.Parse()
conn, err := net.Dial("tcp", *addr+":"+strconv.Itoa(*port))
if err != nil {
return err
}
/*conn.Write(handshake(*addr, *port, *ver))
conn.Write([]byte{0x01, 0x00})*/
recvBuf := make([]byte, 512)
n, err := conn.Read(recvBuf)
if err != nil {
return err
}
conn.Write([]byte{0x00})
fmt.Println(string(recvBuf[:n]))
return nil
}
type response struct { type response struct {
Version struct { Version struct {
Name string `json:"name"` Name string `json:"name"`