diff --git a/src/network.cpp b/src/network.cpp index cc8ab8f..f63dc8d 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -5,43 +5,9 @@ #include #include -void TestSockets(int sender_s, int receiver_s) { - char in_buf[BUF_SIZE]; // Input buffer for GET resquest - char out_buf[BUF_SIZE]; // Output buffer for HTML response - 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 { - buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - pipe packet recv size: " << buf_len << std::endl; - if (buf_len == -1) { - std::cout << "ERROR (info) - recv" << std::endl; - return; - } - if (buf_len == 0) { break; } - buf_len = send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; - } while (buf_len == BUF_SIZE); - std::cout << "Sent data to receiver" << std::endl; - - // Pass response along from server to browser - std::cout << "Attempting to send to browser" << std::endl; - do { - buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; - if (buf_len == -1) { - std::cout << "ERROR (info) - recv" << std::endl; - return; - } - if (buf_len == 0) { break; } - buf_len = send(receiver_s, out_buf, buf_len, 0); - std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; - } while (buf_len == BUF_SIZE); - std::cout << "Sent to browser" << std::endl; - close(sender_s); - close(receiver_s); -} +// 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 @@ -61,6 +27,19 @@ void PipeSockets(int sender_s, int receiver_s) { } 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); + close(sender_s); + close(receiver_s); + return; + } + // Send buf_len = send(receiver_s, in_buf, buf_len, 0); std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n'; @@ -74,6 +53,7 @@ void ProxySockets(int sender_s, int receiver_s) { 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 @@ -84,6 +64,19 @@ void ProxySockets(int sender_s, int receiver_s) { 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); + close(sender_s); + close(receiver_s); + return; + } + // Send buf_len = send(receiver_s, out_buf, buf_len, 0); if (buf_len == 96) { break; } diff --git a/src/network.hpp b/src/network.hpp index bcab3d9..f3c6ac3 100644 --- a/src/network.hpp +++ b/src/network.hpp @@ -9,8 +9,13 @@ #define kProxyPort 9080 #define kWebserverIP "127.0.0.1" #define kWebserverPort 7080 +#define FORBIDDEN_403 "HTTP/1.0 403 Forbidden\nContent-Type:text/html\n\n" +#define MESS_403 "

FORBIDDEN ACCESS

" + +// Hazardous globals +extern char hazardous_contents_CS_01[256]; +extern char hazardous_contents_CS_02[256]; -void TestSockets(int sender_s, int receiver_s); void PipeSockets(int sender_s, int receiver_s); void ProxySockets(int sender_s, int receiver_s); diff --git a/src/proxy.cpp b/src/proxy.cpp index 980c8f8..3e60e83 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -1,18 +1,15 @@ //----- Include files --------------------------------------------------------- #include +#include #include #include #include #include "network.hpp" -//----- HTTP response messages ---------------------------------------------- -#define OK_IMAGE "HTTP/1.0 200 OK\nContent-Type:image/gif\n\n" -#define OK_TEXT "HTTP/1.0 200 OK\nContent-Type:text/html\n\n" -#define NOTOK_404 "HTTP/1.0 404 Not Found\nContent-Type:text/html\n\n" -#define MESS_404 "

FILE NOT FOUND

" - int main(void) { + strcpy(hazardous_contents_CS_01, "password.txt"); + strcpy(hazardous_contents_CS_02, "admin.config"); std::vector> pending_futures; Server proxy(kProxyPort); Client browser; @@ -35,12 +32,6 @@ int main(void) auto newThreadRequest2 = std::async(std::launch::async, ProxySockets, webserver.socketFD, browser.socketFD); pending_futures.push_back(std::move(newThreadRequest1)); pending_futures.push_back(std::move(newThreadRequest2)); - /* - auto newThreadRequest = std::async(std::launch::async, TestSockets, browser.socketFD, webserver.socketFD); - pending_futures.push_back(std::move(newThreadRequest)); - std::cout << "Returned from creating threads, continuing..." << std::endl; - */ - //TestSockets(browser.socketFD, webserver.socketFD); } proxy.Close(); return 0; diff --git a/src/web_server.cpp b/src/web_server.cpp index 595a8d5..1df925c 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -34,7 +34,6 @@ #include // Needed for strcpy() and strlen() #include // Needed for file i/o constants #include // Needed for file i/o constants -#include #include #include #include @@ -123,6 +122,10 @@ void ClientRequest(int client_s) { retcode = recv(client_s, in_buf, BUF_SIZE, 0); if (retcode == -1) { std::cerr << "ERROR (info) - recv" << std::endl; } + if (retcode == 0) { + std::cout << "LOG (info) - Webserver received no data" << std::endl; + return; + } // Handle the GET if there is one (see note #3 in the header) // Parse out the filename from the GET request strtok(in_buf, " "); diff --git a/test/admin.config b/test/admin.config new file mode 100644 index 0000000..aa7d66b --- /dev/null +++ b/test/admin.config @@ -0,0 +1 @@ +data=true diff --git a/test/password.txt b/test/password.txt new file mode 100644 index 0000000..baed26a --- /dev/null +++ b/test/password.txt @@ -0,0 +1,2 @@ +admin +admin123