diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 47d0962..0000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -save -.env -src/*.o -hds/*.o diff --git a/README.md b/README.md deleted file mode 100644 index a47063b..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# apic - -Trying to learn c diff --git a/apic b/apic deleted file mode 100755 index 4a36ea2..0000000 Binary files a/apic and /dev/null differ diff --git a/makefile b/makefile deleted file mode 100644 index 5b4afaa..0000000 --- a/makefile +++ /dev/null @@ -1,19 +0,0 @@ -## Simple C Makefile - -SRC = ./src/main.c -NAME = apic -CC = gcc -RM = rm -f -OBJ = $(SRC:.c=.o) -CFLAGS = -O2 -W -Wall -Wextra -Werror -CFLAGS += -I./hds/ -all : $(NAME) -$(NAME) : $(OBJ) - $(CC) -o $(NAME) $(OBJ) -lcurl -clean : - $(RM) $(OBJ) -fclean : clean - $(RM) $(NAME) -re : fclean all -.PHONY : all clean fclean re - diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 60874b6..0000000 --- a/src/main.c +++ /dev/null @@ -1,179 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define BUFFER_SIZE 256 - -#define SAVE_FILE "./save" - -struct url_data { - size_t size; - char* data; -}; -char *UID; -char *PLATFORM; -char *HOSTNAME; -char *USERNAME; -const char *URL; -const char *URL_TOR; - -size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) { - size_t index = data->size; - size_t n = (size * nmemb); - char* tmp; - data->size += (size * nmemb); - fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb); - tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */ - if(tmp) { - data->data = tmp; - } else { - if(data->data) { - free(data->data); - } - fprintf(stderr, "Failed to allocate memory.\n"); - return 0; - } - memcpy((data->data + index), ptr, n); - data->data[data->size] = '\0'; - return size * nmemb; -} - -char* getUniqueID() -{ - char *data = malloc(BUFFER_SIZE); - FILE *f; - f = fopen(SAVE_FILE,"a+"); - if(f != NULL) fseek(f, 0, SEEK_END); - if(!(UID!= NULL && UID[0] == '\0')) return UID; - if(ftell(f) == 0) - { - fclose(f); - srand(time(0)); - int random = rand(); - f = fopen(SAVE_FILE,"w"); - fprintf(f,"%s_%d\n",USERNAME,random); - sprintf(data,"%s_%d",USERNAME,random); - fclose(f); - strcpy(UID,data); - return data; - } else { - fclose(f); - f = fopen(SAVE_FILE,"r"); - fscanf(f,"%s",data); - fclose(f); - strcpy(UID,data); - return data; - } -} - -char* getRequest(char *path) -{ - CURL *curl; - CURLcode res; - struct url_data data; - data.size = 0; - data.data = malloc(4096); - if(data.data == NULL) { - fprintf(stderr, "Failed to allocate memory.\n"); - return NULL; - } - curl = curl_easy_init(); - if(curl) { - char url[BUFFER_SIZE]; - strcpy(url, URL); - strcat(url, path); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_PROXY, URL_TOR); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); - res = curl_easy_perform(curl); - if(res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - curl_easy_cleanup(curl); - } - return data.data; -} - -char* postRequest(char *path, char *post_data) -{ - CURL *curl; - CURLcode res; - struct url_data data; - data.size = 0; - data.data = malloc(4096); - if(data.data == NULL) { - fprintf(stderr, "Failed to allocate memory.\n"); - return NULL; - } - curl = curl_easy_init(); - if(curl) { - char url[BUFFER_SIZE]; - strcpy(url, URL); - strcat(url, path); - printf("%s",post_data); - struct curl_slist *headers = NULL; - headers = curl_slist_append(headers, "Accept: application/json"); - headers = curl_slist_append(headers, "Content-Type: application/json"); - headers = curl_slist_append(headers, "charset: utf-8"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_PROXY, URL_TOR); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); - res = curl_easy_perform(curl); - if(res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - curl_easy_cleanup(curl); - } - return data.data; -} - -char* sayhello() -{ - char *data = malloc(BUFFER_SIZE); - char *path = malloc(BUFFER_SIZE); - char *post_data = malloc(BUFFER_SIZE); - sprintf(path, "/api/%s/hello", getUniqueID()); - sprintf(post_data, - "{\"platform\": \"%s\",\ - \"hostname\": \"%s\",\ - \"username\": \"%s\"}", - PLATFORM,HOSTNAME,USERNAME); - data = postRequest(path, post_data); - return data; -} - -void setup() -{ - UID = malloc(BUFFER_SIZE); - PLATFORM = malloc(BUFFER_SIZE); - HOSTNAME = malloc(BUFFER_SIZE); - USERNAME = malloc(BUFFER_SIZE); - URL = getenv("URL"); - URL_TOR = getenv("URL_TOR"); - #if __APPLE__ - PLATFORM = "MAC OS"; - #elif _WIN32 - PLATFORM = "WINDOWS"; - #elif __LINUX__ - PLATFORM = "LINUX"; - #else - PLATFORM = "NONE"; - #endif - gethostname(HOSTNAME, BUFFER_SIZE); - getlogin_r(USERNAME, BUFFER_SIZE); -} - -int main() -{ - setup(); - char *data = malloc(BUFFER_SIZE); - data = sayhello(); - printf("%s", data); -}