From d62aef0511753ae48b156937a2ebf8dc5b16bf42 Mon Sep 17 00:00:00 2001 From: atom0s Date: Wed, 21 Mar 2018 00:09:16 -0700 Subject: [PATCH] Initial code commit. Updated README.md --- README.md | 33 ++++++++- ashita.functions.php | 159 +++++++++++++++++++++++++++++++++++++++++++ ashita.php | 141 ++++++++++++++++++++++++++++++++++++++ config.php | 95 ++++++++++++++++++++++++++ index.html | 0 5 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 ashita.functions.php create mode 100644 ashita.php create mode 100644 config.php create mode 100644 index.html diff --git a/README.md b/README.md index 6fc0e83..6c9f071 100644 --- a/README.md +++ b/README.md @@ -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.) \ No newline at end of file +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 \ No newline at end of file diff --git a/ashita.functions.php b/ashita.functions.php new file mode 100644 index 0000000..36b82d0 --- /dev/null +++ b/ashita.functions.php @@ -0,0 +1,159 @@ +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; + } +} \ No newline at end of file diff --git a/ashita.php b/ashita.php new file mode 100644 index 0000000..e7e8317 --- /dev/null +++ b/ashita.php @@ -0,0 +1,141 @@ +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; +} \ No newline at end of file diff --git a/config.php b/config.php new file mode 100644 index 0000000..543a392 --- /dev/null +++ b/config.php @@ -0,0 +1,95 @@ +