Files
amnezia-client/client/ui/pages_logic/protocols/ShadowSocksLogic.cpp
T

98 lines
4.4 KiB
C++
Raw Normal View History

2021-09-06 13:41:45 +03:00
#include "ShadowSocksLogic.h"
2021-09-06 14:35:57 +03:00
#include "core/servercontroller.h"
2021-09-06 13:41:45 +03:00
#include <functional>
2021-09-06 14:35:57 +03:00
#include "../../uilogic.h"
2021-09-06 13:41:45 +03:00
using namespace amnezia;
using namespace PageEnumNS;
2021-09-07 21:01:56 +03:00
ShadowSocksLogic::ShadowSocksLogic(UiLogic *logic, QObject *parent):
PageLogicBase(logic, parent),
2021-09-06 14:35:57 +03:00
m_widgetProtoSsEnabled{false},
m_comboBoxProtoShadowsocksCipherText{"chacha20-poly1305"},
m_lineEditProtoShadowsocksPortText{},
2021-09-08 13:52:36 +03:00
m_pushButtonShadowsocksSaveVisible{false},
2021-09-06 14:35:57 +03:00
m_progressBarProtoShadowsocksResetVisible{false},
m_lineEditProtoShadowsocksPortEnabled{false},
m_pageProtoShadowsocksEnabled{true},
m_labelProtoShadowsocksInfoVisible{true},
m_labelProtoShadowsocksInfoText{},
m_progressBarProtoShadowsocksResetValue{0},
m_progressBarProtoShadowsocksResetMaximium{100}
{
}
void ShadowSocksLogic::updateShadowSocksPage(const QJsonObject &ssConfig, DockerContainer container, bool haveAuthData)
{
2021-09-08 13:52:36 +03:00
set_widgetProtoSsEnabled(haveAuthData);
set_pushButtonShadowsocksSaveVisible(haveAuthData);
set_progressBarProtoShadowsocksResetVisible(haveAuthData);
2021-09-06 14:35:57 +03:00
2021-09-08 13:52:36 +03:00
set_comboBoxProtoShadowsocksCipherText(ssConfig.value(config_key::cipher).
2021-09-06 14:35:57 +03:00
toString(protocols::shadowsocks::defaultCipher));
2021-09-08 13:52:36 +03:00
set_lineEditProtoShadowsocksPortText(ssConfig.value(config_key::port).
2021-09-06 14:35:57 +03:00
toString(protocols::shadowsocks::defaultPort));
2021-09-08 13:52:36 +03:00
set_lineEditProtoShadowsocksPortEnabled(container == DockerContainer::OpenVpnOverShadowSocks);
2021-09-06 14:35:57 +03:00
}
QJsonObject ShadowSocksLogic::getShadowSocksConfigFromPage(QJsonObject oldConfig)
{
2021-09-08 13:52:36 +03:00
oldConfig.insert(config_key::cipher, comboBoxProtoShadowsocksCipherText());
oldConfig.insert(config_key::port, lineEditProtoShadowsocksPortText());
2021-09-06 14:35:57 +03:00
return oldConfig;
}
void ShadowSocksLogic::onPushButtonProtoShadowsocksSaveClicked()
{
2021-09-07 21:01:56 +03:00
QJsonObject protocolConfig = m_settings.protocolConfig(uiLogic()->selectedServerIndex, uiLogic()->selectedDockerContainer, Protocol::ShadowSocks);
2021-09-06 14:35:57 +03:00
protocolConfig = getShadowSocksConfigFromPage(protocolConfig);
2021-09-07 21:01:56 +03:00
QJsonObject containerConfig = m_settings.containerConfig(uiLogic()->selectedServerIndex, uiLogic()->selectedDockerContainer);
2021-09-06 14:35:57 +03:00
QJsonObject newContainerConfig = containerConfig;
newContainerConfig.insert(config_key::shadowsocks, protocolConfig);
UiLogic::PageFunc page_proto_shadowsocks;
page_proto_shadowsocks.setEnabledFunc = [this] (bool enabled) -> void {
2021-09-08 13:52:36 +03:00
set_pageProtoShadowsocksEnabled(enabled);
2021-09-06 14:35:57 +03:00
};
UiLogic::ButtonFunc pushButton_proto_shadowsocks_save;
pushButton_proto_shadowsocks_save.setVisibleFunc = [this] (bool visible) ->void {
2021-09-08 13:52:36 +03:00
set_pushButtonShadowsocksSaveVisible(visible);
2021-09-06 14:35:57 +03:00
};
UiLogic::LabelFunc label_proto_shadowsocks_info;
label_proto_shadowsocks_info.setVisibleFunc = [this] (bool visible) ->void {
2021-09-08 13:52:36 +03:00
set_labelProtoShadowsocksInfoVisible(visible);
2021-09-06 14:35:57 +03:00
};
label_proto_shadowsocks_info.setTextFunc = [this] (const QString& text) ->void {
2021-09-08 13:52:36 +03:00
set_labelProtoShadowsocksInfoText(text);
2021-09-06 14:35:57 +03:00
};
UiLogic::ProgressFunc progressBar_proto_shadowsocks_reset;
progressBar_proto_shadowsocks_reset.setVisibleFunc = [this] (bool visible) ->void {
2021-09-08 13:52:36 +03:00
set_progressBarProtoShadowsocksResetVisible(visible);
2021-09-06 14:35:57 +03:00
};
progressBar_proto_shadowsocks_reset.setValueFunc = [this] (int value) ->void {
2021-09-08 13:52:36 +03:00
set_progressBarProtoShadowsocksResetValue(value);
2021-09-06 14:35:57 +03:00
};
progressBar_proto_shadowsocks_reset.getValueFunc = [this] (void) -> int {
2021-09-08 13:52:36 +03:00
return progressBarProtoShadowsocksResetValue();
2021-09-06 14:35:57 +03:00
};
progressBar_proto_shadowsocks_reset.getMaximiumFunc = [this] (void) -> int {
2021-09-08 13:52:36 +03:00
return progressBarProtoShadowsocksResetMaximium();
2021-09-06 14:35:57 +03:00
};
2021-09-07 21:01:56 +03:00
ErrorCode e = uiLogic()->doInstallAction([this, containerConfig, newContainerConfig](){
return ServerController::updateContainer(m_settings.serverCredentials(uiLogic()->selectedServerIndex), uiLogic()->selectedDockerContainer, containerConfig, newContainerConfig);
2021-09-06 14:35:57 +03:00
},
page_proto_shadowsocks, progressBar_proto_shadowsocks_reset,
pushButton_proto_shadowsocks_save, label_proto_shadowsocks_info);
2021-09-06 13:41:45 +03:00
2021-09-06 14:35:57 +03:00
if (!e) {
2021-09-07 21:01:56 +03:00
m_settings.setContainerConfig(uiLogic()->selectedServerIndex, uiLogic()->selectedDockerContainer, newContainerConfig);
m_settings.clearLastConnectionConfig(uiLogic()->selectedServerIndex, uiLogic()->selectedDockerContainer);
2021-09-06 14:35:57 +03:00
}
2021-09-07 21:01:56 +03:00
qDebug() << "Protocol saved with code:" << e << "for" << uiLogic()->selectedServerIndex << uiLogic()->selectedDockerContainer;
2021-09-06 13:41:45 +03:00
}