Pass StreamCipher to PacketCodecs

A small number of packets have encrypted payloads.

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent a50e0ea998
commit 55d72dacbe
  1. 5
      protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt
  2. 5
      protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt
  3. 2
      protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt
  4. 2
      protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt
  5. 5
      protocol/src/main/kotlin/org/openrs2/protocol/login/InitJs5RemoteConnectionCodec.kt
  6. 5
      protocol/src/main/kotlin/org/openrs2/protocol/login/RequestWorldListCodec.kt
  7. 5
      protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt
  8. 5
      protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt
  9. 5
      protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt
  10. 5
      protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt
  11. 5
      protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt
  12. 5
      protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt

@ -1,16 +1,17 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
public abstract class EmptyPacketCodec<T : Packet>( public abstract class EmptyPacketCodec<T : Packet>(
private val packet: T, private val packet: T,
opcode: Int opcode: Int
) : PacketCodec<T>(packet.javaClass, opcode, length = 0) { ) : PacketCodec<T>(packet.javaClass, opcode, length = 0) {
override fun decode(input: ByteBuf): T { override fun decode(input: ByteBuf, cipher: StreamCipher): T {
return packet return packet
} }
override fun encode(input: T, output: ByteBuf) { override fun encode(input: T, output: ByteBuf, cipher: StreamCipher) {
// empty // empty
} }
} }

@ -2,6 +2,7 @@ package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator import io.netty.buffer.ByteBufAllocator
import org.openrs2.crypto.StreamCipher
public abstract class PacketCodec<T : Packet>( public abstract class PacketCodec<T : Packet>(
public val type: Class<T>, public val type: Class<T>,
@ -13,8 +14,8 @@ public abstract class PacketCodec<T : Packet>(
require(length >= PacketLength.VARIABLE_SHORT) require(length >= PacketLength.VARIABLE_SHORT)
} }
public abstract fun decode(input: ByteBuf): T public abstract fun decode(input: ByteBuf, cipher: StreamCipher): T
public abstract fun encode(input: T, output: ByteBuf) public abstract fun encode(input: T, output: ByteBuf, cipher: StreamCipher)
public open fun getLength(input: T): Int { public open fun getLength(input: T): Int {
return length return length

@ -65,7 +65,7 @@ public class Rs2Decoder(public var protocol: Protocol) : ByteToMessageDecoder()
val payload = input.readSlice(length) val payload = input.readSlice(length)
out += try { out += try {
decoder.decode(payload) decoder.decode(payload, cipher)
} catch (ex: NotImplementedError) { } catch (ex: NotImplementedError) {
// TODO(gpe): remove this catch block when every packet is implemented // TODO(gpe): remove this catch block when every packet is implemented
logger.warn { "Skipping unimplemented packet: ${decoder.javaClass}" } logger.warn { "Skipping unimplemented packet: ${decoder.javaClass}" }

@ -24,7 +24,7 @@ public class Rs2Encoder(public var protocol: Protocol) : MessageToByteEncoder<Pa
} }
val payloadIndex = out.writerIndex() val payloadIndex = out.writerIndex()
encoder.encode(msg, out) encoder.encode(msg, out, cipher)
val written = out.writerIndex() - payloadIndex val written = out.writerIndex() - payloadIndex

@ -1,6 +1,7 @@
package org.openrs2.protocol.login package org.openrs2.protocol.login
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
import org.openrs2.protocol.PacketCodec import org.openrs2.protocol.PacketCodec
public object InitJs5RemoteConnectionCodec : PacketCodec<LoginRequest.InitJs5RemoteConnection>( public object InitJs5RemoteConnectionCodec : PacketCodec<LoginRequest.InitJs5RemoteConnection>(
@ -8,12 +9,12 @@ public object InitJs5RemoteConnectionCodec : PacketCodec<LoginRequest.InitJs5Rem
opcode = 15, opcode = 15,
length = 4 length = 4
) { ) {
override fun decode(input: ByteBuf): LoginRequest.InitJs5RemoteConnection { override fun decode(input: ByteBuf, cipher: StreamCipher): LoginRequest.InitJs5RemoteConnection {
val build = input.readInt() val build = input.readInt()
return LoginRequest.InitJs5RemoteConnection(build) return LoginRequest.InitJs5RemoteConnection(build)
} }
override fun encode(input: LoginRequest.InitJs5RemoteConnection, output: ByteBuf) { override fun encode(input: LoginRequest.InitJs5RemoteConnection, output: ByteBuf, cipher: StreamCipher) {
output.writeInt(input.build) output.writeInt(input.build)
} }
} }

@ -1,6 +1,7 @@
package org.openrs2.protocol.login package org.openrs2.protocol.login
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
import org.openrs2.protocol.PacketCodec import org.openrs2.protocol.PacketCodec
public object RequestWorldListCodec : PacketCodec<LoginRequest.RequestWorldList>( public object RequestWorldListCodec : PacketCodec<LoginRequest.RequestWorldList>(
@ -8,12 +9,12 @@ public object RequestWorldListCodec : PacketCodec<LoginRequest.RequestWorldList>
opcode = 23, opcode = 23,
length = 4 length = 4
) { ) {
override fun decode(input: ByteBuf): LoginRequest.RequestWorldList { override fun decode(input: ByteBuf, cipher: StreamCipher): LoginRequest.RequestWorldList {
val checksum = input.readInt() val checksum = input.readInt()
return LoginRequest.RequestWorldList(checksum) return LoginRequest.RequestWorldList(checksum)
} }
override fun encode(input: LoginRequest.RequestWorldList, output: ByteBuf) { override fun encode(input: LoginRequest.RequestWorldList, output: ByteBuf, cipher: StreamCipher) {
output.writeInt(input.checksum) output.writeInt(input.checksum)
} }
} }

@ -1,18 +1,19 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object FixedPacketCodec : PacketCodec<FixedPacket>( internal object FixedPacketCodec : PacketCodec<FixedPacket>(
type = FixedPacket::class.java, type = FixedPacket::class.java,
opcode = 0, opcode = 0,
length = 4 length = 4
) { ) {
override fun decode(input: ByteBuf): FixedPacket { override fun decode(input: ByteBuf, cipher: StreamCipher): FixedPacket {
val value = input.readInt() val value = input.readInt()
return FixedPacket(value) return FixedPacket(value)
} }
override fun encode(input: FixedPacket, output: ByteBuf) { override fun encode(input: FixedPacket, output: ByteBuf, cipher: StreamCipher) {
output.writeInt(input.value) output.writeInt(input.value)
} }
} }

@ -1,18 +1,19 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object LengthMismatchPacketCodec : PacketCodec<FixedPacket>( internal object LengthMismatchPacketCodec : PacketCodec<FixedPacket>(
type = FixedPacket::class.java, type = FixedPacket::class.java,
opcode = 0, opcode = 0,
length = 5 length = 5
) { ) {
override fun decode(input: ByteBuf): FixedPacket { override fun decode(input: ByteBuf, cipher: StreamCipher): FixedPacket {
val value = input.readInt() val value = input.readInt()
return FixedPacket(value) return FixedPacket(value)
} }
override fun encode(input: FixedPacket, output: ByteBuf) { override fun encode(input: FixedPacket, output: ByteBuf, cipher: StreamCipher) {
output.writeInt(input.value) output.writeInt(input.value)
} }
} }

@ -1,19 +1,20 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object VariableByteOptimisedPacketCodec : PacketCodec<VariableByteOptimisedPacket>( internal object VariableByteOptimisedPacketCodec : PacketCodec<VariableByteOptimisedPacket>(
type = VariableByteOptimisedPacket::class.java, type = VariableByteOptimisedPacket::class.java,
opcode = 3, opcode = 3,
length = PacketLength.VARIABLE_BYTE length = PacketLength.VARIABLE_BYTE
) { ) {
override fun decode(input: ByteBuf): VariableByteOptimisedPacket { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableByteOptimisedPacket {
val value = ByteArray(input.readableBytes()) val value = ByteArray(input.readableBytes())
input.readBytes(value) input.readBytes(value)
return VariableByteOptimisedPacket(value) return VariableByteOptimisedPacket(value)
} }
override fun encode(input: VariableByteOptimisedPacket, output: ByteBuf) { override fun encode(input: VariableByteOptimisedPacket, output: ByteBuf, cipher: StreamCipher) {
output.writeBytes(input.value) output.writeBytes(input.value)
} }

@ -1,19 +1,20 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object VariableBytePacketCodec : PacketCodec<VariableBytePacket>( internal object VariableBytePacketCodec : PacketCodec<VariableBytePacket>(
type = VariableBytePacket::class.java, type = VariableBytePacket::class.java,
opcode = 1, opcode = 1,
length = PacketLength.VARIABLE_BYTE length = PacketLength.VARIABLE_BYTE
) { ) {
override fun decode(input: ByteBuf): VariableBytePacket { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableBytePacket {
val value = ByteArray(input.readableBytes()) val value = ByteArray(input.readableBytes())
input.readBytes(value) input.readBytes(value)
return VariableBytePacket(value) return VariableBytePacket(value)
} }
override fun encode(input: VariableBytePacket, output: ByteBuf) { override fun encode(input: VariableBytePacket, output: ByteBuf, cipher: StreamCipher) {
output.writeBytes(input.value) output.writeBytes(input.value)
} }
} }

@ -1,19 +1,20 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object VariableShortOptimisedPacketCodec : PacketCodec<VariableShortOptimisedPacket>( internal object VariableShortOptimisedPacketCodec : PacketCodec<VariableShortOptimisedPacket>(
type = VariableShortOptimisedPacket::class.java, type = VariableShortOptimisedPacket::class.java,
opcode = 4, opcode = 4,
length = PacketLength.VARIABLE_SHORT length = PacketLength.VARIABLE_SHORT
) { ) {
override fun decode(input: ByteBuf): VariableShortOptimisedPacket { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortOptimisedPacket {
val value = ByteArray(input.readableBytes()) val value = ByteArray(input.readableBytes())
input.readBytes(value) input.readBytes(value)
return VariableShortOptimisedPacket(value) return VariableShortOptimisedPacket(value)
} }
override fun encode(input: VariableShortOptimisedPacket, output: ByteBuf) { override fun encode(input: VariableShortOptimisedPacket, output: ByteBuf, cipher: StreamCipher) {
output.writeBytes(input.value) output.writeBytes(input.value)
} }

@ -1,19 +1,20 @@
package org.openrs2.protocol package org.openrs2.protocol
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
internal object VariableShortPacketCodec : PacketCodec<VariableShortPacket>( internal object VariableShortPacketCodec : PacketCodec<VariableShortPacket>(
type = VariableShortPacket::class.java, type = VariableShortPacket::class.java,
opcode = 2, opcode = 2,
length = PacketLength.VARIABLE_SHORT length = PacketLength.VARIABLE_SHORT
) { ) {
override fun decode(input: ByteBuf): VariableShortPacket { override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortPacket {
val value = ByteArray(input.readableBytes()) val value = ByteArray(input.readableBytes())
input.readBytes(value) input.readBytes(value)
return VariableShortPacket(value) return VariableShortPacket(value)
} }
override fun encode(input: VariableShortPacket, output: ByteBuf) { override fun encode(input: VariableShortPacket, output: ByteBuf, cipher: StreamCipher) {
output.writeBytes(input.value) output.writeBytes(input.value)
} }
} }

Loading…
Cancel
Save