[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 ext.*
import icu.samnyan.aqua.sega.aimedb.AimeDbClient import icu.samnyan.aqua.sega.aimedb.AimeDbClient
import icu.samnyan.aqua.sega.aimedb.AimeDbClient.Companion.sendAimePacket
import icu.samnyan.aqua.sega.util.AllNetBillingDecoder import icu.samnyan.aqua.sega.util.AllNetBillingDecoder
val keychipPattern = Regex("([A-Z\\d]{4}-[A-Z\\d]{11}|[A-Z\\d]{11})") 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 if (keychip.length == 11) keychip
else keychip.substring(0, 4) + keychip.substring(5, 11) 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 // Send AllNet PowerOn request to obtain game URL
val gameUrl by lazy { 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") ?: throw Exception("PowerOn Failed: No game URL returned")
} }
val userId by lazy { val userId by lazy { aime.execLookup(card) }
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)
}
fun findDataBroker(log: (String) -> Unit) = when (game) { fun findDataBroker(log: (String) -> Unit) = when (game) {
"SDHD" -> ChusanDataBroker(this, log) "SDHD" -> ChusanDataBroker(this, log)

View File

@ -5,10 +5,10 @@ import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import java.net.Socket 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 // https://sega.bsnk.me/allnet/aimedb/common/#packet-header
fun createRequest(type: UShort, writer: ByteBuf.() -> Unit): ByteBuf fun createRequest(type: UShort, writer: ByteBuf.() -> Unit) =
= AimeDbEncryption.encrypt(Unpooled.buffer(1024).clear().run { AimeDbEncryption.encrypt(Unpooled.buffer(1024).clear().run {
writeShortLE(0xa13e) // 00 2b: Magic writeShortLE(0xa13e) // 00 2b: Magic
writeShortLE(0x3087) // 02 2b: Version writeShortLE(0x3087) // 02 2b: Version
writeShortLE(type.toInt()) // 04 2b: Type writeShortLE(type.toInt()) // 04 2b: Type
@ -22,11 +22,11 @@ class AimeDbClient(val gameId: String, val keychipShort: String) {
copy(0, writerIndex()) // Trim unused bytes copy(0, writerIndex()) // Trim unused bytes
}) })
private fun ByteBuf.writeAscii(value: String, length: Int) private fun ByteBuf.writeAscii(value: String, length: Int) =
= writeBytes(value.toByteArray(Charsets.US_ASCII).copyOf(length)) writeBytes(value.toByteArray(Charsets.US_ASCII).copyOf(length))
fun createReqLookupV2(accessCode: String) fun createReqLookupV2(accessCode: String) =
= createRequest(0x0fu) { createRequest(0x0fu) {
// Access code is a 20-digit number, should be converted to a 10-byte array // Access code is a 20-digit number, should be converted to a 10-byte array
writeBytes(ByteBufUtil.decodeHexDump(accessCode.padStart(20, '0'))) writeBytes(ByteBufUtil.decodeHexDump(accessCode.padStart(20, '0')))
writeByte(0) // 0A 1b: Company code writeByte(0) // 0A 1b: Company code
@ -34,8 +34,8 @@ class AimeDbClient(val gameId: String, val keychipShort: String) {
writeIntLE(0) // 0C 4b: Serial number writeIntLE(0) // 0C 4b: Serial number
} }
fun createReqFelicaLookupV2(felicaIdm: String) fun createReqFelicaLookupV2(felicaIdm: String) =
= createRequest(0x11u) { createRequest(0x11u) {
writeBytes(ByteArray(16)) // 00 16b: Random Challenge writeBytes(ByteArray(16)) // 00 16b: Random Challenge
// 10 8b: Felica IDm // 10 8b: Felica IDm
writeBytes(ByteBufUtil.decodeHexDump(felicaIdm.padStart(16, '0'))) 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 writeIntLE(0) // 4C 4b: Unknown padding
} }
companion object { fun createReqRegister(accessCode: String) =
fun ByteBuf.sendAimePacket(server: String): ByteBuf createRequest(0x05u) {
= Unpooled.wrappedBuffer(Socket(server, 22345).use { // Access code is a 20-digit number, should be converted to a 10-byte array
it.getOutputStream().write(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() it.getInputStream().readBytes()
}).let { AimeDbEncryption.decrypt(it) } }).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)
} }