Enable detailed debug logging for mod identity validation

This commit refactors the mod identity extraction and validation process to include detailed debug logs, which are conditionally logged based on the `athShield.debug` flag. It also updates the import statements and correctly references the `ConfigManager.getNameDataPath` function for error messages.
This commit is contained in:
Sandro642 2024-10-26 16:46:05 +02:00
parent f366b8b86a
commit 50ad0e8a91
2 changed files with 71 additions and 72 deletions

View File

@ -9,9 +9,13 @@
* @Added support for the new HeliosLauncher version * @Added support for the new HeliosLauncher version
*/ */
import fs from 'fs'
import crypto from 'crypto'
import {DistributionIndexProcessor, FullRepair, MojangIndexProcessor} from 'helios-core/dl'
/** /**
* @Reviewed on XX.XX.2024 expires on 01.01.2025 * @Reviewed on XX.XX.2024 expires on 01.01.2025
* @Bugs discovered: 0 * @Bugs discovereds: 0
* @Athena's Shield * @Athena's Shield
* @Sandro642 * @Sandro642
*/ */
@ -85,32 +89,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
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', { loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', {
'moduleName': mdl.rawModule.name, 'moduleName': mdl.rawModule.name,
'moduleIdentity': modIdentity '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) => {
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath})) loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath}))
const zip = new AdmZip(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
@ -118,10 +112,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) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.identityNotFoundUsingHash', {
'filePath': filePath, 'filePath': filePath,
'hash': hash 'hash': hash
})) }))
}
return hash return hash
} }
@ -145,18 +142,23 @@ async function dlAsync(login = true) {
if (expectedIdentity) { if (expectedIdentity) {
const modIdentity = extractModIdentity(modPath) const modIdentity = extractModIdentity(modPath)
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', { loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', {
'expectedIdentity': expectedIdentity, 'expectedIdentity': expectedIdentity,
'mod': mod, 'mod': mod,
'modIdentity': modIdentity 'modIdentity': modIdentity
})) }))
}
if (modIdentity !== expectedIdentity) { if (modIdentity !== expectedIdentity) {
if (athShield.debug) {
loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', { loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', {
'mod': mod, 'mod': mod,
'expectedIdentity': expectedIdentity, 'expectedIdentity': expectedIdentity,
'modIdentity': modIdentity 'modIdentity': modIdentity
})) }))
}
valid = false valid = false
break break
} }
@ -173,7 +175,7 @@ async function dlAsync(login = true) {
// Perform mod validation before proceeding // Perform mod validation before proceeding
if (!validateMods()) { if (!validateMods()) {
const errorMessage = Lang.queryJS('landing.dlAsync.AthShield.invalidModsDetectedMessage', {'folder': dataPath}) const errorMessage = Lang.queryJS('landing.dlAsync.AthShield.invalidModsDetectedMessage', {'folder': ConfigManager.getNameDataPath()})
loggerLanding.error(errorMessage) loggerLanding.error(errorMessage)
showLaunchFailure(errorMessage, null) showLaunchFailure(errorMessage, null)
return return

View File

@ -30,8 +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 dataPath = require('./assets/js/configmanager') const crypto = require('crypto')
const athShield = require('./assets/athshield/parserAthShield')
const fs = require('fs') const fs = require('fs')
// Launch Elements // Launch Elements
@ -453,7 +452,7 @@ async function downloadJava(effectiveJavaOptions, launchAfter = true) {
/** /**
* @Reviewed on XX.XX.2024 expires on 01.01.2025 * @Reviewed on XX.XX.2024 expires on 01.01.2025
* @Bugs discovered: 0 * @Bugs discovereds: 0
* @Athena's Shield * @Athena's Shield
* @Sandro642 * @Sandro642
*/ */
@ -527,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
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', { loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.distributionIdentityError', {
'moduleName': mdl.rawModule.name, 'moduleName': mdl.rawModule.name,
'moduleIdentity': modIdentity '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) => {
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath})) loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.modIdentityExtraction', {'filePath': filePath}))
const zip = new AdmZip(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
@ -560,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) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.identityNotFoundUsingHash', {
'filePath': filePath, 'filePath': filePath,
'hash': hash 'hash': hash
})) }))
}
return hash return hash
} }
@ -587,18 +579,23 @@ async function dlAsync(login = true) {
if (expectedIdentity) { if (expectedIdentity) {
const modIdentity = extractModIdentity(modPath) const modIdentity = extractModIdentity(modPath)
if (athShield.debug) {
loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', { loggerLanding.info(Lang.queryJS('landing.dlAsync.AthShield.expectedAndCalculatedIdentity', {
'expectedIdentity': expectedIdentity, 'expectedIdentity': expectedIdentity,
'mod': mod, 'mod': mod,
'modIdentity': modIdentity 'modIdentity': modIdentity
})) }))
}
if (modIdentity !== expectedIdentity) { if (modIdentity !== expectedIdentity) {
if (athShield.debug) {
loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', { loggerLanding.error(Lang.queryJS('landing.dlAsync.AthShield.modIdentityMismatchError', {
'mod': mod, 'mod': mod,
'expectedIdentity': expectedIdentity, 'expectedIdentity': expectedIdentity,
'modIdentity': modIdentity 'modIdentity': modIdentity
})) }))
}
valid = false valid = false
break break
} }
@ -615,7 +612,7 @@ async function dlAsync(login = true) {
// Perform mod validation before proceeding // Perform mod validation before proceeding
if (!validateMods()) { if (!validateMods()) {
const errorMessage = Lang.queryJS('landing.dlAsync.AthShield.invalidModsDetectedMessage', {'folder': dataPath}) const errorMessage = Lang.queryJS('landing.dlAsync.AthShield.invalidModsDetectedMessage', {'folder': ConfigManager.getNameDataPath()})
loggerLanding.error(errorMessage) loggerLanding.error(errorMessage)
showLaunchFailure(errorMessage, null) showLaunchFailure(errorMessage, null)
return return