From 42a5bb2806ff22a29b7d8d2d85c63d69cee24bba Mon Sep 17 00:00:00 2001 From: turret Date: Mon, 18 Dec 2023 14:06:19 -0600 Subject: net: get gateway url basic api http_get function, (which the definition of will def. change in the future) uses libcurl to get the api /gateway to get the wss url, and assumes a url in case of failure so we at least try authenticating --- net/api.c | 43 +++++++++++++++++++++++++++++++++++++++++++ net/net.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 net/api.c (limited to 'net') diff --git a/net/api.c b/net/api.c new file mode 100644 index 0000000..e664c42 --- /dev/null +++ b/net/api.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +#include + +extern char *token; + +int http_get(char *url) +{ + int pipefd[2]; + if(pipe(pipefd) < 0) + return -errno; + + FILE *write_end = fdopen(pipefd[1], "w"); + + int ret = pipefd[0]; + + CURL *job = curl_easy_init(); + if(job == 0) { + close(pipefd[0]); + ret = -1; + goto close_writepipe; + } + + curl_easy_setopt(job, CURLOPT_URL, url); + curl_easy_setopt(job, CURLOPT_WRITEDATA, write_end); + curl_easy_setopt(job, CURLOPT_HTTPHEADER, curl_slist_append(NULL, token)); + CURLcode res = curl_easy_perform(job); + + if(res > 0) { + ret = -res; + close(pipefd[0]); + goto cleanup; + } + +cleanup: + curl_easy_cleanup(job); +close_writepipe: + fclose(write_end); + return ret; +} diff --git a/net/net.c b/net/net.c index 4288ae4..9ed4653 100644 --- a/net/net.c +++ b/net/net.c @@ -1,18 +1,63 @@ +#include +#include #include +#include +#include + #include #include #include +extern int http_get(char *url); +char *gateway_url; + int net_subsystem(void) { - print(LOG_NOTICE "net: starting net subsystem"); - usleep(10000); // do net stuff + print(LOG_INFO "net: starting net subsystem"); + + return 0; } +void net_get_gateway_url() +{ + int fd = http_get("https://discord.com/api/gateway/bot"); + if(fd < 0) { + print(LOG_ERR "net: failed to get gateway url (error %d)", -fd); + goto assume; + } + + char buf[512]; + int buf_length = read(fd, buf, 512); + close(fd); + + cJSON *gateway_info = cJSON_ParseWithLength(buf, buf_length); + cJSON *gateway_url_json = cJSON_GetObjectItemCaseSensitive(gateway_info, "url"); + if(!cJSON_IsString(gateway_url_json) || gateway_url_json->valuestring == NULL) { + print(LOG_ERR "net: cannot get gateway url from api (token invalid?)"); + cJSON_Delete(gateway_info); + goto assume; + } + + char *gateway_url = calloc(strlen(gateway_url_json->valuestring) + 1, sizeof(char)); + strcpy(gateway_url, gateway_url_json->valuestring); + print(LOG_DEBUG "net: using gateway url %s", gateway_url); + + free(gateway_url); + cJSON_Delete(gateway_info); + return; + +assume: + print(LOG_DEBUG "net: assuming gateway url wss://gateway.discord.gg"); + gateway_url = calloc(strlen("wss://gateway.discord.gg") + 1, sizeof(char)); + strcpy(gateway_url, "wss://gateway.discord.gg"); + return; +} +l1_initcall(net_get_gateway_url); + void net_initcall() { start_subsystem(net_subsystem); } -l1_initcall(net_initcall); +l2_initcall(net_initcall); -- cgit v1.2.3