initial mod pdef stuff and project structure improvements

This commit is contained in:
BobTheBob 2021-11-27 00:37:34 +00:00
parent f65a2e8d07
commit fda61da56f
12 changed files with 4689 additions and 46 deletions

121
.gitignore vendored Normal file
View File

@ -0,0 +1,121 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
.env.production
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# db files
.db

View File

@ -4,6 +4,8 @@ const { GameServer, GetGameServers } = require( path.join( __dirname, "../shared
const accounts = require( path.join( __dirname, "../shared/accounts.js" ) )
const asyncHttp = require( path.join( __dirname, "../shared/asynchttp.js" ) )
let shouldRequireSessionToken = !process.argv.includes( "-nosessiontoken" )
module.exports = ( fastify, opts, done ) => {
// exported routes
@ -20,24 +22,28 @@ module.exports = ( fastify, opts, done ) => {
}
},
async ( request, reply ) => {
let authResponse = await asyncHttp.request( {
method: "GET",
host: "https://r2-pc.stryder.respawn.com",
port: 443,
path: `/nucleus-oauth.php?qt=origin-requesttoken&type=server_token&code=${ request.query.token }&forceTrial=0&proto=0&json=1&&env=production&userId=${ parseInt( request.query.id ).toString(16).toUpperCase() }`
} )
let authJson
try {
authJson = JSON.parse( authResponse.toString() )
} catch (error) {
return { success: false }
// only do this if we're in an environment that actually requires session tokens
if ( shouldRequireSessionToken )
{
let authResponse = await asyncHttp.request( {
method: "GET",
host: "https://r2-pc.stryder.respawn.com",
port: 443,
path: `/nucleus-oauth.php?qt=origin-requesttoken&type=server_token&code=${ request.query.token }&forceTrial=0&proto=0&json=1&&env=production&userId=${ parseInt( request.query.id ).toString(16).toUpperCase() }`
} )
let authJson
try {
authJson = JSON.parse( authResponse.toString() )
} catch (error) {
return { success: false }
}
// check origin auth was fine
// unsure if we can check the exact value of storeUri? doing an includes check just in case
if ( !authResponse.length || !authJson.hasOnlineAccess || !authJson.storeUri.includes( "titanfall-2" ) )
return { success: false }
}
// check origin auth was fine
// unsure if we can check the exact value of storeUri? doing an includes check just in case
if ( !authResponse.length || !authJson.hasOnlineAccess || !authJson.storeUri.includes( "titanfall-2" ) )
return { success: false }
let account = await accounts.AsyncGetPlayerByID( request.query.id )
if ( !account ) // create account for user
@ -54,6 +60,7 @@ module.exports = ( fastify, opts, done ) => {
// POST /client/auth_with_server
// attempts to authenticate a client with a gameserver, so they can connect
// authentication includes giving them a 1-time token to join the gameserver, as well as sending their persistent data to the gameserver
fastify.post( '/client/auth_with_server',
{
schema: {
@ -75,23 +82,29 @@ module.exports = ( fastify, opts, done ) => {
if ( !account )
return { success: false }
// check token
if ( request.query.playerToken != account.currentAuthToken )
return { success: false }
if ( shouldRequireSessionToken )
{
// check token
if ( request.query.playerToken != account.currentAuthToken )
return { success: false }
// check expired token
if ( account.currentAuthTokenExpirationTime < Date.now() )
return { success: false }
// check expired token
if ( account.currentAuthTokenExpirationTime < Date.now() )
return { success: false }
}
// fix this: game doesnt seem to set serverFilter right if it's >31 chars long, so restrict it to 31
let authToken = crypto.randomBytes( 16 ).toString( "hex" ).substr( 0, 31 )
// todo: build persistent data here, rather than sending baseline only
let pdata = accounts.AsyncGetPlayerPersistenceBufferForMods( request.query.id, server.modInfo.Mods.filter( m => !!m.pdiff ).map( m => m.pdiff ) )
let authResponse = await asyncHttp.request( {
method: "POST",
host: server.ip,
port: server.authPort,
path: `/authenticate_incoming_player?id=${request.query.id}&authToken=${authToken}`
}, account.persistentDataBaseline )
}, pdata )
if ( !authResponse )
return { success: false }
@ -126,13 +139,16 @@ module.exports = ( fastify, opts, done ) => {
if ( !account )
return { success: false }
// check token
if ( request.query.playerToken != account.currentAuthToken )
return { success: false }
if ( shouldRequireSessionToken )
{
// check token
if ( request.query.playerToken != account.currentAuthToken )
return { success: false }
// check expired token
if ( account.currentAuthTokenExpirationTime < Date.now() )
return { success: false }
// check expired token
if ( account.currentAuthTokenExpirationTime < Date.now() )
return { success: false }
}
// fix this: game doesnt seem to set serverFilter right if it's >31 chars long, so restrict it to 31
let authToken = crypto.randomBytes( 16 ).toString("hex").substr( 0, 31 )

View File

@ -1,4 +1,4 @@
const fastify = require( "fastify" )({ logger: true })
const fastify = require( "fastify" )({ logger: process.argv.includes( "-usefastifylogger" ) })
const fs = require( "fs" )
const path = require( "path" )
@ -23,7 +23,7 @@ async function start() {
}
catch ( ex )
{
fastify.log.error( ex )
console.error( ex )
process.exit( 1 )
}
}

2924
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

15
package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "NorthstarMasterServer",
"author": "BobTheBob#1150",
"license": "MIT",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node index.js -nosessiontoken -usefastifylogger"
},
"dependencies": {
"fastify": "^3.24.0",
"fastify-multipart": "^5.1.0",
"sqlite3": "^5.0.2"
}
}

15
package.json.bak Normal file
View File

@ -0,0 +1,15 @@
{
"name": "NorthstarMasterServer",
"author": "BobTheBob#1150",
"license": "MIT",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node index.js -nosessiontoken -uselogger"
},
"dependencies": {
"fastify": "^3.24.0",
"fastify-multipart": "^5.1.0",
"sqlite3": "^5.0.2"
}
}

File diff suppressed because it is too large Load Diff

BIN
playerdata.db Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
const path = require( "path" )
const crypto = require( "crypto" )
const { GameServer, GetGameServers, AddGameServer, RemoveGameServer } = require( path.join( __dirname, "../shared/gameserver.js" ) )
const asyncHttp = require( path.join( __dirname, "../shared/asynchttp.js" ) )
const pjson = require( path.join( __dirname, "../shared/pjson.js" ) )
@ -55,15 +56,17 @@ module.exports = ( fastify, opts, done ) => {
return { success: false }
// pdiff stuff
if ( modInfo )
if ( modInfo && modInfo.Mods )
{
for ( let mod of modInfo )
for ( let mod of modInfo.Mods )
{
if ( !!mod.pdiff )
{
try
{
let pdiffHash = crypto.createHash( "sha1" ).update( mod.pdiff ).digest( "hex" )
mod.pdiff = pjson.ParseDefinitionDiffs( mod.pdiff )
mod.pdiff.hash = pdiffHash
}
catch ( ex )
{

View File

@ -1,10 +1,13 @@
const sqlite = require( "sqlite3" ).verbose()
const path = require( "path" )
const fs = require( "fs" )
const pjson = require( path.join( __dirname, "../shared/pjson.js" ) )
const TOKEN_EXPIRATION_TIME = 3600 * 24 // 24 hours
const DEFAULT_PDATA_BASELINE = fs.readFileSync( "pdata/default.pdata" )
const DEFAULT_PDATA_BASELINE = fs.readFileSync( "default.pdata" )
const DEFAULT_PDEF_OBJECT = pjson.ParseDefinition( fs.readFileSync( "persistent_player_data_version_231.pdef" ).toString() )
let playerDB = new sqlite.Database( 'db/temp_pdata.db', sqlite.OPEN_CREATE | sqlite.OPEN_READWRITE, ex => {
let playerDB = new sqlite.Database( 'playerdata.db', sqlite.OPEN_CREATE | sqlite.OPEN_READWRITE, ex => {
if ( ex )
console.error( ex )
else
@ -27,12 +30,12 @@ let playerDB = new sqlite.Database( 'db/temp_pdata.db', sqlite.OPEN_CREATE | sql
console.log( "Created player account table successfully" )
})
// create custom persistent data table
// create mod persistent data table
// this should mirror the PlayerAccount class's properties
playerDB.run( `
CREATE TABLE IF NOT EXISTS customPersistentData (
CREATE TABLE IF NOT EXISTS modPeristentData (
id TEXT NOT NULL,
pdiffHash INTEGER NOT NULL,
pdiffHash TEXT NOT NULL,
data TEXT NOT NULL,
PRIMARY KEY ( id, pdiffHash )
)
@ -40,7 +43,7 @@ let playerDB = new sqlite.Database( 'db/temp_pdata.db', sqlite.OPEN_CREATE | sql
if ( ex )
console.error( ex )
else
console.log( "Created custom persistent data table successfully" )
console.log( "Created mod persistent data table successfully" )
})
})
@ -95,7 +98,7 @@ class PlayerAccount
}
module.exports = {
AsyncGetPlayerByID: async function ( id ) {
AsyncGetPlayerByID: async function AsyncGetPlayerByID( id ) {
let row = await asyncDBGet( "SELECT * FROM accounts WHERE id = ?", [ id ] )
if ( !row )
@ -104,19 +107,50 @@ module.exports = {
return new PlayerAccount( row.id, row.currentAuthToken, row.currentAuthTokenExpirationTime, row.currentServerId, row.persistentDataBaseline )
},
AsyncCreateAccountForID: async function ( id ) {
AsyncCreateAccountForID: async function AsyncCreateAccountForID( id ) {
await asyncDBRun( "INSERT INTO accounts ( id, persistentDataBaseline ) VALUES ( ?, ? )", [ id, DEFAULT_PDATA_BASELINE ] )
},
AsyncUpdateCurrentPlayerAuthToken: async function( id, token ) {
AsyncUpdateCurrentPlayerAuthToken: async function AsyncUpdateCurrentPlayerAuthToken( id, token ) {
await asyncDBRun( "UPDATE accounts SET currentAuthToken = ?, currentAuthTokenExpirationTime = ? WHERE id = ?", [ token, Date.now() + TOKEN_EXPIRATION_TIME, id ] )
},
AsyncUpdatePlayerCurrentServer: async function( id, serverId ) {
AsyncUpdatePlayerCurrentServer: async function AsyncUpdatePlayerCurrentServer( id, serverId ) {
await asyncDBRun( "UPDATE accounts SET currentServerId = ? WHERE id = ?", [ serverId, id ] )
},
AsyncWritePlayerPersistenceBaseline: async function ( id, persistentDataBaseline ) {
AsyncWritePlayerPersistenceBaseline: async function AsyncWritePlayerPersistenceBaseline( id, persistentDataBaseline ) {
await asyncDBRun( "UPDATE accounts SET persistentDataBaseline = ? WHERE id = ?", [ persistentDataBaseline, id ] )
},
AsyncGetPlayerModPersistence: async function AsyncGetPlayerModPersistence( id, pdiffHash ) {
return JSON.parse( await asyncDBGet( "SELECT data from modPersistentData WHERE id = ? AND pdiffHash = ?", [ id, pdiffHash ] ) )
},
AsyncWritePlayerModPersistence: async function AsyncWritePlayerModPersistence( id, pdiffHash, data ) {
},
AsyncGetPlayerPersistenceBufferForMods: async function( id, pdiffs ) {
let player = AsyncGetPlayerByID( id )
let pdefCopy = DEFAULT_PDEF_OBJECT
let baselineJson = pjson.PdataToJson( player.persistentDataBaseline, DEFAULT_PDEF_OBJECT )
let newPdataJson = baselineJson
if ( !player )
return null
for ( let pdiff of pdiffs )
{
for ( let enumAdd in pdiff.enums )
pdefCopy.enums[ enumAdd ] = [ ...pdefCopy.enums[ enumAdd ], ...pdiff.enums[ enumAdd ] ]
pdefCopy = Object.assign( pdefCopy, pdiff.pdef )
// this assign call won't work, but basically what it SHOULD do is replace any pdata keys that are in the mod pdata and append new ones to the end
newPdataJson = Object.assign( newPdataJson, AsyncGetPlayerModPersistence( id, pdiff.hash ) )
}
return PdataJsonToBuffer( newPdataJson, pdefCopy )
}
}

View File

@ -61,7 +61,7 @@ class GameServer
// restrict modinfo keys
this.modInfo = { Mods:[] }
for ( let mod of modInfo.Mods )
this.modInfo.Mods.push( { Name: mod.Name || "", Version: mod.Version || "0.0.0" } )
this.modInfo.Mods.push( { Name: mod.Name || "", Version: mod.Version || "0.0.0", Pdiff: mod.Pdiff || null } )
}
}