From 6eff0a081ecd054e5b57faa34fc93d4d08b1c87b Mon Sep 17 00:00:00 2001 From: Hunter104 <000plagueinc@gmail.com> Date: Sat, 14 Sep 2024 22:05:50 -0300 Subject: [PATCH] refactor: removes real time updates --- include/proxy.h | 5 +- src/main.c | 154 ++++++------------------------------------------ src/proxy.c | 81 +++++++++++++++++++++---- 3 files changed, 93 insertions(+), 147 deletions(-) diff --git a/include/proxy.h b/include/proxy.h index 698ccbd..bdb210d 100644 --- a/include/proxy.h +++ b/include/proxy.h @@ -1,7 +1,8 @@ #pragma once -#include #include +#include #include +#include #define URL_MAX_SIZE 50 #define IP_MAX_SIZE 22 @@ -21,3 +22,5 @@ ProxyAddress CreateProxy(uint32_t ipAddress, uint16_t port); ProxyVector *CreateVector(); void Insert(ProxyVector *vector, ProxyAddress address); void freeVector(ProxyVector *vector); +ProxyVector *GetProxiesFromFile(FILE *file); +bool CheckIsProxyAlive(ProxyAddress address); diff --git a/src/main.c b/src/main.c index 9a5e5eb..0b04fa9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,132 +1,37 @@ -#include +#include "proxy.h" #include -#include -#include -#include -#include #include -#include #include -#include #include -#include "proxy.h" -#define TEST_URL "https://lumtest.com/myip.json" +#include +#include +#include +#include +#include +#include +#define THREAD_COUNT 16 #define GREEN_TEXT "\033[0;32m" #define RED_TEXT "\033[0;31m" #define RESET_TEXT "\033[0m" -ProxyVector *ParseProxies(FILE *file) { - - ProxyVector *vector = CreateVector(); - char *buffer = NULL; - size_t len = 0; - ssize_t nread; - - while ((nread = getline(&buffer, &len, file)) != -1) { - buffer[strcspn(buffer, "\n")] = 0; - uint32_t ipAddress; - uint16_t port; - - uint8_t *bytes = (uint8_t*)(&ipAddress); - int n = sscanf(buffer, "%hhu.%hhu.%hhu.%hhu:%hu", - &bytes[0], - &bytes[1], - &bytes[2], - &bytes[3], - &port); - - if (n != 5) { - printf("Error while parsing line {%s}.\n", buffer); - continue; - } - - Insert(vector, CreateProxy(ipAddress, port)); - } - - free(buffer); - - return vector; -} - -size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) -{ - return size * nmemb; -} - -bool IsProxyAlive(ProxyAddress address) { - CURL *curl; - CURLcode res; - char *url = address.url; - - curl_global_init(CURL_GLOBAL_DEFAULT); - curl = curl_easy_init(); - if (!curl) { - perror("Failed to initialize libcurl\n"); - abort(); - } - - curl_easy_setopt(curl, CURLOPT_URL, TEST_URL); - curl_easy_setopt(curl, CURLOPT_PROXY, url); - curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - curl_global_cleanup(); - - return res == CURLE_OK; -} - -typedef enum { WAITING=0, FAIL, SUCCESS } ProxyState; - -pthread_mutex_t lock; - -typedef struct { ProxyAddress address; - ProxyState *states; +typedef struct { + ProxyAddress address; int thread_id; } ThreadArgs; void *TestProxyThread(void *arg) { ThreadArgs *args = (ThreadArgs *)arg; - bool success = IsProxyAlive(args->address); - - pthread_mutex_lock(&lock); - args->states[args->thread_id] = success ? SUCCESS : FAIL; - pthread_mutex_unlock(&lock); + char *status = CheckIsProxyAlive(args->address) + ? GREEN_TEXT "Success" RESET_TEXT + : RED_TEXT "Failure" RESET_TEXT; + printf("Proxy %s status: %s\n", args->address.url, status); free(args); pthread_exit(NULL); } -void PrintProxyStates(ProxyState *states, int length, bool done) { - for (int i=0; i < length; i++) { - pthread_mutex_lock(&lock); - - printf("\rProxy %d: ", i + 1); - switch (states[i]) { - case WAITING: - printf(RESET_TEXT "WAITING" RESET_TEXT); - break; - case SUCCESS: - printf(GREEN_TEXT "SUCCESS" RESET_TEXT); - break; - case FAIL: - printf(RED_TEXT "FAILURE" RESET_TEXT); - break; - } - pthread_mutex_unlock(&lock); - - printf("\n"); - } - - if (!done) - printf("\033[%dA", length); -} - -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: [ip_list_path]"); return EXIT_FAILURE; @@ -137,49 +42,26 @@ int main(int argc, char *argv[]) fprintf(stderr, "Failed to open file: %s\n", argv[1]); exit(EXIT_FAILURE); } - ProxyVector *list = ParseProxies(file); + ProxyVector *list = GetProxiesFromFile(file); fclose(file); - pthread_mutex_init(&lock, NULL); pthread_t threads[list->len]; - ProxyState states[list->len]; - for (int i=0; ilen; i++) { + for (int i = 0; i < list->len; i++) { ThreadArgs *args = malloc(sizeof(ThreadArgs)); args->address = list->data[i]; args->thread_id = i; - states[i] = WAITING; - args->states = states; - if (pthread_create(&threads[i], NULL, TestProxyThread, (void *)args)) { fprintf(stderr, "Error creating thread %d\n", i + 1); free(args); } } - while (true) { - bool done = true; - - for (int i=0; i < list->len; i++) { - if (states[i] == WAITING) { - done = false; - break; - } - } - - PrintProxyStates(states, list->len, done); - if (done) - break; - - usleep(100000); - } - for (int i = 0; i < list->len; i++) { pthread_join(threads[i], NULL); - } + } - pthread_mutex_destroy(&lock); freeVector(list); return EXIT_SUCCESS; } diff --git a/src/proxy.c b/src/proxy.c index c467dd9..f14ac1a 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1,11 +1,13 @@ -#include -#include +#include "proxy.h" +#include "memory.h" #include -#include #include -#include "memory.h" -#include "proxy.h" +#include +#include +#include +#include #define INITIAL_CAPACITY 10 +#define TEST_URL "https://lumtest.com/myip.json" uint8_t getByte(uint32_t ipAddress, int n) { int power = 24 - (n << 3); @@ -17,9 +19,9 @@ ProxyAddress CreateProxy(uint32_t ipAddress, uint16_t port) { proxy.ipAddress = ipAddress; proxy.port = port; - uint8_t *bytes = (uint8_t*)(&ipAddress); - sprintf(proxy.url, "http://%hhu.%hhu.%hhu.%hhu:%hu", - bytes[0], bytes[1], bytes[2], bytes[3], port); + uint8_t *bytes = (uint8_t *)(&ipAddress); + sprintf(proxy.url, "http://%hhu.%hhu.%hhu.%hhu:%hu", bytes[0], bytes[1], + bytes[2], bytes[3], port); return proxy; } @@ -28,14 +30,15 @@ ProxyVector *CreateVector() { ProxyVector *vector = safe_malloc(sizeof *vector); vector->len = 0; vector->capacity = INITIAL_CAPACITY; - vector->data = safe_malloc(vector->capacity*sizeof(ProxyAddress)); + vector->data = safe_malloc(vector->capacity * sizeof(ProxyAddress)); return vector; } void Insert(ProxyVector *vector, ProxyAddress address) { if (vector->len >= vector->capacity) { vector->capacity *= 2; - vector->data = safe_realloc(vector->data, vector->capacity*sizeof(ProxyAddress)); + vector->data = + safe_realloc(vector->data, vector->capacity * sizeof(ProxyAddress)); } vector->data[vector->len++] = address; } @@ -44,3 +47,61 @@ void freeVector(ProxyVector *vector) { free(vector->data); free(vector); } + +ProxyVector *GetProxiesFromFile(FILE *file) { + + ProxyVector *vector = CreateVector(); + char *buffer = NULL; + size_t len = 0; + ssize_t nread; + + while ((nread = getline(&buffer, &len, file)) != -1) { + buffer[strcspn(buffer, "\n")] = 0; + uint32_t ipAddress; + uint16_t port; + + uint8_t *bytes = (uint8_t *)(&ipAddress); + int n = sscanf(buffer, "%hhu.%hhu.%hhu.%hhu:%hu", &bytes[0], &bytes[1], + &bytes[2], &bytes[3], &port); + + if (n != 5) { + printf("Error while parsing line {%s}.\n", buffer); + continue; + } + + Insert(vector, CreateProxy(ipAddress, port)); + } + + free(buffer); + + return vector; +} + +static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) { + return size * nmemb; +} + +bool CheckIsProxyAlive(ProxyAddress address) { + CURL *curl; + CURLcode res; + char *url = address.url; + + curl_global_init(CURL_GLOBAL_DEFAULT); + curl = curl_easy_init(); + if (!curl) { + perror("Failed to initialize libcurl\n"); + abort(); + } + + curl_easy_setopt(curl, CURLOPT_URL, TEST_URL); + curl_easy_setopt(curl, CURLOPT_PROXY, url); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + curl_global_cleanup(); + + return res == CURLE_OK; +}