// HandleFE handles incoming requests of packet 0xFE: ServerListPing func HandleFE(server *Server, sender *player.Player) { pkt := new(packet.ServerListPing) pkt.ReadFrom(sender.Conn) if pkt.Magic != 1 { s := "Invalid %#x packet. Field Magic should be 1, got %d." reason := fmt.Sprintf(s, pkt.Id(), pkt.Magic) resp := packet.Disconnect{reason} resp.WriteTo(sender.Conn) return } in := fmt.Sprintf("%d", server.Players.Len()) max := server.config.Get("server.max_players") resp := ping.Ping(ping.Prepare(server.Motd, in, max)) resp.WriteTo(sender.Conn) }
func handle(c net.Conn) { defer c.Close() log.Println("Got connection from:", c.RemoteAddr()) // Read first two bytes NMC sends var buf = make([]byte, 2) _, err := c.Read(buf) if err != nil { log.Printf("%s: %s\n", c.RemoteAddr(), err) return } // Equal to 0xFE? if buf[0] != packet.PacketServerListPing || buf[1] != 0x01 { return } // Send response p := ping.Ping(Flags[:]) p.WriteTo(c) }