Enable debug logging conditionally for AthShield

Wrapped several logging statements related to module identity extraction and validation with a conditional check on the athShield.debug flag. This ensures that detailed logging information is recorded only when debugging is enabled, optimizing performance and log clarity.
This commit is contained in:
Sandro642 2024-10-26 16:27:25 +02:00
parent 3cdad516c9
commit 485facf1f1

View File

@ -30,6 +30,7 @@ const {
// Internal Requirements // Internal Requirements
const DiscordWrapper = require('./assets/js/discordwrapper') const DiscordWrapper = require('./assets/js/discordwrapper')
const ProcessBuilder = require('./assets/js/processbuilder') const ProcessBuilder = require('./assets/js/processbuilder')
const crypto = require('crypto')
const fs = require('fs') const fs = require('fs')
// Launch Elements // Launch Elements
@ -525,32 +526,22 @@ async function dlAsync(login = true) {
mdls.forEach(mdl => { mdls.forEach(mdl => {
if (mdl.rawModule.name.endsWith('.jar')) { if (mdl.rawModule.name.endsWith('.jar')) {
const modPath = path.join(modsDir, mdl.rawModule.name) const modPath = path.join(modsDir, mdl.rawModule.name)
const modIdentity = mdl.rawModule.identity || mdl.rawModule.MD5 const modIdentity = mdl.rawModule.identity || mdl.rawModule.artifact.MD5
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', { if (athShield.debug) {
'moduleName': mdl.rawModule.name, loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', {
'moduleIdentity': modIdentity 'moduleName': mdl.rawModule.name,
})) 'moduleIdentity': modIdentity
}))
}
distroMods[modPath] = modIdentity distroMods[modPath] = modIdentity
} }
}) })
// Function to extract mod identity from the jar file // Function to extract mod identity from the jar file
const extractModIdentity = (filePath) => { const extractModIdentity = (filePath) => {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath})) if (athShield.debug) {
const zip = new AdmZip(filePath) loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath}))
const manifestEntry = zip.getEntry('META-INF/MANIFEST.MF')
if (manifestEntry) {
const manifestContent = manifestEntry.getData().toString('utf8')
const lines = manifestContent.split('\n')
const identityLine = lines.find(line => line.startsWith('Mod-Id:') || line.startsWith('Implementation-Title:'))
if (identityLine) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.manifestIdentityFound', {
'filePath': filePath,
'identityLine': identityLine
}))
return identityLine.split(':')[1].trim()
}
} }
// Fall back to a hash if no identity is found // Fall back to a hash if no identity is found
@ -558,10 +549,13 @@ async function dlAsync(login = true) {
const hashSum = crypto.createHash('md5') // Use MD5 to match the distribution configuration const hashSum = crypto.createHash('md5') // Use MD5 to match the distribution configuration
hashSum.update(fileBuffer) hashSum.update(fileBuffer)
const hash = hashSum.digest('hex') const hash = hashSum.digest('hex')
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.identityNotFoundInManifest', { if (athShield.debug) {
'filePath': filePath, loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.identityNotFoundUsingHash', {
'hash': hash 'filePath': filePath,
})) 'hash': hash
}))
}
return hash return hash
} }
@ -585,18 +579,23 @@ async function dlAsync(login = true) {
if (expectedIdentity) { if (expectedIdentity) {
const modIdentity = extractModIdentity(modPath) const modIdentity = extractModIdentity(modPath)
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', { if (athShield.debug) {
'expectedIdentity': expectedIdentity, loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', {
'mod': mod,
'modIdentity': modIdentity
}))
if (modIdentity !== expectedIdentity) {
loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', {
'mod': mod,
'expectedIdentity': expectedIdentity, 'expectedIdentity': expectedIdentity,
'mod': mod,
'modIdentity': modIdentity 'modIdentity': modIdentity
})) }))
}
if (modIdentity !== expectedIdentity) {
if (athShield.debug) {
loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', {
'mod': mod,
'expectedIdentity': expectedIdentity,
'modIdentity': modIdentity
}))
}
valid = false valid = false
break break
} }