diff --git a/Dockerfile b/Dockerfile index 0df2f626..34f5efd5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,33 +1,9 @@ -FROM lsiobase/alpine:3.12 as build-stage - -# Install apk stuff -RUN apk add --no-cache --upgrade \ - gcc \ - cmake \ - curl-dev \ - libxml2-dev \ - build-base \ - openssl-dev \ - git && \ - cd /tmp && \ - git clone https://github.com/taganaka/SpeedTest && \ - cd SpeedTest && \ - cmake -DCMAKE_BUILD_TYPE=Release . && \ - cd /tmp/SpeedTest && \ - make install - FROM linuxserver/nginx MAINTAINER henrywhitaker3@outlook.com -# Install apt stuff -# RUN apk add --no-cache --upgrade supervisor - # Copy over static files COPY conf/ / -# Get SpeedTest++ -COPY --from=build-stage /usr/local/bin/SpeedTest /site/app/Bin/ - EXPOSE 80 443 VOLUME ["/config"] diff --git a/conf/etc/cont-init.d/50-speedtest b/conf/etc/cont-init.d/50-speedtest index 219be51b..42a25cc9 100644 --- a/conf/etc/cont-init.d/50-speedtest +++ b/conf/etc/cont-init.d/50-speedtest @@ -1,6 +1,33 @@ #!/usr/bin/with-contenv bash # # This script sets up the speedtest app +function eulaError() +{ + echo "##################################################################################################################################" + echo "##################################################################################################################################" + echo "You haven't accepted the Ookla EULA. Please re-create the container with the environment variable 'OOKLA_EULA_GDPR' set to 'true'." + echo "##################################################################################################################################" + echo "##################################################################################################################################" + exit 1 +} + +# Do Ookla stuff +if [ -z ${OOKLA_EULA_GDPR+x} ]; then + eulaError +else + if [ $OOKLA_EULA_GDPR != "true" ]; then + eulaError + fi + + echo "Ookla GDPR and EULA accepted. Downloading Speedtest CLI." + cd /tmp + wget https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-x86_64-linux.tgz -O speedtest.tgz > /dev/null + tar zxvf speedtest.tgz > /dev/null + cp speedtest /site/app/Bin/ + HOME=/config && s6-setuidgid abc /site/app/Bin/speedtest --accept-license --accept-gdpr > /dev/null& + HOME=/root +fi + # Copy site files to /config echo "Copying latest site files to config" cp -rfT /site/ /config/www/ @@ -14,7 +41,6 @@ else chown abc:abc /config/speed.db fi - # Check for .env if [ ! -f /config/www/.env ]; then echo "Env file not found! Creating .env file" diff --git a/conf/other/TestConfigTemplate.h b/conf/other/TestConfigTemplate.h deleted file mode 100644 index 7ce75140..00000000 --- a/conf/other/TestConfigTemplate.h +++ /dev/null @@ -1,120 +0,0 @@ -// -// Created by Francesco Laurita on 6/2/16. -// - -#ifndef SPEEDTEST_TESTCONFIGTEMPLATE_H -#define SPEEDTEST_TESTCONFIGTEMPLATE_H - -#include "SpeedTest.h" - -const TestConfig preflightConfigDownload = { - 600000, // start_size - 2000000, // max_size - 125000, // inc_size - 4096, // buff_size - 10000, // min_test_time_ms - 1, // Concurrency - "Preflight check" -}; - -const TestConfig slowConfigDownload = { - 100000, // start_size - 500000, // max_size - 10000, // inc_size - 1024, // buff_size - 20000, // min_test_time_ms - 1, // Concurrency - "Very-slow-line line type detected: profile selected slowband" -}; - -const TestConfig slowConfigUpload = { - 50000, // start_size - 80000, // max_size - 1000, // inc_size - 1024, // buff_size - 20000, // min_test_time_ms - 1, // Concurrency - "Very-slow-line line type detected: profile selected slowband" -}; - - -const TestConfig narrowConfigDownload = { - 1000000, // start_size - 100000000, // max_size - 750000, // inc_size - 4096, // buff_size - 20000, // min_test_time_ms - 1, // Concurrency - "Buffering-lover line type detected: profile selected narrowband" -}; - -const TestConfig narrowConfigUpload = { - 1000000, // start_size - 100000000, // max_size - 550000, // inc_size - 4096, // buff_size - 20000, // min_test_time_ms - 1, // Concurrency - "Buffering-lover line type detected: profile selected narrowband" -}; - -const TestConfig broadbandConfigDownload = { - 1000000, // start_size - 100000000, // max_size - 750000, // inc_size - 65536, // buff_size - 20000, // min_test_time_ms - 1, // concurrency - "Broadband line type detected: profile selected broadband" - -}; - -const TestConfig broadbandConfigUpload = { - 1000000, // start_size - 70000000, // max_size - 250000, // inc_size - 65536, // buff_size - 20000, // min_test_time_ms - 1, // concurrency - "Broadband line type detected: profile selected broadband" -}; - -const TestConfig fiberConfigDownload = { - 5000000, // start_size - 120000000, // max_size - 950000, // inc_size - 65536, // buff_size - 20000, // min_test_time_ms - 1, // concurrency - "Fiber / Lan line type detected: profile selected fiber" -}; - -const TestConfig fiberConfigUpload = { - 1000000, // start_size - 70000000, // max_size - 250000, // inc_size - 65536, // buff_size - 20000, // min_test_time_ms - 1, // concurrency - "Fiber / Lan line type detected: profile selected fiber" -}; - -void testConfigSelector(const double preSpeed, TestConfig& uploadConfig, TestConfig& downloadConfig){ - uploadConfig = slowConfigUpload; - downloadConfig = slowConfigDownload; - - - if (preSpeed > 4 && preSpeed <= 30){ - downloadConfig = narrowConfigDownload; - uploadConfig = narrowConfigUpload; - } else if (preSpeed > 30 && preSpeed < 150) { - downloadConfig = broadbandConfigDownload; - uploadConfig = broadbandConfigUpload; - } else if (preSpeed >= 150) { - downloadConfig = fiberConfigDownload; - uploadConfig = fiberConfigUpload; - } - -} - -#endif //SPEEDTEST_TESTCONFIGTEMPLATE_H \ No newline at end of file diff --git a/conf/site/app/Helpers/SpeedtestHelper.php b/conf/site/app/Helpers/SpeedtestHelper.php index 00373cf9..e5c4247a 100644 --- a/conf/site/app/Helpers/SpeedtestHelper.php +++ b/conf/site/app/Helpers/SpeedtestHelper.php @@ -22,9 +22,9 @@ class SpeedtestHelper { try { $output = json_decode($output, true, 512, JSON_THROW_ON_ERROR); $test = Speedtest::create([ - 'ping' => $output['ping'], - 'download' => $output['download'] / 1000000, - 'upload' => $output['upload'] / 1000000 + 'ping' => $output['ping']['latency'], + 'download' => SpeedtestHelper::convert($output['download']['bandwidth']), + 'upload' => SpeedtestHelper::convert($output['upload']['bandwidth']), ]); } catch(JsonException $e) { Log::error('Failed to parse speedtest JSON'); @@ -39,102 +39,23 @@ class SpeedtestHelper { public static function output() { $server = SettingsHelper::get('server')['value']; - $binPath = app_path() . DIRECTORY_SEPARATOR . 'Bin' . DIRECTORY_SEPARATOR . 'SpeedTest'; + $binPath = app_path() . DIRECTORY_SEPARATOR . 'Bin' . DIRECTORY_SEPARATOR . 'speedtest'; if($server != '' && $server != false) { $server = explode(',', $server); $server = $server[array_rand($server)]; - $server = SpeedtestHelper::resolveServer($server); if($server == false) { Log::error('Speedtest server undefined'); return false; } - $server = $server['host']; - return shell_exec($binPath . ' --output json --test-server ' . $server); + return shell_exec('HOME=/config && ' . $binPath . ' -f json -s ' . $server); } - return shell_exec($binPath . ' --output json'); + return shell_exec('HOME=/config && ' . $binPath . ' -f json'); } - /* - * Resolve the server host/port from speedtest server id - */ - public static function resolveServer($id) - { - $ttl = Carbon::now()->addMinutes(120); - $ids = Cache::remember('servers', $ttl, function () { - $urls = [ - 'http://www.speedtest.net/speedtest-servers-static.php', - 'http://c.speedtest.net/speedtest-servers-static.php', - 'http://www.speedtest.net/speedtest-servers.php', - 'http://c.speedtest.net/speedtest-servers.php', - ]; - - $client = new Client([]); - - $ids = []; - foreach($urls as $url) { - try { - $response = $client->get($url); - if($response->getStatusCode() == 200) { - $data = (string) $response->getBody(); - $data = simplexml_load_string($data); - $data = SpeedtestHelper::xmlToArray($data); - $data = $data['settings']['servers']['server']; - foreach($data as $s) { - $ids[$s['attributes']['id']] = $s['attributes']; - } - } else { - continue; - } - } catch(Exception $e) { - Log::error('Speedtest server resolver error'); - Log::error($e->getMessage()); - continue; - } - } - return $ids; - }); - - if(array_key_exists($id, $ids)) { - return $ids[$id]; - } else { - return false; - } - } - - public static function xmlToArray(SimpleXMLElement $xml): array - { - $parser = function (SimpleXMLElement $xml, array $collection = []) use (&$parser) { - $nodes = $xml->children(); - $attributes = $xml->attributes(); - - if (0 !== count($attributes)) { - foreach ($attributes as $attrName => $attrValue) { - $collection['attributes'][$attrName] = strval($attrValue); - } - } - - if (0 === $nodes->count()) { - $collection['value'] = strval($xml); - return $collection; - } - - foreach ($nodes as $nodeName => $nodeValue) { - if (count($nodeValue->xpath('../' . $nodeName)) < 2) { - $collection[$nodeName] = $parser($nodeValue); - continue; - } - - $collection[$nodeName][] = $parser($nodeValue); - } - - return $collection; - }; - - return [ - $xml->getName() => $parser($xml) - ]; + public static function convert($bytes) { + return ( $bytes * 8 ) / 1000000; } public static function latest()