Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.
12 changes: 12 additions & 0 deletions client/resource/verified_mods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Mod Settings":
{
"DependencyPrefix": "EladNLG-ModSettings",
"Versions": [ "1.0.0", "1.1.0" ]
},
"Moblin.Archon":
{
"DependencyPrefix": "GalacticMoblin-MoblinArchon",
"Versions": [ "1.3.0", "1.3.1" ]
}
}
86 changes: 86 additions & 0 deletions client/verified_mods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const path = require( "path" )
const fs = require( "fs" )

// Checks if a mod entry is an object, has "DependencyPrefix" and "Versions" properties,
// and also checks if both fields have correct format.
function checkModEntry ( modContent )
{
if ( typeof modContent !== "object" || Array.isArray( modContent ) || modContent === null )
return false
if ( !Object.prototype.hasOwnProperty.call( modContent, "DependencyPrefix" ) || !Object.prototype.hasOwnProperty.call( modContent, "Versions" ) )
return false

// Checking TeamName-ModName format.
const dependencyPrefix = modContent["DependencyPrefix"]
if ( !/^[a-zA-Z0-9_]+-[a-zA-Z0-9_]+$/.test( dependencyPrefix ) )
return false

// Checking x.y.z versions format.
const versions = modContent["Versions"]
for ( const version of versions )
{
if ( Object.prototype.toString.call( version ) !== "[object String]" || !/[0-9]+\.[0-9]+\.[0-9]$/.test( version ) )
return false
}

return true
}

// Remove mod entries that do not match formatting convention from exposed mods list.
function checkAllMods()
{
const mods = Object.keys( verifiedMods )

for ( let i=0; i<mods.length; i++ )
{
const mod = mods[i]
if ( !checkModEntry( verifiedMods[mod] ) )
{
console.warn( `Since "${mod}" does not have correct format, it was removed from verified mods list.` )
delete verifiedMods[mod]
}
}
}

const { getRatelimit } = require( "../shared/ratelimit.js" )
const verifiedModsPath = path.join( __dirname, "resource", "verified_mods.json" )

// watch the JSON file so we can update it without a masterserver restart
fs.watch( verifiedModsPath, () =>
{
try
{
verifiedMods = JSON.parse( fs.readFileSync( verifiedModsPath ).toString() )
checkAllMods()
console.log( "Updated verified mods list successfully!" )
}
catch ( ex )
{
console.log( `Encountered error updating verified mods list: ${ ex }` )
}
} )

let verifiedMods = {}
if ( fs.existsSync( verifiedModsPath ) )
{
verifiedMods = JSON.parse( fs.readFileSync( verifiedModsPath ).toString() )
checkAllMods()
}

module.exports = ( fastify, opts, done ) =>
{
// exported routes

// GET /client/verifiedmods
// returns a list of manually-verified mods
fastify.get( "/client/verifiedmods",
{
config: { rateLimit: getRatelimit( "REQ_PER_MINUTE__CLIENT_VERIFIEDMODS" ) }, // ratelimit
},
async ( ) =>
{
return verifiedMods
} )

done()
}
1 change: 1 addition & 0 deletions dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ REQ_PER_MINUTE__CLIENT_ORIGINAUTH=5
REQ_PER_MINUTE__CLIENT_AUTHWITHSERVER=10
REQ_PER_MINUTE__CLIENT_AUTHWITHSELF=25
REQ_PER_MINUTE__CLIENT_MAINMENUPROMOS=20
REQ_PER_MINUTE__CLIENT_VERIFIEDMODS=1
REQ_PER_MINUTE__CLIENT_SERVERS=100
REQ_PER_MINUTE__SERVER_ADDSERVER=5
REQ_PER_MINUTE__SERVER_HEARTBEAT=60
Expand Down