Skip to content

Commit

Permalink
feat: adds dynamic terminal update
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunter104 committed Sep 14, 2024
1 parent 32dd101 commit 7edecb1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 52 deletions.
8 changes: 6 additions & 2 deletions include/proxy.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once
#include <stdbool.h>
#include <inttypes.h>
#include <stdint.h>
#define URL_MAX_SIZE 50
#define IP_MAX_SIZE 22

typedef struct ProxyAddress {
bool alive;
uint8_t ipAddress[4];
char url[URL_MAX_SIZE];
uint32_t ipAddress;
uint16_t port;
} ProxyAddress;

Expand All @@ -14,6 +17,7 @@ typedef struct ProxyVector {
ProxyAddress *data;
} ProxyVector;

ProxyAddress CreateProxy(uint32_t ipAddress, uint16_t port);
ProxyVector *CreateVector();
void Insert(ProxyVector *vector, ProxyAddress address);
void freeVector(ProxyVector *vector);
128 changes: 79 additions & 49 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <unistd.h>
#include <bits/pthreadtypes.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
Expand All @@ -8,9 +10,7 @@
#include <cjson/cJSON.h>
#include <pthread.h>
#include "proxy.h"
#define URL_MAX_SIZE 31
#define TEST_URL "https://lumtest.com/myip.json"
#define TIMEOUT 2000

#define GREEN_TEXT "\033[0;32m"
#define RED_TEXT "\033[0;31m"
Expand All @@ -25,52 +25,39 @@ ProxyVector *ParseProxies(FILE *file) {

while ((nread = getline(&buffer, &len, file)) != -1) {
buffer[strcspn(buffer, "\n")] = 0;
ProxyAddress *address = malloc(sizeof *address);
int n = 0;

n =sscanf(buffer, "%hhu.%hhu.%hhu.%hhu:%hu",
&address->ipAddress[0],
&address->ipAddress[1],
&address->ipAddress[2],
&address->ipAddress[3],
&address->port);
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);
free(address);
continue;
}

Insert(vector, *address);
Insert(vector, CreateProxy(ipAddress, port));
}

free(buffer);

return vector;
}

char *GetUrl(ProxyAddress *address) {
char *str = malloc(31*sizeof(char));
sprintf(str, "http://%hhu.%hhu.%hhu.%hhu:%hu", address->ipAddress[0], address->ipAddress[1], address->ipAddress[2], address->ipAddress[3], address->port);

return str;
}


typedef struct string {
char *ptr;
size_t len;
} string;

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
return size * nmemb;
}

bool TestProxy(ProxyAddress *address) {
bool IsProxyAlive(ProxyAddress address) {
CURL *curl;
CURLcode res;
char *url = GetUrl(address);
char *url = address.url;

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
Expand All @@ -86,39 +73,58 @@ bool TestProxy(ProxyAddress *address) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

res = curl_easy_perform(curl);
if (res != CURLE_OK) {
curl_easy_cleanup(curl);
curl_global_cleanup();
free(url);
return false;
}

curl_easy_cleanup(curl);
curl_global_cleanup();
free(url);
return true;

return res == CURLE_OK;
}

typedef struct {
ProxyAddress address;
typedef enum { WAITING=0, FAIL, SUCCESS } ProxyState;

pthread_mutex_t lock;

typedef struct { ProxyAddress address;
ProxyState *states;
int thread_id;
} ThreadArgs;

void *TestProxyThread(void *arg) {
ThreadArgs *args = (ThreadArgs *)arg;
char *url = GetUrl(&args->address);
bool success = IsProxyAlive(args->address);

if (TestProxy(&args->address)) {
printf(GREEN_TEXT "●" RESET_TEXT "Proxy %s Success!\n", url);
} else {
printf(RED_TEXT "●" RESET_TEXT "Proxy %s Failure\n", url);
}
pthread_mutex_lock(&lock);
args->states[args->thread_id] = success ? SUCCESS : FAIL;
pthread_mutex_unlock(&lock);

free(url);
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[])
{
if (argc < 2) {
Expand All @@ -134,22 +140,46 @@ int main(int argc, char *argv[])
ProxyVector *list = ParseProxies(file);
fclose(file);

pthread_mutex_init(&lock, NULL);
pthread_t threads[list->len];
ProxyState states[list->len];

for (int i=0; i<list->len; i++) {
ThreadArgs *args = malloc(sizeof(ThreadArgs));
args->address = list->data[i];
args->thread_id = i + 1;
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);
}
}

for (int i=0; i<list->len; i++) {
pthread_join(threads[i], NULL);
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;
}
18 changes: 17 additions & 1 deletion src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@
#include <inttypes.h>
#include "memory.h"
#include "proxy.h"
#define MAX_SIZE 256
#define INITIAL_CAPACITY 10

uint8_t getByte(uint32_t ipAddress, int n) {
int power = 24 - (n << 3);
return (ipAddress >> power) & 0xFF;
}

ProxyAddress CreateProxy(uint32_t ipAddress, uint16_t port) {
ProxyAddress proxy;
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);

return proxy;
}

ProxyVector *CreateVector() {
ProxyVector *vector = safe_malloc(sizeof *vector);
vector->len = 0;
Expand Down

0 comments on commit 7edecb1

Please sign in to comment.