This repository was archived by the owner on Feb 9, 2021. It is now read-only.
forked from hypfvieh/foo_subsonic
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleHttpClientConfigurator.h
More file actions
executable file
·191 lines (165 loc) · 5.02 KB
/
SimpleHttpClientConfigurator.h
File metadata and controls
executable file
·191 lines (165 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once
#include <stdio.h>
#include <random>
#include <windows.h>
#include <Wincrypt.h>
#include "foo_subsonic.h"
#include "simplehttpclient.h"
#include "logindlg.h"
class SimpleHttpClientConfigurator {
public:
/*
Checks if all required preferences were configured before, and if they are "correct".
*/
static BOOL check_preferences() {
if (Preferences::connect_url_data.is_empty()) {
console::error("URL cannot be empty!");
return FALSE;
}
else { // validate url
pfc::string url = Preferences::connect_url_data;
url = url.toLower();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
console::error("Url has to start with http:// or https://");
return FALSE;
}
if (Preferences::proxy_settings_custom_data) {
url = Preferences::proxy_url_data;
url = url.toLower();
if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("socks://") && !url.startsWith("socks5://")) {
console::error("Proxy-Address has to start with http:// or socks5://");
return FALSE;
}
}
}
return TRUE;
}
/*
Create SimpleHttpClientConfig using parameters found in foobar config.
*/
static BOOL createSimpleHttpClientConfigFromPreferences(SimpleHttpClientConfig &cliConfig) {
if (!SimpleHttpClientConfigurator::check_preferences()) {
return FALSE;
}
if (Preferences::proxy_settings_custom_data) {
pfc::stringcvt::string_wide_from_utf8 proxyHost(Preferences::proxy_url_data);
cliConfig.setProxyUrl(proxyHost);
cliConfig.useProxy = PROXY_CUSTOM;
}
else if (Preferences::proxy_settings_system_data) {
cliConfig.useProxy = PROXY_SYSTEM;
}
else {
cliConfig.useProxy = PROXY_OFF;
}
cliConfig.timeoutInSec = Preferences::connect_timeout_data;
cliConfig.disableCertVerify = Preferences::check_selfsignedcerts_data;
return TRUE;
}
/*
Turn char to Hex-representation.
*/
static char to_hex(char c) {
return c < 0xa ? '0' + c : 'a' - 0xa + c;
}
/*
Encode a URL (which means mask all none ASCII characters).
*/
static pfc::string8 url_encode(const char *in) {
pfc::string8 out;
out.prealloc(strlen(in) * 3 + 1);
for (register const char *tmp = in; *tmp != '\0'; tmp++) {
auto c = static_cast<unsigned char>(*tmp);
if (isalnum(c)) {
out.add_char(c);
}
else if (isspace(c)) {
out.add_char('+');
}
else {
out.add_char('%');
out.add_char(SimpleHttpClientConfigurator::to_hex(c >> 4));
out.add_char(SimpleHttpClientConfigurator::to_hex(c % 16));
}
}
return out;
}
/*
Generate a random alphanumeric string (salt) of length @length
*/
static std::string random_salt(size_t length)
{
auto randchar = []() -> char
{
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
/*
Generate the MD5 hash for a given @input string
*/
static std::string MD5(std::string input)
{
HCRYPTPROV CryptProv;
HCRYPTHASH CryptHash;
BYTE BytesHash[33];
DWORD dwHashLen;
std::string result;
if (CryptAcquireContext(&CryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if (CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash))
{
if (CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0))
{
if (CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0))
{
result.clear();
std::string hexcharset = "0123456789abcdef";
for (int j = 0; j < 16; j++)
{
result += hexcharset.substr(((BytesHash[j] >> 4) & 0xF), 1);
result += hexcharset.substr(((BytesHash[j]) & 0x0F), 1);
}
}
}
}
}
CryptDestroyHash(CryptHash);
CryptReleaseContext(CryptProv, 0);
return result;
}
/*
Build the request URL required for subsonic.
This will build the URL using the configured server and add the required parameters like client (c), user (u), salt (s) and auth token (t).
*/
static pfc::string8 buildRequestUrl(const char* restMethod, pfc::string8 urlparams) {
if (Preferences::password_data.is_empty() || Preferences::username_data.is_empty()) {
LoginDialog *dlg = new LoginDialog();
dlg->DoModal(core_api::get_main_window(), LPARAM(0));
}
std::string pass = Preferences::password_data.c_str();
std::string salt = random_salt(12);
std::string token = MD5(pass + salt);
pfc::string8 url;
url << Preferences::connect_url_data;
url << "/rest/";
url << restMethod;
url << ".view";
url << "?v=1.13.0";
url << "&c=" << COMPONENT_SHORT_NAME;
url << "&u=" << url_encode(Preferences::username_data);
url << "&s=" << salt.c_str();
url << "&t=" << token.c_str();
if (strlen(urlparams) > 0) {
url << "&" << urlparams;
}
return url;
}
};