[O] Better encapsulation

pull/131/head
Azalea 2025-03-11 09:04:39 -04:00
parent 8fd378852f
commit d4d3a2b36c
2 changed files with 32 additions and 25 deletions

View File

@ -2,7 +2,6 @@ package icu.samnyan.aqua.net.transfer
import ext.*
import icu.samnyan.aqua.sega.aimedb.AimeDbClient
import icu.samnyan.aqua.sega.aimedb.AimeDbClient.Companion.sendAimePacket
import icu.samnyan.aqua.sega.util.AllNetBillingDecoder
val keychipPattern = Regex("([A-Z\\d]{4}-[A-Z\\d]{11}|[A-Z\\d]{11})")
@ -21,7 +20,7 @@ class AllNetClient(val dns: String, val keychip: String, val game: String, val v
if (keychip.length == 11) keychip
else keychip.substring(0, 4) + keychip.substring(5, 11)
}
val aime by lazy { AimeDbClient(game, keychipShort) }
val aime by lazy { AimeDbClient(game, keychipShort, dns.substringAfter("://")) }
// Send AllNet PowerOn request to obtain game URL
val gameUrl by lazy {
@ -43,13 +42,7 @@ class AllNetClient(val dns: String, val keychip: String, val game: String, val v
?: throw Exception("PowerOn Failed: No game URL returned")
}
val userId by lazy {
when (card.length) {
20 -> aime.createReqLookupV2(card)
16 -> aime.createReqFelicaLookupV2(card)
else -> throw Exception("Invalid card. Please input either 20-digit numeric access code (e.g. 5010000...0) or 16-digit hex Felica ID (e.g. 012E123456789ABC).")
}.sendAimePacket(dns.substringAfter("://")).getLongLE(0x20)
}
val userId by lazy { aime.execLookup(card) }
fun findDataBroker(log: (String) -> Unit) = when (game) {
"SDHD" -> ChusanDataBroker(this, log)

View File

@ -5,10 +5,10 @@ import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled
import java.net.Socket
class AimeDbClient(val gameId: String, val keychipShort: String) {
class AimeDbClient(val gameId: String, val keychipShort: String, val server: String) {
// https://sega.bsnk.me/allnet/aimedb/common/#packet-header
fun createRequest(type: UShort, writer: ByteBuf.() -> Unit): ByteBuf
= AimeDbEncryption.encrypt(Unpooled.buffer(1024).clear().run {
fun createRequest(type: UShort, writer: ByteBuf.() -> Unit) =
AimeDbEncryption.encrypt(Unpooled.buffer(1024).clear().run {
writeShortLE(0xa13e) // 00 2b: Magic
writeShortLE(0x3087) // 02 2b: Version
writeShortLE(type.toInt()) // 04 2b: Type
@ -22,11 +22,11 @@ class AimeDbClient(val gameId: String, val keychipShort: String) {
copy(0, writerIndex()) // Trim unused bytes
})
private fun ByteBuf.writeAscii(value: String, length: Int)
= writeBytes(value.toByteArray(Charsets.US_ASCII).copyOf(length))
private fun ByteBuf.writeAscii(value: String, length: Int) =
writeBytes(value.toByteArray(Charsets.US_ASCII).copyOf(length))
fun createReqLookupV2(accessCode: String)
= createRequest(0x0fu) {
fun createReqLookupV2(accessCode: String) =
createRequest(0x0fu) {
// Access code is a 20-digit number, should be converted to a 10-byte array
writeBytes(ByteBufUtil.decodeHexDump(accessCode.padStart(20, '0')))
writeByte(0) // 0A 1b: Company code
@ -34,8 +34,8 @@ class AimeDbClient(val gameId: String, val keychipShort: String) {
writeIntLE(0) // 0C 4b: Serial number
}
fun createReqFelicaLookupV2(felicaIdm: String)
= createRequest(0x11u) {
fun createReqFelicaLookupV2(felicaIdm: String) =
createRequest(0x11u) {
writeBytes(ByteArray(16)) // 00 16b: Random Challenge
// 10 8b: Felica IDm
writeBytes(ByteBufUtil.decodeHexDump(felicaIdm.padStart(16, '0')))
@ -49,11 +49,25 @@ class AimeDbClient(val gameId: String, val keychipShort: String) {
writeIntLE(0) // 4C 4b: Unknown padding
}
companion object {
fun ByteBuf.sendAimePacket(server: String): ByteBuf
= Unpooled.wrappedBuffer(Socket(server, 22345).use {
it.getOutputStream().write(array())
fun createReqRegister(accessCode: String) =
createRequest(0x05u) {
// Access code is a 20-digit number, should be converted to a 10-byte array
writeBytes(ByteBufUtil.decodeHexDump(accessCode.padStart(20, '0')))
writeByte(0) // 0A 1b: Company code
writeByte(0) // 0B 1b: R/W Firmware version
writeIntLE(0) // 0C 4b: Serial number
}
fun sendAimePacket(buf: ByteBuf): ByteBuf =
Unpooled.wrappedBuffer(Socket(server, 22345).use {
it.getOutputStream().write(buf.array())
it.getInputStream().readBytes()
}).let { AimeDbEncryption.decrypt(it) }
}
fun execLookup(card: String) =
sendAimePacket(when (card.length) {
20 -> createReqLookupV2(card)
16 -> createReqFelicaLookupV2(card)
else -> throw Exception("Invalid card. Please input either 20-digit numeric access code (e.g. 5010000...0) or 16-digit hex Felica ID (e.g. 012E123456789ABC).")
}).getLongLE(0x20)
}