Use spinbox special value to represent "Use any available port"
[qBittorrent.git] / src / webui / api / appcontroller.cpp
blob201892d7e3607c3970a47c7198c886f1b83f7a1e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
5 * Copyright (C) 2006-2012 Ishan Arora <ishan@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #include "appcontroller.h"
33 #include <algorithm>
35 #include <QCoreApplication>
36 #include <QDebug>
37 #include <QJsonArray>
38 #include <QJsonDocument>
39 #include <QJsonObject>
40 #include <QNetworkInterface>
41 #include <QRegularExpression>
42 #include <QStringList>
43 #include <QTimer>
44 #include <QTranslator>
46 #include "base/bittorrent/session.h"
47 #include "base/global.h"
48 #include "base/net/portforwarder.h"
49 #include "base/net/proxyconfigurationmanager.h"
50 #include "base/preferences.h"
51 #include "base/rss/rss_autodownloader.h"
52 #include "base/rss/rss_session.h"
53 #include "base/torrentfileguard.h"
54 #include "base/torrentfileswatcher.h"
55 #include "base/utils/fs.h"
56 #include "base/utils/misc.h"
57 #include "base/utils/net.h"
58 #include "base/utils/password.h"
59 #include "base/utils/string.h"
60 #include "base/version.h"
61 #include "../webapplication.h"
63 void AppController::webapiVersionAction()
65 setResult(static_cast<QString>(API_VERSION));
68 void AppController::versionAction()
70 setResult(QBT_VERSION);
73 void AppController::buildInfoAction()
75 const QJsonObject versions =
77 {"qt", QT_VERSION_STR},
78 {"libtorrent", Utils::Misc::libtorrentVersionString()},
79 {"boost", Utils::Misc::boostVersionString()},
80 {"openssl", Utils::Misc::opensslVersionString()},
81 {"zlib", Utils::Misc::zlibVersionString()},
82 {"bitness", (QT_POINTER_SIZE * 8)}
84 setResult(versions);
87 void AppController::shutdownAction()
89 qDebug() << "Shutdown request from Web UI";
91 // Special case handling for shutdown, we
92 // need to reply to the Web UI before
93 // actually shutting down.
94 QTimer::singleShot(100, qApp, &QCoreApplication::quit);
97 void AppController::preferencesAction()
99 const Preferences *const pref = Preferences::instance();
100 const auto *session = BitTorrent::Session::instance();
101 QJsonObject data;
103 // Downloads
104 // When adding a torrent
105 data["torrent_content_layout"] = Utils::String::fromEnum(session->torrentContentLayout());
106 data["start_paused_enabled"] = session->isAddTorrentPaused();
107 data["auto_delete_mode"] = static_cast<int>(TorrentFileGuard::autoDeleteMode());
108 data["preallocate_all"] = session->isPreallocationEnabled();
109 data["incomplete_files_ext"] = session->isAppendExtensionEnabled();
110 // Saving Management
111 data["auto_tmm_enabled"] = !session->isAutoTMMDisabledByDefault();
112 data["torrent_changed_tmm_enabled"] = !session->isDisableAutoTMMWhenCategoryChanged();
113 data["save_path_changed_tmm_enabled"] = !session->isDisableAutoTMMWhenDefaultSavePathChanged();
114 data["category_changed_tmm_enabled"] = !session->isDisableAutoTMMWhenCategorySavePathChanged();
115 data["save_path"] = Utils::Fs::toNativePath(session->defaultSavePath());
116 data["temp_path_enabled"] = session->isTempPathEnabled();
117 data["temp_path"] = Utils::Fs::toNativePath(session->tempPath());
118 data["export_dir"] = Utils::Fs::toNativePath(session->torrentExportDirectory());
119 data["export_dir_fin"] = Utils::Fs::toNativePath(session->finishedTorrentExportDirectory());
121 // TODO: The following code is deprecated. Delete it once replaced by updated API method.
122 // === BEGIN DEPRECATED CODE === //
123 TorrentFilesWatcher *fsWatcher = TorrentFilesWatcher::instance();
124 const QHash<QString, TorrentFilesWatcher::WatchedFolderOptions> watchedFolders = fsWatcher->folders();
125 QJsonObject nativeDirs;
126 for (auto i = watchedFolders.cbegin(); i != watchedFolders.cend(); ++i)
128 const QString watchedFolder = i.key();
129 const BitTorrent::AddTorrentParams params = i.value().addTorrentParams;
130 if (params.savePath.isEmpty())
131 nativeDirs.insert(Utils::Fs::toNativePath(watchedFolder), 1);
132 else if (params.savePath == watchedFolder)
133 nativeDirs.insert(Utils::Fs::toNativePath(watchedFolder), 0);
134 else
135 nativeDirs.insert(Utils::Fs::toNativePath(watchedFolder), Utils::Fs::toNativePath(params.savePath));
137 data["scan_dirs"] = nativeDirs;
138 // === END DEPRECATED CODE === //
140 // Email notification upon download completion
141 data["mail_notification_enabled"] = pref->isMailNotificationEnabled();
142 data["mail_notification_sender"] = pref->getMailNotificationSender();
143 data["mail_notification_email"] = pref->getMailNotificationEmail();
144 data["mail_notification_smtp"] = pref->getMailNotificationSMTP();
145 data["mail_notification_ssl_enabled"] = pref->getMailNotificationSMTPSSL();
146 data["mail_notification_auth_enabled"] = pref->getMailNotificationSMTPAuth();
147 data["mail_notification_username"] = pref->getMailNotificationSMTPUsername();
148 data["mail_notification_password"] = pref->getMailNotificationSMTPPassword();
149 // Run an external program on torrent completion
150 data["autorun_enabled"] = pref->isAutoRunEnabled();
151 data["autorun_program"] = Utils::Fs::toNativePath(pref->getAutoRunProgram());
153 // Connection
154 // Listening Port
155 data["listen_port"] = session->port();
156 data["random_port"] = (session->port() == 0); // deprecated
157 data["upnp"] = Net::PortForwarder::instance()->isEnabled();
158 // Connections Limits
159 data["max_connec"] = session->maxConnections();
160 data["max_connec_per_torrent"] = session->maxConnectionsPerTorrent();
161 data["max_uploads"] = session->maxUploads();
162 data["max_uploads_per_torrent"] = session->maxUploadsPerTorrent();
164 // Proxy Server
165 const auto *proxyManager = Net::ProxyConfigurationManager::instance();
166 Net::ProxyConfiguration proxyConf = proxyManager->proxyConfiguration();
167 data["proxy_type"] = static_cast<int>(proxyConf.type);
168 data["proxy_ip"] = proxyConf.ip;
169 data["proxy_port"] = proxyConf.port;
170 data["proxy_auth_enabled"] = proxyManager->isAuthenticationRequired(); // deprecated
171 data["proxy_username"] = proxyConf.username;
172 data["proxy_password"] = proxyConf.password;
174 data["proxy_peer_connections"] = session->isProxyPeerConnectionsEnabled();
175 data["proxy_torrents_only"] = proxyManager->isProxyOnlyForTorrents();
177 // IP Filtering
178 data["ip_filter_enabled"] = session->isIPFilteringEnabled();
179 data["ip_filter_path"] = Utils::Fs::toNativePath(session->IPFilterFile());
180 data["ip_filter_trackers"] = session->isTrackerFilteringEnabled();
181 data["banned_IPs"] = session->bannedIPs().join('\n');
183 // Speed
184 // Global Rate Limits
185 data["dl_limit"] = session->globalDownloadSpeedLimit();
186 data["up_limit"] = session->globalUploadSpeedLimit();
187 data["alt_dl_limit"] = session->altGlobalDownloadSpeedLimit();
188 data["alt_up_limit"] = session->altGlobalUploadSpeedLimit();
189 data["bittorrent_protocol"] = static_cast<int>(session->btProtocol());
190 data["limit_utp_rate"] = session->isUTPRateLimited();
191 data["limit_tcp_overhead"] = session->includeOverheadInLimits();
192 data["limit_lan_peers"] = !session->ignoreLimitsOnLAN();
193 // Scheduling
194 data["scheduler_enabled"] = session->isBandwidthSchedulerEnabled();
195 const QTime start_time = pref->getSchedulerStartTime();
196 data["schedule_from_hour"] = start_time.hour();
197 data["schedule_from_min"] = start_time.minute();
198 const QTime end_time = pref->getSchedulerEndTime();
199 data["schedule_to_hour"] = end_time.hour();
200 data["schedule_to_min"] = end_time.minute();
201 data["scheduler_days"] = pref->getSchedulerDays();
203 // Bittorrent
204 // Privacy
205 data["dht"] = session->isDHTEnabled();
206 data["pex"] = session->isPeXEnabled();
207 data["lsd"] = session->isLSDEnabled();
208 data["encryption"] = session->encryption();
209 data["anonymous_mode"] = session->isAnonymousModeEnabled();
210 // Torrent Queueing
211 data["queueing_enabled"] = session->isQueueingSystemEnabled();
212 data["max_active_downloads"] = session->maxActiveDownloads();
213 data["max_active_torrents"] = session->maxActiveTorrents();
214 data["max_active_uploads"] = session->maxActiveUploads();
215 data["dont_count_slow_torrents"] = session->ignoreSlowTorrentsForQueueing();
216 data["slow_torrent_dl_rate_threshold"] = session->downloadRateForSlowTorrents();
217 data["slow_torrent_ul_rate_threshold"] = session->uploadRateForSlowTorrents();
218 data["slow_torrent_inactive_timer"] = session->slowTorrentsInactivityTimer();
219 // Share Ratio Limiting
220 data["max_ratio_enabled"] = (session->globalMaxRatio() >= 0.);
221 data["max_ratio"] = session->globalMaxRatio();
222 data["max_seeding_time_enabled"] = (session->globalMaxSeedingMinutes() >= 0.);
223 data["max_seeding_time"] = session->globalMaxSeedingMinutes();
224 data["max_ratio_act"] = session->maxRatioAction();
225 // Add trackers
226 data["add_trackers_enabled"] = session->isAddTrackersEnabled();
227 data["add_trackers"] = session->additionalTrackers();
229 // Web UI
230 // Language
231 data["locale"] = pref->getLocale();
232 // HTTP Server
233 data["web_ui_domain_list"] = pref->getServerDomains();
234 data["web_ui_address"] = pref->getWebUiAddress();
235 data["web_ui_port"] = pref->getWebUiPort();
236 data["web_ui_upnp"] = pref->useUPnPForWebUIPort();
237 data["use_https"] = pref->isWebUiHttpsEnabled();
238 data["web_ui_https_cert_path"] = pref->getWebUIHttpsCertificatePath();
239 data["web_ui_https_key_path"] = pref->getWebUIHttpsKeyPath();
240 // Authentication
241 data["web_ui_username"] = pref->getWebUiUsername();
242 data["bypass_local_auth"] = !pref->isWebUiLocalAuthEnabled();
243 data["bypass_auth_subnet_whitelist_enabled"] = pref->isWebUiAuthSubnetWhitelistEnabled();
244 QStringList authSubnetWhitelistStringList;
245 for (const Utils::Net::Subnet &subnet : asConst(pref->getWebUiAuthSubnetWhitelist()))
246 authSubnetWhitelistStringList << Utils::Net::subnetToString(subnet);
247 data["bypass_auth_subnet_whitelist"] = authSubnetWhitelistStringList.join('\n');
248 data["web_ui_max_auth_fail_count"] = pref->getWebUIMaxAuthFailCount();
249 data["web_ui_ban_duration"] = static_cast<int>(pref->getWebUIBanDuration().count());
250 data["web_ui_session_timeout"] = pref->getWebUISessionTimeout();
251 // Use alternative Web UI
252 data["alternative_webui_enabled"] = pref->isAltWebUiEnabled();
253 data["alternative_webui_path"] = pref->getWebUiRootFolder();
254 // Security
255 data["web_ui_clickjacking_protection_enabled"] = pref->isWebUiClickjackingProtectionEnabled();
256 data["web_ui_csrf_protection_enabled"] = pref->isWebUiCSRFProtectionEnabled();
257 data["web_ui_secure_cookie_enabled"] = pref->isWebUiSecureCookieEnabled();
258 data["web_ui_host_header_validation_enabled"] = pref->isWebUIHostHeaderValidationEnabled();
259 // Custom HTTP headers
260 data["web_ui_use_custom_http_headers_enabled"] = pref->isWebUICustomHTTPHeadersEnabled();
261 data["web_ui_custom_http_headers"] = pref->getWebUICustomHTTPHeaders();
262 // Reverse proxy
263 data["web_ui_reverse_proxy_enabled"] = pref->isWebUIReverseProxySupportEnabled();
264 data["web_ui_reverse_proxies_list"] = pref->getWebUITrustedReverseProxiesList();
265 // Update my dynamic domain name
266 data["dyndns_enabled"] = pref->isDynDNSEnabled();
267 data["dyndns_service"] = pref->getDynDNSService();
268 data["dyndns_username"] = pref->getDynDNSUsername();
269 data["dyndns_password"] = pref->getDynDNSPassword();
270 data["dyndns_domain"] = pref->getDynDomainName();
272 // RSS settings
273 data["rss_refresh_interval"] = RSS::Session::instance()->refreshInterval();
274 data["rss_max_articles_per_feed"] = RSS::Session::instance()->maxArticlesPerFeed();
275 data["rss_processing_enabled"] = RSS::Session::instance()->isProcessingEnabled();
276 data["rss_auto_downloading_enabled"] = RSS::AutoDownloader::instance()->isProcessingEnabled();
277 data["rss_download_repack_proper_episodes"] = RSS::AutoDownloader::instance()->downloadRepacks();
278 data["rss_smart_episode_filters"] = RSS::AutoDownloader::instance()->smartEpisodeFilters().join('\n');
280 // Advanced settings
281 // qBitorrent preferences
282 // Current network interface
283 data["current_network_interface"] = session->networkInterface();
284 // Current network interface address
285 data["current_interface_address"] = BitTorrent::Session::instance()->networkInterfaceAddress();
286 // Save resume data interval
287 data["save_resume_data_interval"] = session->saveResumeDataInterval();
288 // Recheck completed torrents
289 data["recheck_completed_torrents"] = pref->recheckTorrentsOnCompletion();
290 // Resolve peer countries
291 data["resolve_peer_countries"] = pref->resolvePeerCountries();
292 // Reannounce to all trackers when ip/port changed
293 data["reannounce_when_address_changed"] = session->isReannounceWhenAddressChangedEnabled();
295 // libtorrent preferences
296 // Async IO threads
297 data["async_io_threads"] = session->asyncIOThreads();
298 // Hashing threads
299 data["hashing_threads"] = session->hashingThreads();
300 // File pool size
301 data["file_pool_size"] = session->filePoolSize();
302 // Checking memory usage
303 data["checking_memory_use"] = session->checkingMemUsage();
304 // Disk write cache
305 data["disk_cache"] = session->diskCacheSize();
306 data["disk_cache_ttl"] = session->diskCacheTTL();
307 // Enable OS cache
308 data["enable_os_cache"] = session->useOSCache();
309 // Coalesce reads & writes
310 data["enable_coalesce_read_write"] = session->isCoalesceReadWriteEnabled();
311 // Piece Extent Affinity
312 data["enable_piece_extent_affinity"] = session->usePieceExtentAffinity();
313 // Suggest mode
314 data["enable_upload_suggestions"] = session->isSuggestModeEnabled();
315 // Send buffer watermark
316 data["send_buffer_watermark"] = session->sendBufferWatermark();
317 data["send_buffer_low_watermark"] = session->sendBufferLowWatermark();
318 data["send_buffer_watermark_factor"] = session->sendBufferWatermarkFactor();
319 // Outgoing connections per second
320 data["connection_speed"] = session->connectionSpeed();
321 // Socket listen backlog size
322 data["socket_backlog_size"] = session->socketBacklogSize();
323 // Outgoing ports
324 data["outgoing_ports_min"] = session->outgoingPortsMin();
325 data["outgoing_ports_max"] = session->outgoingPortsMax();
326 // UPnP lease duration
327 data["upnp_lease_duration"] = session->UPnPLeaseDuration();
328 // Type of service
329 data["peer_tos"] = session->peerToS();
330 // uTP-TCP mixed mode
331 data["utp_tcp_mixed_mode"] = static_cast<int>(session->utpMixedMode());
332 // Support internationalized domain name (IDN)
333 data["idn_support_enabled"] = session->isIDNSupportEnabled();
334 // Multiple connections per IP
335 data["enable_multi_connections_from_same_ip"] = session->multiConnectionsPerIpEnabled();
336 // Validate HTTPS tracker certificate
337 data["validate_https_tracker_certificate"] = session->validateHTTPSTrackerCertificate();
338 // Disallow connection to peers on privileged ports
339 data["block_peers_on_privileged_ports"] = session->blockPeersOnPrivilegedPorts();
340 // Embedded tracker
341 data["enable_embedded_tracker"] = session->isTrackerEnabled();
342 data["embedded_tracker_port"] = pref->getTrackerPort();
343 // Choking algorithm
344 data["upload_slots_behavior"] = static_cast<int>(session->chokingAlgorithm());
345 // Seed choking algorithm
346 data["upload_choking_algorithm"] = static_cast<int>(session->seedChokingAlgorithm());
347 // Announce
348 data["announce_to_all_trackers"] = session->announceToAllTrackers();
349 data["announce_to_all_tiers"] = session->announceToAllTiers();
350 data["announce_ip"] = session->announceIP();
351 data["max_concurrent_http_announces"] = session->maxConcurrentHTTPAnnounces();
352 data["stop_tracker_timeout"] = session->stopTrackerTimeout();
353 // Peer Turnover
354 data["peer_turnover"] = session->peerTurnover();
355 data["peer_turnover_cutoff"] = session->peerTurnoverCutoff();
356 data["peer_turnover_interval"] = session->peerTurnoverInterval();
358 setResult(data);
361 void AppController::setPreferencesAction()
363 requireParams({"json"});
365 Preferences *const pref = Preferences::instance();
366 auto session = BitTorrent::Session::instance();
367 const QVariantHash m = QJsonDocument::fromJson(params()["json"].toUtf8()).toVariant().toHash();
369 QVariantHash::ConstIterator it;
370 const auto hasKey = [&it, &m](const char *key) -> bool
372 it = m.find(QLatin1String(key));
373 return (it != m.constEnd());
376 // Downloads
377 // When adding a torrent
378 if (hasKey("torrent_content_layout"))
379 session->setTorrentContentLayout(Utils::String::toEnum(it.value().toString(), BitTorrent::TorrentContentLayout::Original));
380 if (hasKey("start_paused_enabled"))
381 session->setAddTorrentPaused(it.value().toBool());
382 if (hasKey("auto_delete_mode"))
383 TorrentFileGuard::setAutoDeleteMode(static_cast<TorrentFileGuard::AutoDeleteMode>(it.value().toInt()));
385 if (hasKey("preallocate_all"))
386 session->setPreallocationEnabled(it.value().toBool());
387 if (hasKey("incomplete_files_ext"))
388 session->setAppendExtensionEnabled(it.value().toBool());
390 // Saving Management
391 if (hasKey("auto_tmm_enabled"))
392 session->setAutoTMMDisabledByDefault(!it.value().toBool());
393 if (hasKey("torrent_changed_tmm_enabled"))
394 session->setDisableAutoTMMWhenCategoryChanged(!it.value().toBool());
395 if (hasKey("save_path_changed_tmm_enabled"))
396 session->setDisableAutoTMMWhenDefaultSavePathChanged(!it.value().toBool());
397 if (hasKey("category_changed_tmm_enabled"))
398 session->setDisableAutoTMMWhenCategorySavePathChanged(!it.value().toBool());
399 if (hasKey("save_path"))
400 session->setDefaultSavePath(it.value().toString());
401 if (hasKey("temp_path_enabled"))
402 session->setTempPathEnabled(it.value().toBool());
403 if (hasKey("temp_path"))
404 session->setTempPath(it.value().toString());
405 if (hasKey("export_dir"))
406 session->setTorrentExportDirectory(it.value().toString());
407 if (hasKey("export_dir_fin"))
408 session->setFinishedTorrentExportDirectory(it.value().toString());
410 // TODO: The following code is deprecated. Delete it once replaced by updated API method.
411 // === BEGIN DEPRECATED CODE === //
412 if (hasKey("scan_dirs"))
414 QStringList scanDirs;
415 TorrentFilesWatcher *fsWatcher = TorrentFilesWatcher::instance();
416 const QStringList oldScanDirs = fsWatcher->folders().keys();
417 const QVariantHash nativeDirs = it.value().toHash();
418 for (auto i = nativeDirs.cbegin(); i != nativeDirs.cend(); ++i)
422 const QString watchedFolder = TorrentFilesWatcher::makeCleanPath(i.key());
423 TorrentFilesWatcher::WatchedFolderOptions options = fsWatcher->folders().value(watchedFolder);
424 BitTorrent::AddTorrentParams &params = options.addTorrentParams;
426 bool isInt = false;
427 const int intVal = i.value().toInt(&isInt);
428 if (isInt)
430 if (intVal == 0)
432 params.savePath = watchedFolder;
433 params.useAutoTMM = false;
436 else
438 const QString customSavePath = i.value().toString();
439 params.savePath = customSavePath;
440 params.useAutoTMM = false;
443 fsWatcher->setWatchedFolder(watchedFolder, options);
444 scanDirs.append(watchedFolder);
446 catch (...)
451 // Update deleted folders
452 for (const QString &path : oldScanDirs)
454 if (!scanDirs.contains(path))
455 fsWatcher->removeWatchedFolder(path);
458 // === END DEPRECATED CODE === //
460 // Email notification upon download completion
461 if (hasKey("mail_notification_enabled"))
462 pref->setMailNotificationEnabled(it.value().toBool());
463 if (hasKey("mail_notification_sender"))
464 pref->setMailNotificationSender(it.value().toString());
465 if (hasKey("mail_notification_email"))
466 pref->setMailNotificationEmail(it.value().toString());
467 if (hasKey("mail_notification_smtp"))
468 pref->setMailNotificationSMTP(it.value().toString());
469 if (hasKey("mail_notification_ssl_enabled"))
470 pref->setMailNotificationSMTPSSL(it.value().toBool());
471 if (hasKey("mail_notification_auth_enabled"))
472 pref->setMailNotificationSMTPAuth(it.value().toBool());
473 if (hasKey("mail_notification_username"))
474 pref->setMailNotificationSMTPUsername(it.value().toString());
475 if (hasKey("mail_notification_password"))
476 pref->setMailNotificationSMTPPassword(it.value().toString());
477 // Run an external program on torrent completion
478 if (hasKey("autorun_enabled"))
479 pref->setAutoRunEnabled(it.value().toBool());
480 if (hasKey("autorun_program"))
481 pref->setAutoRunProgram(it.value().toString());
483 // Connection
484 // Listening Port
485 if (hasKey("random_port") && it.value().toBool()) // deprecated
487 session->setPort(0);
489 else if (hasKey("listen_port"))
491 session->setPort(it.value().toInt());
493 if (hasKey("upnp"))
494 Net::PortForwarder::instance()->setEnabled(it.value().toBool());
495 // Connections Limits
496 if (hasKey("max_connec"))
497 session->setMaxConnections(it.value().toInt());
498 if (hasKey("max_connec_per_torrent"))
499 session->setMaxConnectionsPerTorrent(it.value().toInt());
500 if (hasKey("max_uploads"))
501 session->setMaxUploads(it.value().toInt());
502 if (hasKey("max_uploads_per_torrent"))
503 session->setMaxUploadsPerTorrent(it.value().toInt());
505 // Proxy Server
506 auto proxyManager = Net::ProxyConfigurationManager::instance();
507 Net::ProxyConfiguration proxyConf = proxyManager->proxyConfiguration();
508 if (hasKey("proxy_type"))
509 proxyConf.type = static_cast<Net::ProxyType>(it.value().toInt());
510 if (hasKey("proxy_ip"))
511 proxyConf.ip = it.value().toString();
512 if (hasKey("proxy_port"))
513 proxyConf.port = it.value().toUInt();
514 if (hasKey("proxy_username"))
515 proxyConf.username = it.value().toString();
516 if (hasKey("proxy_password"))
517 proxyConf.password = it.value().toString();
518 proxyManager->setProxyConfiguration(proxyConf);
520 if (hasKey("proxy_peer_connections"))
521 session->setProxyPeerConnectionsEnabled(it.value().toBool());
522 if (hasKey("proxy_torrents_only"))
523 proxyManager->setProxyOnlyForTorrents(it.value().toBool());
525 // IP Filtering
526 if (hasKey("ip_filter_enabled"))
527 session->setIPFilteringEnabled(it.value().toBool());
528 if (hasKey("ip_filter_path"))
529 session->setIPFilterFile(it.value().toString());
530 if (hasKey("ip_filter_trackers"))
531 session->setTrackerFilteringEnabled(it.value().toBool());
532 if (hasKey("banned_IPs"))
533 session->setBannedIPs(it.value().toString().split('\n', Qt::SkipEmptyParts));
535 // Speed
536 // Global Rate Limits
537 if (hasKey("dl_limit"))
538 session->setGlobalDownloadSpeedLimit(it.value().toInt());
539 if (hasKey("up_limit"))
540 session->setGlobalUploadSpeedLimit(it.value().toInt());
541 if (hasKey("alt_dl_limit"))
542 session->setAltGlobalDownloadSpeedLimit(it.value().toInt());
543 if (hasKey("alt_up_limit"))
544 session->setAltGlobalUploadSpeedLimit(it.value().toInt());
545 if (hasKey("bittorrent_protocol"))
546 session->setBTProtocol(static_cast<BitTorrent::BTProtocol>(it.value().toInt()));
547 if (hasKey("limit_utp_rate"))
548 session->setUTPRateLimited(it.value().toBool());
549 if (hasKey("limit_tcp_overhead"))
550 session->setIncludeOverheadInLimits(it.value().toBool());
551 if (hasKey("limit_lan_peers"))
552 session->setIgnoreLimitsOnLAN(!it.value().toBool());
553 // Scheduling
554 if (hasKey("scheduler_enabled"))
555 session->setBandwidthSchedulerEnabled(it.value().toBool());
556 if (m.contains("schedule_from_hour") && m.contains("schedule_from_min"))
557 pref->setSchedulerStartTime(QTime(m["schedule_from_hour"].toInt(), m["schedule_from_min"].toInt()));
558 if (m.contains("schedule_to_hour") && m.contains("schedule_to_min"))
559 pref->setSchedulerEndTime(QTime(m["schedule_to_hour"].toInt(), m["schedule_to_min"].toInt()));
560 if (hasKey("scheduler_days"))
561 pref->setSchedulerDays(SchedulerDays(it.value().toInt()));
563 // Bittorrent
564 // Privacy
565 if (hasKey("dht"))
566 session->setDHTEnabled(it.value().toBool());
567 if (hasKey("pex"))
568 session->setPeXEnabled(it.value().toBool());
569 if (hasKey("lsd"))
570 session->setLSDEnabled(it.value().toBool());
571 if (hasKey("encryption"))
572 session->setEncryption(it.value().toInt());
573 if (hasKey("anonymous_mode"))
574 session->setAnonymousModeEnabled(it.value().toBool());
575 // Torrent Queueing
576 if (hasKey("queueing_enabled"))
577 session->setQueueingSystemEnabled(it.value().toBool());
578 if (hasKey("max_active_downloads"))
579 session->setMaxActiveDownloads(it.value().toInt());
580 if (hasKey("max_active_torrents"))
581 session->setMaxActiveTorrents(it.value().toInt());
582 if (hasKey("max_active_uploads"))
583 session->setMaxActiveUploads(it.value().toInt());
584 if (hasKey("dont_count_slow_torrents"))
585 session->setIgnoreSlowTorrentsForQueueing(it.value().toBool());
586 if (hasKey("slow_torrent_dl_rate_threshold"))
587 session->setDownloadRateForSlowTorrents(it.value().toInt());
588 if (hasKey("slow_torrent_ul_rate_threshold"))
589 session->setUploadRateForSlowTorrents(it.value().toInt());
590 if (hasKey("slow_torrent_inactive_timer"))
591 session->setSlowTorrentsInactivityTimer(it.value().toInt());
592 // Share Ratio Limiting
593 if (hasKey("max_ratio_enabled"))
595 if (it.value().toBool())
596 session->setGlobalMaxRatio(m["max_ratio"].toReal());
597 else
598 session->setGlobalMaxRatio(-1);
600 if (hasKey("max_seeding_time_enabled"))
602 if (it.value().toBool())
603 session->setGlobalMaxSeedingMinutes(m["max_seeding_time"].toInt());
604 else
605 session->setGlobalMaxSeedingMinutes(-1);
607 if (hasKey("max_ratio_act"))
608 session->setMaxRatioAction(static_cast<MaxRatioAction>(it.value().toInt()));
609 // Add trackers
610 if (hasKey("add_trackers_enabled"))
611 session->setAddTrackersEnabled(it.value().toBool());
612 if (hasKey("add_trackers"))
613 session->setAdditionalTrackers(it.value().toString());
615 // Web UI
616 // Language
617 if (hasKey("locale"))
619 QString locale = it.value().toString();
620 if (pref->getLocale() != locale)
622 auto *translator = new QTranslator;
623 if (translator->load(QLatin1String(":/lang/qbittorrent_") + locale))
625 qDebug("%s locale recognized, using translation.", qUtf8Printable(locale));
627 else
629 qDebug("%s locale unrecognized, using default (en).", qUtf8Printable(locale));
631 qApp->installTranslator(translator);
633 pref->setLocale(locale);
636 // HTTP Server
637 if (hasKey("web_ui_domain_list"))
638 pref->setServerDomains(it.value().toString());
639 if (hasKey("web_ui_address"))
640 pref->setWebUiAddress(it.value().toString());
641 if (hasKey("web_ui_port"))
642 pref->setWebUiPort(it.value().toUInt());
643 if (hasKey("web_ui_upnp"))
644 pref->setUPnPForWebUIPort(it.value().toBool());
645 if (hasKey("use_https"))
646 pref->setWebUiHttpsEnabled(it.value().toBool());
647 if (hasKey("web_ui_https_cert_path"))
648 pref->setWebUIHttpsCertificatePath(it.value().toString());
649 if (hasKey("web_ui_https_key_path"))
650 pref->setWebUIHttpsKeyPath(it.value().toString());
651 // Authentication
652 if (hasKey("web_ui_username"))
653 pref->setWebUiUsername(it.value().toString());
654 if (hasKey("web_ui_password"))
655 pref->setWebUIPassword(Utils::Password::PBKDF2::generate(it.value().toByteArray()));
656 if (hasKey("bypass_local_auth"))
657 pref->setWebUiLocalAuthEnabled(!it.value().toBool());
658 if (hasKey("bypass_auth_subnet_whitelist_enabled"))
659 pref->setWebUiAuthSubnetWhitelistEnabled(it.value().toBool());
660 if (hasKey("bypass_auth_subnet_whitelist"))
662 // recognize new lines and commas as delimiters
663 pref->setWebUiAuthSubnetWhitelist(it.value().toString().split(QRegularExpression("\n|,"), Qt::SkipEmptyParts));
665 if (hasKey("web_ui_max_auth_fail_count"))
666 pref->setWebUIMaxAuthFailCount(it.value().toInt());
667 if (hasKey("web_ui_ban_duration"))
668 pref->setWebUIBanDuration(std::chrono::seconds {it.value().toInt()});
669 if (hasKey("web_ui_session_timeout"))
670 pref->setWebUISessionTimeout(it.value().toInt());
671 // Use alternative Web UI
672 if (hasKey("alternative_webui_enabled"))
673 pref->setAltWebUiEnabled(it.value().toBool());
674 if (hasKey("alternative_webui_path"))
675 pref->setWebUiRootFolder(it.value().toString());
676 // Security
677 if (hasKey("web_ui_clickjacking_protection_enabled"))
678 pref->setWebUiClickjackingProtectionEnabled(it.value().toBool());
679 if (hasKey("web_ui_csrf_protection_enabled"))
680 pref->setWebUiCSRFProtectionEnabled(it.value().toBool());
681 if (hasKey("web_ui_secure_cookie_enabled"))
682 pref->setWebUiSecureCookieEnabled(it.value().toBool());
683 if (hasKey("web_ui_host_header_validation_enabled"))
684 pref->setWebUIHostHeaderValidationEnabled(it.value().toBool());
685 // Custom HTTP headers
686 if (hasKey("web_ui_use_custom_http_headers_enabled"))
687 pref->setWebUICustomHTTPHeadersEnabled(it.value().toBool());
688 if (hasKey("web_ui_custom_http_headers"))
689 pref->setWebUICustomHTTPHeaders(it.value().toString());
690 // Reverse proxy
691 if (hasKey("web_ui_reverse_proxy_enabled"))
692 pref->setWebUIReverseProxySupportEnabled(it.value().toBool());
693 if (hasKey("web_ui_reverse_proxies_list"))
694 pref->setWebUITrustedReverseProxiesList(it.value().toString());
695 // Update my dynamic domain name
696 if (hasKey("dyndns_enabled"))
697 pref->setDynDNSEnabled(it.value().toBool());
698 if (hasKey("dyndns_service"))
699 pref->setDynDNSService(it.value().toInt());
700 if (hasKey("dyndns_username"))
701 pref->setDynDNSUsername(it.value().toString());
702 if (hasKey("dyndns_password"))
703 pref->setDynDNSPassword(it.value().toString());
704 if (hasKey("dyndns_domain"))
705 pref->setDynDomainName(it.value().toString());
707 if (hasKey("rss_refresh_interval"))
708 RSS::Session::instance()->setRefreshInterval(it.value().toInt());
709 if (hasKey("rss_max_articles_per_feed"))
710 RSS::Session::instance()->setMaxArticlesPerFeed(it.value().toInt());
711 if (hasKey("rss_processing_enabled"))
712 RSS::Session::instance()->setProcessingEnabled(it.value().toBool());
713 if (hasKey("rss_auto_downloading_enabled"))
714 RSS::AutoDownloader::instance()->setProcessingEnabled(it.value().toBool());
715 if (hasKey("rss_download_repack_proper_episodes"))
716 RSS::AutoDownloader::instance()->setDownloadRepacks(it.value().toBool());
717 if (hasKey("rss_smart_episode_filters"))
718 RSS::AutoDownloader::instance()->setSmartEpisodeFilters(it.value().toString().split('\n'));
720 // Advanced settings
721 // qBittorrent preferences
722 // Current network interface
723 if (hasKey("current_network_interface"))
725 const QString ifaceValue {it.value().toString()};
727 const QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
728 const auto ifacesIter = std::find_if(ifaces.cbegin(), ifaces.cend(), [&ifaceValue](const QNetworkInterface &iface)
730 return (!iface.addressEntries().isEmpty()) && (iface.name() == ifaceValue);
732 const QString ifaceName = (ifacesIter != ifaces.cend()) ? ifacesIter->humanReadableName() : QString {};
734 session->setNetworkInterface(ifaceValue);
735 session->setNetworkInterfaceName(ifaceName);
737 // Current network interface address
738 if (hasKey("current_interface_address"))
740 const QHostAddress ifaceAddress {it.value().toString().trimmed()};
741 session->setNetworkInterfaceAddress(ifaceAddress.isNull() ? QString {} : ifaceAddress.toString());
743 // Save resume data interval
744 if (hasKey("save_resume_data_interval"))
745 session->setSaveResumeDataInterval(it.value().toInt());
746 // Recheck completed torrents
747 if (hasKey("recheck_completed_torrents"))
748 pref->recheckTorrentsOnCompletion(it.value().toBool());
749 // Resolve peer countries
750 if (hasKey("resolve_peer_countries"))
751 pref->resolvePeerCountries(it.value().toBool());
752 // Reannounce to all trackers when ip/port changed
753 if (hasKey("reannounce_when_address_changed"))
754 session->setReannounceWhenAddressChangedEnabled(it.value().toBool());
756 // libtorrent preferences
757 // Async IO threads
758 if (hasKey("async_io_threads"))
759 session->setAsyncIOThreads(it.value().toInt());
760 // Hashing threads
761 if (hasKey("hashing_threads"))
762 session->setHashingThreads(it.value().toInt());
763 // File pool size
764 if (hasKey("file_pool_size"))
765 session->setFilePoolSize(it.value().toInt());
766 // Checking Memory Usage
767 if (hasKey("checking_memory_use"))
768 session->setCheckingMemUsage(it.value().toInt());
769 // Disk write cache
770 if (hasKey("disk_cache"))
771 session->setDiskCacheSize(it.value().toInt());
772 if (hasKey("disk_cache_ttl"))
773 session->setDiskCacheTTL(it.value().toInt());
774 // Enable OS cache
775 if (hasKey("enable_os_cache"))
776 session->setUseOSCache(it.value().toBool());
777 // Coalesce reads & writes
778 if (hasKey("enable_coalesce_read_write"))
779 session->setCoalesceReadWriteEnabled(it.value().toBool());
780 // Piece extent affinity
781 if (hasKey("enable_piece_extent_affinity"))
782 session->setPieceExtentAffinity(it.value().toBool());
783 // Suggest mode
784 if (hasKey("enable_upload_suggestions"))
785 session->setSuggestMode(it.value().toBool());
786 // Send buffer watermark
787 if (hasKey("send_buffer_watermark"))
788 session->setSendBufferWatermark(it.value().toInt());
789 if (hasKey("send_buffer_low_watermark"))
790 session->setSendBufferLowWatermark(it.value().toInt());
791 if (hasKey("send_buffer_watermark_factor"))
792 session->setSendBufferWatermarkFactor(it.value().toInt());
793 // Outgoing connections per second
794 if (hasKey("connection_speed"))
795 session->setConnectionSpeed(it.value().toInt());
796 // Socket listen backlog size
797 if (hasKey("socket_backlog_size"))
798 session->setSocketBacklogSize(it.value().toInt());
799 // Outgoing ports
800 if (hasKey("outgoing_ports_min"))
801 session->setOutgoingPortsMin(it.value().toInt());
802 if (hasKey("outgoing_ports_max"))
803 session->setOutgoingPortsMax(it.value().toInt());
804 // UPnP lease duration
805 if (hasKey("upnp_lease_duration"))
806 session->setUPnPLeaseDuration(it.value().toInt());
807 // Type of service
808 if (hasKey("peer_tos"))
809 session->setPeerToS(it.value().toInt());
810 // uTP-TCP mixed mode
811 if (hasKey("utp_tcp_mixed_mode"))
812 session->setUtpMixedMode(static_cast<BitTorrent::MixedModeAlgorithm>(it.value().toInt()));
813 // Support internationalized domain name (IDN)
814 if (hasKey("idn_support_enabled"))
815 session->setIDNSupportEnabled(it.value().toBool());
816 // Multiple connections per IP
817 if (hasKey("enable_multi_connections_from_same_ip"))
818 session->setMultiConnectionsPerIpEnabled(it.value().toBool());
819 // Validate HTTPS tracker certificate
820 if (hasKey("validate_https_tracker_certificate"))
821 session->setValidateHTTPSTrackerCertificate(it.value().toBool());
822 // Disallow connection to peers on privileged ports
823 if (hasKey("block_peers_on_privileged_ports"))
824 session->setBlockPeersOnPrivilegedPorts(it.value().toBool());
825 // Embedded tracker
826 if (hasKey("embedded_tracker_port"))
827 pref->setTrackerPort(it.value().toInt());
828 if (hasKey("enable_embedded_tracker"))
829 session->setTrackerEnabled(it.value().toBool());
830 // Choking algorithm
831 if (hasKey("upload_slots_behavior"))
832 session->setChokingAlgorithm(static_cast<BitTorrent::ChokingAlgorithm>(it.value().toInt()));
833 // Seed choking algorithm
834 if (hasKey("upload_choking_algorithm"))
835 session->setSeedChokingAlgorithm(static_cast<BitTorrent::SeedChokingAlgorithm>(it.value().toInt()));
836 // Announce
837 if (hasKey("announce_to_all_trackers"))
838 session->setAnnounceToAllTrackers(it.value().toBool());
839 if (hasKey("announce_to_all_tiers"))
840 session->setAnnounceToAllTiers(it.value().toBool());
841 if (hasKey("announce_ip"))
843 const QHostAddress announceAddr {it.value().toString().trimmed()};
844 session->setAnnounceIP(announceAddr.isNull() ? QString {} : announceAddr.toString());
846 if (hasKey("max_concurrent_http_announces"))
847 session->setMaxConcurrentHTTPAnnounces(it.value().toInt());
848 if (hasKey("stop_tracker_timeout"))
849 session->setStopTrackerTimeout(it.value().toInt());
850 // Peer Turnover
851 if (hasKey("peer_turnover"))
852 session->setPeerTurnover(it.value().toInt());
853 if (hasKey("peer_turnover_cutoff"))
854 session->setPeerTurnoverCutoff(it.value().toInt());
855 if (hasKey("peer_turnover_interval"))
856 session->setPeerTurnoverInterval(it.value().toInt());
858 // Save preferences
859 pref->apply();
862 void AppController::defaultSavePathAction()
864 setResult(BitTorrent::Session::instance()->defaultSavePath());
867 void AppController::networkInterfaceListAction()
869 QJsonArray ifaceList;
870 for (const QNetworkInterface &iface : asConst(QNetworkInterface::allInterfaces()))
872 if (!iface.addressEntries().isEmpty())
874 ifaceList.append(QJsonObject
876 {"name", iface.humanReadableName()},
877 {"value", iface.name()}
882 setResult(ifaceList);
885 void AppController::networkInterfaceAddressListAction()
887 requireParams({"iface"});
889 const QString ifaceName = params().value("iface");
890 QJsonArray addressList;
892 const auto appendAddress = [&addressList](const QHostAddress &addr)
894 if (addr.protocol() == QAbstractSocket::IPv6Protocol)
895 addressList.append(Utils::Net::canonicalIPv6Addr(addr).toString());
896 else
897 addressList.append(addr.toString());
900 if (ifaceName.isEmpty())
902 for (const QHostAddress &addr : asConst(QNetworkInterface::allAddresses()))
903 appendAddress(addr);
905 else
907 const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName);
908 for (const QNetworkAddressEntry &entry : asConst(iface.addressEntries()))
909 appendAddress(entry.ip());
912 setResult(addressList);