From 2a4c78c152714b061db795dbdd1860048c7b8092 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Wed, 1 Dec 2021 13:56:41 -0600 Subject: [PATCH] Changing language to go It is being changed to go in order to practice the language before I start another project. --- .gitignore | 56 --------- CMakeLists.txt | 16 --- src/download.c | 218 -------------------------------- src/go.mod | 3 + src/rhosts.c | 226 ---------------------------------- src/{download.h => rhosts.go} | 13 +- src/rhosts.h | 69 ----------- src/rhostsConfig.h.in | 1 - 8 files changed, 4 insertions(+), 598 deletions(-) delete mode 100644 CMakeLists.txt delete mode 100644 src/download.c create mode 100644 src/go.mod delete mode 100644 src/rhosts.c rename src/{download.h => rhosts.go} (70%) delete mode 100644 src/rhosts.h delete mode 100644 src/rhostsConfig.h.in diff --git a/.gitignore b/.gitignore index 9c09028..0e90706 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -# ---> Build -build/* - # ---> Vim # Swap [._]*.s[a-v][a-z] @@ -22,57 +19,4 @@ tags # Persistent undo [._]*.un~ -# ---> C -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 947fd78..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(rhosts VERSION 0.0.0 LANGUAGES C) -message(" Project Binary dir: '${PROJECT_SOURCE_DIR}'") -include_directories(${PROJECT_SOURCE_DIR}) -add_executable(rhosts src/rhosts.c src/download.c) -target_link_libraries(rhosts curl) -target_include_directories(rhosts PUBLIC "${PROJECT_BINARY_DIR}") -configure_file(src/rhostsConfig.h.in config.h) -set(CMAKE_INSTALL_PREFIX "/usr/local") -install(TARGETS rhosts DESTINATION bin) -install(FILES "${PROJECT_SOURCE_DIR}/src/systemd/rhosts.service" - DESTINATION rhosts/systemd) -install(FILES "${PROJECT_SOURCE_DIR}/src/systemd/rhosts.timer" - DESTINATION rhosts/systemd) -install(FILES "${PROJECT_SOURCE_DIR}/src/systemd/rhosts.path" - DESTINATION rhosts/systemd) diff --git a/src/download.c b/src/download.c deleted file mode 100644 index 9e2dc26..0000000 --- a/src/download.c +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright 2021 Justin Reichardt - * - * This file is part of rhosts. - * - * rhosts is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * rhosts is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rhosts. If not, see . - */ - - -#ifndef DOWNLOAD_HEADER -#include "download.h" -#endif - -int clean_download(); -int skip_to_next_line(FILE **fp); - -int download_entries(struct entry **entries){ - int i = (*entries)[0].entrytype; - int rc = 0; - FILE *tmpf; - tmpf = fopen(TMPLOCATION,"a"); - if (tmpf == NULL){ - return 1; - } - FILE *tmpdf; - tmpdf = fopen(TMPDOWNLOADLOCATION,"w+"); - if (tmpdf == NULL){ - printf("Failed to open %s\n",TMPDOWNLOADLOCATION); - fclose(tmpf); - return 1; - } - - - for (;i >0 ; i--){ - if ((*entries)[i].entrytype == CONTENTTYPE_DOWNLOAD){ - printf("Download: %s\n",(*entries)[i].entry); - rc = fprintf(tmpf, "# rhosts download - %s", \ - (*entries)[i].entry); - fflush(tmpf); - if (rc == EOF){ - printf("Failed to write to %s\n", \ - TMPLOCATION); - fclose(tmpdf); - fclose(tmpf); - return 1; - } - download_libcurl((*entries)[i].entry); - } - } - - fclose(tmpf); - fclose(tmpdf); - remove(TMPDOWNLOADLOCATION); - return 0; -} -int download_libcurl(char *e){ - CURL *curl; - CURLcode res; - - curl_global_init(CURL_GLOBAL_DEFAULT); - curl = curl_easy_init(); - if(curl){ - // Add the url - curl_easy_setopt(curl, CURLOPT_URL, e); - // Skip cert check - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - // Ignore if cert has a different HostName - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - // Send what is recieved to function - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse_download); - - // Download the file - res = curl_easy_perform(curl); - if(res != CURLE_OK){ - printf("Failed to download the file: %d\n", res); - fflush(stdin); - copy_old_download(e); - return 1; - } - clean_download(); - curl_easy_cleanup(curl); - } - curl_global_cleanup(); - remove(TMPDOWNLOADLOCATION); - - return 0; -} -int parse_download(char *buff, size_t size, size_t nmemb){ - FILE *tmpdf; - int i=0; - int rc=0; - tmpdf = fopen(TMPDOWNLOADLOCATION, "a"); - for(i=0;i MAXSTRSIZE){ - printf("String too long when cleaning download: %s\n",buff); - return 1; - } - if (b!=EOF) // Later needs to check for NULL - strncat(buff,&c,1); - if (buffsize==2){ - if (buff[0]=='#' || buff[0]=='\n'){ - skip_to_next_line(&tmpdf); - buff[0] = '\0'; - buffsize=1; - } - } - if (c == '\n' || b == EOF){ - fputs(buff,tmpf); - buff[0]='\0'; - buffsize=1; - } - }while (b!=EOF); - fflush(tmpf); - fclose(tmpf); - fclose(tmpdf); - return 0; -} -int skip_to_next_line(FILE **fp){ - char c; - int b; - do{ - b = fgetc(*fp); - c = (char)b; - }while(c != '\n' && b != EOF); - return 0; -} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..32c6cb7 --- /dev/null +++ b/src/go.mod @@ -0,0 +1,3 @@ +module jbreich/rhosts + +go 1.17 diff --git a/src/rhosts.c b/src/rhosts.c deleted file mode 100644 index 7e3ae1b..0000000 --- a/src/rhosts.c +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright 2021 Justin Reichardt - * - * This file is part of rhosts. - * - * rhosts is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * rhosts is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rhosts. If not, see . - */ - - -/* rhosts - Program used to maintain a blocklist within a hostfile */ -#ifndef RHOSTS_HEADER -#include "rhosts.h" -#endif - -int main(int argc, char *argv[]){ - struct entry *entries; - int rc =0; - - printf("version: %s\n",rhosts_VERSION); - - rc = parse_config(&entries); - if (rc != 0){ - printf("%d - parse_config failed",rc); - return rc; - } - rc = preserve_static_entries(); - if (rc != 0){ - printf("%d - preserve_static_entries failed",rc); - return rc; - } - rc = download_entries(&entries); - if (rc != 0){ - printf("%d - download_entries failed",rc); - return rc; - } - rc = add_site_entries(&entries); - if (rc != 0){ - printf("%d - download_entries failed",rc); - return rc; - } - rc = copy_tmp_to_hosts(); - if (rc != 0){ - printf("%d - failed to copy to hosts file",rc); - return rc; - } - return 0; -} -int parse_config(struct entry **entries){ - int rc=0; - - FILE *configfile; - configfile = fopen(CONFIGFILE, "r+"); - if (configfile == NULL){return 1;} - - *entries = malloc(sizeof(struct entry)); - if (entries == NULL){return 1;} - entries[0]->entrytype = 0; - int *j = NULL; // A shorter reference to how many entries - j = &(*entries)[0].entrytype; - - char c='\0'; - int b=0; - char buff[MAXSTRSIZE]; - buff[0]='\0'; - short int valtyp = CONTENTTYPE_BLANK; - - // Loop through config file - do{ - b = fgetc(configfile); - c = (char)b; - // Detect if a comment - if (strncmp(buff, "#",(long unsigned int)1) == 0 && \ - valtyp == CONTENTTYPE_BLANK){ - while (c != '\n' && b != EOF){c =fgetc(configfile);} - } - // Detect end of value type string - if (c == '=' && valtyp == CONTENTTYPE_BLANK){ - valtyp = determine_config_entry_value(buff); - if (valtyp == CONTENTTYPE_ERROR){ - return 1; - } - buff[0]='\0'; - } - - // Detect end of entry - else if ((c == '\n' || b == EOF) \ - && valtyp != CONTENTTYPE_BLANK){ - (*entries)[0].entrytype++; - *entries = (struct entry *)realloc(*entries,\ - (*j + 1) * sizeof(struct entry)); - if (*entries == NULL){return 1;} - j = &(*entries)[0].entrytype; - (*entries)[*j].entrytype=valtyp; - strcpy((*entries)[*j].entry,buff); - buff[0] = '\0'; - valtyp = CONTENTTYPE_BLANK; - } - else if (c == '\n' || b == EOF){ - buff[0] = '\0'; - } - else{ - strncat(buff, &c, 1); - } - }while (b != EOF); - - rc = fclose(configfile); - if (rc != 0){return 1;} - return 0; -} -short int determine_config_entry_value(char *buff){ - if (strncmp(buff,"#", 1) == 0){return CONTENTTYPE_COMMENT;} - else if (strcmp(buff,"site") == 0){return CONTENTTYPE_SITE;} - else if (strcmp(buff,"download") == 0){return CONTENTTYPE_DOWNLOAD;} - else {return CONTENTTYPE_ERROR;} -} -int preserve_static_entries(){ - FILE *hostsf; - FILE *tmpf; - hostsf = fopen(HOSTSLOCATION, "r"); - if (hostsf == NULL){return 1;} - tmpf = fopen(TMPLOCATION,"w"); - if (tmpf == NULL){ - fclose(hostsf); - return 1; - } - char buff[MAXSTRSIZE]; - char c = EOF; - int b = 0; - int rc = 0; - - printf("Static hosts are:\n"); - do{ - b = fgetc(hostsf); - c = (char)b; - strncat(buff, &c, 1); - if (strncmp(buff, "# rhosts begin", 14) == 0){b = EOF;} - if (c == '\n'){ - rc = fputs(buff, tmpf); - if (rc == EOF){ - fclose(hostsf); - fclose(tmpf); - return 1; - } - printf("%s",buff); - buff[0] = '\0'; - } - }while ( b != EOF); - rc = fputs("# rhosts begin\n", tmpf); - if (rc == EOF){ - fclose(hostsf); - fclose(tmpf); - return 1; - } - - - - fclose(hostsf); - fclose(tmpf); - return 0; -} -int add_site_entries(struct entry **entries){ - int i = (*entries)[0].entrytype; - int rc = 0; - FILE *tmpf; - tmpf = fopen(TMPLOCATION,"a"); - if (tmpf == NULL){ - return 1; - } - - - rc = fputs("# rhosts - static begin\n", tmpf); - if (rc == EOF){ - printf("Failed to write to tmp file\n"); - fclose(tmpf); - return 1; - } - for (;i >0 ; i--){ - if ((*entries)[i].entrytype == CONTENTTYPE_SITE){ - fprintf(tmpf, "0.0.0.0 %s\n:: %s\n", \ - (*entries)[i].entry, \ - (*entries)[i].entry); - - } - - } - rc = fputs("# rhosts - static end\n# rhosts end\n", tmpf); - if (rc == EOF){ - printf("Failed to write to tmp file\n"); - fclose(tmpf); - return 1; - } - - fclose(tmpf); - return 0; -} -int copy_tmp_to_hosts(){ - FILE *tmpf; - tmpf = fopen(TMPLOCATION,"r"); - if (tmpf == NULL) - return 1; - FILE *hostsf; - hostsf = fopen(HOSTSLOCATION, "w"); - if (hostsf == NULL){ - printf("Failed to open %s\n",HOSTSLOCATION); - fclose(tmpf); - return 1; - } - int b; - - for(b = fgetc(tmpf);b != EOF;b = fgetc(tmpf)){ - fputc((char)b,hostsf); - } - remove(TMPLOCATION); - return 0; -} diff --git a/src/download.h b/src/rhosts.go similarity index 70% rename from src/download.h rename to src/rhosts.go index 1cbf708..01a5c6e 100644 --- a/src/download.h +++ b/src/rhosts.go @@ -18,15 +18,4 @@ */ -#ifndef RHOSTS_HEADER -#include "rhosts.h" -#endif -#ifndef DOWNLOAD_HEADER -#define DOWNLOAD_HEADER -#include - -int download_entries(struct entry **entries); -int download_libcurl(char *e); -int parse_download(char *buff, size_t size, size_t nmemb); -int copy_old_download(char *url); -#endif +/* rhosts - Program used to maintain a blocklist within a hostfile */ diff --git a/src/rhosts.h b/src/rhosts.h deleted file mode 100644 index cae96a8..0000000 --- a/src/rhosts.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2021 Justin Reichardt - * - * This file is part of rhosts. - * - * rhosts is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * rhosts is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rhosts. If not, see . - */ - - -#ifndef RHOSTS_HEADER -#define RHOSTS_HEADER - -#include "config.h" - -#include -#include -#include -#include - -#ifdef _WIN64 -#define TMPLOCATION "/tmp/rhosts" -#define TMPDOWNLOADLOCATION "/tmp/rhostsdownload" -#define HOSTSLOCATION "/Windows/System32/drivers/etc/hosts" -#define CONFIGFILE "/ProgramData/rhosts/rhosts.cfg" -#elif __APPLE__ -#define TMPLOCATION "/tmp/" -#elif __linux__ -#define TMPLOCATION "/tmp/rhosts" -#define TMPDOWNLOADLOCATION "/tmp/rhostsdownload" -#define HOSTSLOCATION "/etc/hosts" -#define CONFIGFILE "/etc/rhosts/rhosts.cfg" -#else -#endif - -#define STRUCTS_HEADER -#define MAXSTRSIZE 500 -// entry types -#define CONTENTTYPE_ERROR 5 -#define CONTENTTYPE_BLANK 0 -#define CONTENTTYPE_SITE 1 -#define CONTENTTYPE_DOWNLOAD 2 -#define CONTENTTYPE_COMMENT 3 - -struct entry{ - int entrytype; - char entry[MAXSTRSIZE]; -}; - -int parse_config(struct entry **entries); - -short int determine_config_entry_value(char *buff); -int preserve_static_entries(); -int add_site_entries(struct entry **entries); -int copy_tmp_to_hosts(); -#endif -#ifndef DOWNLOAD_HEADER -#include "download.h" -#endif diff --git a/src/rhostsConfig.h.in b/src/rhostsConfig.h.in deleted file mode 100644 index e68951c..0000000 --- a/src/rhostsConfig.h.in +++ /dev/null @@ -1 +0,0 @@ -#define rhosts_VERSION "@rhosts_VERSION@"