diff --git a/.idea/Bit.ASICmon.iml b/.idea/Bit.ASICmon.iml index bc2cd87..f08604b 100644 --- a/.idea/Bit.ASICmon.iml +++ b/.idea/Bit.ASICmon.iml @@ -1,8 +1,2 @@ - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 25e2d25..ba64977 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,5 +5,5 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") -add_executable(bitwi2dl main.cpp inc/getosname.hpp inc/json.hpp) -target_link_libraries(bitwi2dl "-lcurl") +add_executable(bitasicmon main.cpp inc/getosname.hpp inc/json.hpp inc/args.hpp inc/macros.hpp inc/colors.hpp) +target_link_libraries(bitasicmon "-lcurl") diff --git a/inc/args.hpp b/inc/args.hpp new file mode 100644 index 0000000..ecf3759 --- /dev/null +++ b/inc/args.hpp @@ -0,0 +1,28 @@ +#include +#include +#include + + +class InputParser { +public: + InputParser (int &argc, char **argv){ + for (int i=1; i < argc; ++i) + this->tokens.push_back(std::string(argv[i])); + } + /// @author iain + const std::string& getCmdOption(const std::string &option) const{ + std::vector::const_iterator itr; + itr = std::find(this->tokens.begin(), this->tokens.end(), option); + if (itr != this->tokens.end() && ++itr != this->tokens.end()){ + return *itr; + } + static const std::string empty_string(""); + return empty_string; + } + /// @author iain + bool cmdOptionExists(const std::string &option) const{ + return std::find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end(); + } +private: + std::vector tokens; +}; \ No newline at end of file diff --git a/inc/colors.hpp b/inc/colors.hpp new file mode 100644 index 0000000..07d8223 --- /dev/null +++ b/inc/colors.hpp @@ -0,0 +1,15 @@ +#ifndef BIT_COLORS_HPP +#define BIT_COLORS_HPP + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" +#define BOLD "\033[1m" + +#endif //BIT_COLORS_HPP diff --git a/inc/getosname.hpp b/inc/getosname.hpp index 7564e28..d5cbf12 100644 --- a/inc/getosname.hpp +++ b/inc/getosname.hpp @@ -1,15 +1,15 @@ -#ifndef BITWI2DL_GETOSNAME_H -#define BITWI2DL_GETOSNAME_H +#ifndef BIT_GETOSNAME_H +#define BIT_GETOSNAME_H #include std::string getOsName() { #ifdef _WIN64 - return "windows 64-bit"; + return "mustdie64"; #elif _WIN32 - return "windows 32-bit"; + return "mustdie32"; #elif __APPLE__ || __MACH__ - return "mac osx"; + return "osx"; #elif __linux__ return "linux"; #elif __FreeBSD__ diff --git a/inc/macros.hpp b/inc/macros.hpp new file mode 100644 index 0000000..e05c363 --- /dev/null +++ b/inc/macros.hpp @@ -0,0 +1,24 @@ +#include "colors.hpp" + + +#ifndef BIT_MACROS_HPP +#define BIT_MACROS_HPP + +#define INFO(...) std::cout , "[INFO] " , __VA_ARGS__ , std::endl; +#define WARN(...) std::cout , YELLOW , "[WARN] " , __VA_ARGS__ , RESET , std::endl; +#define ERR(...) std::cout , RED , "[ERROR] " , __VA_ARGS__ , RESET , std::endl; +#define CRIT(...) std::cout , BOLD , RED , "[CRIT] " , __VA_ARGS__ , RESET , std::endl; + +template +std::ostream& operator,(std::ostream& out, const T& t) { + out << t; + return out; +} + +//overloaded version to handle all those special std::endl and others... +std::ostream& operator,(std::ostream& out, std::ostream&(*f)(std::ostream&)) { + out << f; + return out; +} + +#endif //BIT_MACROS_HPP diff --git a/main.cpp b/main.cpp index 9ea6855..c433db6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,37 +1,44 @@ #include #include -#include +#include #include #include #include #include -#include +#include #include -#include +#include #include #include #include #include +#include +#include #include #include +#include + +#include "inc/args.hpp" +#include "inc/json.hpp" +#include "inc/macros.hpp" using namespace std; -//Client side -int main(int argc, char *argv[]) -{ - //we need 2 things: ip address and port number, in that order - if(argc != 3) - { - cerr << "Usage: ip_address port" << endl; exit(0); - } //grab the IP address and port number - char *serverIp = argv[1]; int port = atoi(argv[2]); - //create a message buffer - char msg[1500]; - //setup a socket and connection tools - struct hostent* host = gethostbyname(serverIp); +int port = 9070; + + +char* getFrom1066(char* ip, char* cmd) { + INFO("------ AVALON 1066 ------") + INFO("IP: ", ip) + INFO("Command: ", cmd) + + int port = 4028; + int msglen = 1024*8; + char* msg = new char[msglen]; + + struct hostent* host = gethostbyname(ip); sockaddr_in sendSockAddr; bzero((char*)&sendSockAddr, sizeof(sendSockAddr)); sendSockAddr.sin_family = AF_INET; @@ -40,45 +47,172 @@ int main(int argc, char *argv[]) int clientSd = socket(AF_INET, SOCK_STREAM, 0); //try to connect... int status = connect(clientSd, (sockaddr*) &sendSockAddr, sizeof(sendSockAddr)); - if(status < 0) - { - cout<<"Error connecting to socket!"<"; - string data; - getline(cin, data); - memset(&msg, 0, sizeof(msg));//clear the buffer - strcpy(msg, data.c_str()); - if(data == "exit") - { - send(clientSd, (char*)&msg, strlen(msg), 0); - break; - } - bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0); - cout << "Awaiting server response..." << endl; - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0); - if(!strcmp(msg, "exit")) - { - cout << "Server has quit the session" << endl; - break; - } - cout << "Server: " << msg << endl; - } - gettimeofday(&end1, NULL); + INFO("Connected to the server!") + + memset(msg, 0, strlen(msg)); + send(clientSd, cmd, sizeof(cmd), 0); + INFO("Awaiting server response..."); + recv(clientSd, msg, msglen, MSG_WAITALL); + INFO("Server: ", msg) close(clientSd); - cout << "********Session********" << endl; - cout << "Bytes written: " << bytesWritten << - " Bytes read: " << bytesRead << endl; - cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec) - << " secs" << endl; - cout << "Connection closed" << endl; + INFO("-------------------------") + + return msg; +} + +char* httpGetURL(char line[], const char symbol[]) { + char *copy = (char*)malloc(strlen(line) + 1); + strcpy(copy, line); + + char *message; + char *token = strtok(copy, symbol); + int current = 0; + + while(token != nullptr) { + token = strtok(nullptr, " "); + if(current == 0) { + message = token; + if (message == nullptr) { + message = (char *) ""; + } + return message; + } + current = current + 1; + } + free(copy); + free(token); + return message; +} + +void httpServer(int port) { + char http_header[25] = "HTTP/1.1 200 Ok\r\n"; + int server_fd, new_socket, pid; + struct sockaddr_in address; + int addrlen = sizeof(address); + + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + CRIT("In sockets") + exit(EXIT_FAILURE); + } + + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(port); + + memset(address.sin_zero, '\0', sizeof address.sin_zero); + + if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { + CRIT("In bind") + close(server_fd); + exit(EXIT_FAILURE); + } + if (listen(server_fd, 10) < 0) { + CRIT("In listen") + exit(EXIT_FAILURE); + } + + INFO("HTTP server started on: http://127.0.0.1:", port) + + while(1) { + printf("\n+++++++ Waiting for new connection ++++++++\n\n"); + if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { + CRIT("In accept") + exit(EXIT_FAILURE); + } + + pid = fork(); + if(pid < 0) { + CRIT("Error on fork") + exit(EXIT_FAILURE); + } + + if(pid == 0) { + char buffer[30000] = {0}; + read(new_socket , buffer, 30000); + char *url = httpGetURL(buffer, " "); + INFO(url) + + char *copy_head = new char[strlen(http_header) + 200]; + strcpy(copy_head, http_header); + + if(strstr(url, "/api/")) + strcat(copy_head, "Content-Type: application/json\r\n\r\n"); + else if(strstr(url, ".js")) + strcat(copy_head, "Content-Type: text/javascript\r\n\r\n"); + else + strcat(copy_head, "Content-Type: text/html\r\n\r\n"); + + write(new_socket, copy_head, strlen(copy_head)); + + + if(strstr(url, ".js")) { + char file_path[512] = "."; + strcat(file_path, url); + + int file = open(file_path, O_RDONLY); + if(file < 0){ + WARN("Cannot Open file path : ", file_path, " with error ", file) + send(new_socket, R"({"error":"FILE_NOT_EXISTS"})", 27, MSG_DONTWAIT); + } + + struct stat stat_buf; + fstat(file, &stat_buf); + sendfile(new_socket, file, nullptr, stat_buf.st_blksize); + } + else if(strcmp(url, "/") == 0) { + char* msg = (char*)R"()"; + send(new_socket, msg, strlen(msg), MSG_DONTWAIT); + } + else { + char* msg = getFrom1066((char*)"128.128.128.56", (char*)"estats"); + send(new_socket, msg, strlen(msg), MSG_DONTWAIT); + } + + close(new_socket); + free(copy_head); + } + else{ + printf(">>>>>>>>>>Parent create child with pid: %d <<<<<<<<<", pid); + close(new_socket); + } + } + close(server_fd); +} + +int main(int argc, char **argv) { + INFO("Bit.ASICmon started!") + + InputParser input(argc, argv); + if(input.cmdOptionExists("-h")) { + INFO("HELP") + + exit(0); + } + if(input.cmdOptionExists("-p")) { + const char* aport = input.getCmdOption("-p").c_str(); + if (strcmp(aport, "") == 0){ + port = atoi(aport); + INFO("Set port: ", port) + } + else { + WARN("Port invalid using default: ", port) + } + } + + getFrom1066((char*)"128.128.128.56", (char*)"version"); + char* msg = getFrom1066((char*)"128.128.128.56", (char*)"estats"); + + regex re(R"(BOOTBY\[([\d\s\w.]+)\])"); + cmatch m; + + regex_search(msg, m, re); + INFO(m[1]) + + httpServer(port); + return 0; }