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
|
// Copyright 2011 Eugene Petrov <dhamp@ya.ru>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "dyndns.h"
#include "dcpp/SettingsManager.h"
#include "dcpp/ClientManager.h"
namespace dcpp {
DynDNS::DynDNS() :
request(false),
minutesCounter(0)
{
httpConnection.addListener(this);
}
DynDNS::~DynDNS() {
httpConnection.removeListener(this);
}
void DynDNS::load()
{
request = true;
minutesCounter = 0;
Request();
}
void DynDNS::stop()
{
request = false;
}
void DynDNS::Request() {
if (BOOLSETTING(DYNDNS_ENABLE)) {
string tmps = SETTING(DYNDNS_SERVER);
if (!SETTING(DYNDNS_SERVER).compare(0,7,"http://") &&
!SETTING(DYNDNS_SERVER).compare(0,8,"https://")) {
tmps = "http://" + SETTING(DYNDNS_SERVER);
}
httpConnection.downloadFile(tmps);
}
}
void DynDNS::on(TimerManagerListener::Minute, uint64_t) noexcept {
++minutesCounter;
if (minutesCounter < 2) {
return;
}
else {
minutesCounter = 0;
}
if (request) {
Request();
}
}
void DynDNS::on(HttpConnectionListener::Data, HttpConnection*, const uint8_t* buf, size_t len) noexcept {
html += string((const char*)buf, len);
}
void DynDNS::on(HttpConnectionListener::Complete, HttpConnection*, string const&) noexcept {
request = false;
string internetIP;
if (!html.empty()) {
int start = html.find(":")+2;
int end = html.find("</body>");
if ((start == -1) || (end < start)) {
internetIP = "";
} else {
internetIP = html.substr(start, end - start);
}
}
else {
internetIP = "";
}
if (!internetIP.empty()) {
SettingsManager::getInstance()->set(SettingsManager::INTERNETIP, internetIP);
Client::List clients = ClientManager::getInstance()->getClients();
for(auto c : clients) {
if(c->isConnected()) {
c->reloadSettings(false);
}
}
}
request = true;
}
void DynDNS::on(HttpConnectionListener::Failed, HttpConnection*, const string&) noexcept {
if (request) {
Request();
}
}
}
|