Browse Source

Initial code commit.

Updated README.md
master
atom0s 6 years ago
parent
commit
d62aef0511
  1. 33
      README.md
  2. 159
      ashita.functions.php
  3. 141
      ashita.php
  4. 95
      config.php
  5. 0
      index.html

33
README.md

@ -1,3 +1,34 @@ @@ -1,3 +1,34 @@
# CollectionApi
The basic php script used for private servers to submit information about their server to the Ashita backend services. (Used to display private server information within the Ashita launcher.)
The basic php script used for private servers to submit information about their server to the Ashita backend services. (Used to display private server information within the Ashita launcher.)
## Setup
This script is designed to be super light-weight and minimal on requirements and similar.
In order to use this script, you need to have:
* PHP (At least 5.1.x is needed, 7.x is recommended.)
* PHP PDO Enabled (Automatically enabled on newer versions of PHP)
To install, simply download, or clone, this respository into a new directory on your website/webserver.
Next, open and edit the config.php file to be adjusted for your private server information.
* $config['db'] - This setting array controls access to the database server to gain access to the 'accounts_sessions' table of your server.
* $config['cache'] - This setting array controls the various options for the caching. (These are used to help reduce the load on your server and limit how often the script queries your database for new information.)
* $config['server'] - this setting array controls the server specific information for your server. (The 'lifetime' option handles how often Ashita's service will attempt to re-query for your servers information limiting how often it accesses this script.)
## Testing
Lastly, to test you simply just need to browse to your website folder where you have installed this script, such as: `http://www.example.com/ashita/ashita.php`
If properly installed the site should return a json blob of data about your servers current information.
## Recommended MySQL Setup
This script ONLY does a single SELECT call to the database. Because of this small footprint, it is highly recommended to create a custom account specifically for this script and ONLY give it access to the SELECT command on the DarkStar database of your server. This helps limit any potential exploits with this script or any future changes that will come to it.
## Getting Listed Within Ashita Launcher
In order to get your server listed in the Ashita launcher, you will need to contact atom0s directly on Discord. You can find me within the Ashita general chat channel here: https://discord.gg/CfZu6Ya

159
ashita.functions.php

@ -0,0 +1,159 @@ @@ -0,0 +1,159 @@
<?php
/**
* Ashita Private Server Collection API - Copyright (c) 2018 atom0s [[email protected]]
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*
* By using this script, you agree to the above license and its terms.
*
* Attribution - You must give appropriate credit, provide a link to the license and indicate if changes were
* made. You must do so in any reasonable manner, but not in any way that suggests the licensor
* endorses you or your use.
*
* Non-Commercial - You may not use the material (this script) for commercial purposes.
*
* No-Derivatives - If you remix, transform, or build upon the material (this script), you may not distribute the
* modified material. You are, however, allowed to submit the modified works back to the original
* Ashita Private Server Collection API project in attempt to have it added to the original project.
*
* You may not apply legal terms or technological measures that legally restrict others
* from doing anything the license permits.
*
* You may contact me, atom0s, at [email protected] for more information or if you are seeking commercial use.
*
* No warranties are given.
*/
// Prevent hack attempts..
if (!defined('ASHITA_PSCAPI')) { exit; }
/**
* Generates a JSON response object.
*
* @param {number} $errorc - The error code of the response.
* @param {string} $errorm - The error message of the response.
* @return {$string} A JSON formatted object of the error information.
*/
function generateErrorResponse($errorc, $errorm)
{
$err = "{ ";
$err .= "\"status\": 1,";
$err .= "\"errorc\": " . $errorc;
$err .= "\"errorm\": " . $errorm;
$err .= "}";
return $err;
}
/**
* Checks if the given configuration object is valid.
*
* @param {object} $config - The configuration object to validate.
* @return {bool} True on success, false otherwise.
*/
function isValidConfiguration($config)
{
// Ensure the configuration arrays are valid and exist..
if (!is_array($config) ||
!array_key_exists('db', $config) || !is_array($config['db']) ||
!array_key_exists('cache', $config) || !is_array($config['cache']) ||
!array_key_exists('server', $config) || !is_array($config['server']))
{
return false;
}
// Validate the database configurations..
if (!array_key_exists('host', $config['db']) || !is_string($config['db']['host']) ||
!array_key_exists('port', $config['db']) || !is_string($config['db']['port']) ||
!array_key_exists('user', $config['db']) || !is_string($config['db']['user']) ||
!array_key_exists('pass', $config['db']) || !is_string($config['db']['pass']) ||
!array_key_exists('name', $config['db']) || !is_string($config['db']['name']))
{
return false;
}
// Validate the cache configurations..
if (!array_key_exists('lifetime', $config['cache']) || !is_numeric($config['cache']['lifetime']))
{
return false;
}
// Validate the server configurations..
if (!array_key_exists('lifetime', $config['server']) || !is_numeric($config['server']['lifetime']) ||
!array_key_exists('name', $config['server']) || !is_string($config['server']['name']) ||
!array_key_exists('levelcap', $config['server']) || !is_numeric($config['server']['levelcap']) ||
!array_key_exists('ip', $config['server']) || !is_string($config['server']['ip']) ||
!array_key_exists('website', $config['server']) || !is_string($config['server']['website']) ||
!array_key_exists('discord', $config['server']) || !is_string($config['server']['discord']))
{
return false;
}
return true;
}
/**
* Checks if the given cache data is valid.
*
* @param {object} $data - The cache object to validate.
* @return {bool} True on success, false otherwise.
*/
function isValidCachedFile($data)
{
// Ensure the incoming object is valid..
if (!$data || $data === null || !is_object($data)) { return false; }
// Check for the required data entries of the cached data..
if (!isset($data->status) || !is_numeric($data->status) || $data->status !== 0) { return false; }
if (!isset($data->version) || !is_string($data->version)) { return false; }
if (!isset($data->lifetime) || !is_numeric($data->lifetime)) { return false; }
if (!isset($data->timestamp) || !is_numeric($data->timestamp)) { return false; }
if (!isset($data->name) || !is_string($data->name)) { return false; }
if (!isset($data->levelcap) || !is_numeric($data->levelcap)) { return false; }
if (!isset($data->ip) || !is_string($data->ip)) { return false; }
if (!isset($data->website) || !is_string($data->website)) { return false; }
if (!isset($data->discord) || !is_string($data->discord)) { return false; }
if (!isset($data->total) || !is_numeric($data->total)) { return false; }
if (!isset($data->unique) || !is_numeric($data->unique)) { return false; }
return true;
}
/**
* Obtains the cached data from the given file.
*
* @param {string} $file - The file path to the cache data to obtain.
* @return {bool|string} False if invalid or not found, string of the cached data if found and valid.
*/
function getCachedData($file)
{
// Ensure the cache file exists..
if (!file_exists($file)) { return false; }
try
{
// Open the cache file for reading..
$f = fopen($file, "r");
if ($f === FALSE) { return false; }
// Read the cached file contents..
$cache = fread($f, filesize($file));
fclose($f);
// Parse the file data as json..
$data = json_decode($cache);
if (json_last_error() !== JSON_ERROR_NONE) { return false; }
// Validate the cache file..
if (!isValidCachedFile($data)) { return false; }
// Cache is valid, return it..
return $cache;
}
catch (Exception $e)
{
return false;
}
}

141
ashita.php

@ -0,0 +1,141 @@ @@ -0,0 +1,141 @@
<?php
/**
* Ashita Private Server Collection API - Copyright (c) 2018 atom0s [[email protected]]
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*
* By using this script, you agree to the above license and its terms.
*
* Attribution - You must give appropriate credit, provide a link to the license and indicate if changes were
* made. You must do so in any reasonable manner, but not in any way that suggests the licensor
* endorses you or your use.
*
* Non-Commercial - You may not use the material (this script) for commercial purposes.
*
* No-Derivatives - If you remix, transform, or build upon the material (this script), you may not distribute the
* modified material. You are, however, allowed to submit the modified works back to the original
* Ashita Private Server Collection API project in attempt to have it added to the original project.
*
* You may not apply legal terms or technological measures that legally restrict others
* from doing anything the license permits.
*
* You may contact me, atom0s, at [email protected] for more information or if you are seeking commercial use.
*
* No warranties are given.
*/
// Define the script globals..
define('ASHITA_PSCAPI', true);
define('ASHITA_PSCAPI_VERSION', '1');
// Prepare needed variables and includes..
$ashita_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($ashita_root_path . 'config.' . $phpEx);
require($ashita_root_path . 'ashita.functions.' . $phpEx);
$cacheFile = $ashita_root_path . 'cache.json';
/**
* Generates a timestamp used to determine the time the script was ran to compare
* against the current cache data. Offset is based on Los Angeles so that all servers
* cached information updates are on the same time schedule.
*/
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
$timestamp = $date->getTimestamp();
// Ensure the configuration file is valid..
if (!isValidConfiguration($config))
{
echo generateErrorResponse(0x80000001, "Invalid configuration file detected.");
exit;
}
// Attempt to get the cached data..
$data = getCachedData($cacheFile);
if ($data !== false)
{
// Check if the cached data has expired..
if (json_decode($data)->timestamp + $config['cache']['lifetime'] > $timestamp)
{
// Erase configurations from memory..
unset($config);
// Data is still valid, use it..
echo $data;
exit;
}
}
/**
* No cached data present or it is invalid or expired. Instead, we must query the
* database for the needed information to populate the cache with new data.
*/
try
{
// Connect to the database..
$db = new PDO("mysql:host={$config['db']['host']};port={$config['db']['port']};dbname={$config['db']['name']}", $config['db']['user'], $config['db']['pass']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Empty the database configurations..
unset($config['db']);
// Query the database for the current online counts..
$sql = $db->query("SELECT COUNT(DISTINCT(client_addr)) AS u, COUNT(*) AS t FROM accounts_sessions;")->fetchall();
if (!is_array($sql) || count($sql) !== 1)
{
// Invalid results, return an error..
echo generateErrorResponse(0x80000002, 'Failed to query database for count information.');
exit;
}
// Build the new server information array..
$data = array(
'status' => 0,
'version' => ASHITA_PSCAPI_VERSION,
'timestamp' => $timestamp,
'lifetime' => $config['server']['lifetime'],
'name' => $config['server']['name'],
'levelcap' => $config['server']['levelcap'],
'ip' => $config['server']['ip'],
'website' => $config['server']['website'],
'discord' => $config['server']['discord'],
'total' => $sql[0]['t'],
'unique' => $sql[0]['u']
);
try
{
// Open the cache file for writing..
$f = fopen($cacheFile, 'w');
if ($f === FALSE)
{
echo generateErrorResponse(0x80000003, 'Failed to update cache file. (1)');
exit;
}
// Write the data to the cache file..
fwrite($f, json_encode($data));
fclose($f);
}
catch (Exception $e)
{
echo generateErrorResponse(0x80000003, 'Failed to update cache file. (2)');
exit;
}
// Cleanup the pdo objects..
$sql = null;
$db = null;
// Echo the new data..
echo json_encode($data);
exit;
}
catch (Exception $e)
{
echo generateErrorResponse(0x80000004, 'Failed to get server information.');
exit;
}

95
config.php

@ -0,0 +1,95 @@ @@ -0,0 +1,95 @@
<?php
/**
* Ashita Private Server Collection API - Copyright (c) 2018 atom0s [[email protected]]
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*
* By using this script, you agree to the above license and its terms.
*
* Attribution - You must give appropriate credit, provide a link to the license and indicate if changes were
* made. You must do so in any reasonable manner, but not in any way that suggests the licensor
* endorses you or your use.
*
* Non-Commercial - You may not use the material (this script) for commercial purposes.
*
* No-Derivatives - If you remix, transform, or build upon the material (this script), you may not distribute the
* modified material. You are, however, allowed to submit the modified works back to the original
* Ashita Private Server Collection API project in attempt to have it added to the original project.
*
* You may not apply legal terms or technological measures that legally restrict others
* from doing anything the license permits.
*
* You may contact me, atom0s, at [email protected] for more information or if you are seeking commercial use.
*
* No warranties are given.
*/
// Prevent hack attempts..
if (!defined('ASHITA_PSCAPI')) { exit; }
/**
* Database Configurations
*
* host (string)
* The MySQL server host name to connect to.
*
* port (string)
* The MySQL server port to connect on.
*
* user (string)
* The MySQL username of the account to login with.
*
* pass (string)
* The MySQL password of the account to login with.
*
* name (string)
* The MySQL database name to query informaton from.
*/
$config['db']['host'] = 'localhost';
$config['db']['port'] = '3306';
$config['db']['user'] = 'root';
$config['db']['pass'] = 'root';
$config['db']['name'] = 'dspdb';
/**
* Cache Configurations
*
* lifetime (number)
* The amount of time, in seconds, the cache.json file should be used to retain information.
* This is used so that the database is not queried constantly each attempt this script is ran.
*/
$config['cache']['lifetime'] = 300;
/**
* Server Configurations
*
* lifetime
* The amount of time, in seconds, Ashita's server should hold onto your servers cached data
* before it will attempt to query your server again for new information. This is used so that
* Ashita's server does not constantly request information from your server.
*
* name
* The name of the server that will be displayed in the server list.
*
* levelcap
* The maximum level players can reach on your server.
*
* ip
* The connection string / IP used to connect to your server.
*
* website
* The home website for your server. (Can be left blank.)
*
* discord
* The Discord invite code for your servers general chat that players can use to join and chat
* to other players of your server. (This should be JUST the invite code, not the full url.)
*/
$config['server']['lifetime'] = 300;
$config['server']['name'] = 'MyServerName';
$config['server']['levelcap'] = 75;
$config['server']['ip'] = '127.0.0.1';
$config['server']['website'] = 'https://www.google.com/';
$config['server']['discord'] = 'CfZu6Ya';

0
index.html

Loading…
Cancel
Save