Skip to content

Commit

Permalink
Handle FPP UI passwords a bit better.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulp committed Nov 28, 2024
1 parent e10bba7 commit fdc946d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
40 changes: 27 additions & 13 deletions xLights/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ bool xlPasswordEntryDialog::GetStoredPasswordForService(const std::string &servi

bool xlPasswordEntryDialog::StorePasswordForService(const std::string &service, const std::string &user, const std::string &pwd) {
if (pwdStore.IsOk()) {
wxSecretValue password(pwd);
if (pwdStore.Save("xLights/Discovery/" + service, user, password)) {
return true;
if (pwd.empty()) {
pwdStore.Delete("xLights/Discovery/" + service);
} else {
wxSecretValue password(pwd);
if (pwdStore.Save("xLights/Discovery/" + service, user, password)) {
return true;
}
}
}
return false;
Expand Down Expand Up @@ -339,11 +343,12 @@ void Discovery::AddCurl(const std::string &host, const std::string &url, std::fu
data->urls.insert(furl);
CURL *curl = CurlManager::INSTANCE.createCurl(furl);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 30000L);

logger_curl.info("Discovery Adding CURL - URL: %s Method: GET", furl.c_str());

CurlManager::INSTANCE.addCURL(furl, curl, [this, url, furl, host, callback](CURL* c) {
int authStatus = data->authStatus;
CurlManager::INSTANCE.addCURL(furl, curl, [this, url, furl, host, callback, authStatus, data](CURL* c) {
if (finished) {
return;
}
Expand All @@ -354,8 +359,9 @@ void Discovery::AddCurl(const std::string &host, const std::string &url, std::fu

logger_curl.info(" Discovery CURL Callback - URL: %s RC: %d Size: %d", furl.c_str(), rc, cpd ? cpd->resp.size() : -1);
if (rc == 401) {
if (HandleAuth(host, c)) {
AddCurl(host, url, [callback](int rc, const std::string &buffer, const std::string &errorBuffer) {
if (HandleAuth(host, c, authStatus) || authStatus != data->authStatus) {
data->urls.erase(furl);
AddCurl(host, url, [callback, furl](int rc, const std::string &buffer, const std::string &errorBuffer) {
return callback(rc, buffer, errorBuffer);
});
} else {
Expand All @@ -368,13 +374,18 @@ void Discovery::AddCurl(const std::string &host, const std::string &url, std::fu
});
}

bool Discovery::HandleAuth(const std::string &host, CURL* curl) {
bool Discovery::HandleAuth(const std::string &host, CURL* curl, int origAuthStatus) {
// we need to authenticate and redo
Discovery::CurlData *data = https[host];

if (data->authStatus != 1) {
if (origAuthStatus != 0) {
// still 401 so password must not be correct, remove it and re-prompt
data->authStatus = 0;
xlPasswordEntryDialog::StorePasswordForService(host, "admin", "");
}
if (data->authStatus == 0) {
bool handled = false;
std::string ip = host;
CurlManager::INSTANCE.setProcessCallbacks(false);
if (data->authStatus != 2) {
std::string username;
std::string password;
Expand All @@ -398,8 +409,11 @@ bool Discovery::HandleAuth(const std::string &host, CURL* curl) {
}
CurlManager::INSTANCE.setHostUsernamePassword(host, username, password);
handled = true;
discoveryFinishTime = wxGetLocalTimeMillis().GetValue() + 10000L;
data->authStatus = 1;
}
}
CurlManager::INSTANCE.setProcessCallbacks(true);
return handled;
}
return false;
Expand Down Expand Up @@ -631,14 +645,14 @@ DiscoveredData* Discovery::DetectControllerType(const std::string &ip, const std


void Discovery::Discover() {
auto endBroadcastTime = wxGetLocalTimeMillis().GetValue() + 1500l;
auto maxTime = wxGetLocalTimeMillis().GetValue() + 10000L; // 10 seconds max
auto endBroadcastTime = wxGetLocalTimeMillis().GetValue() + 2000l;
discoveryFinishTime = wxGetLocalTimeMillis().GetValue() + 10000L; // 10 seconds max
bool running = CurlManager::INSTANCE.processCurls();
uint8_t buffer[1500];
finished = false;

while ((running || (wxGetLocalTimeMillis().GetValue() < endBroadcastTime))
&& (wxGetLocalTimeMillis().GetValue() < maxTime)){
&& (wxGetLocalTimeMillis().GetValue() < discoveryFinishTime)){
memset(buffer, 0x00, sizeof(buffer));
int readSize = 0;
//first check to see if any of the socket have received data
Expand Down
4 changes: 3 additions & 1 deletion xLights/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Discovery
DiscoveredData *FindByIp(const std::string &ip, const std::string &hostname = "", bool create = false);
DiscoveredData *FindByUUID(const std::string &uuid, const std::string &ip);
private:
bool HandleAuth(const std::string &host, CURL* curl);
bool HandleAuth(const std::string &host, CURL* curl, int authStatus);

class DatagramData {
public:
Expand Down Expand Up @@ -171,4 +171,6 @@ class Discovery
std::list<BonjourData *> bonjours;
std::vector<DiscoveredData*> results;
bool finished = false;

uint64_t discoveryFinishTime;
};
7 changes: 4 additions & 3 deletions xLights/controllers/FPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3964,12 +3964,13 @@ std::list<FPP*> FPP::GetInstances(wxWindow* frame, OutputManager* outputManager)
for (auto& it : outputManager->GetControllers()) {
auto eth = dynamic_cast<ControllerEthernet*>(it);
if (eth != nullptr && eth->GetIP() != "" && eth->GetIP() != "MULTICAST") {
if (eth->GetResolvedIP() == "") {
std::string resolvedIP = eth->GetResolvedIP(true);
if (resolvedIP == "") {
startAddresses.push_back(::Lower(eth->GetIP()));
} else {
// only add the instances where we were actually able to resolve an IP address
if (eth->IsActive() && (ip_utils::IsIPValid(eth->GetResolvedIP()) || eth->GetResolvedIP() != eth->GetIP())) {
startAddresses.push_back(::Lower(eth->GetResolvedIP()));
if (eth->IsActive() && (ip_utils::IsIPValid(resolvedIP) || resolvedIP != eth->GetIP())) {
startAddresses.push_back(::Lower(resolvedIP));
}
}
if (eth->GetFPPProxy() != "") {
Expand Down
2 changes: 1 addition & 1 deletion xLights/utils/CurlManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool CurlManager::doProcessCurls() {
int stillRunning = 0;
std::unique_lock<std::mutex> l(lock);
mc = curl_multi_perform(curlMulti, &stillRunning);
if (stillRunning != numCurls) {
if (doCurlCallbacks && stillRunning != numCurls) {
CURLMsg* m = nullptr;
do {
int msgq = 0;
Expand Down
4 changes: 4 additions & 0 deletions xLights/utils/CurlManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class CurlManager {
return false;
}

void setProcessCallbacks(bool b) {
doCurlCallbacks = b;
}
private:
CurlManager();
~CurlManager();
Expand All @@ -66,6 +69,7 @@ class CurlManager {

CURLM* curlMulti = nullptr;
int numCurls = 0;
bool doCurlCallbacks = true;

class CurlInfo {
public:
Expand Down

0 comments on commit fdc946d

Please sign in to comment.