Permalink
Browse files

Force IPv4 on external IP addresses

  • Loading branch information...
yourWaifu committed Aug 6, 2018
1 parent c2aedb7 commit 158c0d54b1b4f332ab8fca897a8396bde5686e74
@@ -29,6 +29,7 @@ class HttpRequest::Impl final
bool IsValid() const;
void SetCookies(const std::string& cookies);
void UseIPv4();
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
size_t size);
@@ -62,6 +63,11 @@ void HttpRequest::SetCookies(const std::string& cookies)
m_impl->SetCookies(cookies);
}
void HttpRequest::UseIPv4()
{
m_impl->UseIPv4();
}
HttpRequest::Response HttpRequest::Get(const std::string& url, const Headers& headers)
{
return m_impl->Fetch(url, Impl::Method::GET, headers, nullptr, 0);
@@ -136,6 +142,11 @@ void HttpRequest::Impl::SetCookies(const std::string& cookies)
curl_easy_setopt(m_curl.get(), CURLOPT_COOKIE, cookies.c_str());
}
void HttpRequest::Impl::UseIPv4()
{
curl_easy_setopt(m_curl.get(), CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
static size_t CurlWriteCallback(char* data, size_t size, size_t nmemb, void* userdata)
{
auto* buffer = static_cast<std::vector<u8>*>(userdata);
@@ -32,6 +32,7 @@ class HttpRequest final
using Headers = std::map<std::string, std::optional<std::string>>;
void SetCookies(const std::string& cookies);
void UseIPv4();
Response Get(const std::string& url, const Headers& headers = {});
Response Post(const std::string& url, const std::vector<u8>& payload,
const Headers& headers = {});
@@ -4,16 +4,19 @@
#ifdef USE_DISCORD_PRESENCE
#include "DolphinQt/DiscordHandler.h"
#include <iterator>
#include <QApplication>
#include "DolphinQt/DiscordHandler.h"
#include "Common/Thread.h"
#include "UICommon/DiscordPresence.h"
#include "DolphinQt/DiscordJoinRequestDialog.h"
#include "DolphinQt/QtUtils/RunOnObject.h"
DiscordHandler::DiscordHandler(QWidget* parent) : QObject{parent}, m_parent{parent}
{
connect(this, &DiscordHandler::JoinRequest, this, &DiscordHandler::ShowNewJoinRequest);
@@ -41,18 +44,23 @@ void DiscordHandler::Stop()
void DiscordHandler::DiscordJoinRequest(const char* id, const std::string& discord_tag,
const char* avatar)
{
m_request_dialogs.emplace_front(m_parent, id, discord_tag, avatar);
emit DiscordHandler::JoinRequest();
emit DiscordHandler::JoinRequest(id, discord_tag, avatar);
}
void DiscordHandler::DiscordJoin()
{
emit DiscordHandler::Join();
}
void DiscordHandler::ShowNewJoinRequest()
void DiscordHandler::ShowNewJoinRequest(const std::string& id, const std::string& discord_tag,
const std::string& avatar)
{
m_request_dialogs.front().show();
std::lock_guard<std::mutex> lock(m_request_dialogs_mutex);
m_request_dialogs.emplace_front(m_parent, id, discord_tag, avatar);
DiscordJoinRequestDialog& request_dialog = m_request_dialogs.front();
request_dialog.show();
request_dialog.raise();
request_dialog.activateWindow();
QApplication::alert(nullptr, DiscordJoinRequestDialog::s_max_lifetime_seconds * 1000);
}
@@ -64,14 +72,23 @@ void DiscordHandler::Run()
Discord::CallPendingCallbacks();
// close and remove dead requests
for (auto request_dialog = m_request_dialogs.rbegin();
request_dialog != m_request_dialogs.rend(); ++request_dialog)
{
if (std::time(nullptr) < request_dialog->GetCloseTimestamp())
continue;
request_dialog->close();
std::advance(request_dialog, 1);
m_request_dialogs.erase(request_dialog.base());
std::lock_guard<std::mutex> lock(m_request_dialogs_mutex);
for (auto request_dialog = m_request_dialogs.begin();
request_dialog != m_request_dialogs.end();)
{
if (std::time(nullptr) < request_dialog->GetCloseTimestamp())
{
++request_dialog;
continue;
}
RunOnObject(m_parent, [this, &request_dialog] {
request_dialog->close();
request_dialog = m_request_dialogs.erase(request_dialog);
return nullptr;
});
}
}
Common::SleepCurrentThread(1000 * 2);
@@ -4,22 +4,22 @@
#pragma once
// Note using a ifdef around this class causes link issues with qt
#include <list>
#include <mutex>
#include <thread>
#include <QObject>
#include "Common/Flag.h"
#include "DolphinQt/DiscordJoinRequestDialog.h"
#include "UICommon/DiscordPresence.h"
class DiscordJoinRequestDialog;
class DiscordHandler : public QObject, public Discord::Handler
{
Q_OBJECT
#ifdef USE_DISCORD_PRESENCE
public:
explicit DiscordHandler(QWidget* parent);
~DiscordHandler();
@@ -29,15 +29,21 @@ class DiscordHandler : public QObject, public Discord::Handler
void DiscordJoin() override;
void DiscordJoinRequest(const char* id, const std::string& discord_tag,
const char* avatar) override;
void ShowNewJoinRequest();
void ShowNewJoinRequest(const std::string& id, const std::string& discord_tag,
const std::string& avatar);
#endif
signals:
void Join();
void JoinRequest();
void JoinRequest(const std::string id, const std::string discord_tag, const std::string avatar);
#ifdef USE_DISCORD_PRESENCE
private:
void Run();
QWidget* m_parent;
Common::Flag m_stop_requested;
std::thread m_thread;
std::list<DiscordJoinRequestDialog> m_request_dialogs;
std::mutex m_request_dialogs_mutex;
#endif
};
@@ -4,6 +4,8 @@
#ifdef USE_DISCORD_PRESENCE
#include "DolphinQt/DiscordJoinRequestDialog.h"
#include <QGridLayout>
#include <QLabel>
#include <QPixmap>
@@ -14,22 +16,20 @@
#include "Common/HttpRequest.h"
#include "Common/StringUtil.h"
#include "DolphinQt/DiscordJoinRequestDialog.h"
DiscordJoinRequestDialog::DiscordJoinRequestDialog(QWidget* parent, const char* id,
DiscordJoinRequestDialog::DiscordJoinRequestDialog(QWidget* parent, const std::string& id,
const std::string& discord_tag,
const char* avatar)
const std::string& avatar)
: QDialog(parent), m_user_id(id), m_close_timestamp(std::time(nullptr) + s_max_lifetime_seconds)
{
setWindowTitle(tr("Request to Join Your Party"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QPixmap avatar_pixmap;
if (avatar[0] != '\0')
if (!avatar.empty())
{
const std::string avatar_endpoint =
StringFromFormat("https://cdn.discordapp.com/avatars/%s/%s.png", id, avatar);
const std::string avatar_endpoint = StringFromFormat(
"https://cdn.discordapp.com/avatars/%s/%s.png", id.c_str(), avatar.c_str());
Common::HttpRequest request;
Common::HttpRequest::Response response = request.Get(avatar_endpoint);
@@ -38,7 +38,7 @@ DiscordJoinRequestDialog::DiscordJoinRequestDialog(QWidget* parent, const char*
avatar_pixmap.loadFromData(response->data(), static_cast<uint>(response->size()), "png");
}
CreateMainLayout(discord_tag, avatar_pixmap);
CreateLayout(discord_tag, avatar_pixmap);
ConnectWidgets();
}
@@ -47,25 +47,26 @@ std::time_t DiscordJoinRequestDialog::GetCloseTimestamp() const
return m_close_timestamp;
}
void DiscordJoinRequestDialog::CreateMainLayout(const std::string& discord_tag,
const QPixmap& avatar)
void DiscordJoinRequestDialog::CreateLayout(const std::string& discord_tag, const QPixmap& avatar)
{
m_main_layout = new QGridLayout;
m_invite_button = new QPushButton(QString::fromWCharArray(L"\u2714 Invite"));
m_decline_button = new QPushButton(QString::fromWCharArray(L"\u2716 Decline"));
m_invite_button = new QPushButton(tr("\u2714 Invite"));
m_decline_button = new QPushButton(tr("\u2716 Decline"));
m_ignore_button = new QPushButton(tr("Ignore"));
QLabel* text =
new QLabel(tr("%1\nwants to join your party.").arg(QString::fromStdString(discord_tag)));
text->setAlignment(Qt::AlignCenter);
if (!avatar.isNull())
{
QLabel* picture = new QLabel();
picture->setPixmap(avatar);
m_main_layout->addWidget(picture, 1, 0, 1, 3, Qt::AlignHCenter);
}
m_main_layout->addWidget(new QLabel(tr(discord_tag.c_str())), 2, 0, 3, 3, Qt::AlignHCenter);
m_main_layout->addWidget(new QLabel(tr("wants to join your party.")), 4, 0, 4, 3,
Qt::AlignHCenter);
m_main_layout->addWidget(text, 2, 0, 3, 3, Qt::AlignHCenter);
m_main_layout->addWidget(m_invite_button, 8, 0);
m_main_layout->addWidget(m_decline_button, 8, 1);
m_main_layout->addWidget(m_ignore_button, 8, 2);
@@ -75,15 +76,15 @@ void DiscordJoinRequestDialog::CreateMainLayout(const std::string& discord_tag,
void DiscordJoinRequestDialog::ConnectWidgets()
{
connect(m_invite_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_YES); });
connect(m_decline_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_NO); });
connect(m_ignore_button, &QPushButton::clicked, [this] { Reply(DISCORD_REPLY_IGNORE); });
connect(m_invite_button, &QPushButton::pressed, [this] { Reply(DISCORD_REPLY_YES); });
connect(m_decline_button, &QPushButton::pressed, [this] { Reply(DISCORD_REPLY_NO); });
connect(m_ignore_button, &QPushButton::pressed, [this] { Reply(DISCORD_REPLY_IGNORE); });
connect(this, &QDialog::rejected, this, [this] { Reply(DISCORD_REPLY_IGNORE); });
}
void DiscordJoinRequestDialog::Reply(int reply)
{
Discord_Respond(m_user_id, reply);
Discord_Respond(m_user_id.c_str(), reply);
close();
}
@@ -14,14 +14,14 @@ class DiscordJoinRequestDialog : public QDialog
{
Q_OBJECT
public:
explicit DiscordJoinRequestDialog(QWidget* parent, const char* id, const std::string& discord_tag,
const char* avatar);
explicit DiscordJoinRequestDialog(QWidget* parent, const std::string& id,
const std::string& discord_tag, const std::string& avatar);
std::time_t GetCloseTimestamp() const;
static constexpr std::time_t s_max_lifetime_seconds = 30;
private:
void CreateMainLayout(const std::string& discord_tag, const QPixmap& avatar);
void CreateLayout(const std::string& discord_tag, const QPixmap& avatar);
void ConnectWidgets();
void Reply(int reply);
@@ -30,6 +30,6 @@ class DiscordJoinRequestDialog : public QDialog
QPushButton* m_decline_button;
QPushButton* m_ignore_button;
const char* const m_user_id;
const std::string m_user_id;
const std::time_t m_close_timestamp;
};
@@ -640,7 +640,8 @@ void MainWindow::OnStopComplete()
HideRenderWidget();
EnableScreenSaver(true);
#ifdef USE_DISCORD_PRESENCE
Discord::UpdateDiscordPresence();
if (!m_netplay_dialog->isVisible())
Discord::UpdateDiscordPresence();
#endif
SetFullScreenResolution(false);
@@ -794,7 +795,8 @@ void MainWindow::StartGame(std::unique_ptr<BootParameters>&& parameters)
ShowRenderWidget();
#ifdef USE_DISCORD_PRESENCE
Discord::UpdateDiscordPresence();
if (!NetPlay::IsNetPlayRunning())
Discord::UpdateDiscordPresence();
#endif
if (SConfig::GetInstance().bFullscreen)
@@ -1045,7 +1047,6 @@ void MainWindow::NetPlayInit()
{
m_netplay_setup_dialog = new NetPlaySetupDialog(this);
m_netplay_dialog = new NetPlayDialog;
m_netplay_dialog = new NetPlayDialog(this);
#ifdef USE_DISCORD_PRESENCE
m_netplay_discord = new DiscordHandler(this);
#endif
@@ -1180,7 +1181,9 @@ void MainWindow::NetPlayQuit()
{
Settings::Instance().ResetNetPlayClient();
Settings::Instance().ResetNetPlayServer();
#ifdef USE_DISCORD_PRESENCE
Discord::UpdateDiscordPresence();
#endif
}
void MainWindow::EnableScreenSaver(bool enable)
Oops, something went wrong.

0 comments on commit 158c0d5

Please sign in to comment.