Skip to content

Commit f0d0381

Browse files
committed
Fixed upload into account on directupload.eu; Fixed crash after changing login
* Fixed crash after changing login in Login dialog
1 parent 9c5769f commit f0d0381

File tree

6 files changed

+70
-52
lines changed

6 files changed

+70
-52
lines changed

Data/Scripts/directupload.nut

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
const BASE_HOST = "https://www.directupload.eu";
22

33
function Authenticate() {
4+
nm.doGet(BASE_HOST + "/mitglieder/");
5+
if (nm.responseCode() != 200) {
6+
WriteLog("error", "[directupload.eu] Failed to load the login page.");
7+
return ResultCode.Failure;
8+
}
9+
local doc = Document(nm.responseBody());
10+
local inputElement = doc.find("input[name=\"csrf_token\"]");
11+
if (!inputElement.length()) {
12+
WriteLog("error", "[directupload.eu] Failed to obtain CSRF token.");
13+
return ResultCode.Failure;
14+
}
15+
local csrfToken = inputElement.attribute("value");
16+
417
local login = ServerParams.getParam("Login");
518
local pass = ServerParams.getParam("Password");
619
if (login == "" || pass == "") {
7-
return 0;
20+
return ResultCode.Failure;
821
}
922
nm.doGet(BASE_HOST + "/");
1023

1124
nm.setReferer(BASE_HOST + "/");
1225
nm.setUrl(BASE_HOST + "/index.php?mode=user");
1326
nm.addQueryParam("benutzername", login);
27+
nm.addQueryParam("csrf_token", csrfToken);
1428
nm.addQueryParam("passwort", pass);
1529
nm.addQueryParam("everlasting", "");
1630
nm.addQueryParam("anmelden", "Einloggen");
1731
nm.doPost("");
1832

1933
if (nm.responseCode() == 200 || nm.responseCode() == 302) {
20-
return 1;
34+
doc = Document(nm.responseBody());
35+
if (doc.find("div.message.error").length()) {
36+
WriteLog("error", "[directupload.eu] Failed to authenticate. Invalid login or password.");
37+
return ResultCode.Failure;
38+
}
39+
return ResultCode.Success;
40+
} else {
41+
WriteLog("error", "[directupload.eu] Failed to authenticate. Response code: " + nm.responseCode());
2142
}
22-
return 0;
43+
return ResultCode.Failure;
2344
}
2445

2546
function _AnonymousUpload(fileName, options) {

Source/Core/Upload/UploadEngineManager.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
6161
LOG(ERROR) << "No such server " << serverProfile.serverName();
6262
return nullptr;
6363
}
64-
CAbstractUploadEngine* result = nullptr;
64+
std::shared_ptr<CAbstractUploadEngine> result = nullptr;
6565
std::string serverName = serverProfile.serverName();
6666
std::thread::id threadId = std::this_thread::get_id();
6767

6868
BasicSettings* Settings = ServiceLocator::instance()->basicSettings();
6969
ServerSettingsStruct* serverSettings = Settings->getServerSettings(serverProfile, true);
7070
std::string authDataLogin = serverSettings ? serverSettings->authData.Login : std::string();
71+
auto key = std::make_pair(serverName, serverProfile.profileName());
72+
7173
if (ue->UsingPlugin) {
7274
// Try to load Squirrel (.nut) script
7375
result = getPlugin(serverProfile, ue->PluginName);
@@ -80,9 +82,9 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
8082
CAbstractUploadEngine* plugin = nullptr;
8183
auto it = m_plugins.find(threadId);
8284
if (it != m_plugins.end()) {
83-
auto it2 = it->second.find(serverName);
85+
auto it2 = it->second.find(key);
8486
if (it2 != it->second.end()) {
85-
plugin = it2->second;
87+
plugin = it2->second.get();
8688
}
8789
}
8890

@@ -96,48 +98,48 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
9698
if (!ue->Engine.empty()) {
9799
#ifdef IU_ENABLE_MEGANZ
98100
if (ue->Engine == "MegaNz") {
99-
result = new CMegaNzUploadEngine(serverSync, serverSettings, errorCallback);
101+
result = std::make_shared<CMegaNzUploadEngine>(serverSync, serverSettings, errorCallback);
100102
}
101103
#endif
102104
if (!result) {
103105
LOG(ERROR) << "There is no built-in upload engine named '" << ue->Engine << "'.";
104106
return nullptr;
105107
}
106108
} else {
107-
result = new CDefaultUploadEngine(serverSync, errorCallback);
109+
result = std::make_shared<CDefaultUploadEngine>(serverSync, errorCallback);
108110
}
109111
result->setServerSettings(serverSettings);
110112
result->setUploadData(ue);
111113

112-
m_plugins[threadId][serverName] = result;
114+
m_plugins[threadId][key] = result;
113115
}
114116

115117
result->setServerSettings(serverSettings);
116118
result->setUploadData(ue);
117119
result->setOnErrorMessageCallback(std::bind(&IUploadErrorHandler::ErrorMessage,uploadErrorHandler_.get(),std::placeholders::_1));
118-
return result;
120+
return result.get();
119121
}
120122

121123
CScriptUploadEngine* UploadEngineManager::getScriptUploadEngine(ServerProfile& serverProfile)
122124
{
123125
return dynamic_cast<CScriptUploadEngine*>(getUploadEngine(serverProfile));
124126
}
125127

126-
CScriptUploadEngine* UploadEngineManager::getPlugin(ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting) {
128+
std::shared_ptr<CScriptUploadEngine> UploadEngineManager::getPlugin(ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting) {
127129
std::lock_guard<std::mutex> lock(pluginsMutex_);
128130
std::string serverName = serverProfile.serverName();
129131

130132
BasicSettings* basicSettings = ServiceLocator::instance()->basicSettings();
131133
ServerSettingsStruct* params = basicSettings->getServerSettings(serverProfile, true);
132134

133135
std::thread::id threadId = std::this_thread::get_id();
134-
CScriptUploadEngine* plugin = nullptr;
135-
136+
std::shared_ptr<CScriptUploadEngine> plugin;
137+
auto key = std::make_pair(serverName, serverProfile.profileName());
136138
auto it = m_plugins.find(threadId);
137139
if (it != m_plugins.end()) {
138-
auto it2 = it->second.find(serverName);
140+
auto it2 = it->second.find(key);
139141
if (it2 != it->second.end()) {
140-
plugin = dynamic_cast<CScriptUploadEngine*>(it2->second);;
142+
plugin = std::dynamic_pointer_cast<CScriptUploadEngine>(it2->second);
141143
}
142144
}
143145

@@ -155,40 +157,31 @@ CScriptUploadEngine* UploadEngineManager::getPlugin(ServerProfile& serverProfile
155157
}
156158

157159
if (plugin) {
158-
delete plugin;
159-
plugin = 0;
160-
m_plugins[threadId][serverName] = nullptr;
160+
m_plugins[threadId][key] = nullptr;
161161
}
162162
ServerSync* serverSync = getServerSync(serverProfile);
163163
std::string fileName = scriptsDirectory_ + pluginName + ".nut";
164-
CScriptUploadEngine* newPlugin = new CScriptUploadEngine(fileName, serverSync, params, networkClientFactory_,
164+
auto newPlugin = std::make_shared<CScriptUploadEngine>(fileName, serverSync, params, networkClientFactory_,
165165
std::bind(&IUploadErrorHandler::ErrorMessage, uploadErrorHandler_.get(), std::placeholders::_1));
166166

167167
if (newPlugin->isLoaded()) {
168-
m_plugins[threadId][serverName] = newPlugin;
168+
m_plugins[threadId][key] = newPlugin;
169169
return newPlugin;
170170
}
171-
else {
172-
delete newPlugin;
173-
}
171+
174172
return nullptr;
175173
}
176174

177175
void UploadEngineManager::unloadUploadEngines() {
178176
std::lock_guard<std::mutex> lock(pluginsMutex_);
179-
for (auto it = m_plugins.begin(); it != m_plugins.end(); ++it) {
180-
for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
181-
delete it2->second;
182-
}
183-
}
184177
m_plugins.clear();
185178
}
186179

187180

188181
void UploadEngineManager::unloadUploadEngines(const std::string& serverName, const std::string& profileName) {
189182
std::lock_guard<std::mutex> lock(pluginsMutex_);
190183
for (auto &pr: m_plugins) {
191-
pr.second.erase(serverName);
184+
pr.second.erase({ serverName, profileName });
192185
}
193186
}
194187

@@ -202,9 +195,6 @@ void UploadEngineManager::clearThreadData()
202195
std::thread::id threadId = std::this_thread::get_id();
203196
auto it = m_plugins.find(threadId);
204197
if (it != m_plugins.end()) {
205-
for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
206-
delete it2->second;
207-
}
208198
m_plugins.erase(it);
209199
}
210200
}

Source/Core/Upload/UploadEngineManager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
#pragma once
55
#include <thread>
66
#include <mutex>
7+
#include <memory>
8+
#include <unordered_map>
79

10+
#include "Core/Utils/CoreUtils.h"
811
#include "UploadEngine.h"
912

1013
// Forward class declarations
@@ -58,9 +61,9 @@ class UploadEngineManager
5861
*/
5962
void resetFailedAuthorization();
6063
protected:
61-
CScriptUploadEngine* getPlugin(ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting = false);
64+
std::shared_ptr<CScriptUploadEngine> getPlugin(ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting = false);
6265
ServerSync* getServerSync(const ServerProfile& serverProfile);
63-
std::map<std::thread::id, std::map< std::string, CAbstractUploadEngine*>> m_plugins;
66+
std::map<std::thread::id, std::unordered_map<std::pair<std::string, std::string>, std::shared_ptr<CAbstractUploadEngine>>> m_plugins;
6467
std::mutex pluginsMutex_;
6568
std::string scriptsDirectory_;
6669
CUploadEngineList* uploadEngineList_;

Source/Core/Utils/CoreUtils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ make_unique_malloc(std::size_t size) noexcept
8585
static_assert(std::is_trivial_v<T>);
8686
return unique_c_ptr<T>{static_cast<T*>(std::malloc(size))};
8787
}
88+
89+
template <typename T>
90+
inline void hash_combine(std::size_t& seed, const T& val) {
91+
std::hash<T> hasher;
92+
seed ^= hasher(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
93+
}
94+
95+
// taken from https://stackoverflow.com/a/7222201/916549
96+
//
97+
template <typename S, typename T>
98+
struct std::hash<std::pair<S, T>> {
99+
inline size_t operator()(const std::pair<S, T>& val) const {
100+
size_t seed = 0;
101+
hash_combine(seed, val.first);
102+
hash_combine(seed, val.second);
103+
return seed;
104+
}
105+
};
106+
88107
namespace IuCoreUtils
89108
{
90109
// A version of fopen() function which supports utf8 file names

Source/Core/WinServerIconCache.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,10 @@
77
#include <future>
88

99
#include "AbstractServerIconCache.h"
10+
#include "Core/Utils/CoreUtils.h"
1011

1112
class IconBitmapUtils;
1213

13-
template <typename T>
14-
inline void hash_combine(std::size_t& seed, const T& val) {
15-
std::hash<T> hasher;
16-
seed ^= hasher(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
17-
}
18-
19-
// taken from https://stackoverflow.com/a/7222201/916549
20-
//
21-
template <typename S, typename T>
22-
struct std::hash<std::pair<S, T>> {
23-
inline size_t operator()(const std::pair<S, T>& val) const {
24-
size_t seed = 0;
25-
hash_combine(seed, val.first);
26-
hash_combine(seed, val.second);
27-
return seed;
28-
}
29-
};
3014

3115
/*struct PairHash {
3216
template <typename T1, typename T2>

Source/Gui/Dialogs/LoginDlg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ void CLoginDlg::Accept()
283283

284284
if (!accountName_.IsEmpty()) {
285285
// If user has changed account's name, delete account with old name
286-
Settings.ServersSettings[serverName].erase(oldAccountName);
286+
uploadEngineManager_->unloadUploadEngines(serverProfile_.serverName(), serverProfile_.profileName());
287+
Settings.deleteProfile(serverName, oldAccountName);
287288
}
288289
}
289290

0 commit comments

Comments
 (0)