mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-10 15:17:26 +08:00
[O] More logging
This commit is contained in:
@@ -30,7 +30,10 @@ annotation class Doc(
|
|||||||
|
|
||||||
// Make it easier to throw a ResponseStatusException
|
// Make it easier to throw a ResponseStatusException
|
||||||
operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ApiException(value(), message ?: this.reasonPhrase)
|
operator fun HttpStatus.invoke(message: String? = null): Nothing = throw ApiException(value(), message ?: this.reasonPhrase)
|
||||||
operator fun Int.minus(message: String): Nothing = throw ApiException(this, message)
|
operator fun Int.minus(message: String): Nothing {
|
||||||
|
ApiException.log.info("> Error $this: $message")
|
||||||
|
throw ApiException(this, message)
|
||||||
|
}
|
||||||
|
|
||||||
// Email validation
|
// Email validation
|
||||||
// https://www.baeldung.com/java-email-validation-regex
|
// https://www.baeldung.com/java-email-validation-regex
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import icu.samnyan.aqua.sega.general.dao.CardRepository
|
|||||||
import icu.samnyan.aqua.sega.general.model.Card
|
import icu.samnyan.aqua.sega.general.model.Card
|
||||||
import icu.samnyan.aqua.sega.general.service.CardService
|
import icu.samnyan.aqua.sega.general.service.CardService
|
||||||
import jakarta.servlet.http.HttpServletRequest
|
import jakarta.servlet.http.HttpServletRequest
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder
|
import org.springframework.security.crypto.password.PasswordEncoder
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
@@ -35,6 +36,8 @@ class UserRegistrar(
|
|||||||
// This is because games can only take uint32 for card ID, which is at max 10 digits (4294967295)
|
// This is because games can only take uint32 for card ID, which is at max 10 digits (4294967295)
|
||||||
const val cardExtIdStart = 1e9.toLong()
|
const val cardExtIdStart = 1e9.toLong()
|
||||||
const val cardExtIdEnd = 4294967295
|
const val cardExtIdEnd = 4294967295
|
||||||
|
|
||||||
|
val log = LoggerFactory.getLogger(UserRegistrar::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
@API("/register")
|
@API("/register")
|
||||||
@@ -43,8 +46,8 @@ class UserRegistrar(
|
|||||||
@RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str,
|
@RP username: Str, @RP email: Str, @RP password: Str, @RP turnstile: Str,
|
||||||
request: HttpServletRequest
|
request: HttpServletRequest
|
||||||
): Any {
|
): Any {
|
||||||
|
|
||||||
val ip = geoIP.getIP(request)
|
val ip = geoIP.getIP(request)
|
||||||
|
log.info("Net: /user/register from $ip : $username")
|
||||||
|
|
||||||
// Check captcha
|
// Check captcha
|
||||||
if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha"
|
if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha"
|
||||||
@@ -89,9 +92,9 @@ class UserRegistrar(
|
|||||||
@RP email: Str, @RP password: Str, @RP turnstile: Str,
|
@RP email: Str, @RP password: Str, @RP turnstile: Str,
|
||||||
request: HttpServletRequest
|
request: HttpServletRequest
|
||||||
): Any {
|
): Any {
|
||||||
|
|
||||||
// Check captcha
|
// Check captcha
|
||||||
val ip = geoIP.getIP(request)
|
val ip = geoIP.getIP(request)
|
||||||
|
log.info("Net: /user/login from $ip : $email")
|
||||||
if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha"
|
if (!turnstileService.validate(turnstile, ip)) 400 - "Invalid captcha"
|
||||||
|
|
||||||
// Treat email as email / username
|
// Treat email as email / username
|
||||||
@@ -124,6 +127,7 @@ class UserRegistrar(
|
|||||||
|
|
||||||
// Set last login time
|
// Set last login time
|
||||||
async { userRepo.save(user.apply { lastLogin = millis() }) }
|
async { userRepo.save(user.apply { lastLogin = millis() }) }
|
||||||
|
log.info("> Login success: ${user.username} ${user.auId}")
|
||||||
|
|
||||||
return mapOf("token" to token)
|
return mapOf("token" to token)
|
||||||
}
|
}
|
||||||
@@ -131,6 +135,8 @@ class UserRegistrar(
|
|||||||
@API("/confirm-email")
|
@API("/confirm-email")
|
||||||
@Doc("Confirm email address with a token sent through email to the user.", "Success message")
|
@Doc("Confirm email address with a token sent through email to the user.", "Success message")
|
||||||
suspend fun confirmEmail(@RP token: Str): Any {
|
suspend fun confirmEmail(@RP token: Str): Any {
|
||||||
|
log.info("Net: /user/confirm-email with token $token")
|
||||||
|
|
||||||
// Find the confirmation
|
// Find the confirmation
|
||||||
val confirmation = async { confirmationRepo.findByToken(token) }
|
val confirmation = async { confirmationRepo.findByToken(token) }
|
||||||
|
|
||||||
@@ -173,6 +179,7 @@ class UserRegistrar(
|
|||||||
@Doc("Get a Keychip ID so that the user can connect to the server.", "Success message")
|
@Doc("Get a Keychip ID so that the user can connect to the server.", "Success message")
|
||||||
suspend fun setupConnection(@RP token: Str) = jwt.auth(token) { u ->
|
suspend fun setupConnection(@RP token: Str) = jwt.auth(token) { u ->
|
||||||
if (u.keychip != null) return mapOf("keychip" to u.keychip)
|
if (u.keychip != null) return mapOf("keychip" to u.keychip)
|
||||||
|
log.info("Net: /user/keychip setup: ${u.auId} for ${u.username}")
|
||||||
|
|
||||||
// Generate a keychip id with 10 digits (e.g. A1234567890)
|
// Generate a keychip id with 10 digits (e.g. A1234567890)
|
||||||
var new = "A" + keychipRange.random()
|
var new = "A" + keychipRange.random()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package icu.samnyan.aqua.net.utils
|
package icu.samnyan.aqua.net.utils
|
||||||
|
|
||||||
import ext.Str
|
import ext.Str
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice
|
import org.springframework.web.bind.annotation.ControllerAdvice
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||||
@@ -8,7 +9,11 @@ import org.springframework.web.bind.annotation.ExceptionHandler
|
|||||||
|
|
||||||
val SUCCESS = ResponseEntity.ok().body(mapOf("status" to "ok"))
|
val SUCCESS = ResponseEntity.ok().body(mapOf("status" to "ok"))
|
||||||
|
|
||||||
class ApiException(val code: Int, message: Str) : RuntimeException(message)
|
class ApiException(val code: Int, message: Str) : RuntimeException(message) {
|
||||||
|
companion object {
|
||||||
|
val log = LoggerFactory.getLogger(ApiException::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
class GlobalExceptionHandler {
|
class GlobalExceptionHandler {
|
||||||
|
|||||||
Reference in New Issue
Block a user