Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changed user-agent, more logging, removed infinite loop #883

Merged
merged 20 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/dpp/httpsclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@
#include <vector>
#include <variant>
#include <dpp/sslclient.h>
#include <dpp/version.h>
#include <dpp/stringops.h>

namespace dpp {

static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, "
+ to_hex(DPP_VERSION_MAJOR, false) + "."
+ to_hex(DPP_VERSION_MINOR, false) + "."
+ to_hex(DPP_VERSION_PATCH, false) + ")";

static inline constexpr const char* DISCORD_HOST = "https://discord.com";

/**
* @brief HTTP connection status
Expand Down
24 changes: 24 additions & 0 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,35 @@ bool discord_voice_client::is_playing() {
void discord_voice_client::thread_run()
{
utility::set_thread_name(std::string("vc/") + std::to_string(server_id));

size_t times_looped = 0;
time_t last_loop_time = time(nullptr);

do {
bool error = false;
ssl_client::read_loop();
ssl_client::close();

time_t current_time = time(nullptr);
/* Here, we check if it's been longer than 3 seconds since the previous loop,
* this gives us time to see if it's an actual disconnect, or an error.
* This will prevent us from looping too much, meaning error codes do not cause an infinite loop.
*/
if(current_time - last_loop_time >= 3)
times_looped = 0;

/* This does mean we'll always have times_looped at a minimum of 1, this is intended. */
times_looped++;
/* If we've looped 5 or more times, abort the loop. */
if(times_looped >= 5) {
log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket.");
break;
}

last_loop_time = current_time;

if (!terminating) {
log(dpp::ll_debug, "Attempting to reconnect the websocket...");
do {
try {
ssl_client::connect();
Expand Down
4 changes: 0 additions & 4 deletions src/dpp/queues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
#include <dpp/cluster.h>
#include <dpp/httpsclient.h>
#include <dpp/stringops.h>
#include <dpp/version.h>

namespace dpp {

static std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " + std::to_string(DPP_VERSION_MAJOR) + "." + std::to_string(DPP_VERSION_MINOR) + "." + std::to_string(DPP_VERSION_PATCH) + ")";
static const char* DISCORD_HOST = "https://discord.com";

http_request::http_request(const std::string &_endpoint, const std::string &_parameters, http_completion_event completion, const std::string &_postdata, http_method _method, const std::string &audit_reason, const std::string &filename, const std::string &filecontent, const std::string &filemimetype)
: complete_handler(completion), completed(false), non_discord(false), endpoint(_endpoint), parameters(_parameters), postdata(_postdata), method(_method), reason(audit_reason), mimetype("application/json"), waiting(false)
{
Expand Down
21 changes: 21 additions & 0 deletions src/dpp/sslclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,27 @@ void ssl_client::read_loop()
case SSL_ERROR_NONE:
/* Data received, add it to the buffer */
if (r > 0) {
const std::string data(server_to_client_buffer, r);
/* Split the data into an array for every line. */
const std::vector<std::string> data_lines = utility::tokenize(data);
/* Get the first line as we always know that's the HTTP response. */
const std::string http_reponse(data_lines[0]);

/* Does the first line begin with a http code? */
if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos) {
/* Now let's split the first line by every space, meaning we can check the actual HTTP code. */
const std::vector<std::string> line_split_by_space = utility::tokenize(data_lines[0], " ");

/* We need to make sure there's at least 3 elements in line_split_by_space. */
if(line_split_by_space.size() >= 3) {
const int http_code = std::stoi(line_split_by_space[1]);

/* If the http_code isn't 204, 101, or 200, log it. */
if(http_code != 204 && http_code != 101 && http_code != 200)
log(ll_warning, "Received unhandled code: " + http_reponse);
}
}

buffer.append(server_to_client_buffer, r);
if (!this->handle_buffer(buffer)) {
return;
Expand Down
1 change: 0 additions & 1 deletion src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <thread>
#include <functional>
#include <chrono>
#include <ctime>
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
#include <algorithm>
#include <fstream>
#include <streambuf>
Expand Down
3 changes: 2 additions & 1 deletion src/dpp/wsclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <fstream>
#include <dpp/wsclient.h>
#include <dpp/utility.h>
#include <dpp/httpsclient.h>

namespace dpp {

Expand Down Expand Up @@ -52,7 +53,7 @@ void websocket_client::connect()
"GET " + this->path + " HTTP/1.1\r\n"
"Host: " + this->hostname + "\r\n"
"pragma: no-cache\r\n"
"User-Agent: DPP/0.1\r\n"
"User-Agent: " + http_version + "\r\n"
"Upgrade: WebSocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: " + this->key + "\r\n"
Expand Down