Merge branch 'librex_fallback'
This commit is contained in:
commit
2f87e05d6e
|
@ -20,6 +20,9 @@
|
|||
|
||||
"disable_hidden_service_search" => false,
|
||||
|
||||
// Fallback to another librex instance if google search fails
|
||||
"instance_fallback" => true,
|
||||
|
||||
/*
|
||||
Preset privacy friendly frontends for users, these can be overwritten by users in the settings
|
||||
e.g.: Preset the invidious instance URL: "instance_url" => "https://yewtu.be",
|
||||
|
|
|
@ -70,11 +70,11 @@
|
|||
do {
|
||||
curl_multi_exec($mh, $running);
|
||||
} while ($running);
|
||||
if (curl_getinfo($google_ch)['http_code'] == '302') {
|
||||
$instances_json = json_decode(file_get_contents("instances.json"), true);
|
||||
$instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
|
||||
header("Location: " . $instances[array_rand($instances)] . "search.php?q=$query");
|
||||
die();
|
||||
|
||||
if (curl_getinfo($google_ch)['http_code'] != '200')
|
||||
{
|
||||
require "engines/librex/text.php";
|
||||
return get_librex_results($query, $page);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
function get_librex_results($query, $page)
|
||||
{
|
||||
global $config;
|
||||
|
||||
if (!$config->instance_fallback)
|
||||
return array();
|
||||
|
||||
$instances_json = json_decode(file_get_contents("instances.json"), true);
|
||||
|
||||
if (empty($instances_json["instances"]))
|
||||
return array();
|
||||
|
||||
|
||||
$instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
|
||||
shuffle($instances);
|
||||
|
||||
$query_encoded = urlencode($query);
|
||||
|
||||
$results = array();
|
||||
$tries = 0;
|
||||
|
||||
do {
|
||||
$tries++;
|
||||
|
||||
// after "too many" requests, give up
|
||||
if ($tries > 5)
|
||||
return array();
|
||||
|
||||
$instance = array_pop($instances);
|
||||
|
||||
if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
|
||||
continue;
|
||||
|
||||
$url = $instance . "api.php?q=$query_encoded&p=$page&t=0";
|
||||
|
||||
$librex_ch = curl_init($url);
|
||||
curl_setopt_array($librex_ch, $config->curl_settings);
|
||||
copy_cookies($librex_ch);
|
||||
$response = curl_exec($librex_ch);
|
||||
curl_close($librex_ch);
|
||||
|
||||
$code = curl_getinfo($librex_ch)["http_code"];
|
||||
$results = json_decode($response, true);
|
||||
|
||||
} while ( $results == null || count($results) <= 1);
|
||||
|
||||
return array_values($results);
|
||||
}
|
||||
?>
|
|
@ -230,4 +230,11 @@
|
|||
echo "<button type=\"submit\">$text</button>";
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
function copy_cookies($curl)
|
||||
{
|
||||
if (array_key_exists("HTTP_COOKIE", $_SERVER))
|
||||
curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
$config = require "config.php";
|
||||
|
||||
// clear all coookies on changes
|
||||
// Reset all cookies when resetting, or before saving new cookies
|
||||
if (isset($_REQUEST["reset"]) || isset($_REQUEST["save"]))
|
||||
{
|
||||
if (isset($_SERVER["HTTP_COOKIE"]))
|
||||
|
|
Loading…
Reference in New Issue