Finished project on Linux #3

Merged
Trianta merged 3 commits from dev into master 2023-10-04 00:07:48 -05:00
6 changed files with 46 additions and 51 deletions

View File

@ -5,43 +5,9 @@
#include <cstring>
#include <iostream>
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; }

View File

@ -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 "<html><body><h1>FORBIDDEN ACCESS</h1></body></html>"
// 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);

View File

@ -1,18 +1,15 @@
//----- Include files ---------------------------------------------------------
#include <algorithm>
#include <cstring>
#include <future>
#include <iostream>
#include <vector>
#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 "<html><body><h1>FILE NOT FOUND</h1></body></html>"
int main(void)
{
strcpy(hazardous_contents_CS_01, "password.txt");
strcpy(hazardous_contents_CS_02, "admin.config");
std::vector<std::future<void>> 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;

View File

@ -34,7 +34,6 @@
#include <string.h> // Needed for strcpy() and strlen()
#include <fcntl.h> // Needed for file i/o constants
#include <sys/stat.h> // Needed for file i/o constants
#include <errno.h>
#include <future>
#include <iostream>
#include <vector>
@ -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, " ");

1
test/admin.config Normal file
View File

@ -0,0 +1 @@
data=true

2
test/password.txt Normal file
View File

@ -0,0 +1,2 @@
admin
admin123