#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; 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; sendSockAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list)); sendSockAddr.sin_port = htons(port); int clientSd = socket(AF_INET, SOCK_STREAM, 0); //try to connect... int status = connect(clientSd, (sockaddr*) &sendSockAddr, sizeof(sendSockAddr)); if(status < 0) { ERR("Error connecting to socket!") return (char*)""; } 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); 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; }