diff --git a/Makefile b/Makefile index fea2aef..1ab4b0a 100644 --- a/Makefile +++ b/Makefile @@ -48,10 +48,10 @@ cmake_force: SHELL = /bin/sh # The CMake executable. -CMAKE_COMMAND = "/home/skingging/Tar Apps/clion-2025.1.1/bin/cmake/linux/x64/bin/cmake" +CMAKE_COMMAND = /usr/bin/cmake # The command to remove a file. -RM = "/home/skingging/Tar Apps/clion-2025.1.1/bin/cmake/linux/x64/bin/cmake" -E rm -f +RM = /usr/bin/cmake -E rm -f # Escaping for special characters. EQUALS = = @@ -78,7 +78,7 @@ edit_cache/fast: edit_cache # Special rule for the target rebuild_cache rebuild_cache: @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..." - "/home/skingging/Tar Apps/clion-2025.1.1/bin/cmake/linux/x64/bin/cmake" --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) + /usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) .PHONY : rebuild_cache # Special rule for the target rebuild_cache diff --git a/include/MetricsExporter.h b/include/MetricsExporter.h index f5fc86d..761ecf9 100644 --- a/include/MetricsExporter.h +++ b/include/MetricsExporter.h @@ -17,18 +17,4 @@ struct MetricsExporter { static void send_data(int client_fd, const string& data); }; -const string HTML_RESPONSE_HEADER_lol = - "HTTP/1.1 200 OK\r\n" - "Content-Type: text/html; charset=utf-8\r\n" - "Transfer-Encoding: chunked\r\n" - //"Cache-control: max-age=12000\r\n" - "Connection: close\r\n\r\n"; - -const string BEGINNING = - "\n\r\n" - "Drip\r\n" - "\r\n"; - -const string END = "\r\n"; - #endif diff --git a/include/Options.h b/include/Options.h new file mode 100644 index 0000000..f90ce2d --- /dev/null +++ b/include/Options.h @@ -0,0 +1,14 @@ +#ifndef OPTIONS_H +#define OPTIONS_H +#include + +using namespace std; + +class Options { + public: + string word_lists; + string images; + string css; +}; + +#endif diff --git a/include/ServerUtils.h b/include/ServerUtils.h index f2b499c..b1f54ef 100644 --- a/include/ServerUtils.h +++ b/include/ServerUtils.h @@ -6,17 +6,17 @@ #include "../include/DataType.h" #include "../include/Track.h" #include "../include/ConcurrentQueue.h" +#include "../include/Options.h" using namespace std; class ServerUtils { public: - static void serve(shared_ptr> cq_track); + static void serve(shared_ptr> cq_track, const Options& options); private: static void process_request(int client_fd); - static void send_header(int client_fd, data_type type); - static void send_chunked_html(int client_fd, size_t hash); - static void send_chunked_css(int client_fd); + static void send_html(int client_fd, size_t hash); + static void send_css(int client_fd); static void send_data(int client_fd, const string& data); static void send_image(int client_fd, const string& path, image_type type); static size_t send_all(int client_fd, const char* data, size_t length); @@ -26,14 +26,12 @@ class ServerUtils { const string HTML_RESPONSE_HEADER = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html; charset=utf-8\r\n" - "Transfer-Encoding: chunked\r\n" "Cache-control: max-age=12000\r\n" "Connection: close\r\n\r\n"; const string CSS_RESPONSE_HEADER = "HTTP/1.1 200 OK\r\n" "Content-Type: text/css; charset=utf-8\r\n" - "Transfer-Encoding: chunked\r\n" "Cache-control: max-age=12000\r\n" "Connection: close\r\n\r\n"; diff --git a/include/WordUtils.h b/include/WordUtils.h index eae4325..578c7ab 100644 --- a/include/WordUtils.h +++ b/include/WordUtils.h @@ -10,7 +10,7 @@ using namespace std; struct WordUtils { static unordered_map> load_data(const string& path); - static vector load_css(const string& path); + static string load_css(const string& path); static vector predict_next_word(const string& input, const unordered_map>& word_frequencies, size_t count); static vector predict_next_word_2(const string& input, const unordered_map>& word_frequencies, size_t count); static string load_file(const string& path); diff --git a/src/MetricsExporter.cpp b/src/MetricsExporter.cpp index 52d8699..60afbaf 100644 --- a/src/MetricsExporter.cpp +++ b/src/MetricsExporter.cpp @@ -34,7 +34,7 @@ void [[noreturn]] MetricsExporter::serve(shared_ptrbegin(); it != crawler_->end(); ++it) { - oss << it->second.links_pressed << ": " << it->second.user_agent << endl; + int total = 0; + + for (const auto &[user_agent, links_pressed]: *crawler_ | views::values) { + total += links_pressed; } - oss << END; + oss << total << "\r\n"; - send_data(client_fd, oss.str()); - send(client_fd, "0\r\n\r\n", 5, 0); + send(client_fd, oss.str().c_str(), oss.str().size(), 0); close(client_fd); } diff --git a/src/ServerUtils.cpp b/src/ServerUtils.cpp index 07e7ef4..8c43040 100644 --- a/src/ServerUtils.cpp +++ b/src/ServerUtils.cpp @@ -2,37 +2,34 @@ #include "../include/WordUtils.h" #include "../include/DataType.h" #include "../include/FileUtils.h" +#include "../include/Options.h" #include #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include -#include #include #include -#include -vector css; +string css; vector images; vector>> all_lists; shared_ptr> tracks; +Options options_; -void [[noreturn]] ServerUtils::serve(shared_ptr> cq_track) { +void [[noreturn]] ServerUtils::serve(shared_ptr> cq_track, const Options& options) { + options_ = options; tracks = std::move(cq_track); - css = WordUtils::load_css("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/content/style.css"); - images = FileUtils::get_image_list("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/content/"); - const vector words = FileUtils::get_wordlists("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/wordlist/"); + css = WordUtils::load_css(options_.css); + images = FileUtils::get_image_list(options_.images); + const vector words = FileUtils::get_wordlists(options_.word_lists); for (const auto& word : words) { all_lists.push_back(WordUtils::load_data(word)); @@ -59,7 +56,7 @@ void [[noreturn]] ServerUtils::serve(shared_ptr> cq_track int client_fd = accept(server_fd, nullptr, nullptr); if (client_fd == -1) continue; - //cerr << client_fd << endl; + thread(process_request, client_fd).detach(); } @@ -90,16 +87,10 @@ void ServerUtils::process_request(const int client_fd) { cerr << "AAAA \n"; } - Track track; - track.Ip = ip; - track.UserAgent = user_agent; - - tracks->push(track); - if (url == "/style.css") { // This sends the header, that instructs how the browser should interpret the data. - send_header(client_fd, CSS); - send_chunked_css(client_fd); + //send_header(client_fd, CSS); + send_css(client_fd); } else if (url == "/favicon.png") { @@ -113,76 +104,71 @@ void ServerUtils::process_request(const int client_fd) { } else { + Track track; + track.Ip = ip; + track.UserAgent = user_agent; + + tracks->push(track); + const unsigned long hash3 = WordUtils::fnv1aHash(url); - send_header(client_fd, HTML); - send_chunked_html(client_fd, hash3); + //send_header(client_fd, HTML); + send_html(client_fd, hash3); } - - // Send final zero-length chunk to end the response - send(client_fd, "0\r\n\r\n", 5, 0); close(client_fd); } -void ServerUtils::send_header(const int client_fd, const data_type type) { - if (type == HTML) [[likely]] { - send_data(client_fd, HTML_RESPONSE_HEADER); - } - - else if (type == CSS) [[unlikely]] { - send_data(client_fd, CSS_RESPONSE_HEADER); - } -} - -void ServerUtils::send_chunked_html(const int client_fd, const size_t hash) { +void ServerUtils::send_html(const int client_fd, const size_t hash) { const string hashes = to_string(hash); unsigned short itr = 0; const unsigned short end = hashes.size(); - send_data(client_fd, HTML_BEGINNING); - send_data(client_fd, HTML_NAV); - send_data(client_fd, HTML_MAIN_1); - minstd_rand generator(hash); - uniform_int_distribution distribution_1(0, end - 1); + uniform_int_distribution distribution(0, end - 1); - const int link = distribution_1(generator); - distribution_1.param(uniform_int_distribution::param_type(0, images.size() - 1)); - const int image = distribution_1(generator); - distribution_1.param(uniform_int_distribution::param_type(0, 8)); - const int l = distribution_1(generator); - distribution_1.param(uniform_int_distribution::param_type(0, end - 2)); - const int img = distribution_1(generator); + const int link = distribution(generator); + distribution.param(uniform_int_distribution::param_type(0, images.size() - 1)); + const int image = distribution(generator); + distribution.param(uniform_int_distribution::param_type(0, 8)); + const int l = distribution(generator); + distribution.param(uniform_int_distribution::param_type(0, end - 2)); + const int img = distribution(generator); + + string html = HTML_RESPONSE_HEADER; + html += HTML_BEGINNING; + html += '\n'; + html += HTML_NAV; + html += '\n'; + html += HTML_MAIN_1; while (itr < end) { - send_data(client_fd, WordUtils::create_tag_2(all_lists[l], hashes[itr])); + html += WordUtils::create_tag_2(all_lists[l], hashes[itr]); + //send_data(client_fd, WordUtils::create_tag_2(all_lists[l], hashes[itr])); if (itr == link) { - send_data(client_fd, WordUtils::create_link(all_lists[l], hash)); + html += WordUtils::create_link(all_lists[l], hash); + //send_data(client_fd, WordUtils::create_link(all_lists[l], hash)); } if (itr == img) { - send_data(client_fd, WordUtils::create_image(images[image])); + html += WordUtils::create_image(images[image]); + //send_data(client_fd, WordUtils::create_image(images[image])); } - //this_thread::sleep_for(chrono::milliseconds(75)); - itr++; } - send_data(client_fd, HTML_MAIN_2); - send_data(client_fd, HTML_FOOTER); - send_data(client_fd, HTML_END); + html += HTML_MAIN_2; + html += HTML_FOOTER; + html += HTML_END; + + send_data(client_fd, html); } -void ServerUtils::send_chunked_css(const int client_fd) { - for (const auto & cs : css) { - send_data(client_fd, cs); - - //this_thread::sleep_for(chrono::milliseconds(25)); - } +void ServerUtils::send_css(const int client_fd) { + send_data(client_fd, CSS_RESPONSE_HEADER + css); } void ServerUtils::send_image(const int client_fd, const string& path, const image_type type) { @@ -225,17 +211,7 @@ size_t ServerUtils::send_all(const int client_fd, const char *data, const size_t } void ServerUtils::send_data(const int client_fd, const string& data) { - char size_hex[20]; // hold 64-bit hex value - snprintf(size_hex, sizeof(size_hex), "%zx", data.size()); - - string message; - message.reserve(strlen(size_hex) + 4 + data.size()); - message += size_hex; - message += "\r\n"; - message += data; - message += "\r\n"; - - send(client_fd, message.c_str(), message.size(), 0); + send(client_fd, data.c_str(), data.size(), 0); } uint32_t ServerUtils::get_ip_2(const int client_fd) { diff --git a/src/TrackerUtils.cpp b/src/TrackerUtils.cpp index a1e6f6d..9286b23 100644 --- a/src/TrackerUtils.cpp +++ b/src/TrackerUtils.cpp @@ -26,14 +26,6 @@ void [[noreturn]] TrackerUtils::track(const shared_ptr>& continue; } - /*vector temp = WordUtils::split_string(track.Ip, IP); - const int ip1 = stoi(temp[0]); - const int ip2 = stoi(temp[1]); - const int ip3 = stoi(temp[2]); - const int ip4 = stoi(temp[3]); - - const uint32_t ip = static_cast(ip1 << 24) | static_cast(ip2 << 16) | static_cast(ip3 << 8) | static_cast(ip4);*/ - (*urls)[track.Ip].links_pressed++; (*urls)[track.Ip].user_agent = track.UserAgent; } diff --git a/src/WordUtils.cpp b/src/WordUtils.cpp index 55b57be..177f3e2 100644 --- a/src/WordUtils.cpp +++ b/src/WordUtils.cpp @@ -26,8 +26,8 @@ unordered_map> WordUtils::load_data(const str return word_frequencies; } -vector WordUtils::load_css(const string& path) { - return split_string(load_file(path), data_type::CSS); +string WordUtils::load_css(const string& path) { + return load_file(path); } vector WordUtils::predict_next_word(const string& input, const unordered_map>& word_frequencies, const size_t count) { diff --git a/src/main.cpp b/src/main.cpp index 6a0bb55..ca8cd15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,40 +2,56 @@ #include #include "../include/FileUtils.h" -#include "../include/WordUtils.h" #include "../include/ServerUtils.h" #include "../include/ConcurrentQueue.h" #include "../include/TrackerUtils.h" #include "../include/Track.h" #include "../include/MetricsExporter.h" +#include "../include/Options.h" using namespace std; -int main(int argc, const char* argv[]) { - /*if (argc > 2) +int main(const int argc, const char* argv[]) { + Options options; + + if (argc > 4) { cout << "Too many arguments"; return 0; } - + if (!FileUtils::fileExists(argv[1])) { cout << "Filepath: " << argv[1] << " doesn't exist."; return 0; - }*/ + } + options.word_lists = argv[1]; - cout << "lol" << endl; + if (!FileUtils::fileExists(argv[2])) + { + cout << "Filepath: " << argv[2] << " doesn't exist."; + return 0; + } + options.css = argv[2]; - auto queue = std::make_shared>(); - auto metrics = std::make_shared>(); + if (!FileUtils::fileExists(argv[3])) + { + cout << "Filepath: " << argv[3] << " doesn't exist."; + return 0; + } + options.images = argv[3]; + + cout << options.word_lists << " " << options.css << " " << options.images << " " << endl; + + auto queue = make_shared>(); + auto metrics = make_shared>(); thread(TrackerUtils::track, queue, metrics).detach(); thread(MetricsExporter::serve, metrics).detach(); - //argv[1] signal(SIGPIPE, SIG_IGN); - ServerUtils::serve(queue); + ServerUtils::serve(queue, options); return 0; } \ No newline at end of file