diff options
Diffstat (limited to 'net/net.c')
-rw-r--r-- | net/net.c | 51 |
1 files changed, 48 insertions, 3 deletions
@@ -1,18 +1,63 @@ +#include <stdlib.h> +#include <string.h> #include <unistd.h> +#include <cJSON.h> +#include <curl/curl.h> + #include <init.h> #include <log.h> #include <subsys.h> +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); |