From 0c61a9cc7cb78a2c2d9945b9d2ceb389a7155ca9 Mon Sep 17 00:00:00 2001 From: Gregory <56975502+Trimutex@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:19:01 -0500 Subject: [PATCH] Finished project using VS2022 --- .gitignore | 2 + CMakeSettings.json | 15 ++++ README.md | 9 +++ src/CMakeLists.txt | 2 - src/network.cpp | 171 ---------------------------------------- src/network.hpp | 5 +- src/proxy.cpp | 190 +++++++++++++++++++++++++++++++++++++++++++++ src/web_server.cpp | 188 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 407 insertions(+), 175 deletions(-) create mode 100644 CMakeSettings.json delete mode 100644 src/network.cpp diff --git a/.gitignore b/.gitignore index 3d45f7f..6a0a87a 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,5 @@ build test/web_server .vscode +.vs +out \ No newline at end of file diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 0000000..9204f06 --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "x64-Debug", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ "msvc_x64_x64" ], + "buildRoot": "${projectDir}\\out\\build\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "", + "ctestCommandArgs": "" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 1fd686c..8a3cc58 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,12 @@ The program should now be compiled at ./build/bin/proxy Simply run the program using: build/bin/proxy + +## Windows Notes +Project was built under the CMake directives using Visual Studio 2022 + +No errors occurred and project was built successfully + +web_server.cpp is included so CMake does not break + +proxy.cpp and network.hpp are all that are needed (one .cpp and one .h file) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8d4184..3702c98 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,11 +1,9 @@ add_executable(proxy ./proxy.cpp - ./network.cpp ) add_executable(web_server ./web_server.cpp - ./network.cpp ) if(WIN32) diff --git a/src/network.cpp b/src/network.cpp deleted file mode 100644 index 0752c3c..0000000 --- a/src/network.cpp +++ /dev/null @@ -1,171 +0,0 @@ -#include "network.hpp" -#ifdef UNIX -#include -#endif -#include -#include // Needed for exit() -#include -#include - -// Hazardous globals -char hazardous_contents_CS_01[256] = "password.txt"; -char hazardous_contents_CS_02[256] = "admin.config"; - -void PipeSockets(int sender_s, int receiver_s) { - char in_buf[BUF_SIZE]; // Input buffer for GET resquest - ssize_t buf_len; // Buffer length for file reads - - // Pass GET along from browser to server - std::cout << "Attempting to send data to receiver" << std::endl; - do { - // Receive - buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n'; - if (buf_len == -1) { - std::cout << "ERROR (info) - recv" << std::endl; - close(sender_s); - close(receiver_s); - return; - } - if (buf_len == 0) { continue; } - - // Hazardous check - if ((strstr(in_buf, hazardous_contents_CS_01) != NULL) - || (strstr(in_buf, hazardous_contents_CS_02) != NULL)) { - std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; - strcpy(in_buf, FORBIDDEN_403); - send(sender_s, in_buf, strlen(in_buf), 0); - strcpy(in_buf, MESS_403); - send(sender_s, in_buf, strlen(in_buf), 0); -#ifdef WIN - closesocket(sender_s); - closesocket(receiver_s); -#endif -#ifdef UNIX - close(sender_s); - close(receiver_s); -#endif - return; - } - - // Send - buf_len = send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n'; - } while (buf_len == BUF_SIZE); - std::cout << "Sent data to receiver" << std::endl; - -} - -void ProxySockets(int sender_s, int receiver_s) { - char out_buf[BUF_SIZE]; // Output buffer for HTML response - ssize_t buf_len; // Buffer length for file reads - - // Pass response along from server to browser - sleep(1); - std::cout << "Attempting to send to browser" << std::endl; - do { - // Receive - buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n'; - if (buf_len == -1) { - std::cout << "ERROR (info) - recv -1" << std::endl; - return; - } - - // Hazardous check - if ((strstr(out_buf, hazardous_contents_CS_01) != NULL) - || (strstr(out_buf, hazardous_contents_CS_02) != NULL)) { - std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; - strcpy(out_buf, FORBIDDEN_403); - send(receiver_s, out_buf, strlen(out_buf), 0); - strcpy(out_buf, MESS_403); - send(receiver_s, out_buf, strlen(out_buf), 0); -#ifdef WIN - closesocket(sender_s); - closesocket(receiver_s); -#endif -#ifdef UNIX - close(sender_s); - close(receiver_s); -#endif - return; - } - - // Send - buf_len = send(receiver_s, out_buf, buf_len, 0); - if (buf_len == 96) { break; } - std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n'; - } while ((buf_len == BUF_SIZE) || (buf_len == 40)); - std::cout << "Sent to browser" << std::endl; -#ifdef WIN - closesocket(sender_s); - closesocket(receiver_s); -#endif -#ifdef UNIX - close(sender_s); - close(receiver_s); -#endif -} - -Client::Client(void) { - addressLength = sizeof(address); - address.sin_family = AF_INET; -} - -bool Client::ConnectFrom(int serverFD) { - socketFD = accept(serverFD, (struct sockaddr *)&address, &addressLength); - if (socketFD == -1) { return 1; } - return 0; -} - -bool Client::ConnectTo(int portNumber) { - std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl; - address.sin_port = htons(portNumber); - if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) { - std::cerr << "ERROR (info) - inet_pton" << std::endl; - return 1; - } - socketFD = socket(address.sin_family, SOCK_STREAM, 0); - if (connect(socketFD, (struct sockaddr *)&address, addressLength) == -1) { - std::cerr << "ERROR (info) - connect" << std::endl; - return 1; - } - std::cout << "Connection successful" << std::endl; - return 0; -} - -Server::Server(int portNumber) { - std::cout << "Opening the server" << std::endl; -#ifdef WIN - socketFD = socket(AF_INET, SOCK_STREAM, 0); -#endif -#ifdef UNIX - socketFD = socket(AF_INET, SOCK_STREAM, 0); -#endif - if (socketFD == -1) { - std::cerr << "ERROR - Unable to create socket on server" << std::endl; - exit(1); - } - address.sin_family = AF_INET; - address.sin_port = htons(portNumber); - address.sin_addr.s_addr = htonl(INADDR_ANY); - // Create a socket, fill-in address information, and then bind it - if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) { - std::cerr << "ERROR - Unable to bind socket" << std::endl; - exit(1); - } - // Listen for connections and then accept - listen(socketFD, 100); - std::cout << "Server is now open" << std::endl; -} - -void Server::Close(void) { - std::cout << "Closing the server" << std::endl; -#ifdef WIN - closesocket(socketFD); - WSACleanup(); -#endif -#ifdef UNIX - close(socketFD); -#endif -} diff --git a/src/network.hpp b/src/network.hpp index cb291fd..9349d08 100644 --- a/src/network.hpp +++ b/src/network.hpp @@ -5,12 +5,13 @@ #ifdef UNIX #include +#include #endif #ifdef WIN -#include +#include #include +typedef int socklen_t; #endif -#include #define BUF_SIZE 4096 // Buffer size (big enough for a GET) diff --git a/src/proxy.cpp b/src/proxy.cpp index 1f76144..cef3cd8 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -1,10 +1,21 @@ //----- Include files --------------------------------------------------------- +#include +#include // Needed for exit() #include +#include #include #include #include +#include #include #include "network.hpp" +#ifdef UNIX +#include +#endif + +// Hazardous globals +char hazardous_contents_CS_01[256] = "password.txt"; +char hazardous_contents_CS_02[256] = "admin.config"; int main(void) { @@ -49,3 +60,182 @@ int main(void) return 0; } +void PipeSockets(int sender_s, int receiver_s) { + char in_buf[BUF_SIZE]; // Input buffer for GET resquest +#ifdef UNIX + ssize_t buf_len; // Buffer length for file reads +#endif +#ifdef WIN + SSIZE_T buf_len; // Buffer length for file reads +#endif + + // Pass GET along from browser to server + std::cout << "Attempting to send data to receiver" << std::endl; + do { + // Receive + buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n'; + if (buf_len == -1) { + std::cout << "ERROR (info) - recv" << std::endl; +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + if (buf_len == 0) { continue; } + + // Hazardous check + if ((strstr(in_buf, hazardous_contents_CS_01) != NULL) + || (strstr(in_buf, hazardous_contents_CS_02) != NULL)) { + std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; + strcpy(in_buf, FORBIDDEN_403); + send(sender_s, in_buf, strlen(in_buf), 0); + strcpy(in_buf, MESS_403); + send(sender_s, in_buf, strlen(in_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + + // Send + buf_len = send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n'; + } while (buf_len == BUF_SIZE); + std::cout << "Sent data to receiver" << std::endl; + +} + +void ProxySockets(int sender_s, int receiver_s) { + char out_buf[BUF_SIZE]; // Output buffer for HTML response +#ifdef UNIX + ssize_t buf_len; // Buffer length for file reads +#endif +#ifdef WIN + SSIZE_T buf_len; // Buffer length for file reads +#endif + + // Pass response along from server to browser + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << "Attempting to send to browser" << std::endl; + do { + // Receive + buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n'; + if (buf_len == -1) { + std::cout << "ERROR (info) - recv -1" << std::endl; + return; + } + + // Hazardous check + if ((strstr(out_buf, hazardous_contents_CS_01) != NULL) + || (strstr(out_buf, hazardous_contents_CS_02) != NULL)) { + std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; + strcpy(out_buf, FORBIDDEN_403); + send(receiver_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_403); + send(receiver_s, out_buf, strlen(out_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + + // Send + buf_len = send(receiver_s, out_buf, buf_len, 0); + if (buf_len == 96) { break; } + std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n'; + } while ((buf_len == BUF_SIZE) || (buf_len == 40)); + std::cout << "Sent to browser" << std::endl; +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif +} + +Client::Client(void) { + addressLength = sizeof(address); + address.sin_family = AF_INET; +} + +bool Client::ConnectFrom(int serverFD) { + socketFD = accept(serverFD, (struct sockaddr*)&address, &addressLength); + if (socketFD == -1) { return 1; } + return 0; +} + +bool Client::ConnectTo(int portNumber) { + std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl; + address.sin_port = htons(portNumber); +#ifdef WIN + if (InetPton(address.sin_family, (PCSTR)kWebserverIP, &address.sin_addr) <= 0) { +#endif +#ifdef UNIX + if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) { +#endif + std::cerr << "ERROR (info) - inet_pton" << std::endl; + return 1; + } + socketFD = socket(address.sin_family, SOCK_STREAM, 0); + if (connect(socketFD, (struct sockaddr*)&address, addressLength) == -1) { + std::cerr << "ERROR (info) - connect" << std::endl; + return 1; + } + std::cout << "Connection successful" << std::endl; + return 0; + } + + Server::Server(int portNumber) { + std::cout << "Opening the server" << std::endl; +#ifdef WIN + socketFD = socket(AF_INET, SOCK_STREAM, 0); +#endif +#ifdef UNIX + socketFD = socket(AF_INET, SOCK_STREAM, 0); +#endif + if (socketFD == -1) { + std::cerr << "ERROR - Unable to create socket on server" << std::endl; + exit(1); + } + address.sin_family = AF_INET; + address.sin_port = htons(portNumber); + address.sin_addr.s_addr = htonl(INADDR_ANY); + // Create a socket, fill-in address information, and then bind it + if (bind(socketFD, (struct sockaddr*)&address, sizeof(address)) == -1) { + std::cerr << "ERROR - Unable to bind socket" << std::endl; + exit(1); + } + // Listen for connections and then accept + listen(socketFD, 100); + std::cout << "Server is now open" << std::endl; + } + + void Server::Close(void) { + std::cout << "Closing the server" << std::endl; +#ifdef WIN + closesocket(socketFD); + WSACleanup(); +#endif +#ifdef UNIX + close(socketFD); +#endif + } diff --git a/src/web_server.cpp b/src/web_server.cpp index 1001240..0dee785 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -33,11 +33,20 @@ #include // Needed for strcpy() and strlen() #include // Needed for file i/o constants #include // Needed for file i/o constants +#include // Needed for exit() +#include +#include #include #include +#include #include #include "network.hpp" +// Hazardous globals +char hazardous_contents_CS_01[256] = "password.txt"; +char hazardous_contents_CS_02[256] = "admin.config"; + + /* FOR BSD UNIX/LINUX ---------------------------------------------------- */ #ifdef UNIX #include // @@ -172,3 +181,182 @@ void ClientRequest(int client_s) { #endif } +void PipeSockets(int sender_s, int receiver_s) { + char in_buf[BUF_SIZE]; // Input buffer for GET resquest +#ifdef UNIX + ssize_t buf_len; // Buffer length for file reads +#endif +#ifdef WIN + SSIZE_T buf_len; // Buffer length for file reads +#endif + + // Pass GET along from browser to server + std::cout << "Attempting to send data to receiver" << std::endl; + do { + // Receive + buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n'; + if (buf_len == -1) { + std::cout << "ERROR (info) - recv" << std::endl; +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + if (buf_len == 0) { continue; } + + // Hazardous check + if ((strstr(in_buf, hazardous_contents_CS_01) != NULL) + || (strstr(in_buf, hazardous_contents_CS_02) != NULL)) { + std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; + strcpy(in_buf, FORBIDDEN_403); + send(sender_s, in_buf, strlen(in_buf), 0); + strcpy(in_buf, MESS_403); + send(sender_s, in_buf, strlen(in_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + + // Send + buf_len = send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n'; + } while (buf_len == BUF_SIZE); + std::cout << "Sent data to receiver" << std::endl; + +} + +void ProxySockets(int sender_s, int receiver_s) { + char out_buf[BUF_SIZE]; // Output buffer for HTML response +#ifdef UNIX + ssize_t buf_len; // Buffer length for file reads +#endif +#ifdef WIN + SSIZE_T buf_len; // Buffer length for file reads +#endif + + // Pass response along from server to browser + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << "Attempting to send to browser" << std::endl; + do { + // Receive + buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n'; + if (buf_len == -1) { + std::cout << "ERROR (info) - recv -1" << std::endl; + return; + } + + // Hazardous check + if ((strstr(out_buf, hazardous_contents_CS_01) != NULL) + || (strstr(out_buf, hazardous_contents_CS_02) != NULL)) { + std::cerr << "LOG (warn) - Hazardous contents detected" << std::endl; + strcpy(out_buf, FORBIDDEN_403); + send(receiver_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_403); + send(receiver_s, out_buf, strlen(out_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif + return; + } + + // Send + buf_len = send(receiver_s, out_buf, buf_len, 0); + if (buf_len == 96) { break; } + std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n'; + } while ((buf_len == BUF_SIZE) || (buf_len == 40)); + std::cout << "Sent to browser" << std::endl; +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX + close(sender_s); + close(receiver_s); +#endif +} + +Client::Client(void) { + addressLength = sizeof(address); + address.sin_family = AF_INET; +} + +bool Client::ConnectFrom(int serverFD) { + socketFD = accept(serverFD, (struct sockaddr*)&address, &addressLength); + if (socketFD == -1) { return 1; } + return 0; +} + +bool Client::ConnectTo(int portNumber) { + std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl; + address.sin_port = htons(portNumber); +#ifdef WIN + if (InetPton(address.sin_family, (PCSTR)kWebserverIP, &address.sin_addr) <= 0) { +#endif +#ifdef UNIX + if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) { +#endif + std::cerr << "ERROR (info) - inet_pton" << std::endl; + return 1; + } + socketFD = socket(address.sin_family, SOCK_STREAM, 0); + if (connect(socketFD, (struct sockaddr*)&address, addressLength) == -1) { + std::cerr << "ERROR (info) - connect" << std::endl; + return 1; + } + std::cout << "Connection successful" << std::endl; + return 0; + } + + Server::Server(int portNumber) { + std::cout << "Opening the server" << std::endl; +#ifdef WIN + socketFD = socket(AF_INET, SOCK_STREAM, 0); +#endif +#ifdef UNIX + socketFD = socket(AF_INET, SOCK_STREAM, 0); +#endif + if (socketFD == -1) { + std::cerr << "ERROR - Unable to create socket on server" << std::endl; + exit(1); + } + address.sin_family = AF_INET; + address.sin_port = htons(portNumber); + address.sin_addr.s_addr = htonl(INADDR_ANY); + // Create a socket, fill-in address information, and then bind it + if (bind(socketFD, (struct sockaddr*)&address, sizeof(address)) == -1) { + std::cerr << "ERROR - Unable to bind socket" << std::endl; + exit(1); + } + // Listen for connections and then accept + listen(socketFD, 100); + std::cout << "Server is now open" << std::endl; + } + + void Server::Close(void) { + std::cout << "Closing the server" << std::endl; +#ifdef WIN + closesocket(socketFD); + WSACleanup(); +#endif +#ifdef UNIX + close(socketFD); +#endif + }