Browse Source

added ahmia search, code cleanup, removed dead instances, added some new instances

hnhx 2 years ago
parent
commit
9ad077535e
10 changed files with 155 additions and 81 deletions
  1. 4 4
      README.md
  2. 10 1
      api.php
  3. 3 1
      config.php.example
  4. 53 0
      engines/ahmia/hidden_service.php
  5. 9 54
      engines/google/text.php
  6. 16 16
      instances.json
  7. 47 3
      misc/tools.php
  8. 1 1
      opensearch.xml.example
  9. 12 1
      search.php
  10. BIN
      static/images/hidden_service_result.png

+ 4 - 4
README.md

@@ -14,23 +14,23 @@
 | Clearnet | TOR | I2P | Country |
 |-|-|-|-|
 | [librex.beparanoid.de](https://librex.beparanoid.de/) | [✅](http://librex.2356uhnbpv5nk3bni5bv6jg2cd6lgj664kwx3lhyelstpttpyv4kk2qd.onion/) | ❌ | 🇭🇺 HU (OFFICIAL INSTANCE) |
-| [librex.extravi.dev](https://librex.extravi.dev/) | [✅](http://ncblhz7q4sfbf755bdbhebfzxcpypz7ewafgi4agatecojz7pln4i3id.onion/) | [✅](http://rra33hiaf6nmby7jfpqe2gqmng3jnzkvbu2n7jgce7vbhoyuhzya.b32.i2p/) | 🇩🇪 DE |
 | [lx.vern.cc](https://lx.vern.cc/) | [✅](http://lx.vernccvbvyi5qhfzyqengccj7lkove6bjot2xhh5kajhwvidqafczrad.onion/) | [✅](http://vernziqfqvweijfaacmwazohgpdo2bt2ib2jlupt2pwwu27bhgxq.b32.i2p/) | 🇺🇸 US |
-| [linxer.org/search](https://linxer.org/search/) | ❌ | ❌ | 🇷🇴 RO |
 | [search.davidovski.xyz](https://search.davidovski.xyz/) | ❌ | ❌ | 🇬🇧 UK |
 | [search.funami.tech](https://search.funami.tech/) | ❌ | ❌ | 🇰🇷 KR |
-| [librex.catalyst.sx](https://librex.catalyst.sx/) | ❌ | ❌ | 🇺🇸 US |
 | [search.madreyk.xyz](https://search.madreyk.xyz/) | ❌ | ❌ | 🇩🇪 DE |
 | [search.pabloferreiro.es](https://search.pabloferreiro.es/) | ❌ | ❌ | 🇩🇪 DE |
 | [buscar.weblibre.org](https://buscar.weblibre.org/) | ❌ | ❌ | 🇨🇱 CL |
 | [search.ahwx.org](https://search.ahwx.org/) | ❌ | ❌ | 🇳🇱 NL |
 | [pufe.org](https://pufe.org/) | ❌ | ❌ | :new_zealand: NZ |
+| [librex.kitscomputer.tk](https://librex.kitscomputer.tk/) | ❌ | ❌ | 🇺🇸 US |
+| [librex.smlan.dev](https://librex.smlan.dev/) | ❌ | ❌ | 🇭🇺 HU |
+| [linxer.org](https://linxer.org/search/) | ❌ | ❌ | 🏳️ DYN (FORK) |
 
 <br>
 
 ### About LibreX
 
-LibreX gives you results from Google, Brave, Qwant and popular torrent sites without spying on you.
+LibreX gives you results from Google, Brave, Qwant, Ahmia and popular torrent sites without spying on you.
 <br>
 <br>
 If you would like to learn more about LibreX check out the [Wiki](https://github.com/hnhx/librex/wiki).

+ 10 - 1
api.php

@@ -46,6 +46,15 @@
                 $results = get_merged_torrent_results($query_encoded);
             }       
             break;
+        case 4:
+            if ($config->disable_hidden_service_search)
+                $results = array("error" => "disabled");
+            else
+            {
+                require "engines/ahmia/hidden_service.php";
+                $results = get_hidden_service_results($query_encoded);
+            }       
+            break;
         default:
             require "engines/google/text.php";
             $results = get_text_results($query_encoded, $page);
@@ -53,5 +62,5 @@
     }
 
     header("Content-Type: application/json");
-    echo json_encode($results, JSON_PRETTY_PRINT);
+    echo json_encode($results);
 ?>

+ 3 - 1
config.php.example

@@ -10,6 +10,8 @@
         "disable_bittorent_search" => false,
         "bittorent_trackers" => "&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce",
 
+        "disable_hidden_service_search" => false,
+
         /*
             Preset privacy friendly frontends for users, these can be overwritten by users in settings
             e.g.: "invidious" => "https://yewtu.be",
@@ -39,7 +41,7 @@
             // CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
             CURLOPT_RETURNTRANSFER => true,
             CURLOPT_ENCODING => "",
-            CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
+            CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
             CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
             CURLOPT_CUSTOMREQUEST => "GET",
             CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,

+ 53 - 0
engines/ahmia/hidden_service.php

@@ -0,0 +1,53 @@
+<?php
+    function get_hidden_service_results($query)
+    {
+        global $config;
+
+        $url = "https://ahmia.fi/search/?q=$query";
+        $response = request($url);
+        $xpath = get_xpath($response);
+
+        $results = array();
+
+        foreach($xpath->query("//ol[@class='searchResults']//li[@class='result']") as $result)
+        {
+            $url = "http://" . $xpath->evaluate(".//cite", $result)[0]->textContent;
+            $title = remove_special($xpath->evaluate(".//h4", $result)[0]->textContent);
+            $description = $xpath->evaluate(".//p", $result)[0]->textContent;
+
+            array_push($results,
+                array (
+                    "title" => htmlspecialchars($title),
+                    "url" =>  htmlspecialchars($url),
+                    "base_url" => htmlspecialchars(get_base_url($url)),
+                    "description" => htmlspecialchars($description)
+                )
+            );
+        }
+
+        return $results;
+    }
+
+    function print_hidden_service_results($results)
+    {
+        echo "<div class=\"text-result-container\">";
+
+        foreach($results as $result)
+        {
+            $title = $result["title"];
+            $url = $result["url"];
+            $base_url = $result["base_url"];
+            $description = $result["description"];
+
+            echo "<div class=\"text-result-wrapper\">";
+            echo "<a href=\"$url\">";
+            echo "$base_url";
+            echo "<h2>$title</h2>";
+            echo "</a>";
+            echo "<span>$description</span>";
+            echo "</div>";
+        }
+
+        echo "</div>";
+    }
+?>

+ 9 - 54
engines/google/text.php

@@ -1,55 +1,9 @@
 <?php
-     function check_for_special_search($query)
-     {
-        if (isset($_COOKIE["disable_special"]) || isset($_REQUEST["disable_special"]))
-            return 0;
-
-         $query_lower = strtolower($query);
-         $split_query = explode(" ", $query);
-
-         if (strpos($query_lower, "to") && count($split_query) >= 4) // currency
-         {
-            $amount_to_convert = floatval($split_query[0]);
-            if ($amount_to_convert != 0)
-                return 1;
-         }
-         else if (strpos($query_lower, "mean") && count($split_query) >= 2) // definition
-         {
-             return 2;
-         }
-         else if (strpos($query_lower, "my") !== false)
-         {
-            if (strpos($query_lower, "ip"))
-            {
-                return 3;
-            }
-            else if (strpos($query_lower, "user agent") || strpos($query_lower, "ua"))
-            {
-                return 4;
-            }
-         }
-         else if (strpos($query_lower, "weather") !== false)
-         {
-                return 5;
-         }
-         else if (strpos($query_lower, "tor") !== false)
-         {
-                return 6;
-         }
-         else if (3 > count(explode(" ", $query))) // wikipedia
-         {
-             return 7;
-         }
-
-        return 0;
-     }
-
-    function get_text_results($query, $page=0)
+    function get_text_results($query, $page)
     {
         global $config;
 
         $mh = curl_multi_init();
-        $query_lower = strtolower($query);
         $query_encoded = urlencode($query);
         $results = array();
 
@@ -58,8 +12,7 @@
         curl_setopt_array($google_ch, $config->curl_settings);
         curl_multi_add_handle($mh, $google_ch);
 
-
-        $special_search = $page == 0 ? check_for_special_search($query) : 0;
+        $special_search = check_for_special_search($query);
         $special_ch = null;
         $url = null;
         if ($special_search != 0)
@@ -85,13 +38,15 @@
                     $url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
                     break;
             }
-
-            $special_ch = curl_init($url);
-            curl_setopt_array($special_ch, $config->curl_settings);
-            curl_multi_add_handle($mh, $special_ch);
+            
+            if ($url != NULL)
+            {
+                $special_ch = curl_init($url);
+                curl_setopt_array($special_ch, $config->curl_settings);
+                curl_multi_add_handle($mh, $special_ch);
+            }
         }
 
-
         $running = null;
         do {
             curl_multi_exec($mh, $running);

+ 16 - 16
instances.json

@@ -6,12 +6,6 @@
               "i2p": null,
               "country": "HU"
           },
-          {
-              "clearnet": "https://librex.extravi.dev/",
-              "tor": "http://ncblhz7q4sfbf755bdbhebfzxcpypz7ewafgi4agatecojz7pln4i3id.onion/",
-              "i2p": "http://rra33hiaf6nmby7jfpqe2gqmng3jnzkvbu2n7jgce7vbhoyuhzya.b32.i2p/",
-              "country": "DE"
-          },
           {
               "clearnet": "https://search.davidovski.xyz/",
               "tor": null,
@@ -24,12 +18,6 @@
               "i2p": null,
               "country": "KR"
           },
-          {
-              "clearnet": "https://librex.catalyst.sx/",
-              "tor": null,
-              "i2p": null,
-              "country": "US"
-          },
           {
               "clearnet": "https://search.madreyk.xyz/",
               "tor": null,
@@ -49,16 +37,28 @@
               "country": "NL"
           },
           {
-              "clearnet": "https://linxer.org/search/",
+              "clearnet": "https://pufe.org/",
+              "tor": null,
+              "i2p": null,
+              "country": "NZ"
+          },
+          {
+              "clearnet": "https://librex.kitscomputer.tk/",
               "tor": null,
               "i2p": null,
-              "country": "RO"
+              "country": "US"
           },
           {
-              "clearnet": "https://pufe.org/",
+              "clearnet": "https://librex.smlan.dev/",
               "tor": null,
               "i2p": null,
-              "country": "NZ"
+              "country": "HU"
+          },
+          {
+             "clearnet": "https://linxer.org/search/",
+             "tor": null,
+             "i2p": null,
+             "country": "DYN"
           }
     ]
 }

+ 47 - 3
misc/tools.php

@@ -97,6 +97,51 @@
         }
     }
 
+    function check_for_special_search($query)
+    {
+        if (isset($_COOKIE["disable_special"]) || isset($_REQUEST["disable_special"]))
+            return 0;
+
+         $query_lower = strtolower($query);
+         $split_query = explode(" ", $query);
+
+         if (strpos($query_lower, "to") && count($split_query) >= 4) // currency
+         {
+            $amount_to_convert = floatval($split_query[0]);
+            if ($amount_to_convert != 0)
+                return 1;
+         }
+         else if (strpos($query_lower, "mean") && count($split_query) >= 2) // definition
+         {
+             return 2;
+         }
+         else if (strpos($query_lower, "my") !== false)
+         {
+            if (strpos($query_lower, "ip"))
+            {
+                return 3;
+            }
+            else if (strpos($query_lower, "user agent") || strpos($query_lower, "ua"))
+            {
+                return 4;
+            }
+         }
+         else if (strpos($query_lower, "weather") !== false)
+         {
+                return 5;
+         }
+         else if ($query_lower == "tor")
+         {
+                return 6;
+         }
+         else if (3 > count(explode(" ", $query))) // wikipedia
+         {
+             return 7;
+         }
+
+        return 0;
+    }
+
     function get_xpath($response)
     {
         $htmlDom = new DOMDocument;
@@ -127,9 +172,8 @@
 
     function remove_special($string)
     {
-        $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
-
-        return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
+        $string = preg_replace("/[\r\n]+/", "\n", $string);
+        return trim(preg_replace("/\s+/", ' ', $string));
      }
 
     function print_elapsed_time($start_time)

+ 1 - 1
opensearch.xml.example

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
   <ShortName>LibreX</ShortName>
-  <Description>A meta search engine for Google.</Description>
+  <Description>A privacy respecting meta search engine.</Description>
   <InputEncoding>UTF-8</InputEncoding>
   <LongName>LibreX search</LongName>
   <Url rel="results" type="text/html" method="get" template="http://localhost/search.php?q={searchTerms}" />

+ 12 - 1
search.php

@@ -42,6 +42,7 @@
                 <button name="t" value="1"><img src="static/images/image_result.png" alt="image result" />Images</button>
                 <button name="t" value="2"><img src="static/images/video_result.png" alt="video result" />Videos</button>
                 <button name="t" value="3"><img src="static/images/torrent_result.png" alt="torrent result" />Torrents</button>
+                <button name="t" value="4"><img src="static/images/hidden_service_result.png" alt="hidden service result" />Hidden services</button>
             </div>
         <hr>
         </form>
@@ -90,9 +91,19 @@
                         $results = get_merged_torrent_results($query_encoded);
                         print_elapsed_time($start_time);
                         print_merged_torrent_results($results);
-                        break;
                     }
+                    break;
 
+                case 4:
+                    if ($config->disable_hidden_service_search)
+                        echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
+                    else
+                    {
+                        require "engines/ahmia/hidden_service.php";
+                        $results = get_hidden_service_results($query_encoded);
+                        print_elapsed_time($start_time);
+                        print_hidden_service_results($results);
+                    }
                     break;
 
                 default:

BIN
static/images/hidden_service_result.png