aboutsummaryrefslogtreecommitdiff
path: root/src/Scraping/Startpage.c
diff options
context:
space:
mode:
authorfrosty <frostinator33@airmail.cc>2026-11-14 15:00:07 -0500
committerfrosty <frostinator33@airmail.cc>2026-11-14 15:00:07 -0500
commit2d9a58d6639a0382bae1933e5b58f5eb4f8ded85 (patch)
tree52d33dbd4823db646d0e6eb5bc6f77e9d27b3dee /src/Scraping/Startpage.c
parent0ac79f4ff0134f0145117322932bd7883fd76b26 (diff)
fix: restore Startpage search resultsHEADmasterindev
Diffstat (limited to 'src/Scraping/Startpage.c')
-rw-r--r--src/Scraping/Startpage.c355
1 files changed, 355 insertions, 0 deletions
diff --git a/src/Scraping/Startpage.c b/src/Scraping/Startpage.c
new file mode 100644
index 0000000..15c26f9
--- /dev/null
+++ b/src/Scraping/Startpage.c
@@ -0,0 +1,355 @@
+#include "Scraping.h"
+#include "../Utility/Unescape.h"
+#include "../Utility/XmlHelper.h"
+#include <ctype.h>
+#include <openssl/sha.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#define HAS_CLASS(name) \
+ "contains(concat(' ', normalize-space(@class), ' '), ' " name " ')"
+
+typedef struct {
+ const char *begin;
+ const char *end;
+} JsonValue;
+
+static const char *skip_space(const char *p, const char *end) {
+ while (p < end && isspace((unsigned char)*p))
+ p++;
+ return p;
+}
+
+static const char *json_value_end(const char *p, const char *end, int depth) {
+ if (p >= end || depth > 64)
+ return NULL;
+ if (*p == '"') {
+ for (p++; p < end; p++) {
+ if (*p == '"')
+ return p + 1;
+ if (*p == '\\') {
+ if (++p >= end)
+ return NULL;
+ }
+ }
+ return NULL;
+ }
+ if (*p == '{' || *p == '[') {
+ char close = *p == '{' ? '}' : ']';
+ p = skip_space(p + 1, end);
+ if (p < end && *p == close)
+ return p + 1;
+ while (p < end) {
+ if (close == '}') {
+ if (*p != '"')
+ return NULL;
+ p = json_value_end(p, end, depth + 1);
+ if (!p)
+ return NULL;
+ p = skip_space(p, end);
+ if (p >= end || *p != ':')
+ return NULL;
+ p = skip_space(p + 1, end);
+ }
+ p = json_value_end(p, end, depth + 1);
+ if (!p)
+ return NULL;
+ p = skip_space(p, end);
+ if (p < end && *p == close)
+ return p + 1;
+ if (p >= end || *p != ',')
+ return NULL;
+ p = skip_space(p + 1, end);
+ }
+ return NULL;
+ }
+ const char *start = p;
+ while (p < end && !isspace((unsigned char)*p) && *p != ',' && *p != ']' &&
+ *p != '}')
+ p++;
+ return p > start ? p : NULL;
+}
+
+static JsonValue json_member(JsonValue object, const char *key) {
+ JsonValue missing = {0};
+ if (!object.begin || *object.begin != '{')
+ return missing;
+ const char *p = skip_space(object.begin + 1, object.end);
+ while (p < object.end && *p == '"') {
+ const char *name_end = json_value_end(p, object.end, 0);
+ if (!name_end)
+ break;
+ int match = (size_t)(name_end - p - 2) == strlen(key) &&
+ strncmp(p + 1, key, strlen(key)) == 0;
+ p = skip_space(name_end, object.end);
+ if (p >= object.end || *p != ':')
+ break;
+ p = skip_space(p + 1, object.end);
+ const char *value_end = json_value_end(p, object.end, 0);
+ if (!value_end)
+ break;
+ if (match)
+ return (JsonValue){p, value_end};
+ p = skip_space(value_end, object.end);
+ if (p >= object.end || *p != ',')
+ break;
+ p = skip_space(p + 1, object.end);
+ }
+ return missing;
+}
+
+static JsonValue json_next(JsonValue array, const char **cursor) {
+ JsonValue missing = {0};
+ if (!array.begin || *array.begin != '[')
+ return missing;
+ const char *p = skip_space(*cursor ? *cursor : array.begin + 1, array.end);
+ if (p < array.end && *p == ',')
+ p = skip_space(p + 1, array.end);
+ if (p >= array.end || *p == ']')
+ return missing;
+ const char *end = json_value_end(p, array.end, 0);
+ if (!end)
+ return missing;
+ *cursor = end;
+ return (JsonValue){p, end};
+}
+
+static char *json_text(JsonValue value, int html) {
+ if (!value.begin || *value.begin != '"')
+ return NULL;
+ char *text = json_string_unescape(value.begin + 1, value.end - 1);
+ if (!text || !html || !*text)
+ return text;
+ htmlDocPtr doc = htmlReadMemory(text, strlen(text), NULL, "UTF-8",
+ HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |
+ HTML_PARSE_NOWARNING | HTML_PARSE_NONET);
+ if (doc) {
+ char *content = xml_node_content(xmlDocGetRootElement(doc));
+ if (content) {
+ free(text);
+ text = strdup(content);
+ xmlFree(content);
+ }
+ xmlFreeDoc(doc);
+ }
+ return text;
+}
+
+static int valid_result(const char *url, const char *title) {
+ return url && title && *title &&
+ (strncmp(url, "https://", 8) == 0 || strncmp(url, "http://", 7) == 0);
+}
+
+static char *solve_startpage_challenge(const char *id, const char *random,
+ int zeros, const char *redirect_url) {
+ size_t length = strlen(random);
+ char *input = malloc(length + 32);
+ if (!input)
+ return NULL;
+
+ memcpy(input, random, length);
+ struct timespec start = {0}, finish = {0};
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ unsigned char digest[SHA256_DIGEST_LENGTH];
+ unsigned int nonce, max_attempts = 8U << (4 * zeros);
+ for (nonce = 0; nonce < max_attempts; nonce++) {
+ int digits = snprintf(input + length, 32, "%u", nonce);
+ SHA256((const unsigned char *)input, length + digits, digest);
+ int i = 0;
+ for (; i < zeros; i++) {
+ int nibble = i % 2 ? digest[i / 2] & 15 : digest[i / 2] >> 4;
+ if (nibble)
+ break;
+ }
+ if (i == zeros)
+ break;
+ }
+ free(input);
+ if (nonce == max_attempts)
+ return NULL;
+
+ clock_gettime(CLOCK_MONOTONIC, &finish);
+ long elapsed = (finish.tv_sec - start.tv_sec) * 1000 +
+ (finish.tv_nsec - start.tv_nsec) / 1000000;
+ char hex[SHA256_DIGEST_LENGTH * 2 + 1];
+ for (size_t i = 0; i < sizeof(digest); i++)
+ snprintf(hex + i * 2, 3, "%02x", digest[i]);
+ char *encoded_id = curl_easy_escape(NULL, id, 0);
+ char *encoded_redirect = curl_easy_escape(NULL, redirect_url, 0);
+ char *url = NULL;
+ if (encoded_id && encoded_redirect) {
+ size_t capacity = strlen(encoded_id) + strlen(encoded_redirect) + 256;
+ url = malloc(capacity);
+ if (url)
+ snprintf(url, capacity,
+ "https://www.startpage.com/.within.website/x/cmd/anubis/api/"
+ "pass-challenge?id=%s&response=%s&nonce=%u&redir=%s&elapsedTime=%ld",
+ encoded_id, hex, nonce, encoded_redirect, elapsed > 0 ? elapsed : 1);
+ }
+ curl_free(encoded_id);
+ curl_free(encoded_redirect);
+ return url;
+}
+
+char *build_startpage_challenge_url(const char *html, size_t size,
+ const char *redirect_url) {
+ htmlDocPtr doc = htmlReadMemory(html, size, NULL, "UTF-8",
+ HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |
+ HTML_PARSE_NOWARNING | HTML_PARSE_NONET);
+ if (!doc)
+ return NULL;
+ char *payload = xpath_text(doc, "//script[@id='anubis_challenge']");
+ xmlFreeDoc(doc);
+ if (!payload)
+ return NULL;
+
+ JsonValue data = {skip_space(payload, payload + strlen(payload)),
+ payload + strlen(payload)};
+ const char *end = json_value_end(data.begin, data.end, 0);
+ if (!end) {
+ free(payload);
+ return NULL;
+ }
+ data.end = end;
+ JsonValue rules = json_member(data, "rules");
+ JsonValue challenge = json_member(data, "challenge");
+ JsonValue difficulty = json_member(rules, "difficulty");
+ char *method = json_text(json_member(challenge, "method"), 0);
+ char *id = json_text(json_member(challenge, "id"), 0);
+ char *random = json_text(json_member(challenge, "randomData"), 0);
+ char *url = NULL;
+ if (method && strcmp(method, "fast") == 0 && id && *id &&
+ strlen(id) <= 128 && random && *random && strlen(random) <= 4096 &&
+ redirect_url && difficulty.begin &&
+ difficulty.end - difficulty.begin == 1 &&
+ *difficulty.begin >= '1' && *difficulty.begin <= '4') {
+ int zeros = *difficulty.begin - '0';
+ url = solve_startpage_challenge(id, random, zeros, redirect_url);
+ }
+
+ free(method);
+ free(id);
+ free(random);
+ free(payload);
+ return url;
+}
+
+static int parse_page_data(xmlXPathContextPtr ctx, SearchResult **out_results,
+ int max_results) {
+ xmlXPathObjectPtr scripts = xml_xpath_eval(
+ ctx, "//script[contains(., 'React.createElement(UIStartpage.AppSerpWeb,')]");
+ int count = 0;
+ for (int i = 0; scripts && scripts->nodesetval &&
+ i < scripts->nodesetval->nodeNr && count < max_results; i++) {
+ char *script = xml_node_content(scripts->nodesetval->nodeTab[i]);
+ const char *marker =
+ script ? strstr(script, "React.createElement(UIStartpage.AppSerpWeb,")
+ : NULL;
+ const char *begin = marker ? strchr(marker, '{') : NULL;
+ const char *end = begin ? json_value_end(begin, script + strlen(script), 0)
+ : NULL;
+ JsonValue data = {begin, end};
+ if (!end) {
+ free(script);
+ continue;
+ }
+ data = json_member(data, "render");
+ data = json_member(data, "presenter");
+ data = json_member(data, "regions");
+ data = json_member(data, "mainline");
+ const char *group_cursor = NULL;
+ JsonValue group;
+ while (count < max_results &&
+ (group = json_next(data, &group_cursor)).begin) {
+ char *type = json_text(json_member(group, "display_type"), 0);
+ int web = type && strcmp(type, "web-google") == 0;
+ free(type);
+ if (!web)
+ continue;
+ JsonValue results = json_member(group, "results");
+ JsonValue result;
+ const char *result_cursor = NULL;
+ while (count < max_results &&
+ (result = json_next(results, &result_cursor)).begin) {
+ char *url = json_text(json_member(result, "clickUrl"), 0);
+ char *title = json_text(json_member(result, "title"), 1);
+ char *snippet = json_text(json_member(result, "description"), 1);
+ if (valid_result(url, title)) {
+ if (!*out_results)
+ *out_results = alloc_results_array(max_results, max_results);
+ if (*out_results) {
+ assign_result(&(*out_results)[count], url, title, snippet, 0);
+ count++;
+ }
+ }
+ free(url);
+ free(title);
+ free(snippet);
+ }
+ }
+ free(script);
+ }
+ if (scripts)
+ xmlXPathFreeObject(scripts);
+ return count;
+}
+
+int parse_startpage(const char *engine_name, xmlDocPtr doc,
+ SearchResult **out_results, int max_results) {
+ (void)engine_name;
+ *out_results = NULL;
+ if (max_results <= 0)
+ return 0;
+ xmlXPathContextPtr ctx = create_xpath_context(doc);
+ if (!ctx)
+ return 0;
+
+ int count = parse_page_data(ctx, out_results, max_results);
+ if (count > 0) {
+ xmlXPathFreeContext(ctx);
+ return count;
+ }
+
+ xmlXPathObjectPtr containers = xml_xpath_eval(
+ ctx, "//div[(" HAS_CLASS("w-gl") " or " HAS_CLASS("result") ") and "
+ "not(ancestor-or-self::*[@id='sponsored' or "
+ HAS_CLASS("w-gl--ad") "])]");
+ for (int i = 0; containers && containers->nodesetval &&
+ i < containers->nodesetval->nodeNr && count < max_results; i++) {
+ ctx->node = containers->nodesetval->nodeTab[i];
+ xmlXPathObjectPtr links = xml_xpath_eval(
+ ctx, "(.//a[@data-testid='gl-title-link' or "
+ HAS_CLASS("w-gl__result-title") " or "
+ HAS_CLASS("result-link") "])[1]");
+ xmlNodePtr link = links && links->nodesetval && links->nodesetval->nodeNr
+ ? links->nodesetval->nodeTab[0] : NULL;
+ char *url = link ? (char *)xmlGetProp(link, (const xmlChar *)"href") : NULL;
+ char *title = xml_node_content(link);
+ xmlXPathObjectPtr snippets = xml_xpath_eval(
+ ctx, "(.//*[@data-testid='gl-description' or "
+ HAS_CLASS("w-gl__description") " or "
+ HAS_CLASS("description") "])[1]");
+ char *snippet = snippets && snippets->nodesetval &&
+ snippets->nodesetval->nodeNr
+ ? xml_node_content(snippets->nodesetval->nodeTab[0])
+ : NULL;
+ if (valid_result(url, title)) {
+ if (!*out_results)
+ *out_results = alloc_results_array(containers->nodesetval->nodeNr,
+ max_results);
+ if (*out_results) {
+ assign_result(&(*out_results)[count], url, title, snippet, 0);
+ count++;
+ }
+ }
+ free_xml_node_list(title, url, snippet);
+ if (links)
+ xmlXPathFreeObject(links);
+ if (snippets)
+ xmlXPathFreeObject(snippets);
+ }
+ free_xpath_objects(ctx, containers);
+ return count;
+}