From: Ayabusa Date: Sat, 6 Jun 2026 16:10:35 +0000 (+0200) Subject: fixed downloader using a correct user agent X-Git-Url: https://git.ayabusa.dev/?a=commitdiff_plain;h=fcfd0f363a530b352acd2ec19505a5d4d1450f7c;p=thoryum.git fixed downloader using a correct user agent --- diff --git a/Makefile b/Makefile index 700dab0..ee48c6d 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,29 @@ -CFLAGS = -Wall -Wextra -fsanitize=address -g -lcurl +CFLAGS = -Wall -Wextra -g +LIBS = -lcurl SRC = src/main.c src/cpu.c src/downloader.c OBJ = build/main.o build/cpu.o build/downloader.o RES = build/Thoryum all: setup $(OBJ) build - $(CC) $(CFLAGS) $(OBJ) -o $(RES) + $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS) build/main.o: src/main.c - $(CC) $(CFLAGS) -c $^ -o $@ + $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) build/cpu.o: src/cpu.c - $(CC) $(CFLAGS) -c $^ -o $@ + $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) build/downloader.o: src/downloader.c - $(CC) $(CFLAGS) -c $^ -o $@ + $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) setup: mkdir -p build +run: + ./$(RES) + +debug: + gdb -tui $(RES) + clean: $(RM) $(OBJ) $(RES) diff --git a/src/downloader.c b/src/downloader.c index 0d48967..264740f 100644 --- a/src/downloader.c +++ b/src/downloader.c @@ -1,22 +1,44 @@ +#include #include #include #include +#include #include #include "downloader.h" + +#define THORIUM_RELEASE_API_URL "https://api.github.com/repos/Alex313031/thorium/releases/latest" + // Well its probably usefull -static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream) +static size_t write_file(char *ptr, size_t size, size_t nmemb, void **stream) { - size_t written = fwrite(ptr, size, nmemb, (FILE *)stream); + size_t written = fwrite(ptr, size, nmemb, (FILE *)*stream); return written; } +size_t ver_str_size = 1; // Malloced size +size_t ver_str_len = 0; // Actual length +static size_t write_char(char *ptr, size_t size, size_t nmemb, void **stream) { + printf("s = %zu, l = %zu\n", ver_str_size, ver_str_len); + size_t old_len = ver_str_len; + ver_str_len += size * nmemb; + printf("l++ = %zu\n", ver_str_len); + if (ver_str_size < ver_str_len) { + while (ver_str_size < ver_str_len) + ver_str_size = ver_str_size * 2 + 1; + printf("s++ = %zu\n", ver_str_size); + *stream = realloc(*stream, ver_str_size); + } + memcpy(*stream + old_len, ptr, size*nmemb); + return nmemb; +} + // Ref : https://curl.se/libcurl/c/simple.html // https://curl.se/libcurl/c/url2file.html // returns 0 if good or 1 if that failed -int DL_get_file(char *url, FILE *file) { +int DL_get_file(char *url, struct DL_stream *stream) { CURL *curl; - + CURLcode result = curl_global_init(CURL_GLOBAL_ALL); if(result != CURLE_OK) { err(EXIT_FAILURE, "Failed to initialize CURL, Abort\n"); @@ -28,9 +50,13 @@ int DL_get_file(char *url, FILE *file) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // To debug stuff curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // We want to see the progress - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); // func to write some shit curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // We tell libcurl to follow redirection - curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "thoryum/1.0"); + if (stream->type == FILE_STREAM) + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); // func to write some shit + else + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_char); // other func to write some shit + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(stream->stream)); result = curl_easy_perform(curl); if(result != CURLE_OK) { @@ -43,3 +69,13 @@ int DL_get_file(char *url, FILE *file) { curl_global_cleanup(); return 0; } + +char *DL_get_latest_version() { + struct DL_stream buf = {.stream = NULL, .type = CHAR_STREAM}; + //set some globals + ver_str_size = 1; + ver_str_len = 0; + DL_get_file(THORIUM_RELEASE_API_URL, &buf); + printf("Received :\n%s\n", (char *)buf.stream); + return NULL; +} diff --git a/src/downloader.h b/src/downloader.h index 331b340..f4b41c5 100644 --- a/src/downloader.h +++ b/src/downloader.h @@ -3,6 +3,17 @@ #include -int DL_get_file(char *url, FILE *file); +enum DL_stream_type { + FILE_STREAM, + CHAR_STREAM, +}; + +struct DL_stream { + void *stream; + enum DL_stream_type type; +}; + +int DL_get_file(char *url, struct DL_stream *stream); +char *DL_get_latest_version(); #endif diff --git a/src/main.c b/src/main.c index 276d4d1..115f424 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,10 @@ int main() { char opti_name[5] = {0}; CPU_opti_name(opti_name, opti); printf("You are using the CPU : '%s'\n", opti_name); - + /* FILE *file = fopen("page.out", "w"); DL_get_file("https://ayabusa.dev", file); fclose(file); + */ + DL_get_latest_version(); }