Pass StreamCipher to PacketCodecs

A small number of packets have encrypted payloads.

Signed-off-by: Graham <gpe@openrs2.org>
pull/132/head
Graham 3 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
import io.netty.buffer.ByteBuf
import org.openrs2.crypto.StreamCipher
public abstract class EmptyPacketCodec<T : Packet>(
private val packet: T,
opcode: Int
) : PacketCodec<T>(packet.javaClass, opcode, length = 0) {
override fun decode(input: ByteBuf): T {
override fun decode(input: ByteBuf, cipher: StreamCipher): T {
return packet
}
override fun encode(input: T, output: ByteBuf) {
override fun encode(input: T, output: ByteBuf, cipher: StreamCipher) {
// empty
}
}

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save