ENSLAVED opencode to fix the search thingy

This commit is contained in:
2026-11-13 11:06:37 +00:00
parent 78e458f2b1
commit 95a10c5f9b
8 changed files with 802 additions and 296 deletions

View File

@@ -10,9 +10,11 @@ Create a config.ini, there is an example included in the root. Or if you install
## Dependencies ## Dependencies
- libxml2 - libxml2
- libcurl (may be replaced in the future with curl-impersonate) - curl-impersonate (or libcurl; build with `-DOMNISEARCH_USE_CURL_IMPERSONATE` and link `-lcurl-impersonate` to enable browser impersonation)
- beaker [(source)](https://git.bwaaa.space/beaker/) - beaker [(source)](https://git.bwaaa.space/beaker/)
The included Nix flake and NixOS module build against curl-impersonate automatically. Set `OMNISEARCH_IMPERSONATE_TARGET` to override the impersonated browser profile (default: `chrome136`).
## First Setup ## First Setup
Depending on your system, you may first need to install libcurl and libxml2. Depending on your system, you may first need to install libcurl and libxml2.

View File

@@ -40,7 +40,8 @@
buildInputs = [ buildInputs = [
pkgs.libxml2.dev pkgs.libxml2.dev
pkgs.curl.dev pkgs.curl-impersonate
pkgs.curl-impersonate.dev
pkgs.openssl pkgs.openssl
beaker beaker
]; ];
@@ -48,8 +49,8 @@
preBuild = '' preBuild = ''
makeFlagsArray+=( makeFlagsArray+=(
"PREFIX=$out" "PREFIX=$out"
"CFLAGS=-Wall -Wextra -O2 -Isrc -I${pkgs.libxml2.dev}/include/libxml2" "CFLAGS=-Wall -Wextra -O2 -Isrc -I${pkgs.libxml2.dev}/include/libxml2 -I${pkgs.curl-impersonate.dev}/include -DOMNISEARCH_USE_CURL_IMPERSONATE"
"LIBS=-lbeaker -lcurl -lxml2 -lpthread -lm -lssl -lcrypto" "LIBS=-lbeaker -lcurl-impersonate -lxml2 -lpthread -lm -lssl -lcrypto"
) )
''; '';

View File

@@ -25,9 +25,15 @@
#define INFOBOX_FIELD_COUNT 5 #define INFOBOX_FIELD_COUNT 5
#define MAX_RESULTS_PER_ENGINE 10 #define MAX_RESULTS_PER_ENGINE 10
#define CURL_TIMEOUT_SECS 15L #define CURL_TIMEOUT_SECS 8L
#define CURL_CONNECT_TIMEOUT_SECS 5L
#define CURL_DNS_TIMEOUT_SECS 300L #define CURL_DNS_TIMEOUT_SECS 300L
/* Upper bound for a whole search, even if one engine hangs. */
#define SCRAPE_DEADLINE_MS 8000
#define DEFAULT_IMPERSONATE_TARGET "chrome136"
#define BING_IMAGE_URL "https://www.bing.com/images/search" #define BING_IMAGE_URL "https://www.bing.com/images/search"
#define IMAGE_RESULTS_PER_PAGE 32 #define IMAGE_RESULTS_PER_PAGE 32
#define IMAGE_RESULT_FIELDS 4 #define IMAGE_RESULT_FIELDS 4

File diff suppressed because it is too large Load Diff

View File

@@ -66,13 +66,11 @@ void configure_yacy_engine(const char *instance);
size_t write_memory_callback(void *contents, size_t size, size_t nmemb, size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
void *userp); void *userp);
const char *get_random_user_agent(void); const char *get_random_user_agent(void);
const char *get_first_user_agent(void);
void configure_curl_handle(CURL *curl, const char *full_url, void configure_curl_handle(CURL *curl, const char *full_url,
MemoryBuffer *chunk, struct curl_slist *headers); MemoryBuffer *chunk, struct curl_slist *headers);
char *build_search_url(const char *base_url, const char *page_param, char *build_search_url(const char *base_url, const char *page_param,
int page_multiplier, int page_base, int page_multiplier, int page_base,
const char *encoded_query, int page); const char *encoded_query, int page);
char *build_ddg_next_page_data(const char *html, size_t size);
struct curl_slist *build_request_headers(const char *host_header, struct curl_slist *build_request_headers(const char *host_header,
const char *referer); const char *referer);
void http_delay(void); void http_delay(void);

View File

@@ -1,4 +1,5 @@
#include "../Proxy/Proxy.h" #include "../Proxy/Proxy.h"
#include "../Utility/HttpClient.h"
#include "Config.h" #include "Config.h"
#include "Scraping.h" #include "Scraping.h"
#include <curl/curl.h> #include <curl/curl.h>
@@ -53,27 +54,30 @@ const char *get_random_user_agent(void) {
return USER_AGENTS[rand() % USER_AGENT_COUNT]; return USER_AGENTS[rand() % USER_AGENT_COUNT];
} }
const char *get_first_user_agent(void) { return USER_AGENTS[0]; }
void configure_curl_handle(CURL *curl, const char *full_url, void configure_curl_handle(CURL *curl, const char *full_url,
MemoryBuffer *chunk, struct curl_slist *headers) { MemoryBuffer *chunk, struct curl_slist *headers) {
curl_easy_setopt(curl, CURLOPT_URL, full_url); curl_easy_setopt(curl, CURLOPT_URL, full_url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
curl_easy_setopt(curl, CURLOPT_USERAGENT, get_random_user_agent());
/* Yahoo currently answers HTTP/2 requests with a 500 error. HTTP/1.1 is
accepted by all of the scraped engines, so use it everywhere. */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS); curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SECS); curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CURL_CONNECT_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
apply_proxy_settings(curl); apply_proxy_settings(curl);
#ifdef OMNISEARCH_USE_CURL_IMPERSONATE
http_apply_browser_impersonation(curl);
#else
curl_easy_setopt(curl, CURLOPT_USERAGENT, get_random_user_agent());
/* Yahoo currently answers plain HTTP/2 requests with a 500 error. */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
#endif
} }
char *build_search_url(const char *base_url, const char *page_param, char *build_search_url(const char *base_url, const char *page_param,
@@ -89,69 +93,6 @@ char *build_search_url(const char *base_url, const char *page_param,
return url; return url;
} }
char *build_ddg_next_page_data(const char *html, size_t size) {
htmlDocPtr doc = htmlReadMemory(html, size, NULL, NULL,
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |
HTML_PARSE_NOWARNING);
if (!doc)
return NULL;
xmlXPathContextPtr ctx = create_xpath_context(doc);
if (!ctx) {
xmlFreeDoc(doc);
return NULL;
}
xmlXPathObjectPtr inputs = xmlXPathEvalExpression(
(const xmlChar *)"(//form[.//input[@name='s'] and "
".//input[@name='vqd' and string-length(@value)>0] and "
"(.//input[@type='submit' and contains(@value, 'Next')] or "
".//button[contains(., 'Next')])])[1]"
"//input[@name and not(@disabled) and "
"(not(@type) or @type='hidden')]",
ctx);
if (!inputs || !inputs->nodesetval) {
free_xpath_objects(ctx, inputs);
xmlFreeDoc(doc);
return NULL;
}
char *data = NULL;
size_t length = 0;
int failed = 0;
for (int i = 0; i < inputs->nodesetval->nodeNr; i++) {
xmlNodePtr node = inputs->nodesetval->nodeTab[i];
xmlChar *name = xmlGetProp(node, (const xmlChar *)"name");
xmlChar *value = xmlGetProp(node, (const xmlChar *)"value");
char *key = curl_easy_escape(NULL, (const char *)name, 0);
char *val = curl_easy_escape(NULL, value ? (const char *)value : "", 0);
size_t needed = key && val ? strlen(key) + strlen(val) + 2 : 0;
char *expanded = needed ? realloc(data, length + needed + 1) : NULL;
if (expanded) {
data = expanded;
length += snprintf(data + length, needed + 1, "%s%s=%s",
length ? "&" : "", key, val);
} else {
failed = 1;
}
curl_free(key);
curl_free(val);
xmlFree(name);
xmlFree(value);
if (failed)
break;
}
if (failed) {
free(data);
data = NULL;
}
free_xpath_objects(ctx, inputs);
xmlFreeDoc(doc);
return data;
}
struct curl_slist *build_request_headers(const char *host_header, struct curl_slist *build_request_headers(const char *host_header,
const char *referer) { const char *referer) {
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
@@ -165,12 +106,15 @@ struct curl_slist *build_request_headers(const char *host_header,
snprintf(ref_buf, sizeof(ref_buf), "Referer: %s", referer); snprintf(ref_buf, sizeof(ref_buf), "Referer: %s", referer);
headers = curl_slist_append(headers, ref_buf); headers = curl_slist_append(headers, ref_buf);
} }
#ifndef OMNISEARCH_USE_CURL_IMPERSONATE
headers = curl_slist_append( headers = curl_slist_append(
headers, headers,
"Accept: " "Accept: "
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5"); headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5");
headers = curl_slist_append(headers, "DNT: 1"); headers = curl_slist_append(headers, "DNT: 1");
#endif
return headers; return headers;
} }

View File

@@ -2,9 +2,27 @@
#include "../Cache/Cache.h" #include "../Cache/Cache.h"
#include "../Proxy/Proxy.h" #include "../Proxy/Proxy.h"
#include "Config.h" #include "Config.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void http_apply_browser_impersonation(CURL *curl) {
#ifdef OMNISEARCH_USE_CURL_IMPERSONATE
const char *target = getenv("OMNISEARCH_IMPERSONATE_TARGET");
if (!target || target[0] == '\0')
target = DEFAULT_IMPERSONATE_TARGET;
CURLcode result = curl_easy_impersonate(curl, target, 1);
if (result != CURLE_OK) {
fprintf(stderr,
"[WARN] curl-impersonate target '%s' failed: %s; using defaults\n",
target, curl_easy_strerror(result));
}
#else
(void)curl;
#endif
}
static size_t write_callback(void *contents, size_t size, size_t nmemb, static size_t write_callback(void *contents, size_t size, size_t nmemb,
void *userp) { void *userp) {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
@@ -32,6 +50,10 @@ static size_t write_callback(void *contents, size_t size, size_t nmemb,
} }
static struct curl_slist *build_http_headers(void) { static struct curl_slist *build_http_headers(void) {
#ifdef OMNISEARCH_USE_CURL_IMPERSONATE
/* curl-impersonate supplies a complete, browser-consistent header set. */
return NULL;
#else
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
headers = curl_slist_append( headers = curl_slist_append(
headers, headers,
@@ -40,6 +62,7 @@ static struct curl_slist *build_http_headers(void) {
headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5"); headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5");
headers = curl_slist_append(headers, "DNT: 1"); headers = curl_slist_append(headers, "DNT: 1");
return headers; return headers;
#endif
} }
HttpResponse http_get(const char *url, const char *user_agent) { HttpResponse http_get(const char *url, const char *user_agent) {
@@ -68,16 +91,23 @@ HttpResponse http_get(const char *url, const char *user_agent) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
curl_easy_setopt(curl, CURLOPT_USERAGENT,
user_agent ? user_agent : "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SECS); curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CURL_CONNECT_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS); curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
apply_proxy_settings(curl); apply_proxy_settings(curl);
#ifdef OMNISEARCH_USE_CURL_IMPERSONATE
(void)user_agent;
http_apply_browser_impersonation(curl);
#else
curl_easy_setopt(curl, CURLOPT_USERAGENT,
user_agent ? user_agent : "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
#endif
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);

View File

@@ -15,6 +15,9 @@ typedef struct {
HttpResponse http_get(const char *url, const char *user_agent); HttpResponse http_get(const char *url, const char *user_agent);
void http_response_free(HttpResponse *resp); void http_response_free(HttpResponse *resp);
/* Apply a browser TLS/HTTP fingerprint when built against curl-impersonate. */
void http_apply_browser_impersonation(CURL *curl);
typedef xmlDocPtr (*XmlParserFn)(const char *data, size_t size, typedef xmlDocPtr (*XmlParserFn)(const char *data, size_t size,
const char *url); const char *url);