POGOserver/src/api.js
Felix dbde45fe73 Update
- Global shared print method
- API hearbeat and reconnecting feature
- Serialize avatar
- Basic player authentication
- New packet structure for classes
- Safer url routing
2016-08-27 21:07:40 +02:00

145 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from "fs";
import url from "url";
import prompt from "prompt";
import print from "./print";
import CFG from "../cfg";
prompt.start({
message: " ",
delimiter: " "
});
export function processApiCall(req, res, route) {
let save = JSON.parse(fs.readFileSync(".save", "utf8"));
let allowedHosts = save.allowedApiHosts;
let hoster = url.parse(req.headers.referer).host;
if (!(allowedHosts.indexOf(hoster) > -1)) {
this.grantApiAccess(req, res, route);
return void 0;
}
let raw = req.body.toString();
let json = null;
try {
json = JSON.parse(raw);
} catch (e) {
print(e, 31);
this.answerApiCall(res, "");
return void 0;
}
if (this.isApiCall(json)) {
json.host = hoster;
if (json.action === "login") {
let result = this["api_login"](json);
this.answerApiCall(res, JSON.stringify(result));
}
else {
if (this.apiClients[hoster]) {
let result = this["api_" + json.action](json);
this.answerApiCall(res, JSON.stringify(result));
}
else {
print(`${hoster} isnt logged in! Kicking..`, 31);
}
}
}
else {
if (json.action === "init") {
this.answerApiCall(res, JSON.stringify({ success: true }));
}
}
}
export function grantApiAccess(req, res, route) {
let save = JSON.parse(fs.readFileSync(".save", "utf8"));
let hoster = url.parse(req.headers.referer).host;
let msg = `[Console] \x1b[33mGrant API access to ${hoster}?\x1b[0m`;
prompt.get([{ name: "grant", required: true, description: msg }], (e, result) => {
if (result.grant === "y" || result.grant === "yes") {
save.allowedApiHosts.push(hoster);
fs.writeFileSync(".save", JSON.stringify(save), "utf8");
print(`Successfully added ${hoster} to allowed API hosts!`);
this.processApiCall(req, res, route);
}
else {
print(`Denied API access for ${hoster}`, 31);
this.answerApiCall(res, "");
}
});
}
export function answerApiCall(res, data) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
res.setHeader("Access-Control-Allow-Credentials", true);
res.end(data);
}
export function api_login(data) {
if (typeof data !== "object") return void 0;
let save = JSON.parse(fs.readFileSync(".save", "utf8"));
let success = false;
let username = save.loginDetails.username;
let password = save.loginDetails.password;
if (
username === data.username &&
password === data.password
) {
success = true;
if (!this.apiClients[data.host]) {
print(`API access for ${data.host} granted!`);
}
print(`${data.host} logged in!`, 36);
this.apiClients[data.host] = {
timestamp: +new Date()
};
}
return ({
success: success
});
}
export function api_heartBeat() {
return ({
timestamp: +new Date()
});
}
export function api_getConnectedPlayers() {
return ({
connected_players: this.world.connectedPlayers
});
}
export function api_getServerVersion() {
return ({
version: CFG.VERSION
});
}
export function api_spawnPkmnToPlayer(data) {
let name = String(data.player);
let pkmn = String(data.pkmn).toUpperCase();
print(`Spawned 1x ${pkmn}'s to ${name}!`);
return ({
success: true
});
}