Skip to content

Commit

Permalink
Enable HTTPS on the Client side
Browse files Browse the repository at this point in the history
Re ECFLOW-1957
  • Loading branch information
marcosbento committed Oct 18, 2024
1 parent d364b3b commit bd3425c
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 51 deletions.
17 changes: 17 additions & 0 deletions Viewer/ecflowUI/src/ServerAddDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,24 @@
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Use &amp;HTTPS:</string>
</property>
<property name="buddy">
<cstring>httpsCb</cstring>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QCheckBox" name="httpsCb">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="MessageLabel" name="sslMessageLabel" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
Expand Down
17 changes: 17 additions & 0 deletions Viewer/ecflowUI/src/ServerEditDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,24 @@
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="httpsLabel">
<property name="text">
<string>Use &amp;HTTPS:</string>
</property>
<property name="buddy">
<cstring>httpsCh</cstring>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QCheckBox" name="httpsCh">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="MessageLabel" name="sslMessageLabel" native="true"/>
</item>
</layout>
Expand Down
34 changes: 29 additions & 5 deletions Viewer/ecflowUI/src/ServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ ServerHandler::ServerHandler(const std::string& name,
const std::string& port,
const std::string& user,
bool ssl,
bool http)
bool http,
bool https)
: name_(name),
host_(host),
port_(port),
user_(user),
ssl_(ssl),
http_(http),
https_(https),
client_(nullptr),
updating_(false),
communicating_(false),
Expand Down Expand Up @@ -186,7 +188,7 @@ void ServerHandler::createClient(bool init) {

bool ssl_enabled = false;
std::string ssl_error;
bool http_enabled = false;
// bool http_enabled = false;
std::string http_error;

if (client_) {
Expand All @@ -210,7 +212,17 @@ void ServerHandler::createClient(bool init) {
if (http_) {
try {
client_->enable_http();
http_enabled = true;
// http_enabled = true;
}
catch (std::exception& e) {
http_error = std::string(e.what());
}
}

if (https_) {
try {
client_->enable_https();
// http_enabled = true;
}
catch (std::exception& e) {
http_error = std::string(e.what());
Expand Down Expand Up @@ -339,6 +351,17 @@ void ServerHandler::setHttp(bool http) {
}
}

void ServerHandler::setHttps(bool https) {
if (https != https_) {
https_ = https;

if (connectState_->state() != ConnectState::VersionIncompatible &&
connectState_->state() != ConnectState::FailedClient) {
recreateClient();
}
}
}

void ServerHandler::setUser(const std::string& user) {
if (user != user_) {
user_ = user;
Expand Down Expand Up @@ -553,8 +576,9 @@ ServerHandler* ServerHandler::addServer(const std::string& name,
const std::string& port,
const std::string& user,
bool ssl,
bool http) {
auto* sh = new ServerHandler(name, host, port, user, ssl, http);
bool http,
bool https) {
auto* sh = new ServerHandler(name, host, port, user, ssl, http, https);
return sh;
}

Expand Down
8 changes: 6 additions & 2 deletions Viewer/ecflowUI/src/ServerHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ServerHandler : public QObject {
const std::string& user() { return user_; }
void setSsl(bool);
void setHttp(bool);
void setHttps(bool);
void setUser(const std::string& user);

Activity activity() const { return activity_; }
Expand Down Expand Up @@ -121,7 +122,8 @@ class ServerHandler : public QObject {
const std::string& port,
const std::string& user,
bool ssl,
bool http);
bool http,
bool https);
static void removeServer(ServerHandler*);
static ServerHandler* findServer(const std::string& alias);

Expand All @@ -146,7 +148,8 @@ class ServerHandler : public QObject {
const std::string& port,
const std::string& user,
bool ssl,
bool http);
bool http,
bool https);
~ServerHandler() override;
void logoutAndDelete();
void queueLoggedOut();
Expand All @@ -172,6 +175,7 @@ class ServerHandler : public QObject {
std::string user_;
bool ssl_;
bool http_;
bool https_;
ClientInvoker* client_;
std::string longName_;
std::string fullLongName_;
Expand Down
31 changes: 26 additions & 5 deletions Viewer/ecflowUI/src/ServerItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ ServerItem::ServerItem(const std::string& name,
const std::string& user,
bool favourite,
bool ssl,
bool http)
bool http,
bool https)
: name_(name),
host_(host),
port_(port),
user_(user),
favourite_(favourite),
ssl_(ssl),
http_(http) {
http_(http),
https_(https) {
}

ServerItem::~ServerItem() {
Expand All @@ -52,20 +54,30 @@ void ServerItem::reset(const std::string& name,
const std::string& host,
const std::string& port,
const std::string& user,
bool ssl) {
bool ssl,
bool http,
bool https) {
if (name_ != name) {
name_ = name;
if (handler_) {
handler_->rename(name_);
}
}

if (host == host_ && port == port_) {
if (host == host_ && port == port_ && ssl) {
// TODO: these should be called together
setSsl(ssl);
setUser(user);
}

else if (host == host_ && port == port_ && http) {
setHttp(http);
}

else if (host == host_ && port == port_ && https) {
setHttps(https);
}

// host or port changed: full reload needed and this situation cannot
// be handled here!
else {
Expand Down Expand Up @@ -105,6 +117,15 @@ void ServerItem::setHttp(bool b) {
// broadcastChanged();
}

void ServerItem::setHttps(bool b) {
if (https_ != b) {
https_ = b;
if (handler_)
handler_->setHttps(https_);
}
// broadcastChanged();
}

void ServerItem::setUser(const std::string& user) {
if (user_ != user) {
user_ = user;
Expand All @@ -125,7 +146,7 @@ std::string ServerItem::longName() const {

void ServerItem::registerUsageBegin() {
if (!handler_) {
handler_ = ServerHandler::addServer(name_, host_, port_, user_, ssl_, http_);
handler_ = ServerHandler::addServer(name_, host_, port_, user_, ssl_, http_, https_);
}
if (handler_)
useCnt_++;
Expand Down
15 changes: 12 additions & 3 deletions Viewer/ecflowUI/src/ServerItem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ServerItem {
bool isSystem() const { return system_; }
bool isSsl() const { return ssl_; }
bool isHttp() const { return http_; }
bool isHttps() const { return https_; }

bool isUsed() const;
int useCnt() const { return useCnt_; }
Expand All @@ -58,18 +59,25 @@ class ServerItem {
const std::string& user,
bool favourite,
bool ssl,
bool http);
bool http,
bool https);
~ServerItem();

void name(const std::string& name) { name_ = name; }
void host(const std::string& host) { host_ = host; }
void port(const std::string& port) { port_ = port; }
void
reset(const std::string& name, const std::string& host, const std::string& port, const std::string& user, bool ssl);
void reset(const std::string& name,
const std::string& host,
const std::string& port,
const std::string& user,
bool ssl,
bool http,
bool https);
void setFavourite(bool b);
void setSystem(bool b);
void setSsl(bool b);
void setHttp(bool b);
void setHttps(bool b);
void setUser(const std::string&);

void registerUsageBegin();
Expand All @@ -86,6 +94,7 @@ class ServerItem {
bool system_{false};
bool ssl_{false};
bool http_{false};
bool https_{false};
int useCnt_{0};
ServerHandler* handler_{nullptr};

Expand Down
32 changes: 20 additions & 12 deletions Viewer/ecflowUI/src/ServerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,15 @@ ServerItem* ServerList::add(const std::string& name,
bool favourite,
bool ssl,
bool http,
bool https,
bool saveIt) {
std::string errStr;
if (!checkItemToAdd(name, host, port, true, errStr)) {
throw std::runtime_error(errStr);
return nullptr;
}

auto* item = new ServerItem(name, host, port, user, favourite, ssl, http);
auto* item = new ServerItem(name, host, port, user, favourite, ssl, http, https);

items_.push_back(item);

Expand Down Expand Up @@ -405,7 +406,8 @@ ServerItem* ServerList::reset(ServerItem* item,
const std::string& port,
const std::string& user,
bool ssl,
bool http) {
bool http,
bool https) {
auto it = std::find(items_.begin(), items_.end(), item);
if (it != items_.end()) {
// Check if there is an item with the same name. Names have to be unique!
Expand All @@ -416,14 +418,14 @@ ServerItem* ServerList::reset(ServerItem* item,
items_.erase(it);
broadcastChanged();
delete item;
item = add(name, host, port, user, false, ssl, http, true);
item = add(name, host, port, user, false, ssl, http, https, true);
save();
broadcastChanged();
}
else {
assert(host == item->host());
assert(port == item->port());
item->reset(name, host, port, user, ssl);
item->reset(name, host, port, user, ssl, http, https);
save();
broadcastChanged();
}
Expand Down Expand Up @@ -563,11 +565,16 @@ bool ServerList::load() {
http = (sv[7] == "1") ? true : false;
}

bool https = false;
if (sv.size() >= 9) {
https = (sv[8] == "1") ? true : false;
}

if (sv.size() >= 3) {
std::string name = sv[0], host = sv[1], port = sv[2];
ServerItem* item = nullptr;
try {
item = add(name, host, port, user, favourite, ssl, http, false);
item = add(name, host, port, user, favourite, ssl, http, https, false);
UI_ASSERT(item != nullptr, "name=" << name << " host=" << host << " port=" << port << " user=" << user);
item->setSystem(sys);
}
Expand Down Expand Up @@ -606,15 +613,16 @@ void ServerList::save() {
if (!out.good())
return;

out << "#Name Host Port Favourite System Ssl user Http" << std::endl;
out << "#Name Host Port Favourite System Ssl user Http Https" << std::endl;

for (auto& item : items_) {
std::string fav = (item->isFavourite()) ? "1" : "0";
std::string ssl = (item->isSsl()) ? "1" : "0";
std::string fav = (item->isFavourite()) ? "1" : "0";
std::string ssl = (item->isSsl()) ? "1" : "0";
std::string http = (item->isHttp()) ? "1" : "0";
std::string sys = (item->isSystem()) ? "1" : "0";
std::string https = (item->isHttps()) ? "1" : "0";
std::string sys = (item->isSystem()) ? "1" : "0";
out << item->name() << "," << item->host() << "," << item->port() << "," << fav << "," << sys << "," << ssl
<< "," << item->user() << "," << http << std::endl;
<< "," << item->user() << "," << http << "," << https << std::endl;
}
out.close();
}
Expand Down Expand Up @@ -670,7 +678,7 @@ void ServerList::loadSystemItems(const std::vector<ServerListTmpItem>& sysVec,
changed = true;
std::string name = sysItem.name(), host = sysItem.host(), port = sysItem.port();
try {
item = add(name, host, port, "", false, false, false, false);
item = add(name, host, port, "", false, false, false, false, false);
UI_ASSERT(item != nullptr, "name=" << name << " host=" << host << " port=" << port);
item->setSystem(true);
changeVec.push_back(
Expand All @@ -696,7 +704,7 @@ void ServerList::loadSystemItems(const std::vector<ServerListTmpItem>& sysVec,
ServerListTmpItem localTmp(item);
changeVec.push_back(new ServerListSyncChangeItem(sysItem, localTmp, ServerListSyncChangeItem::MatchChange));

item = reset(item, sysItem.name(), sysItem.host(), sysItem.port(), "", false, false);
item = reset(item, sysItem.name(), sysItem.host(), sysItem.port(), "", false, false, false);
if (item) {
item->setSystem(true);
}
Expand Down
Loading

0 comments on commit bd3425c

Please sign in to comment.