Added 403 content

This commit is contained in:
TriantaTV 2023-10-03 22:45:14 -05:00
parent b3c970beb4
commit 5f14f2f7dc
4 changed files with 32 additions and 45 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,16 @@ 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(receiver_s, in_buf, strlen(in_buf), 0);
strcpy(in_buf, MESS_403);
send(receiver_s, in_buf, strlen(in_buf), 0);
}
// Send
buf_len = send(receiver_s, in_buf, buf_len, 0);
std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n';
@ -84,6 +60,16 @@ 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);
}
// 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;

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>