website/src/util.js
2021-12-12 20:48:20 -05:00

78 lines
1.8 KiB
JavaScript

const fs = require('fs-extra');
const got = require('got');
const crypto = require('crypto');
const logger = require('./logger');
function fullUrl(request) {
return `${request.protocol}://${request.hostname}${request.originalUrl}`;
}
function getLocale(region, language) {
const path = `${__dirname}/../locales/${region}_${language}.json`;
if (fs.pathExistsSync(path)) {
return require(path);
}
logger.warn(`Could not find locale ${region}_${language}! Loading US_en`);
return require(`${__dirname}/../locales/US_en.json`);
}
function apiGetRequest(path, headers) {
return got.get(`https://api.pretendo.cc${path}`, {
responseType: 'json',
throwHttpErrors: false,
https: {
rejectUnauthorized: false, // Needed for self-signed certificates on localhost testing
},
headers
});
}
function apiPostGetRequest(path, headers, json) {
return got.post(`https://api.pretendo.cc${path}`, {
responseType: 'json',
throwHttpErrors: false,
https: {
rejectUnauthorized: false, // Needed for self-signed certificates on localhost testing
},
headers,
json
});
}
function apiDeleteGetRequest(path, headers, json) {
return got.delete(`https://api.pretendo.cc${path}`, {
throwHttpErrors: false,
https: {
rejectUnauthorized: false, // Needed for self-signed certificates on localhost testing
},
headers,
json
});
}
function nintendoPasswordHash(password, pid) {
const pidBuffer = Buffer.alloc(4);
pidBuffer.writeUInt32LE(pid);
const unpacked = Buffer.concat([
pidBuffer,
Buffer.from('\x02\x65\x43\x46'),
Buffer.from(password)
]);
const hashed = crypto.createHash('sha256').update(unpacked).digest().toString('hex');
return hashed;
}
module.exports = {
fullUrl,
getLocale,
apiGetRequest,
apiPostGetRequest,
apiDeleteGetRequest,
nintendoPasswordHash
};