diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.java b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.java deleted file mode 100644 index 42305a13..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.java +++ /dev/null @@ -1,86 +0,0 @@ -/* -This function is based on minime, commit e90068f32510dc4e14eef62d099272ffb6ce8ec3 by xmc -and ported by skogaby. Thanks. -*/ -package icu.samnyan.aqua.sega.aimedb.handler.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler; -import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil; -import icu.samnyan.aqua.sega.aimedb.util.LogMapper; -import icu.samnyan.aqua.sega.general.dao.CardRepository; -import icu.samnyan.aqua.sega.general.model.Card; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -@Component -public class FeliCaLookup2Handler implements BaseHandler { - - private static final Logger logger = LoggerFactory.getLogger(FeliCaLookup2Handler.class); - - private final LogMapper logMapper; - - private final CardRepository cardRepository; - - @Autowired - public FeliCaLookup2Handler(LogMapper logMapper, CardRepository cardRepository) { - this.logMapper = logMapper; - this.cardRepository = cardRepository; - } - - @Override - public void handle(ChannelHandlerContext ctx, ByteBuf msg) throws JsonProcessingException { - Map requestMap = AimeDbUtil.getBaseInfo(msg); - requestMap.put("type", "felica_lookup2"); - requestMap.put("idm", msg.slice(0x0030, 0x0038 - 0x0030)); - requestMap.put("pmm", msg.slice(0x0038, 0x0040 - 0x0038)); - - logger.info("Request: " + logMapper.write(requestMap)); - - // Get the decimal represent of the hex value, same from minime - StringBuilder accessCode = new StringBuilder( - String.valueOf(((ByteBuf) requestMap.get("idm")).getLong(0)).replaceAll("-","") // Prevent negative overflow - ); - while (accessCode.length() < 20) { - accessCode.insert(0, "0"); - } - - long aimeId = -1; - Optional card = cardRepository.findByLuid(accessCode.toString()); - if (card.isPresent()) { - aimeId = card.get().getExtId(); - } - - Map resultMap = new HashMap<>(); - resultMap.put("type", "felica_lookup2"); - resultMap.put("status", 1); - resultMap.put("accessCode", accessCode); - resultMap.put("aimeId", cardRepository.findByLuid(accessCode.toString())); - - logger.info("Response: " + logMapper.write(resultMap)); - - ByteBuf respSrc = Unpooled.copiedBuffer(new byte[0x0140]); - respSrc.setShortLE(0x0004, 0x0012); - respSrc.setShortLE(0x0008, (int) resultMap.get("status")); - respSrc.setLongLE(0x0020, aimeId); - respSrc.setIntLE(0x0024, 0xFFFFFFFF); - respSrc.setIntLE(0x0028, 0xFFFFFFFF); - respSrc.setBytes(0x002c, ByteBufUtil.decodeHexDump(accessCode)); - respSrc.setShortLE(0x0037, 0x0001); - - ctx.writeAndFlush(respSrc); - } -} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.kt new file mode 100644 index 00000000..6f3ff183 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookup2Handler.kt @@ -0,0 +1,57 @@ +package icu.samnyan.aqua.sega.aimedb.handler.impl + +import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler +import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil +import icu.samnyan.aqua.sega.aimedb.util.LogMapper +import icu.samnyan.aqua.sega.general.dao.CardRepository +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufUtil +import io.netty.buffer.Unpooled +import io.netty.channel.ChannelHandlerContext +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +/** + * @author samnyan (privateamusement@protonmail.com) + */ +@Component +class FeliCaLookup2Handler( + val logMapper: LogMapper, + val cardRepository: CardRepository +) : BaseHandler { + val logger: Logger = LoggerFactory.getLogger(FeliCaLookup2Handler::class.java) + + override fun handle(ctx: ChannelHandlerContext, msg: ByteBuf) { + val requestMap = AimeDbUtil.getBaseInfo(msg) + requestMap["type"] = "felica_lookup2" + requestMap["idm"] = msg.slice(0x0030, 0x0038 - 0x0030) + requestMap["pmm"] = msg.slice(0x0038, 0x0040 - 0x0038) + + logger.info("Request: " + logMapper.write(requestMap)) + + // Get the decimal represent of the hex value, same from minime + val accessCode = (requestMap["idm"] as ByteBuf).getLong(0).toString() + .replace("-", "") // Prevent negative overflow + .padStart(20, '0') + + var aimeId: Long = -1 + val card = cardRepository.findByLuid(accessCode) + if (card.isPresent) { + aimeId = card.get().extId + } + + logger.info("Response: $accessCode, $aimeId") + + val respSrc = Unpooled.copiedBuffer(ByteArray(0x0140)) + respSrc.setShortLE(0x0004, 0x0012) + respSrc.setShortLE(0x0008, 1) + respSrc.setLongLE(0x0020, aimeId) + respSrc.setIntLE(0x0024, -0x1) + respSrc.setIntLE(0x0028, -0x1) + respSrc.setBytes(0x002c, ByteBufUtil.decodeHexDump(accessCode)) + respSrc.setShortLE(0x0037, 0x0001) + + ctx.writeAndFlush(respSrc) + } +} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.java b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.java deleted file mode 100644 index ddeb455e..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.java +++ /dev/null @@ -1,68 +0,0 @@ -package icu.samnyan.aqua.sega.aimedb.handler.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler; -import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil; -import icu.samnyan.aqua.sega.aimedb.util.LogMapper; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -@Component -public class FeliCaLookupHandler implements BaseHandler { - - private static final Logger logger = LoggerFactory.getLogger(FeliCaLookupHandler.class); - - private final LogMapper logMapper; - - @Autowired - public FeliCaLookupHandler(LogMapper logMapper) { - this.logMapper = logMapper; - } - - @Override - public void handle(ChannelHandlerContext ctx, ByteBuf msg) throws JsonProcessingException { - Map requestMap = AimeDbUtil.getBaseInfo(msg); - requestMap.put("type", "felica_lookup"); - requestMap.put("idm", msg.slice(0x0020, 0x0028 - 0x0020)); - requestMap.put("pmm", msg.slice(0x0028, 0x0030 - 0x0028)); - - logger.info("Request: " + logMapper.write(requestMap)); - - - // Get the decimal represent of the hex value, same from minime - StringBuilder accessCode = new StringBuilder( - String.valueOf(((ByteBuf) requestMap.get("idm")).getLong(0)).replaceAll("-","") // Prevent negative overflow - ); - while (accessCode.length() < 20) { - accessCode.insert(0, "0"); - } - - - Map resultMap = new HashMap<>(); - resultMap.put("type", "felica_lookup"); - resultMap.put("status", 1); - resultMap.put("accessCode", accessCode); - - logger.info("Response: " + logMapper.write(resultMap)); - - ByteBuf respSrc = Unpooled.copiedBuffer(new byte[0x0030]); - respSrc.setShortLE(0x0004, 0x0003); - respSrc.setShortLE(0x0008, (int) resultMap.get("status")); - respSrc.setBytes(0x0024, ByteBufUtil.decodeHexDump(accessCode)); - - ctx.writeAndFlush(respSrc); - - } -} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.kt new file mode 100644 index 00000000..7edc9556 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/FeliCaLookupHandler.kt @@ -0,0 +1,44 @@ +package icu.samnyan.aqua.sega.aimedb.handler.impl + +import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler +import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil +import icu.samnyan.aqua.sega.aimedb.util.LogMapper +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufUtil +import io.netty.buffer.Unpooled +import io.netty.channel.ChannelHandlerContext +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +/** + * @author samnyan (privateamusement@protonmail.com) + */ +@Component +class FeliCaLookupHandler(val logMapper: LogMapper) : BaseHandler { + val logger: Logger = LoggerFactory.getLogger(FeliCaLookupHandler::class.java) + + override fun handle(ctx: ChannelHandlerContext, msg: ByteBuf) { + val requestMap = AimeDbUtil.getBaseInfo(msg) + requestMap["type"] = "felica_lookup" + requestMap["idm"] = msg.slice(0x0020, 0x0028 - 0x0020) + requestMap["pmm"] = msg.slice(0x0028, 0x0030 - 0x0028) + + logger.info("Request: " + logMapper.write(requestMap)) + + + // Get the decimal represent of the hex value, same from minime + val accessCode = (requestMap["idm"] as ByteBuf).getLong(0).toString() + .replace("-", "") // Prevent negative overflow + .padStart(20, '0') + + logger.info("Response: $accessCode") + + val respSrc = Unpooled.copiedBuffer(ByteArray(0x0030)) + respSrc.setShortLE(0x0004, 0x0003) + respSrc.setShortLE(0x0008, 1) + respSrc.setBytes(0x0024, ByteBufUtil.decodeHexDump(accessCode)) + + ctx.writeAndFlush(respSrc) + } +} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.java b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.java deleted file mode 100644 index 236817a2..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.java +++ /dev/null @@ -1,70 +0,0 @@ -package icu.samnyan.aqua.sega.aimedb.handler.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler; -import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil; -import icu.samnyan.aqua.sega.aimedb.util.LogMapper; -import icu.samnyan.aqua.sega.general.dao.CardRepository; -import icu.samnyan.aqua.sega.general.model.Card; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -@Component -public class Lookup2Handler implements BaseHandler { - - private static final Logger logger = LoggerFactory.getLogger(Lookup2Handler.class); - - private final LogMapper logMapper; - - private final CardRepository cardRepository; - - @Autowired - public Lookup2Handler(LogMapper logMapper, CardRepository cardRepository) { - this.logMapper = logMapper; - this.cardRepository = cardRepository; - } - - @Override - public void handle(ChannelHandlerContext ctx, ByteBuf msg) throws JsonProcessingException { - Map requestMap = AimeDbUtil.getBaseInfo(msg); - requestMap.put("type", "lookup2"); - requestMap.put("luid", ByteBufUtil.hexDump(msg.slice(0x0020, 0x002a - 0x0020))); - - logger.info("Request: " + logMapper.write(requestMap)); - - long aimeId = -1; - Optional card = cardRepository.findByLuid((String) requestMap.get("luid")); - if (card.isPresent()) { - aimeId = card.get().getExtId(); - } - - Map resultMap = new HashMap<>(); - resultMap.put("type", "lookup2"); - resultMap.put("status", 1); - resultMap.put("aimeId", aimeId); - resultMap.put("registerLevel", "none"); - - logger.info("Response: " + logMapper.write(resultMap)); - - ByteBuf respSrc = Unpooled.copiedBuffer(new byte[0x0130]); - respSrc.setShortLE(0x0004, 0x0010); - respSrc.setShortLE(0x0008, (int) resultMap.get("status")); - respSrc.setLongLE(0x0020, (long) resultMap.get("aimeId")); - respSrc.setByte(0x0024, 0); - - ctx.writeAndFlush(respSrc); - } -} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.kt new file mode 100644 index 00000000..f49b6ec7 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/Lookup2Handler.kt @@ -0,0 +1,48 @@ +package icu.samnyan.aqua.sega.aimedb.handler.impl + +import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler +import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil.getBaseInfo +import icu.samnyan.aqua.sega.aimedb.util.LogMapper +import icu.samnyan.aqua.sega.general.dao.CardRepository +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufUtil +import io.netty.buffer.Unpooled +import io.netty.channel.ChannelHandlerContext +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +/** + * @author samnyan (privateamusement@protonmail.com) + */ +@Component +class Lookup2Handler( + val logMapper: LogMapper, + val cardRepository: CardRepository +) : BaseHandler { + val logger: Logger = LoggerFactory.getLogger(Lookup2Handler::class.java) + + override fun handle(ctx: ChannelHandlerContext, msg: ByteBuf) { + val requestMap = getBaseInfo(msg) + requestMap["type"] = "lookup2" + requestMap["luid"] = ByteBufUtil.hexDump(msg.slice(0x0020, 0x002a - 0x0020)) + + logger.info("Request: " + logMapper.write(requestMap)) + + var aimeId: Long = -1 + val card = cardRepository.findByLuid(requestMap["luid"] as String?) + if (card.isPresent) { + aimeId = card.get().extId + } + + logger.info("Response: $aimeId") + + val respSrc = Unpooled.copiedBuffer(ByteArray(0x0130)) + respSrc.setShortLE(0x0004, 0x0010) + respSrc.setShortLE(0x0008, 1) + respSrc.setLongLE(0x0020, aimeId) + respSrc.setByte(0x0024, 0) + + ctx.writeAndFlush(respSrc) + } +} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.java b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.java deleted file mode 100644 index 0844d9c4..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.java +++ /dev/null @@ -1,72 +0,0 @@ -package icu.samnyan.aqua.sega.aimedb.handler.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler; -import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil; -import icu.samnyan.aqua.sega.aimedb.util.LogMapper; -import icu.samnyan.aqua.sega.general.dao.CardRepository; -import icu.samnyan.aqua.sega.general.model.Card; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -/** - * Mifare Card lookup? idk - * - * @author samnyan (privateamusement@protonmail.com) - */ -@Component -public class LookupHandler implements BaseHandler { - - private static final Logger logger = LoggerFactory.getLogger(LookupHandler.class); - - private final LogMapper logMapper; - - private final CardRepository cardRepository; - - @Autowired - public LookupHandler(LogMapper logMapper, CardRepository cardRepository) { - this.logMapper = logMapper; - this.cardRepository = cardRepository; - } - - @Override - public void handle(ChannelHandlerContext ctx, ByteBuf msg) throws JsonProcessingException { - Map requestMap = AimeDbUtil.getBaseInfo(msg); - requestMap.put("type", "lookup"); - requestMap.put("luid", ByteBufUtil.hexDump(msg.slice(0x0020, 0x002a - 0x0020))); - - logger.info("Request: " + logMapper.write(requestMap)); - - long aimeId = -1; - Optional card = cardRepository.findByLuid((String) requestMap.get("luid")); - if (card.isPresent()) { - aimeId = card.get().getExtId(); - } - - Map resultMap = new HashMap<>(); - resultMap.put("type", "lookup"); - resultMap.put("status", 1); - resultMap.put("aimeId", aimeId); - resultMap.put("registerLevel", "none"); - - logger.info("Response: " + logMapper.write(resultMap)); - - ByteBuf respSrc = Unpooled.copiedBuffer(new byte[0x0130]); - respSrc.setShortLE(0x0004, 0x0006); - respSrc.setShortLE(0x0008, (int) resultMap.get("status")); - respSrc.setLongLE(0x0020, (long) resultMap.get("aimeId")); - respSrc.setByte(0x0024, 0); - - ctx.writeAndFlush(respSrc); - } -} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.kt new file mode 100644 index 00000000..9c6fbcd8 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/handler/impl/LookupHandler.kt @@ -0,0 +1,51 @@ +package icu.samnyan.aqua.sega.aimedb.handler.impl + +import icu.samnyan.aqua.sega.aimedb.handler.BaseHandler +import icu.samnyan.aqua.sega.aimedb.util.AimeDbUtil.getBaseInfo +import icu.samnyan.aqua.sega.aimedb.util.LogMapper +import icu.samnyan.aqua.sega.general.dao.CardRepository +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufUtil +import io.netty.buffer.Unpooled +import io.netty.channel.ChannelHandlerContext +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Component + +/** + * Mifare Card lookup? idk + * + * @author samnyan (privateamusement@protonmail.com) + */ +@Component +class LookupHandler( + val logMapper: LogMapper, + val cardRepository: CardRepository +) : BaseHandler { + val logger: Logger = LoggerFactory.getLogger(LookupHandler::class.java) + + override fun handle(ctx: ChannelHandlerContext, msg: ByteBuf) { + val requestMap = getBaseInfo(msg) + requestMap["type"] = "lookup" + requestMap["luid"] = ByteBufUtil.hexDump(msg.slice(0x0020, 0x002a - 0x0020)) + + logger.info("Request: " + logMapper.write(requestMap)) + + var aimeId: Long = -1 + val card = cardRepository.findByLuid(requestMap["luid"] as String?) + if (card.isPresent) { + aimeId = card.get().extId + } + + logger.info("Response: $aimeId") + + val respSrc = Unpooled.copiedBuffer(ByteArray(0x0130)) + respSrc.setShortLE(0x0004, 0x0006) + respSrc.setShortLE(0x0008, 1) + respSrc.setLongLE(0x0020, aimeId) + respSrc.setByte(0x0024, 0) + + ctx.writeAndFlush(respSrc) + } +} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.java b/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.java deleted file mode 100644 index 59825310..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.java +++ /dev/null @@ -1,20 +0,0 @@ -package icu.samnyan.aqua.sega.aimedb.util; - -import io.netty.buffer.ByteBuf; - -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -public class AimeDbUtil { - - public static Map getBaseInfo(ByteBuf in) { - Map resultMap = new HashMap<>(); - resultMap.put("gameId", in.toString(0x000a, 0x000e - 0x000a, StandardCharsets.US_ASCII)); - resultMap.put("keychipId", in.toString(0x0014, 0x001f - 0x0014, StandardCharsets.US_ASCII)); - return resultMap; - } -} diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.kt new file mode 100644 index 00000000..6fd5bd94 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/util/AimeDbUtil.kt @@ -0,0 +1,15 @@ +package icu.samnyan.aqua.sega.aimedb.util + +import io.netty.buffer.ByteBuf +import java.nio.charset.StandardCharsets + +/** + * @author samnyan (privateamusement@protonmail.com) + */ +object AimeDbUtil { + @JvmStatic + fun getBaseInfo(input: ByteBuf) = mutableMapOf( + "gameId" to input.toString(0x000a, 0x000e - 0x000a, StandardCharsets.US_ASCII), + "keychipId" to input.toString(0x0014, 0x001f - 0x0014, StandardCharsets.US_ASCII) + ) +}