diff --git a/app/Bin/SpeedTest b/app/Bin/SpeedTest deleted file mode 100755 index 3d420201..00000000 Binary files a/app/Bin/SpeedTest and /dev/null differ diff --git a/app/Bin/speedtest b/app/Bin/speedtest new file mode 100755 index 00000000..a5a0e7c8 Binary files /dev/null and b/app/Bin/speedtest differ diff --git a/app/Helpers/SpeedtestHelper.php b/app/Helpers/SpeedtestHelper.php index 00373cf9..5a1f18a2 100644 --- a/app/Helpers/SpeedtestHelper.php +++ b/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($binPath . ' -f json -s ' . $server); } - return shell_exec($binPath . ' --output json'); + return shell_exec($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()