Ejemplo n.º 1
0
func handlePacketID1(player *Player, ps protocol.PacketStream) error {
	if ps.GetRemainingBytes() == 0 {
		return nil
	}

	status, found := statuses[player.Hostname]
	if !found {
		status = &ping.Status{
			ShowConnection: false,
		}
	}

	return ping.HandlePingPacket(ps.Stream, *status)
}
Ejemplo n.º 2
0
func handlePacketID0(player *Player, ps protocol.PacketStream) error {
	if ps.GetRemainingBytes() == 0 {
		if player.State != 1 {
			return nil
		}

		status, found := statuses[player.Hostname]
		if !found {
			player.ShouldClose = true
			return nil
		}

		err := ping.WriteHandshakeResponse(ps.Stream, *status)
		if err != nil {
			return err
		}

		return nil
	}

	switch player.State {
	case 1:
		handshake, err := ping.ReadHandshakePacket(ps.Stream)
		if err != nil {
			log.Println("beacon: Handshake packet read error:", err)
		}

		player.Hostname = strings.ToLower(handshake.ServerAddress)

		if address, found := forwarders[player.Hostname]; found {
			// Write the handshake data
			initialPacket := protocol.NewPacketWithID(0x00)
			initialPacket.WriteVarInt(handshake.ProtocolNumber)
			initialPacket.WriteString(handshake.ServerAddress)
			initialPacket.WriteUInt16(handshake.ServerPort)
			initialPacket.WriteVarInt(handshake.NextState)
			player.InitialPacket = initialPacket
			player.ForwardAddress = address
			player.State = handshake.NextState
			return nil
		}

		if handshake.NextState == 1 {
			player.State = 1
		} else if handshake.NextState == 2 {
			player.State = 2
		}
	case 2:
		username, err := ps.ReadString()
		if err != nil {
			return err
		}

		player.Username = username

		handler, found := handlers[player.Hostname]
		if !found {
			log.Println("beacon: Missing handler for hostname: " +
				player.Hostname)
			err := ping.DisplayMessage(ps.Stream,
				"Connection rejected. There is no server on this hostname.")
			if err != nil {
				return err
			}

			return nil
		}

		err = ping.DisplayMessage(ps.Stream, handler(player))
		if err != nil {
			return err
		}
	}

	return nil
}