]> git.ayabusa.dev Git - thoryum.git/commitdiff
fixed downloader using a correct user agent
authorAyabusa <lebgpub@gmail.com>
Sat, 6 Jun 2026 16:10:35 +0000 (18:10 +0200)
committerAyabusa <lebgpub@gmail.com>
Sat, 6 Jun 2026 16:10:35 +0000 (18:10 +0200)
Makefile
src/downloader.c
src/downloader.h
src/main.c

index 700dab008917adb70d3bcc97d857454cd9dd9458..ee48c6d22085e7332fa74d51945e9c87ad860b8d 100644 (file)
--- 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)
index 0d48967d5e7b86c699492939cce5ab9194d80501..264740fd67d979d478b8765bb1a5a7d31c2f6f66 100644 (file)
@@ -1,22 +1,44 @@
+#include <stddef.h>
 #include <curl/curl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <err.h>
 #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;
+}
index 331b3403a083593f9fbe07d154facb8e9952e5d2..f4b41c561de6371bdbd7e7f9e151be80dc479519 100644 (file)
@@ -3,6 +3,17 @@
 
 #include <stdio.h>
 
-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
index 276d4d128137a90ca8fe068d4ee114b1fbdc9883..115f424e7d779998379e7fe7e4462ed651212639 100644 (file)
@@ -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();
 }