Пример #1
0
func (this *PacketGeneric) Decompress() (err error) {
	if !this.compressed {
		return
	}
	buffer := bytes.NewReader(this.Bytes)
	_, err = packet.ReadVarInt(buffer) // compression length
	if err != nil {
		return
	}
	zlibReader, err := zlib.NewReader(buffer)
	if err != nil {
		return
	}
	_, err = packet.ReadVarInt(zlibReader) // id
	if err != nil {
		return
	}
	bytes, err := ioutil.ReadAll(zlibReader)
	if err != nil {
		return
	}
	this.Bytes = bytes
	this.compressed = false
	return
}
Пример #2
0
func (this *PacketGeneric) SwapEntities(a int32, b int32, clientServer bool, protocol17 bool) {
	if a == b {
		return
	}
	if this.id == PACKET_CLIENT_SPAWN_OBJECT && clientServer {
		this.Decompress()
		buffer := bytes.NewBuffer(this.Bytes)
		_, err := packet.ReadVarInt(buffer)
		varIntLength := len(this.Bytes) - buffer.Len()
		if err == nil && len(this.Bytes) > varIntLength+19 {
			objectType := this.Bytes[varIntLength]
			if objectType == 60 || objectType == 61 || objectType == 62 || objectType == 63 || objectType == 64 || objectType == 65 || objectType == 66 || objectType == 90 {
				id := int32(binary.BigEndian.Uint32(this.Bytes[varIntLength+15 : varIntLength+19]))
				if id == a {
					binary.BigEndian.PutUint32(this.Bytes[varIntLength+15:varIntLength+19], uint32(b))
				} else if id == b {
					binary.BigEndian.PutUint32(this.Bytes[varIntLength+15:varIntLength+19], uint32(a))
				}
			}
		}
	} else if this.id == PACKET_CLIENT_DESTROY_ENTITIES && clientServer {
		this.Decompress()
		// TODO
	}
	this.swapEntitiesInt(a, b, clientServer, protocol17)
	this.swapEntitiesVarInt(a, b, clientServer, protocol17)
}
func (this *packetServerPluginMessageCodec) Decode(reader io.Reader, util []byte) (decode packet.Packet, err error) {
	packetServerPluginMessage := new(PacketServerPluginMessage)
	packetServerPluginMessage.Channel, err = packet.ReadString(reader, util)
	if err != nil {
		return
	}
	dataLength, err := packet.ReadVarInt(reader, util)
	if err != nil {
		return
	}
	if dataLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Data length is below zero: %d", dataLength))
		return
	}
	if dataLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Data length is above maximum: %d", dataLength))
		return
	}
	packetServerPluginMessage.Data = make([]byte, dataLength)
	_, err = reader.Read(packetServerPluginMessage.Data)
	if err != nil {
		return
	}
	decode = packetServerPluginMessage
	return
}
Пример #4
0
func (this *PacketGeneric) SwapEntities(a int32, b int32, clientServer bool) {
	if a == b {
		return
	}
	if this.id == PACKET_CLIENT_SPAWN_OBJECT && clientServer {
		buffer := bytes.NewBuffer(this.Bytes)
		bufferUtil := make([]byte, packet.UTIL_BUFFER_LENGTH)
		_, err := packet.ReadVarInt(buffer, bufferUtil)
		varIntLength := len(this.Bytes) - buffer.Len()
		if err == nil && len(this.Bytes) > varIntLength+19 {
			objectType := this.Bytes[varIntLength]
			if objectType == 60 || objectType == 61 || objectType == 62 || objectType == 63 || objectType == 64 || objectType == 65 || objectType == 66 || objectType == 90 {
				id := int32(binary.BigEndian.Uint32(this.Bytes[varIntLength+15 : varIntLength+19]))
				if id == a {
					binary.BigEndian.PutUint32(this.Bytes[varIntLength+15:varIntLength+19], uint32(b))
				} else if id == b {
					binary.BigEndian.PutUint32(this.Bytes[varIntLength+15:varIntLength+19], uint32(a))
				}
			}
		}
	} else if this.id == PACKET_CLIENT_DESTROY_ENTITIES && clientServer {
		// TODO
	}
	this.SwapEntitiesInt(a, b, clientServer)
	this.SwapEntitiesVarInt(a, b, clientServer)
}
func (this *packetClientSetCompressionCodec) Decode(reader io.Reader) (decode packet.Packet, err error) {
	packetClientSetCompression := new(PacketClientSetCompression)
	packetClientSetCompression.Threshold, err = packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	decode = packetClientSetCompression
	return
}
func (this *packetClientLoginEncryptRequestCodec) Decode(reader io.Reader) (decode packet.Packet, err error) {
	packetClientLoginEncryptRequest := new(PacketClientLoginEncryptRequest)
	packetClientLoginEncryptRequest.ServerId, err = packet.ReadString(reader)
	if err != nil {
		return
	}
	publicKeyLength, err := packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	if publicKeyLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Public key length is below zero: %d", publicKeyLength))
		return
	}
	if publicKeyLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Public key length is above maximum: %d", publicKeyLength))
		return
	}
	packetClientLoginEncryptRequest.PublicKey = make([]byte, publicKeyLength)
	_, err = reader.Read(packetClientLoginEncryptRequest.PublicKey)
	if err != nil {
		return
	}
	verifyTokenLength, err := packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	if verifyTokenLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Verify token length is below zero: %d", verifyTokenLength))
		return
	}
	if verifyTokenLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Verify token length is above maximum: %d", verifyTokenLength))
		return
	}
	packetClientLoginEncryptRequest.VerifyToken = make([]byte, verifyTokenLength)
	_, err = reader.Read(packetClientLoginEncryptRequest.VerifyToken)
	if err != nil {
		return
	}
	decode = packetClientLoginEncryptRequest
	return
}
Пример #7
0
func (this *PacketServerHandshakeCodec) Decode(reader io.Reader, util []byte) (decode packet.Packet, err error) {
	packetServerHandshake := &PacketServerHandshake{}
	packetServerHandshake.ProtocolVersion, err = packet.ReadVarInt(reader, util)
	if err != nil {
		return
	}
	packetServerHandshake.ServerAddress, err = packet.ReadString(reader, util)
	if err != nil {
		return
	}
	packetServerHandshake.ServerPort, err = packet.ReadUint16(reader, util)
	if err != nil {
		return
	}
	packetServerHandshake.State, err = packet.ReadVarInt(reader, util)
	if err != nil {
		return
	}
	return packetServerHandshake, nil
}
Пример #8
0
func (this *packetServerHandshakeCodec) Decode(reader io.Reader) (decode packet.Packet, err error) {
	packetServerHandshake := new(PacketServerHandshake)
	packetServerHandshake.ProtocolVersion, err = packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	packetServerHandshake.ServerAddress, err = packet.ReadString(reader)
	if err != nil {
		return
	}
	packetServerHandshake.ServerPort, err = packet.ReadUint16(reader)
	if err != nil {
		return
	}
	packetServerHandshake.State, err = packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	decode = packetServerHandshake
	return
}
func (this *packetServerLoginEncryptResponseCodec) Decode(reader io.Reader, util []byte) (decode packet.Packet, err error) {
	packetServerLoginEncryptResponse := new(PacketServerLoginEncryptResponse)
	sharedSecretLength, err := packet.ReadVarInt(reader, util)
	if err != nil {
		return
	}
	if sharedSecretLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Shared secret length is below zero: %d", sharedSecretLength))
		return
	}
	if sharedSecretLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Shared secret length is above maximum: %d", sharedSecretLength))
		return
	}
	packetServerLoginEncryptResponse.SharedSecret = make([]byte, sharedSecretLength)
	_, err = reader.Read(packetServerLoginEncryptResponse.SharedSecret)
	if err != nil {
		return
	}
	verifyTokenLength, err := packet.ReadVarInt(reader, util)
	if err != nil {
		return
	}
	if verifyTokenLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Verify token length is below zero: %d", verifyTokenLength))
		return
	}
	if verifyTokenLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Verify token length is above maximum: %d", verifyTokenLength))
		return
	}
	packetServerLoginEncryptResponse.VerifyToken = make([]byte, verifyTokenLength)
	_, err = reader.Read(packetServerLoginEncryptResponse.VerifyToken)
	if err != nil {
		return
	}
	decode = packetServerLoginEncryptResponse
	return
}
Пример #10
0
func (this *PacketGeneric) swapEntitiesVarInt(a int32, b int32, clientServer bool, protocol17 bool) {
	var positions []bool
	if clientServer {
		if protocol17 {
			positions = PlayPacketClientEntityVarIntPositions17
		} else {
			positions = PlayPacketClientEntityVarIntPositions
		}
	} else {
		if protocol17 {
			positions = PlayPacketServerEntityVarIntPositions17
		} else {
			positions = PlayPacketServerEntityVarIntPositions
		}
	}
	if this.id < 0 {
		return
	}
	if this.id >= len(positions) {
		return
	}
	if positions[this.id] == false {
		return
	}
	this.Decompress()
	// Read the old Id
	buffer := bytes.NewBuffer(this.Bytes)
	bufferUtil := make([]byte, packet.UTIL_BUFFER_LENGTH)
	id, err := packet.ReadVarInt(buffer, bufferUtil)
	if err != nil {
		return
	}
	// Check the Id
	var newId int
	if id == int(a) {
		newId = int(b)
	} else if id == int(b) {
		newId = int(a)
	} else {
		return
	}
	// Apply the new Id
	newBuffer := new(bytes.Buffer)
	err = packet.WriteVarInt(newBuffer, bufferUtil, newId)
	if err != nil {
		return
	}
	buffer.WriteTo(newBuffer)
	this.Bytes = newBuffer.Bytes()
}
Пример #11
0
func (this *packetClientPlayerListCodec) Decode(reader io.Reader) (decode packet.Packet, err error) {
	packetClientPlayerList := new(PacketClientPlayerList)
	packetClientPlayerList.Action, err = packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	itemLength, err := packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	if itemLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Item length is below zero: %d", itemLength))
		return
	}
	if itemLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Item length is above maximum: %d", itemLength))
		return
	}
	packetClientPlayerList.Items = make([]PacketClientPlayerListItem, itemLength)
	for i, _ := range packetClientPlayerList.Items {
		item := &packetClientPlayerList.Items[i]
		item.UUID, err = packet.ReadUUID(reader)
		if err != nil {
			return
		}
		switch packetClientPlayerList.Action {
		case PACKET_CLIENT_PLAYER_LIST_ACTION_ADD:
			addPlayer := PacketClientPlayerListAddPlayer{}
			addPlayer.Name, err = packet.ReadString(reader)
			if err != nil {
				return
			}
			var propertiesLength int
			propertiesLength, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			if propertiesLength < 0 {
				err = errors.New(fmt.Sprintf("Decode, Properties length is below zero: %d", propertiesLength))
				return
			}
			if propertiesLength > 65535 {
				err = errors.New(fmt.Sprintf("Decode, Properties length is above maximum: %d", propertiesLength))
				return
			}
			addPlayer.Properties = make([]PacketClientPlayerListAddPlayerProperty, propertiesLength)
			for j, _ := range addPlayer.Properties {
				property := &addPlayer.Properties[j]
				property.Name, err = packet.ReadString(reader)
				if err != nil {
					return
				}
				property.Value, err = packet.ReadString(reader)
				if err != nil {
					return
				}
				var signed bool
				signed, err = packet.ReadBool(reader)
				if err != nil {
					return
				}
				if signed {
					property.Signature, err = packet.ReadString(reader)
					if err != nil {
						return
					}
				}
			}
			addPlayer.Gamemode, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			addPlayer.Latency, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			var hasDisplayName bool
			hasDisplayName, err = packet.ReadBool(reader)
			if err != nil {
				return
			}
			if hasDisplayName {
				addPlayer.DisplayName, err = packet.ReadString(reader)
				if err != nil {
					return
				}
			}
			item.Info = addPlayer
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_GAMEMODE:
			updateGamemode := PacketClientPlayerListUpdateGamemode{}
			updateGamemode.Gamemode, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			item.Info = updateGamemode
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_LATENCY:
			updateLatency := PacketClientPlayerListUpdateLatency{}
			updateLatency.Latency, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			item.Info = updateLatency
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_DISPLAY_NAME:
			updateDisplayName := PacketClientPlayerListUpdateDisplayName{}
			var hasDisplayName bool
			hasDisplayName, err = packet.ReadBool(reader)
			if err != nil {
				return
			}
			if hasDisplayName {
				updateDisplayName.DisplayName, err = packet.ReadString(reader)
				if err != nil {
					return
				}
			}
			item.Info = updateDisplayName
		case PACKET_CLIENT_PLAYER_LIST_ACTION_REMOVE:
			// no payload
		default:
			err = errors.New(fmt.Sprintf("Decode, PacketClientPlayerList action is not valid: %d", packetClientPlayerList.Action))
		}
	}
	decode = packetClientPlayerList
	return
}
func (this *packetClientTeamsCodec) Decode(reader io.Reader, util []byte) (decode packet.Packet, err error) {
	packetClientTeams := new(PacketClientTeams)
	packetClientTeams.Name, err = packet.ReadString(reader, util)
	if err != nil {
		return
	}
	packetClientTeams.Action, err = packet.ReadInt8(reader, util)
	if err != nil {
		return
	}
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 2 {
		packetClientTeams.DisplayName, err = packet.ReadString(reader, util)
		if err != nil {
			return
		}
		packetClientTeams.Prefix, err = packet.ReadString(reader, util)
		if err != nil {
			return
		}
		packetClientTeams.Suffix, err = packet.ReadString(reader, util)
		if err != nil {
			return
		}
		packetClientTeams.FriendlyFire, err = packet.ReadInt8(reader, util)
		if err != nil {
			return
		}
		packetClientTeams.NameTagVisibility, err = packet.ReadString(reader, util)
		if err != nil {
			return
		}
		packetClientTeams.Color, err = packet.ReadInt8(reader, util)
		if err != nil {
			return
		}
	}
	if packetClientTeams.Action == 0 || packetClientTeams.Action == 3 || packetClientTeams.Action == 4 {
		var playersLength int
		playersLength, err = packet.ReadVarInt(reader, util)
		if err != nil {
			return
		}
		if playersLength < 0 {
			err = errors.New(fmt.Sprintf("Decode, Players length is below zero: %d", playersLength))
			return
		}
		if playersLength > 65535 {
			err = errors.New(fmt.Sprintf("Decode, Players length is above maximum: %d", playersLength))
			return
		}
		packetClientTeams.Players = make([]string, playersLength)
		for i := 0; i < playersLength; i++ {
			packetClientTeams.Players[i], err = packet.ReadString(reader, util)
			if err != nil {
				return
			}
		}
	}
	decode = packetClientTeams
	return
}