2023-08-07 09:54:25 -04:00
|
|
|
<?php
|
2023-08-07 12:01:12 -04:00
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
class LibreXFallback extends EngineRequest {
|
|
|
|
public function __construct($instance, $opts, $mh) {
|
|
|
|
$this->instance = $instance;
|
|
|
|
parent::__construct($opts, $mh);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_request_url() {
|
|
|
|
return $this->instance . "api.php?" . opts_to_params($this->opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_results() {
|
|
|
|
$response = curl_exec($this->ch);
|
|
|
|
$response = json_decode($response, true);
|
|
|
|
if (!$response)
|
|
|
|
return array();
|
|
|
|
|
|
|
|
return array_values($response);
|
|
|
|
}
|
2023-08-22 18:55:11 -04:00
|
|
|
}
|
|
|
|
|
2023-08-07 09:54:25 -04:00
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
function get_librex_results($opts) {
|
|
|
|
if (!$opts->do_fallback)
|
2023-08-07 09:54:25 -04:00
|
|
|
return array();
|
|
|
|
|
|
|
|
$instances_json = json_decode(file_get_contents("instances.json"), true);
|
2023-08-07 13:27:54 -04:00
|
|
|
|
|
|
|
if (empty($instances_json["instances"]))
|
|
|
|
return array();
|
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
// TODO pick instances which aren't on cooldown
|
2023-08-07 13:27:54 -04:00
|
|
|
|
2023-08-07 09:54:25 -04:00
|
|
|
$instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
|
2023-08-07 13:27:54 -04:00
|
|
|
shuffle($instances);
|
|
|
|
|
2023-08-07 10:05:41 -04:00
|
|
|
$results = array();
|
2023-08-07 13:27:54 -04:00
|
|
|
$tries = 0;
|
2023-08-07 10:05:41 -04:00
|
|
|
|
|
|
|
do {
|
2023-08-07 13:27:54 -04:00
|
|
|
$tries++;
|
|
|
|
|
|
|
|
$instance = array_pop($instances);
|
|
|
|
|
|
|
|
if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
|
|
|
|
continue;
|
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
$librex_request = new LibreXFallback($instance, $opts, null);
|
|
|
|
$results = $librex_request->get_results();
|
2023-08-07 12:01:12 -04:00
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
if (count($results) > 1)
|
|
|
|
return $results;
|
2023-08-07 09:54:25 -04:00
|
|
|
|
2023-08-22 19:38:59 -04:00
|
|
|
} while ( !empty($instances));
|
2023-08-19 15:16:43 -04:00
|
|
|
|
|
|
|
if (empty($instances))
|
|
|
|
return array();
|
2023-08-07 09:54:25 -04:00
|
|
|
|
2023-08-07 10:05:41 -04:00
|
|
|
return array_values($results);
|
2023-08-07 09:54:25 -04:00
|
|
|
}
|
2023-08-22 18:55:11 -04:00
|
|
|
|
2023-08-07 09:54:25 -04:00
|
|
|
?>
|