Added a composite primary key (playerUuid, assetHash) to the player_capes table for better data integrity. Updated userRepository.js to use English messages for cape assignment and removal operations.
80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
const crypto = require("node:crypto")
|
|
const logger = require("../modules/logger")
|
|
const { DefaultError } = require("../errors/errors")
|
|
const certificatesManager = require("./certificatesManager")
|
|
|
|
async function getRegistrationCountryFromIp(ipAddress) {
|
|
const apiUrl = `https://ip-api.com/json/${ipAddress}?fields=countryCode`
|
|
|
|
try {
|
|
const response = await fetch(apiUrl)
|
|
if (!response.ok) {
|
|
return "FR"
|
|
}
|
|
|
|
const data = await response.json()
|
|
if (data && data.countryCode) {
|
|
const countryCode = data.countryCode
|
|
return countryCode
|
|
} else {
|
|
return "FR"
|
|
}
|
|
|
|
} catch (error) {
|
|
return "??"
|
|
}
|
|
}
|
|
|
|
function signProfileData(dataBase64) {
|
|
const serverKeys = certificatesManager.getKeys()
|
|
try {
|
|
const privateKey = serverKeys.profilePropertyKeys.private
|
|
const signer = crypto.createSign("SHA1")
|
|
signer.update(dataBase64)
|
|
signer.end()
|
|
return signer.sign(privateKey, "base64")
|
|
} catch (err) {
|
|
console.error("Signing failed:", err)
|
|
return null
|
|
}
|
|
}
|
|
|
|
function addDashesToUUID(uuid) {
|
|
if (typeof uuid !== "string" || uuid.length !== 32) {
|
|
return uuid
|
|
}
|
|
|
|
return (
|
|
uuid.slice(0, 8) + "-" +
|
|
uuid.slice(8, 12) + "-" +
|
|
uuid.slice(12, 16) + "-" +
|
|
uuid.slice(16, 20) + "-" +
|
|
uuid.slice(20)
|
|
)
|
|
}
|
|
|
|
function isTrueFromDotEnv(key) {
|
|
return (process.env[key] || "").trim().toLowerCase() === "true"
|
|
}
|
|
|
|
function getUrlParam(url, param) {
|
|
const urlParams = new URLSearchParams(url)
|
|
return urlParams.get(param)
|
|
}
|
|
|
|
function handleDBError(error, errorMessage = "Internal Server Error", code = 500) {
|
|
if (error instanceof DefaultError) {
|
|
throw error
|
|
}
|
|
logger.log(errorMessage.bold + " : " + error.toString(), ["MariaDB", "yellow"])
|
|
throw new DefaultError(code, errorMessage, "InternalServerError")
|
|
}
|
|
|
|
module.exports = {
|
|
getUrlParam,
|
|
handleDBError,
|
|
signProfileData,
|
|
addDashesToUUID,
|
|
isTrueFromDotEnv,
|
|
getRegistrationCountryFromIp,
|
|
} |