diff --git a/inc/lektor/internal/config.def b/inc/lektor/internal/config.def index d26bce7781428ee525a98b50327fdfca3cfd9067..40a9e847407296a767f091fbd1986c0096cc95dd 100644 --- a/inc/lektor/internal/config.def +++ b/inc/lektor/internal/config.def @@ -63,11 +63,12 @@ value("id_json", "https://kurisu.iiens.net/api/%ld") value("id_kara", "https://kurisu.iiens.net/api/download/%ld") value("fav_json", "https://kurisu.iiens.net/api/fav/") value("plt_json", "https://kurisu.iiens.net/api/plt/") +value("token", "") /* MODULE: PLAYER */ section("player") comment("The player module, so that people can 'sing' when using lektor.") -comment("By default it's sdl, but one can create any module, for audio support") +comment("By default it's qt (was sdl before), but one can create any module, for audio support") comment("only for example.") value("module", "qt") value("autoclear", "true") diff --git a/src/module/module_repo.c b/src/module/module_repo.c index 2472e3a8eea730f5d950ba2ac81e25e2f9e954ed..7311547283edc063078f67c49f77015f497c25e7 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -3,11 +3,16 @@ #include <lektor/stb/ds.h> #include <lektor/internal/worker.h> +#include <assert.h> + /* Different kinds of updates */ #define REPO_UPDATE_TYPE_COUNT 2 /* Different kinds of download, for some sizes... */ #define REPO_UPDATE_KARA ((unsigned int)(1 << 1)) /* Downloading or rescanning the bakabase */ #define REPO_UPDATE_FAV ((unsigned int)(1 << 2)) /* Downloading the favorites */ #define REPO_UPDATE_ALL (REPO_UPDATE_FAV | REPO_UPDATE_KARA) /* Update all flag */ +#define REPO_TOKEN_MAX 128 +#define REPO_HEADER_AUTH_NAME "X-Token-Authorization: " +#define REPO_HEADER_AUTH_SIZE (sizeof(REPO_HEADER_AUTH_NAME) + REPO_TOKEN_MAX + 1) /*********** * Globals * @@ -45,6 +50,7 @@ struct kara { struct json_parse_arg { void *worker; void *real_arg; + struct module_repo_internal *repo; }; struct module_repo_internal { @@ -56,6 +62,7 @@ struct module_repo_internal { char get_id_json[LKT_LINE_MAX]; char get_id_file[LKT_LINE_MAX]; char get_fav_json[LKT_LINE_MAX]; + char download_token[REPO_TOKEN_MAX]; const uint64_t version; /* The database and the queue */ @@ -273,7 +280,7 @@ err: } PRIVATE_FUNCTION int -___download_kara(const char *url, const char *path, int override) +___download_kara(const char *url, const char *path, int override, struct module_repo_internal *repo) { struct curl_slist *headers = NULL; CURL *curl_handle; @@ -312,10 +319,9 @@ retest: }; memset(file.magic, 0, sizeof(file.magic)); - static const char *const token = "49duf300b8nmm469uzvz6dxwfarb05x3"; - char author_token[LKT_LINE_MAX]; - memset(author_token, 0, LKT_LINE_MAX); - snprintf(author_token, LKT_LINE_MAX - 1, "X-Token-Authorization: %s", token); + char author_token[REPO_HEADER_AUTH_SIZE]; + memset(author_token, 0, REPO_HEADER_AUTH_SIZE); + snprintf(author_token, REPO_HEADER_AUTH_SIZE - 1, REPO_HEADER_AUTH_NAME"%s", repo->download_token); headers = curl_slist_append(headers, "Accept: video/x-matroska"); headers = curl_slist_append(headers, "Content-Type: video/x-matroska"); @@ -347,7 +353,7 @@ err: } PRIVATE_FUNCTION void -___handle_got_json_dl(struct kara *kara) +___handle_got_json_dl(struct kara *kara, struct module_repo_internal *repo) { /* Download the kara */ int current_id; @@ -364,7 +370,7 @@ ___handle_got_json_dl(struct kara *kara) safe_snprintf(kara->url, LKT_LINE_MAX, kara->repo->get_id_file, kara->id); LOG_INFO("REPO", "Start downloading kara %ld to: %s", kara->id, kara->filename); - if (___download_kara(kara->url, kara->filename, true)) { + if (___download_kara(kara->url, kara->filename, true, repo)) { LOG_WARN("REPO", "Could not download kara %ld at path '%s'", kara->id, kara->filename); return; } @@ -421,6 +427,7 @@ ___handle_got_json_internal_callback(const char *key, const char *val, int comp, { struct json_parse_arg *arg = (struct json_parse_arg *)user; struct kara *kara = (struct kara *)arg->real_arg; + struct module_repo_internal *repo = (struct module_repo_internal*)arg->repo; const struct lkt_uri *filter = (const struct lkt_uri *)kara->filter_uri; /* Check if interrupt was asked */ @@ -551,7 +558,7 @@ ___handle_got_json_internal_callback(const char *key, const char *val, int comp, return 0; } - ___handle_got_json_dl(kara); + ___handle_got_json_dl(kara, repo); lkt_queue_send(kara->repo->queue, LKT_EVENT_DB_UPDATE_TICK, NULL); } @@ -576,6 +583,7 @@ ___json_dl_and_handle_for_id(struct module_repo_internal *repo, struct kara *kar struct json_parse_arg arg = { .worker = &repo->workers, .real_arg = (void *)kara, + .repo = repo, }; lkt_queue_send(repo->queue, LKT_EVENT_DB_UPDATE_TOTAL, (void *)1lu); const int ret = json_parse(json, 1, ___handle_got_json_internal_callback, &arg); @@ -638,6 +646,7 @@ ___handle_got_json(struct module_repo_internal *repo, const char *json, struct l struct json_parse_arg arg = { .worker = &repo->workers, .real_arg = (void *)&kara, + .repo = repo, }; const int ret = json_parse(json, 2, ___handle_got_json_internal_callback, &arg); LOG_INFO("REPO", "Updated %u karas, ignored %u karas, total is %d karas", kara.update_count, @@ -929,6 +938,7 @@ ___handle_fav_list(struct module_repo_internal *repo, char *fav, size_t fav_size struct json_parse_arg arg = { .worker = &repo->workers, .real_arg = (void *)&uri, + .repo = repo, }; json_parse(json, 2, ___handle_fav_list_internal, (void *)&arg); @@ -969,6 +979,7 @@ ___worker_import_favorites(void *worker, void *___repo) struct json_parse_arg arg = { .worker = worker, .real_arg = (void *)repo, + .repo = repo, }; char *json; @@ -1049,6 +1060,14 @@ module_repo_new(struct module_repo_internal *repo_, struct queue *queue, lkt_db LOG_ERROR("REPO", "Configuration invalid, but it should not be at this point"); exit(EXIT_FAILURE); } + + static_assert((LKT_LINE_MAX >= REPO_HEADER_AUTH_SIZE), "REPO_HEADER_AUTH_SIZE can't be bigger than LKT_LINE_MAX"); + /* Check token config separately, as it's empty by default, and the error should ask the user to add it to the config */ + if (!database_config_get_text(db, "repo", "token", repo.download_token, REPO_TOKEN_MAX)) { + LOG_ERROR("REPO", "No download token in repo configuration, please add it"); + exit(EXIT_FAILURE); + } + memcpy(repo_, &repo, sizeof(struct module_repo_internal)); /* Init the worker only now ! */