From b2a56aff510aa714685d569d8837fb49bb4146d9 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Fri, 8 Mar 2024 07:06:03 -0500 Subject: [PATCH] Handle downloading VPN database better, avoiding situation where it could query ips without the list downloaded --- src/main.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 69b7794..657ab99 100644 --- a/src/main.c +++ b/src/main.c @@ -54,17 +54,19 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { return written; } -void download_vpn_list(const char *url, const char *output_path) { +int download_vpn_list(const char *url, const char *output_path) { CURL *curl; FILE *fp; CURLcode res; + int result = 1; // Assume failure curl = curl_easy_init(); if (curl) { fp = fopen(output_path, "wb"); if (fp == NULL) { fprintf(stderr, "Cannot open file %s\n", output_path); - return; + curl_easy_cleanup(curl); + return result; } curl_easy_setopt(curl, CURLOPT_URL, url); @@ -72,13 +74,16 @@ void download_vpn_list(const char *url, const char *output_path) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); - if (res != CURLE_OK) { + if (res == CURLE_OK) { + result = 0; // Success + } else { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } fclose(fp); curl_easy_cleanup(curl); } + return result; } static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) { @@ -89,14 +94,18 @@ static size_t write_callback(void *contents, size_t size, size_t nmemb, void *us } int main(int argc, char *argv[]) { - int vpnCheck = is_vpn(argv[1]); - download_vpn_list(VPN_LIST_URL, VPN_LIST_PATH); - if (argc != 2) { printf("Usage: %s \n", argv[0]); return 1; } + if (download_vpn_list(VPN_LIST_URL, VPN_LIST_PATH) != 0) { + fprintf(stderr, "Failed to download VPN list.\n"); + return 1; + } + + int vpnCheck = is_vpn(argv[1]); + char url[256] = "http://ip-api.com/json/"; strcat(url, argv[1]);