2021-01-09 19:55:16 +03:00
# include <QApplication>
2021-01-26 15:01:15 +03:00
# include <QClipboard>
2021-05-07 23:28:37 +03:00
# include <QDebug>
2021-01-09 19:55:16 +03:00
# include <QDesktopServices>
2021-05-10 02:33:31 +03:00
# include <QFileDialog>
2021-02-25 21:16:00 +03:00
# include <QHBoxLayout>
2021-05-27 22:18:36 +03:00
# include <QHostInfo>
# include <QItemSelectionModel>
2021-01-26 15:01:15 +03:00
# include <QJsonDocument>
# include <QJsonObject>
2020-12-16 06:02:22 +03:00
# include <QKeyEvent>
2021-01-09 19:55:16 +03:00
# include <QMenu>
2020-12-16 06:02:22 +03:00
# include <QMessageBox>
2021-01-09 19:55:16 +03:00
# include <QMetaEnum>
2020-12-26 15:03:51 +03:00
# include <QSysInfo>
# include <QThread>
2021-01-06 17:12:24 +03:00
# include <QTimer>
2020-12-16 06:02:22 +03:00
2021-06-12 11:59:36 +03:00
# include "configurators/cloak_configurator.h"
2021-05-10 02:33:31 +03:00
# include "configurators/vpn_configurator.h"
2021-04-04 23:12:36 +03:00
# include "configurators/openvpn_configurator.h"
2021-05-10 02:33:31 +03:00
# include "configurators/shadowsocks_configurator.h"
2021-06-12 11:59:36 +03:00
# include "configurators/ssh_configurator.h"
2021-05-10 02:33:31 +03:00
2020-12-30 17:03:05 +03:00
# include "core/servercontroller.h"
2021-04-26 22:54:31 +03:00
# include "core/server_defs.h"
2021-05-10 02:33:31 +03:00
# include "core/errorstrings.h"
2021-04-26 22:54:31 +03:00
# include "protocols/protocols_defs.h"
2021-05-10 02:33:31 +03:00
# include "protocols/shadowsocksvpnprotocol.h"
2021-02-24 21:58:32 +03:00
# include "ui/qautostart.h"
2021-01-06 17:12:24 +03:00
2020-12-16 06:02:22 +03:00
# include "debug.h"
# include "defines.h"
2020-11-23 16:20:25 +03:00
# include "mainwindow.h"
2020-12-26 15:03:51 +03:00
# include "utils.h"
# include "vpnconnection.h"
2021-04-26 22:54:31 +03:00
# include "ui_mainwindow.h"
2021-04-20 02:09:47 +03:00
# include "ui/server_widget.h"
# include "ui_server_widget.h"
2020-11-23 16:20:25 +03:00
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MAC || defined Q_OS_LINUX
2021-01-03 23:55:04 +03:00
# include "ui/macos_util.h"
# endif
2021-04-26 22:54:31 +03:00
using namespace amnezia ;
2020-12-26 15:03:51 +03:00
MainWindow : : MainWindow ( QWidget * parent ) :
2021-01-10 16:50:38 +03:00
# ifdef Q_OS_WIN
2021-01-03 21:18:20 +03:00
CFramelessWindow ( parent ) ,
2021-01-10 16:50:38 +03:00
# else
2021-01-03 23:55:04 +03:00
QMainWindow ( parent ) ,
2021-01-10 16:50:38 +03:00
# endif
2020-12-26 15:03:51 +03:00
ui ( new Ui : : MainWindow ) ,
2021-01-26 15:01:15 +03:00
m_vpnConnection ( nullptr )
2020-11-23 16:20:25 +03:00
{
ui - > setupUi ( this ) ;
2021-03-14 21:19:11 +03:00
2021-06-16 22:40:34 +03:00
ui - > frame_wireguard_settings - > hide ( ) ;
ui - > frame_wireguard - > hide ( ) ;
ui - > frame_new_server_settings_parent_wireguard - > hide ( ) ;
2021-03-14 21:19:11 +03:00
setupTray ( ) ;
setupUiConnections ( ) ;
2021-05-18 15:50:52 +03:00
setupNewServerConnections ( ) ;
setupWizardConnections ( ) ;
2021-05-27 22:18:36 +03:00
setupVpnPageConnections ( ) ;
setupSitesPageConnections ( ) ;
2021-05-18 15:50:52 +03:00
setupGeneralSettingsConnections ( ) ;
2021-05-11 17:04:04 +03:00
setupAppSettingsConnections ( ) ;
setupNetworkSettingsConnections ( ) ;
2021-04-26 22:54:31 +03:00
setupProtocolsPageConnections ( ) ;
setupNewServerPageConnections ( ) ;
2021-05-10 02:33:31 +03:00
setupSharePageConnections ( ) ;
setupServerSettingsPageConnections ( ) ;
2021-03-14 21:19:11 +03:00
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > clear ( ) ;
2021-04-20 02:09:47 +03:00
installEventFilter ( this ) ;
2021-01-03 19:14:54 +03:00
ui - > widget_tittlebar - > installEventFilter ( this ) ;
ui - > stackedWidget_main - > setSpeed ( 200 ) ;
ui - > stackedWidget_main - > setAnimation ( QEasingCurve : : Linear ) ;
2020-12-04 00:45:21 +03:00
2021-05-27 22:18:36 +03:00
ui - > tableView_sites - > horizontalHeader ( ) - > setSectionResizeMode ( QHeaderView : : Stretch ) ;
// ui->tableView_sites->setColumnWidth(0, 450);
// ui->tableView_sites->setColumnWidth(1, 120);
2021-03-13 13:56:52 +03:00
if ( QOperatingSystemVersion : : current ( ) < = QOperatingSystemVersion : : Windows7 ) {
needToHideCustomTitlebar = true ;
}
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MAC
2021-01-03 23:55:04 +03:00
fixWidget ( this ) ;
2021-03-13 13:56:52 +03:00
needToHideCustomTitlebar = true ;
2021-01-03 23:55:04 +03:00
# endif
2021-03-13 13:56:52 +03:00
if ( needToHideCustomTitlebar ) {
ui - > widget_tittlebar - > hide ( ) ;
2021-05-12 13:07:22 -07:00
resize ( width ( ) , 640 ) ;
2021-03-13 13:56:52 +03:00
ui - > stackedWidget_main - > move ( 0 , 0 ) ;
}
2020-12-16 06:02:22 +03:00
// Post initialization
2021-04-20 02:09:47 +03:00
goToPage ( Page : : Start , true , false ) ;
2020-12-30 17:03:05 +03:00
2021-05-10 05:25:20 -07:00
if ( m_settings . defaultServerIndex ( ) > = 0 & & m_settings . serversCount ( ) > 0 ) {
2021-01-09 19:55:16 +03:00
goToPage ( Page : : Vpn , true , false ) ;
2020-12-30 17:03:05 +03:00
}
2020-12-16 06:02:22 +03:00
2021-02-18 15:00:41 +03:00
//ui->pushButton_general_settings_exit->hide();
2021-05-18 15:50:52 +03:00
updateSharingPage ( selectedServerIndex , m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer ) ;
2021-01-16 15:08:27 +03:00
2020-12-16 06:02:22 +03:00
setFixedSize ( width ( ) , height ( ) ) ;
2020-12-26 15:03:51 +03:00
qInfo ( ) . noquote ( ) < < QString ( " Started %1 version %2 " ) . arg ( APPLICATION_NAME ) . arg ( APP_VERSION ) ;
qInfo ( ) . noquote ( ) < < QString ( " %1 (%2) " ) . arg ( QSysInfo : : prettyProductName ( ) ) . arg ( QSysInfo : : currentCpuArchitecture ( ) ) ;
2020-12-26 23:17:20 +03:00
m_vpnConnection = new VpnConnection ( this ) ;
2020-12-26 15:03:51 +03:00
connect ( m_vpnConnection , SIGNAL ( bytesChanged ( quint64 , quint64 ) ) , this , SLOT ( onBytesChanged ( quint64 , quint64 ) ) ) ;
connect ( m_vpnConnection , SIGNAL ( connectionStateChanged ( VpnProtocol : : ConnectionState ) ) , this , SLOT ( onConnectionStateChanged ( VpnProtocol : : ConnectionState ) ) ) ;
2021-01-08 16:51:58 +03:00
connect ( m_vpnConnection , SIGNAL ( vpnProtocolError ( amnezia : : ErrorCode ) ) , this , SLOT ( onVpnProtocolError ( amnezia : : ErrorCode ) ) ) ;
2020-12-26 15:03:51 +03:00
2021-06-01 18:18:09 +03:00
onConnectionStateChanged ( VpnProtocol : : Disconnected ) ;
2020-12-26 23:17:20 +03:00
2021-05-10 14:19:36 +03:00
if ( m_settings . isAutoConnect ( ) & & m_settings . defaultServerIndex ( ) > = 0 ) {
2021-02-24 21:58:32 +03:00
QTimer : : singleShot ( 1000 , this , [ this ] ( ) {
ui - > pushButton_connect - > setEnabled ( false ) ;
onConnect ( ) ;
} ) ;
}
2021-02-25 18:03:24 +03:00
m_ipAddressValidator . setRegExp ( Utils : : ipAddressRegExp ( ) ) ;
2021-03-17 03:45:38 +03:00
m_ipAddressPortValidator . setRegExp ( Utils : : ipAddressPortRegExp ( ) ) ;
2021-05-07 23:28:37 +03:00
m_ipNetwok24Validator . setRegExp ( Utils : : ipNetwork24RegExp ( ) ) ;
2021-05-10 02:33:31 +03:00
m_ipPortValidator . setRegExp ( Utils : : ipPortRegExp ( ) ) ;
2021-02-24 21:58:32 +03:00
2021-03-17 03:45:38 +03:00
ui - > lineEdit_new_server_ip - > setValidator ( & m_ipAddressPortValidator ) ;
2021-02-24 21:58:32 +03:00
ui - > lineEdit_network_settings_dns1 - > setValidator ( & m_ipAddressValidator ) ;
ui - > lineEdit_network_settings_dns2 - > setValidator ( & m_ipAddressValidator ) ;
2021-04-04 23:12:36 +03:00
2021-05-07 23:28:37 +03:00
ui - > lineEdit_proto_openvpn_subnet - > setValidator ( & m_ipNetwok24Validator ) ;
2021-05-10 02:33:31 +03:00
ui - > lineEdit_proto_openvpn_port - > setValidator ( & m_ipPortValidator ) ;
ui - > lineEdit_proto_shadowsocks_port - > setValidator ( & m_ipPortValidator ) ;
ui - > lineEdit_proto_cloak_port - > setValidator ( & m_ipPortValidator ) ;
//ui->toolBox_share_connection->removeItem(ui->toolBox_share_connection->indexOf(ui->page_share_shadowsocks));
//ui->page_share_shadowsocks->setVisible(false);
2021-05-27 22:18:36 +03:00
sitesModels . insert ( Settings : : VpnOnlyForwardSites , new SitesModel ( Settings : : VpnOnlyForwardSites ) ) ;
sitesModels . insert ( Settings : : VpnAllExceptSites , new SitesModel ( Settings : : VpnAllExceptSites ) ) ;
2020-11-23 16:20:25 +03:00
}
MainWindow : : ~ MainWindow ( )
{
2020-12-26 15:03:51 +03:00
hide ( ) ;
m_vpnConnection - > disconnectFromVpn ( ) ;
for ( int i = 0 ; i < 50 ; i + + ) {
qApp - > processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
QThread : : msleep ( 100 ) ;
2021-02-18 15:00:41 +03:00
if ( m_vpnConnection - > isDisconnected ( ) ) {
2020-12-26 15:03:51 +03:00
break ;
}
}
2020-12-26 23:17:20 +03:00
delete m_vpnConnection ;
delete ui ;
qDebug ( ) < < " Application closed " ;
2020-12-16 06:02:22 +03:00
}
2021-05-11 17:04:04 +03:00
void MainWindow : : showOnStartup ( )
{
if ( ! m_settings . isStartMinimized ( ) ) show ( ) ;
2021-05-12 13:07:22 -07:00
else {
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MACX
2021-05-12 13:07:22 -07:00
setDockIconVisible ( false ) ;
# endif
}
2021-05-11 17:04:04 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : goToPage ( Page page , bool reset , bool slide )
{
2021-05-12 13:07:22 -07:00
//qDebug() << "goToPage" << page;
2021-05-18 15:50:52 +03:00
if ( ui - > stackedWidget_main - > nextWidget ( ) = = getPageWidget ( page ) ) return ;
2021-01-09 19:55:16 +03:00
if ( reset ) {
if ( page = = Page : : ServerSettings ) {
2021-05-07 23:28:37 +03:00
updateServerPage ( ) ;
2021-01-09 19:55:16 +03:00
}
2021-03-17 03:45:38 +03:00
if ( page = = Page : : ShareConnection ) {
2021-05-10 02:33:31 +03:00
2021-05-19 00:15:40 +03:00
}
if ( page = = Page : : Wizard ) {
ui - > radioButton_setup_wizard_medium - > setChecked ( true ) ;
}
if ( page = = Page : : WizardHigh ) {
ui - > lineEdit_setup_wizard_high_website_masking - > setText ( protocols : : cloak : : defaultRedirSite ) ;
}
if ( page = = Page : : ServerConfiguring ) {
ui - > progressBar_new_server_configuring - > setValue ( 0 ) ;
2021-03-17 03:45:38 +03:00
}
2021-05-20 15:59:58 +03:00
if ( page = = Page : : GeneralSettings ) {
updateGeneralSettingPage ( ) ;
}
2021-04-26 22:54:31 +03:00
if ( page = = Page : : ServersList ) {
2021-05-07 23:28:37 +03:00
updateServersListPage ( ) ;
2021-04-20 02:09:47 +03:00
}
if ( page = = Page : : Start ) {
2021-05-10 20:51:38 +03:00
updateStartPage ( ) ;
2021-04-20 02:09:47 +03:00
}
2021-05-18 15:50:52 +03:00
if ( page = = Page : : NewServerProtocols ) {
2021-04-26 22:54:31 +03:00
ui - > pushButton_new_server_settings_cloak - > setChecked ( true ) ;
ui - > pushButton_new_server_settings_cloak - > setChecked ( false ) ;
ui - > pushButton_new_server_settings_ss - > setChecked ( true ) ;
ui - > pushButton_new_server_settings_ss - > setChecked ( false ) ;
ui - > pushButton_new_server_settings_openvpn - > setChecked ( true ) ;
ui - > pushButton_new_server_settings_openvpn - > setChecked ( false ) ;
2021-05-07 23:28:37 +03:00
ui - > lineEdit_new_server_cloak_port - > setText ( amnezia : : protocols : : cloak : : defaultPort ) ;
ui - > lineEdit_new_server_cloak_site - > setText ( amnezia : : protocols : : cloak : : defaultRedirSite ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
ui - > lineEdit_new_server_ss_port - > setText ( amnezia : : protocols : : shadowsocks : : defaultPort ) ;
ui - > comboBox_new_server_ss_cipher - > setCurrentText ( amnezia : : protocols : : shadowsocks : : defaultCipher ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
ui - > lineEdit_new_server_openvpn_port - > setText ( amnezia : : protocols : : openvpn : : defaultPort ) ;
ui - > comboBox_new_server_openvpn_proto - > setCurrentText ( amnezia : : protocols : : openvpn : : defaultTransportProto ) ;
}
if ( page = = Page : : ServerVpnProtocols ) {
updateProtocolsPage ( ) ;
}
if ( page = = Page : : AppSettings ) {
updateAppSettingsPage ( ) ;
}
2021-05-11 17:04:04 +03:00
if ( page = = Page : : NetworkSettings ) {
updateAppSettingsPage ( ) ;
}
2021-05-07 23:28:37 +03:00
if ( page = = Page : : Sites ) {
updateSitesPage ( ) ;
}
if ( page = = Page : : Vpn ) {
updateVpnPage ( ) ;
2021-04-26 22:54:31 +03:00
}
2021-01-09 19:55:16 +03:00
2021-03-14 21:19:11 +03:00
ui - > pushButton_new_server_connect_key - > setChecked ( false ) ;
2021-01-09 19:55:16 +03:00
}
if ( slide )
2021-04-20 02:09:47 +03:00
ui - > stackedWidget_main - > slideInWidget ( getPageWidget ( page ) , SlidingStackedWidget : : RIGHT2LEFT ) ;
2021-01-09 19:55:16 +03:00
else
ui - > stackedWidget_main - > setCurrentWidget ( getPageWidget ( page ) ) ;
2021-04-20 02:09:47 +03:00
pagesStack . push ( page ) ;
}
2021-05-10 14:19:36 +03:00
void MainWindow : : setStartPage ( MainWindow : : Page page , bool slide )
{
if ( slide )
ui - > stackedWidget_main - > slideInWidget ( getPageWidget ( page ) , SlidingStackedWidget : : RIGHT2LEFT ) ;
else
ui - > stackedWidget_main - > setCurrentWidget ( getPageWidget ( page ) ) ;
pagesStack . clear ( ) ;
pagesStack . push ( page ) ;
2021-05-10 20:51:38 +03:00
if ( page = = Page : : Start ) updateStartPage ( ) ;
2021-05-10 14:19:36 +03:00
}
2021-04-20 02:09:47 +03:00
void MainWindow : : closePage ( )
{
2021-05-10 20:51:38 +03:00
if ( pagesStack . size ( ) < = 1 ) return ;
2021-04-20 02:09:47 +03:00
Page prev = pagesStack . pop ( ) ;
2021-04-26 22:54:31 +03:00
//qDebug() << "closePage" << prev << "Set page" << pagesStack.top();
2021-04-20 02:09:47 +03:00
ui - > stackedWidget_main - > slideInWidget ( getPageWidget ( pagesStack . top ( ) ) , SlidingStackedWidget : : LEFT2RIGHT ) ;
2021-01-09 19:55:16 +03:00
}
QWidget * MainWindow : : getPageWidget ( MainWindow : : Page page )
2020-12-16 06:02:22 +03:00
{
2021-01-09 19:55:16 +03:00
switch ( page ) {
case ( Page : : Start ) : return ui - > page_start ;
case ( Page : : NewServer ) : return ui - > page_new_server ;
2021-05-18 15:50:52 +03:00
case ( Page : : NewServerProtocols ) : return ui - > page_new_server_protocols ;
case ( Page : : Wizard ) : return ui - > page_setup_wizard ;
case ( Page : : WizardHigh ) : return ui - > page_setup_wizard_high_level ;
case ( Page : : WizardLow ) : return ui - > page_setup_wizard_low_level ;
case ( Page : : WizardMedium ) : return ui - > page_setup_wizard_medium_level ;
case ( Page : : WizardVpnMode ) : return ui - > page_setup_wizard_vpn_mode ;
case ( Page : : ServerConfiguring ) : return ui - > page_new_server_configuring ;
2021-01-09 19:55:16 +03:00
case ( Page : : Vpn ) : return ui - > page_vpn ;
case ( Page : : GeneralSettings ) : return ui - > page_general_settings ;
2021-02-24 21:58:32 +03:00
case ( Page : : AppSettings ) : return ui - > page_app_settings ;
case ( Page : : NetworkSettings ) : return ui - > page_network_settings ;
2021-01-09 19:55:16 +03:00
case ( Page : : ServerSettings ) : return ui - > page_server_settings ;
2021-04-20 02:09:47 +03:00
case ( Page : : ServerVpnProtocols ) : return ui - > page_server_protocols ;
case ( Page : : ServersList ) : return ui - > page_servers ;
2021-01-09 19:55:16 +03:00
case ( Page : : ShareConnection ) : return ui - > page_share_connection ;
case ( Page : : Sites ) : return ui - > page_sites ;
2021-04-20 02:09:47 +03:00
case ( Page : : OpenVpnSettings ) : return ui - > page_proto_openvpn ;
case ( Page : : ShadowSocksSettings ) : return ui - > page_proto_shadowsocks ;
case ( Page : : CloakSettings ) : return ui - > page_proto_cloak ;
2021-01-09 19:55:16 +03:00
}
return nullptr ;
2021-01-03 19:14:54 +03:00
}
2021-01-09 19:55:16 +03:00
2021-01-03 19:14:54 +03:00
bool MainWindow : : eventFilter ( QObject * obj , QEvent * event )
{
if ( obj = = ui - > widget_tittlebar ) {
QMouseEvent * mouseEvent = static_cast < QMouseEvent * > ( event ) ;
if ( ! mouseEvent )
return false ;
if ( event - > type ( ) = = QEvent : : MouseButtonPress ) {
offset = mouseEvent - > pos ( ) ;
canMove = true ;
}
if ( event - > type ( ) = = QEvent : : MouseButtonRelease ) {
canMove = false ;
}
if ( event - > type ( ) = = QEvent : : MouseMove ) {
if ( canMove & & ( mouseEvent - > buttons ( ) & Qt : : LeftButton ) ) {
move ( mapToParent ( mouseEvent - > pos ( ) - offset ) ) ;
}
event - > ignore ( ) ;
return false ;
}
}
return QMainWindow : : eventFilter ( obj , event ) ;
2020-12-16 06:02:22 +03:00
}
void MainWindow : : keyPressEvent ( QKeyEvent * event )
{
switch ( event - > key ( ) ) {
2020-12-26 15:03:51 +03:00
case Qt : : Key_L :
if ( ! Debug : : openLogsFolder ( ) ) {
QMessageBox : : warning ( this , APPLICATION_NAME , tr ( " Cannot open logs folder! " ) ) ;
}
2020-12-16 06:02:22 +03:00
break ;
2021-04-26 22:54:31 +03:00
# ifdef QT_DEBUG
2021-04-20 02:09:47 +03:00
case Qt : : Key_Q :
qApp - > quit ( ) ;
2021-05-10 02:33:31 +03:00
break ;
2021-06-01 18:18:09 +03:00
// case Qt::Key_0:
// *((char*)-1) = 'x';
// break;
2021-05-18 15:50:52 +03:00
case Qt : : Key_H :
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
selectedDockerContainer = m_settings . defaultContainer ( selectedServerIndex ) ;
updateSharingPage ( selectedServerIndex , m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer ) ;
goToPage ( Page : : ShareConnection ) ;
break ;
2021-05-11 17:04:04 +03:00
# endif
2021-04-26 22:54:31 +03:00
case Qt : : Key_C :
2021-05-10 20:51:38 +03:00
qDebug ( ) . noquote ( ) < < " Def server " < < m_settings . defaultServerIndex ( ) < < m_settings . defaultContainerName ( m_settings . defaultServerIndex ( ) ) ;
2021-05-27 22:18:36 +03:00
//qDebug().noquote() << QJsonDocument(m_settings.containerConfig(m_settings.defaultServerIndex(), m_settings.defaultContainer(m_settings.defaultServerIndex()))).toJson();
qDebug ( ) . noquote ( ) < < QJsonDocument ( m_settings . defaultServer ( ) ) . toJson ( ) ;
2021-04-26 22:54:31 +03:00
break ;
case Qt : : Key_A :
goToPage ( Page : : Start ) ;
break ;
case Qt : : Key_S :
2021-05-10 02:33:31 +03:00
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
2021-04-26 22:54:31 +03:00
goToPage ( Page : : ServerSettings ) ;
break ;
2021-05-10 02:33:31 +03:00
case Qt : : Key_P :
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
selectedDockerContainer = m_settings . defaultContainer ( selectedServerIndex ) ;
goToPage ( Page : : ServerVpnProtocols ) ;
break ;
2021-06-12 11:59:36 +03:00
case Qt : : Key_T :
SshConfigurator : : openSshTerminal ( m_settings . serverCredentials ( m_settings . defaultServerIndex ( ) ) ) ;
break ;
2021-05-10 02:33:31 +03:00
case Qt : : Key_Escape :
2021-05-18 15:50:52 +03:00
if ( currentPage ( ) = = Page : : Vpn ) break ;
if ( currentPage ( ) = = Page : : ServerConfiguring ) break ;
if ( currentPage ( ) = = Page : : Start & & pagesStack . size ( ) < 2 ) break ;
2021-05-27 22:18:36 +03:00
if ( currentPage ( ) = = Page : : Sites & &
ui - > tableView_sites - > selectionModel ( ) - > selection ( ) . indexes ( ) . size ( ) > 0 ) {
ui - > tableView_sites - > clearSelection ( ) ;
break ;
}
2021-05-10 02:33:31 +03:00
if ( ! ui - > stackedWidget_main - > isAnimationRunning ( ) & & ui - > stackedWidget_main - > currentWidget ( ) - > isEnabled ( ) ) {
2021-05-18 15:50:52 +03:00
closePage ( ) ;
2021-05-10 02:33:31 +03:00
}
2020-12-26 15:03:51 +03:00
default :
2020-12-16 06:02:22 +03:00
;
}
}
2021-01-09 19:55:16 +03:00
void MainWindow : : closeEvent ( QCloseEvent * event )
{
2021-05-11 17:04:04 +03:00
if ( m_settings . serversCount ( ) = = 0 ) qApp - > quit ( ) ;
2021-03-14 12:52:19 -07:00
else {
hide ( ) ;
event - > ignore ( ) ;
}
2021-01-09 19:55:16 +03:00
}
2021-01-16 21:04:16 +03:00
void MainWindow : : showEvent ( QShowEvent * event )
{
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MACX
2021-01-16 21:04:16 +03:00
if ( ! event - > spontaneous ( ) ) {
setDockIconVisible ( true ) ;
}
2021-05-12 13:07:22 -07:00
if ( needToHideCustomTitlebar ) {
ui - > widget_tittlebar - > hide ( ) ;
resize ( width ( ) , 640 ) ;
ui - > stackedWidget_main - > move ( 0 , 0 ) ;
}
2021-01-16 21:04:16 +03:00
# endif
}
void MainWindow : : hideEvent ( QHideEvent * event )
{
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MACX
2021-01-16 21:04:16 +03:00
if ( ! event - > spontaneous ( ) ) {
setDockIconVisible ( false ) ;
}
# endif
}
2021-04-26 22:54:31 +03:00
void MainWindow : : onPushButtonNewServerConnect ( bool )
2020-12-30 17:03:05 +03:00
{
2021-03-14 21:19:11 +03:00
if ( ui - > pushButton_new_server_connect_key - > isChecked ( ) ) {
if ( ui - > lineEdit_new_server_ip - > text ( ) . isEmpty ( ) | |
ui - > lineEdit_new_server_login - > text ( ) . isEmpty ( ) | |
ui - > textEdit_new_server_ssh_key - > toPlainText ( ) . isEmpty ( ) ) {
ui - > label_new_server_wait_info - > setText ( tr ( " Please fill in all fields " ) ) ;
return ;
}
}
else {
if ( ui - > lineEdit_new_server_ip - > text ( ) . isEmpty ( ) | |
ui - > lineEdit_new_server_login - > text ( ) . isEmpty ( ) | |
ui - > lineEdit_new_server_password - > text ( ) . isEmpty ( ) ) {
ui - > label_new_server_wait_info - > setText ( tr ( " Please fill in all fields " ) ) ;
return ;
}
2021-01-06 17:12:24 +03:00
}
2021-03-14 21:19:11 +03:00
2021-04-26 22:54:31 +03:00
qDebug ( ) < < " MainWindow::onPushButtonNewServerConnect checking new server " ;
2021-01-06 17:12:24 +03:00
ServerCredentials serverCredentials ;
serverCredentials . hostName = ui - > lineEdit_new_server_ip - > text ( ) ;
if ( serverCredentials . hostName . contains ( " : " ) ) {
serverCredentials . port = serverCredentials . hostName . split ( " : " ) . at ( 1 ) . toInt ( ) ;
serverCredentials . hostName = serverCredentials . hostName . split ( " : " ) . at ( 0 ) ;
}
serverCredentials . userName = ui - > lineEdit_new_server_login - > text ( ) ;
2021-03-14 21:19:11 +03:00
if ( ui - > pushButton_new_server_connect_key - > isChecked ( ) ) {
QString key = ui - > textEdit_new_server_ssh_key - > toPlainText ( ) ;
2021-03-18 22:13:05 +03:00
if ( key . startsWith ( " ssh-rsa " ) ) {
QMessageBox : : warning ( this , APPLICATION_NAME ,
tr ( " It's public key. Private key required " ) ) ;
return ;
}
2021-03-14 21:19:11 +03:00
if ( key . contains ( " OPENSSH " ) & & key . contains ( " BEGIN " ) & & key . contains ( " PRIVATE KEY " ) ) {
2021-06-12 11:59:36 +03:00
key = SshConfigurator : : convertOpenSShKey ( key ) ;
2021-03-14 21:19:11 +03:00
}
serverCredentials . password = key ;
}
else {
serverCredentials . password = ui - > lineEdit_new_server_password - > text ( ) ;
}
2021-04-26 22:54:31 +03:00
ui - > pushButton_new_server_connect - > setEnabled ( false ) ;
ui - > pushButton_new_server_connect - > setText ( tr ( " Connecting... " ) ) ;
2021-05-18 15:50:52 +03:00
2021-04-26 22:54:31 +03:00
ErrorCode e = ErrorCode : : NoError ;
2021-05-18 15:50:52 +03:00
# ifdef Q_DEBUG
2021-04-26 22:54:31 +03:00
//QString output = ServerController::checkSshConnection(serverCredentials, &e);
2021-05-18 15:50:52 +03:00
# else
2021-04-26 22:54:31 +03:00
QString output ;
2021-05-18 15:50:52 +03:00
# endif
2021-04-26 22:54:31 +03:00
bool ok = true ;
if ( e ) {
ui - > label_new_server_wait_info - > show ( ) ;
ui - > label_new_server_wait_info - > setText ( errorString ( e ) ) ;
ok = false ;
}
else {
if ( output . contains ( " Please login as the user " ) ) {
output . replace ( " \n " , " " ) ;
ui - > label_new_server_wait_info - > show ( ) ;
ui - > label_new_server_wait_info - > setText ( output ) ;
ok = false ;
}
}
ui - > pushButton_new_server_connect - > setEnabled ( true ) ;
ui - > pushButton_new_server_connect - > setText ( tr ( " Connect " ) ) ;
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
installCredentials = serverCredentials ;
2021-05-18 15:50:52 +03:00
if ( ok ) goToPage ( Page : : NewServer ) ;
2021-04-26 22:54:31 +03:00
}
2021-05-18 15:50:52 +03:00
QMap < DockerContainer , QJsonObject > MainWindow : : getInstallConfigsFromProtocolsPage ( ) const
2021-04-26 22:54:31 +03:00
{
QJsonObject cloakConfig {
2021-05-07 23:28:37 +03:00
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpnOverCloak ) } ,
2021-04-26 22:54:31 +03:00
{ config_key : : cloak , QJsonObject {
2021-05-11 17:04:04 +03:00
{ config_key : : port , ui - > lineEdit_new_server_cloak_port - > text ( ) } ,
2021-04-26 22:54:31 +03:00
{ config_key : : site , ui - > lineEdit_new_server_cloak_site - > text ( ) } }
}
} ;
QJsonObject ssConfig {
2021-05-07 23:28:37 +03:00
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpnOverShadowSocks ) } ,
2021-04-26 22:54:31 +03:00
{ config_key : : shadowsocks , QJsonObject {
2021-05-11 17:04:04 +03:00
{ config_key : : port , ui - > lineEdit_new_server_ss_port - > text ( ) } ,
2021-04-26 22:54:31 +03:00
{ config_key : : cipher , ui - > comboBox_new_server_ss_cipher - > currentText ( ) } }
}
} ;
QJsonObject openVpnConfig {
2021-05-07 23:28:37 +03:00
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpn ) } ,
2021-04-26 22:54:31 +03:00
{ config_key : : openvpn , QJsonObject {
2021-05-11 17:04:04 +03:00
{ config_key : : port , ui - > lineEdit_new_server_openvpn_port - > text ( ) } ,
2021-05-07 23:28:37 +03:00
{ config_key : : transport_proto , ui - > comboBox_new_server_openvpn_proto - > currentText ( ) } }
2021-04-26 22:54:31 +03:00
}
} ;
2021-05-18 15:50:52 +03:00
QMap < DockerContainer , QJsonObject > containers ;
2021-04-26 22:54:31 +03:00
if ( ui - > checkBox_new_server_cloak - > isChecked ( ) ) {
2021-05-18 15:50:52 +03:00
containers . insert ( DockerContainer : : OpenVpnOverCloak , cloakConfig ) ;
2021-04-26 22:54:31 +03:00
}
if ( ui - > checkBox_new_server_ss - > isChecked ( ) ) {
2021-05-18 15:50:52 +03:00
containers . insert ( DockerContainer : : OpenVpnOverShadowSocks , ssConfig ) ;
2021-04-26 22:54:31 +03:00
}
if ( ui - > checkBox_new_server_openvpn - > isChecked ( ) ) {
2021-05-18 15:50:52 +03:00
containers . insert ( DockerContainer : : OpenVpn , openVpnConfig ) ;
}
return containers ;
}
QMap < DockerContainer , QJsonObject > MainWindow : : getInstallConfigsFromWizardPage ( ) const
{
QJsonObject cloakConfig {
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpnOverCloak ) } ,
{ config_key : : cloak , QJsonObject {
{ config_key : : site , ui - > lineEdit_setup_wizard_high_website_masking - > text ( ) } }
}
} ;
QJsonObject ssConfig {
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpnOverShadowSocks ) }
} ;
QJsonObject openVpnConfig {
{ config_key : : container , amnezia : : containerToString ( DockerContainer : : OpenVpn ) }
} ;
QMap < DockerContainer , QJsonObject > containers ;
if ( ui - > radioButton_setup_wizard_high - > isChecked ( ) ) {
containers . insert ( DockerContainer : : OpenVpnOverCloak , cloakConfig ) ;
}
if ( ui - > radioButton_setup_wizard_medium - > isChecked ( ) ) {
containers . insert ( DockerContainer : : OpenVpnOverShadowSocks , ssConfig ) ;
}
if ( ui - > radioButton_setup_wizard_low - > isChecked ( ) ) {
containers . insert ( DockerContainer : : OpenVpn , openVpnConfig ) ;
2021-04-26 22:54:31 +03:00
}
2021-05-18 15:50:52 +03:00
return containers ;
}
void MainWindow : : installServer ( const QMap < DockerContainer , QJsonObject > & containers )
{
2021-05-10 20:51:38 +03:00
if ( containers . isEmpty ( ) ) return ;
2021-05-18 15:50:52 +03:00
goToPage ( Page : : ServerConfiguring ) ;
QEventLoop loop ;
QTimer : : singleShot ( 500 , & loop , SLOT ( quit ( ) ) ) ;
loop . exec ( ) ;
qApp - > processEvents ( ) ;
bool ok = installContainers ( installCredentials , containers ,
ui - > page_new_server_configuring ,
ui - > progressBar_new_server_configuring ,
nullptr ,
ui - > label_new_server_configuring_wait_info ) ;
2021-01-09 19:55:16 +03:00
if ( ok ) {
2021-04-26 22:54:31 +03:00
QJsonObject server ;
server . insert ( config_key : : hostName , installCredentials . hostName ) ;
server . insert ( config_key : : userName , installCredentials . userName ) ;
server . insert ( config_key : : password , installCredentials . password ) ;
server . insert ( config_key : : port , installCredentials . port ) ;
server . insert ( config_key : : description , m_settings . nextAvailableServerName ( ) ) ;
2021-05-18 15:50:52 +03:00
QJsonArray containerConfigs ;
for ( const QJsonObject & cfg : containers ) {
containerConfigs . append ( cfg ) ;
}
2021-04-26 22:54:31 +03:00
server . insert ( config_key : : containers , containerConfigs ) ;
2021-05-18 15:50:52 +03:00
server . insert ( config_key : : defaultContainer , containerToString ( containers . firstKey ( ) ) ) ;
2021-04-26 22:54:31 +03:00
m_settings . addServer ( server ) ;
2021-05-11 17:04:04 +03:00
m_settings . setDefaultServer ( m_settings . serversCount ( ) - 1 ) ;
2021-01-09 19:55:16 +03:00
2021-05-18 15:50:52 +03:00
setStartPage ( Page : : Vpn ) ;
2021-01-09 19:55:16 +03:00
qApp - > processEvents ( ) ;
}
2021-05-18 15:50:52 +03:00
else {
closePage ( ) ;
}
2021-01-09 19:55:16 +03:00
}
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
void MainWindow : : onPushButtonNewServerImport ( bool )
2021-01-26 15:01:15 +03:00
{
QString s = ui - > lineEdit_start_existing_code - > text ( ) ;
s . replace ( " vpn:// " , " " ) ;
2021-03-13 13:56:52 +03:00
QJsonObject o = QJsonDocument : : fromJson ( QByteArray : : fromBase64 ( s . toUtf8 ( ) , QByteArray : : Base64UrlEncoding | QByteArray : : OmitTrailingEquals ) ) . object ( ) ;
2021-01-26 15:01:15 +03:00
ServerCredentials credentials ;
credentials . hostName = o . value ( " h " ) . toString ( ) ;
2021-05-10 14:19:36 +03:00
if ( credentials . hostName . isEmpty ( ) ) credentials . hostName = o . value ( config_key : : hostName ) . toString ( ) ;
2021-01-26 15:01:15 +03:00
credentials . port = o . value ( " p " ) . toInt ( ) ;
2021-05-10 14:19:36 +03:00
if ( credentials . port = = 0 ) credentials . port = o . value ( config_key : : port ) . toInt ( ) ;
2021-01-26 15:01:15 +03:00
credentials . userName = o . value ( " u " ) . toString ( ) ;
2021-05-10 14:19:36 +03:00
if ( credentials . userName . isEmpty ( ) ) credentials . userName = o . value ( config_key : : userName ) . toString ( ) ;
2021-01-26 15:01:15 +03:00
credentials . password = o . value ( " w " ) . toString ( ) ;
2021-05-10 14:19:36 +03:00
if ( credentials . password . isEmpty ( ) ) credentials . password = o . value ( config_key : : password ) . toString ( ) ;
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
if ( credentials . isValid ( ) ) {
o . insert ( config_key : : hostName , credentials . hostName ) ;
o . insert ( config_key : : port , credentials . port ) ;
o . insert ( config_key : : userName , credentials . userName ) ;
o . insert ( config_key : : password , credentials . password ) ;
o . remove ( " h " ) ;
o . remove ( " p " ) ;
o . remove ( " u " ) ;
o . remove ( " w " ) ;
}
2021-01-26 15:01:15 +03:00
qDebug ( ) < < QString ( " Added server %3@%1:%2 " ) .
arg ( credentials . hostName ) .
arg ( credentials . port ) .
arg ( credentials . userName ) ;
2021-03-13 13:56:52 +03:00
//qDebug() << QString("Password") << credentials.password;
2021-03-14 21:19:11 +03:00
2021-05-10 14:19:36 +03:00
if ( credentials . isValid ( ) | | o . contains ( config_key : : containers ) ) {
m_settings . addServer ( o ) ;
2021-05-11 17:04:04 +03:00
m_settings . setDefaultServer ( m_settings . serversCount ( ) - 1 ) ;
2021-05-10 14:19:36 +03:00
setStartPage ( Page : : Vpn ) ;
}
else {
qDebug ( ) < < " Failed to import profile " ;
qDebug ( ) . noquote ( ) < < QJsonDocument ( o ) . toJson ( ) ;
2021-05-20 15:59:58 +03:00
return ;
2021-03-14 21:19:11 +03:00
}
2021-05-20 15:59:58 +03:00
if ( ! o . contains ( config_key : : containers ) ) {
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
selectedDockerContainer = m_settings . defaultContainer ( selectedServerIndex ) ;
goToPage ( Page : : ServerVpnProtocols ) ;
}
2021-01-26 15:01:15 +03:00
}
2021-05-18 15:50:52 +03:00
bool MainWindow : : installContainers ( ServerCredentials credentials ,
const QMap < DockerContainer , QJsonObject > & containers ,
2021-04-26 22:54:31 +03:00
QWidget * page , QProgressBar * progress , QPushButton * button , QLabel * info )
2021-01-09 19:55:16 +03:00
{
2021-05-18 15:50:52 +03:00
if ( ! progress ) return false ;
2021-01-09 19:55:16 +03:00
2021-05-18 15:50:52 +03:00
if ( page ) page - > setEnabled ( false ) ;
if ( button ) button - > setVisible ( false ) ;
2021-01-06 17:12:24 +03:00
2021-05-18 15:50:52 +03:00
if ( info ) info - > setVisible ( true ) ;
if ( info ) info - > setText ( tr ( " Please wait, configuring process may take up to 5 minutes " ) ) ;
2021-01-06 17:12:24 +03:00
2021-05-18 15:50:52 +03:00
int cnt = 0 ;
for ( QMap < DockerContainer , QJsonObject > : : const_iterator i = containers . constBegin ( ) ; i ! = containers . constEnd ( ) ; i + + , cnt + + ) {
2021-04-26 22:54:31 +03:00
QTimer timer ;
connect ( & timer , & QTimer : : timeout , [ progress ] ( ) {
progress - > setValue ( progress - > value ( ) + 1 ) ;
} ) ;
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
progress - > setValue ( 0 ) ;
timer . start ( 1000 ) ;
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
progress - > setTextVisible ( true ) ;
2021-05-18 15:50:52 +03:00
progress - > setFormat ( QString ( " Installing %1 %2 %3 " ) . arg ( cnt + 1 ) . arg ( tr ( " of " ) ) . arg ( containers . size ( ) ) ) ;
2021-01-06 17:12:24 +03:00
2021-05-18 15:50:52 +03:00
ErrorCode e = ServerController : : setupContainer ( credentials , i . key ( ) , i . value ( ) ) ;
2021-04-26 22:54:31 +03:00
qDebug ( ) < < " Setup server finished with code " < < e ;
ServerController : : disconnectFromHost ( credentials ) ;
2020-12-30 17:03:05 +03:00
2021-04-26 22:54:31 +03:00
if ( e ) {
2021-05-18 15:50:52 +03:00
if ( page ) page - > setEnabled ( true ) ;
if ( button ) button - > setVisible ( true ) ;
if ( info ) info - > setVisible ( false ) ;
2021-04-26 22:54:31 +03:00
QMessageBox : : warning ( this , APPLICATION_NAME ,
tr ( " Error occurred while configuring server. " ) + " \n " +
errorString ( e ) ) ;
2021-01-03 19:14:54 +03:00
2021-04-26 22:54:31 +03:00
return false ;
}
2021-01-03 19:14:54 +03:00
2021-04-26 22:54:31 +03:00
// just ui progressbar tweak
timer . stop ( ) ;
2021-01-03 19:14:54 +03:00
2021-04-26 22:54:31 +03:00
int remaining_val = progress - > maximum ( ) - progress - > value ( ) ;
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
if ( remaining_val > 0 ) {
QTimer timer1 ;
QEventLoop loop1 ;
2021-01-03 19:14:54 +03:00
2021-04-26 22:54:31 +03:00
connect ( & timer1 , & QTimer : : timeout , [ & ] ( ) {
progress - > setValue ( progress - > value ( ) + 1 ) ;
if ( progress - > value ( ) > = progress - > maximum ( ) ) {
loop1 . quit ( ) ;
}
} ) ;
timer1 . start ( 5 ) ;
loop1 . exec ( ) ;
}
2020-12-30 17:03:05 +03:00
}
2021-01-06 17:12:24 +03:00
2021-04-26 22:54:31 +03:00
2021-05-18 15:50:52 +03:00
if ( button ) button - > show ( ) ;
if ( page ) page - > setEnabled ( true ) ;
if ( info ) info - > setText ( tr ( " Amnezia server installed " ) ) ;
2020-12-30 17:03:05 +03:00
2021-01-09 19:55:16 +03:00
return true ;
2020-12-26 15:03:51 +03:00
}
2021-05-07 23:28:37 +03:00
ErrorCode MainWindow : : doInstallAction ( const std : : function < ErrorCode ( ) > & action , QWidget * page , QProgressBar * progress , QPushButton * button , QLabel * info )
{
progress - > show ( ) ;
if ( page ) page - > setEnabled ( false ) ;
if ( button ) button - > setVisible ( false ) ;
if ( info ) info - > setVisible ( true ) ;
if ( info ) info - > setText ( tr ( " Please wait, configuring process may take up to 5 minutes " ) ) ;
QTimer timer ;
connect ( & timer , & QTimer : : timeout , [ progress ] ( ) {
progress - > setValue ( progress - > value ( ) + 1 ) ;
} ) ;
progress - > setValue ( 0 ) ;
timer . start ( 1000 ) ;
ErrorCode e = action ( ) ;
qDebug ( ) < < " doInstallAction finished with code " < < e ;
2021-07-23 12:42:08 +03:00
// if (e) {
// if (page) page->setEnabled(true);
// if (button) button->setVisible(true);
// if (info) info->setVisible(false);
2021-05-07 23:28:37 +03:00
2021-07-23 12:42:08 +03:00
// QMessageBox::warning(this, APPLICATION_NAME,
// tr("Error occurred while configuring server.") + "\n" +
// errorString(e));
2021-05-07 23:28:37 +03:00
2021-07-23 12:42:08 +03:00
// progress->hide();
// return e;
// }
2021-05-07 23:28:37 +03:00
// just ui progressbar tweak
timer . stop ( ) ;
int remaining_val = progress - > maximum ( ) - progress - > value ( ) ;
if ( remaining_val > 0 ) {
QTimer timer1 ;
QEventLoop loop1 ;
connect ( & timer1 , & QTimer : : timeout , [ & ] ( ) {
progress - > setValue ( progress - > value ( ) + 1 ) ;
if ( progress - > value ( ) > = progress - > maximum ( ) ) {
loop1 . quit ( ) ;
}
} ) ;
timer1 . start ( 5 ) ;
loop1 . exec ( ) ;
}
progress - > hide ( ) ;
if ( button ) button - > show ( ) ;
if ( page ) page - > setEnabled ( true ) ;
if ( info ) info - > setText ( tr ( " Operation finished " ) ) ;
return ErrorCode : : NoError ;
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonClearServer ( bool )
2020-12-30 17:03:05 +03:00
{
2021-04-26 22:54:31 +03:00
ui - > page_server_settings - > setEnabled ( false ) ;
ui - > pushButton_server_settings_clear - > setText ( tr ( " Uninstalling Amnezia software... " ) ) ;
if ( m_settings . defaultServerIndex ( ) = = selectedServerIndex ) {
onDisconnect ( ) ;
}
2020-12-30 17:03:05 +03:00
2021-05-07 23:28:37 +03:00
ErrorCode e = ServerController : : removeAllContainers ( m_settings . serverCredentials ( selectedServerIndex ) ) ;
2021-04-26 22:54:31 +03:00
ServerController : : disconnectFromHost ( m_settings . serverCredentials ( selectedServerIndex ) ) ;
2021-01-09 19:55:16 +03:00
if ( e ) {
QMessageBox : : warning ( this , APPLICATION_NAME ,
tr ( " Error occurred while configuring server. " ) + " \n " +
errorString ( e ) + " \n " +
tr ( " See logs for details. " ) ) ;
2020-12-16 06:02:22 +03:00
2021-01-09 19:55:16 +03:00
}
else {
ui - > label_server_settings_wait_info - > show ( ) ;
ui - > label_server_settings_wait_info - > setText ( tr ( " Amnezia server successfully uninstalled " ) ) ;
}
2021-04-26 22:54:31 +03:00
2021-05-10 20:51:38 +03:00
m_settings . setContainers ( selectedServerIndex , { } ) ;
m_settings . setDefaultContainer ( selectedServerIndex , DockerContainer : : None ) ;
2021-04-26 22:54:31 +03:00
ui - > page_server_settings - > setEnabled ( true ) ;
ui - > pushButton_server_settings_clear - > setText ( tr ( " Clear server from Amnezia software " ) ) ;
2020-12-16 06:02:22 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonForgetServer ( bool )
2020-12-16 06:02:22 +03:00
{
2021-05-20 15:59:58 +03:00
if ( m_settings . defaultServerIndex ( ) = = selectedServerIndex & & m_vpnConnection - > isConnected ( ) ) {
2021-04-26 22:54:31 +03:00
onDisconnect ( ) ;
}
m_settings . removeServer ( selectedServerIndex ) ;
2021-01-09 19:55:16 +03:00
2021-05-10 02:33:31 +03:00
if ( m_settings . defaultServerIndex ( ) = = selectedServerIndex ) {
m_settings . setDefaultServer ( 0 ) ;
}
else if ( m_settings . defaultServerIndex ( ) > selectedServerIndex ) {
m_settings . setDefaultServer ( m_settings . defaultServerIndex ( ) - 1 ) ;
}
2021-05-11 17:04:04 +03:00
if ( m_settings . serversCount ( ) = = 0 ) {
m_settings . setDefaultServer ( - 1 ) ;
}
2021-05-10 02:33:31 +03:00
selectedServerIndex = - 1 ;
2021-05-10 20:51:38 +03:00
updateServersListPage ( ) ;
2021-05-10 05:25:20 -07:00
if ( m_settings . serversCount ( ) = = 0 ) {
setStartPage ( Page : : Start ) ;
}
2021-05-10 20:51:38 +03:00
else {
closePage ( ) ;
}
2020-11-23 16:20:25 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onBytesChanged ( quint64 receivedData , quint64 sentData )
2020-12-16 06:02:22 +03:00
{
2021-01-09 19:55:16 +03:00
ui - > label_speed_received - > setText ( VpnConnection : : bytesPerSecToText ( receivedData ) ) ;
ui - > label_speed_sent - > setText ( VpnConnection : : bytesPerSecToText ( sentData ) ) ;
2020-12-16 06:02:22 +03:00
}
2020-12-26 15:03:51 +03:00
void MainWindow : : onConnectionStateChanged ( VpnProtocol : : ConnectionState state )
{
2021-01-15 23:36:35 +03:00
qDebug ( ) < < " MainWindow::onConnectionStateChanged " < < VpnProtocol : : textConnectionState ( state ) ;
2020-12-26 15:03:51 +03:00
bool pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
bool radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
ui - > label_state - > setText ( VpnProtocol : : textConnectionState ( state ) ) ;
2021-01-09 19:55:16 +03:00
setTrayState ( state ) ;
2020-12-26 15:03:51 +03:00
switch ( state ) {
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Disconnected :
2020-12-26 15:03:51 +03:00
onBytesChanged ( 0 , 0 ) ;
ui - > pushButton_connect - > setChecked ( false ) ;
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Preparing :
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Connecting :
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Connected :
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Disconnecting :
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Reconnecting :
2021-01-09 19:55:16 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Error :
2021-02-18 15:00:41 +03:00
ui - > pushButton_connect - > setChecked ( false ) ;
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Unknown :
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
}
ui - > pushButton_connect - > setEnabled ( pushButtonConnectEnabled ) ;
2021-05-27 22:18:36 +03:00
ui - > widget_vpn_mode - > setEnabled ( radioButtonsModeEnabled ) ;
2020-12-26 15:03:51 +03:00
}
2021-01-08 16:51:58 +03:00
void MainWindow : : onVpnProtocolError ( ErrorCode errorCode )
{
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > setText ( errorString ( errorCode ) ) ;
2021-01-08 16:51:58 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonConnectClicked ( bool checked )
2020-12-16 06:02:22 +03:00
{
2020-12-26 15:03:51 +03:00
if ( checked ) {
2021-01-09 19:55:16 +03:00
onConnect ( ) ;
2020-12-26 15:03:51 +03:00
} else {
2021-01-09 19:55:16 +03:00
onDisconnect ( ) ;
}
}
void MainWindow : : setupTray ( )
{
m_menu = new QMenu ( ) ;
2021-02-24 13:41:32 -08:00
m_menu - > addAction ( QIcon ( " :/images/tray/application.png " ) , tr ( " Show " ) + " " + APPLICATION_NAME , this , [ this ] ( ) {
show ( ) ;
raise ( ) ;
} ) ;
2021-01-09 19:55:16 +03:00
m_menu - > addSeparator ( ) ;
m_trayActionConnect = m_menu - > addAction ( tr ( " Connect " ) , this , SLOT ( onConnect ( ) ) ) ;
m_trayActionDisconnect = m_menu - > addAction ( tr ( " Disconnect " ) , this , SLOT ( onDisconnect ( ) ) ) ;
m_menu - > addSeparator ( ) ;
m_menu - > addAction ( QIcon ( " :/images/tray/link.png " ) , tr ( " Visit Website " ) , [ & ] ( ) {
QDesktopServices : : openUrl ( QUrl ( " https://amnezia.org " ) ) ;
} ) ;
m_menu - > addAction ( QIcon ( " :/images/tray/cancel.png " ) , tr ( " Quit " ) + " " + APPLICATION_NAME , this , [ & ] ( ) {
2021-01-16 21:04:16 +03:00
// QMessageBox::question(this, QMessageBox::question(this, tr("Exit"), tr("Do you really want to quit?"), QMessageBox::Yes | QMessageBox::No, );
2021-01-09 19:55:16 +03:00
QMessageBox msgBox ( QMessageBox : : Question , tr ( " Exit " ) , tr ( " Do you really want to quit? " ) ,
2021-01-16 21:04:16 +03:00
QMessageBox : : Yes | QMessageBox : : No , this , Qt : : Dialog | Qt : : MSWindowsFixedSizeDialogHint | Qt : : WindowStaysOnTopHint ) ;
msgBox . setDefaultButton ( QMessageBox : : Yes ) ;
2021-01-09 19:55:16 +03:00
msgBox . raise ( ) ;
if ( msgBox . exec ( ) = = QMessageBox : : Yes ) {
qApp - > quit ( ) ;
}
} ) ;
m_tray . setContextMenu ( m_menu ) ;
2021-06-01 18:18:09 +03:00
setTrayState ( VpnProtocol : : Disconnected ) ;
2021-01-09 19:55:16 +03:00
m_tray . show ( ) ;
connect ( & m_tray , & QSystemTrayIcon : : activated , this , & MainWindow : : onTrayActivated ) ;
}
void MainWindow : : setTrayIcon ( const QString & iconPath )
{
m_tray . setIcon ( QIcon ( QPixmap ( iconPath ) . scaled ( 128 , 128 ) ) ) ;
}
MainWindow : : Page MainWindow : : currentPage ( )
{
2021-04-20 02:09:47 +03:00
ui - > stackedWidget_main - > waitForAnimation ( ) ;
2021-01-09 19:55:16 +03:00
QWidget * currentPage = ui - > stackedWidget_main - > currentWidget ( ) ;
QMetaEnum e = QMetaEnum : : fromType < MainWindow : : Page > ( ) ;
for ( int k = 0 ; k < e . keyCount ( ) ; k + + ) {
Page p = static_cast < MainWindow : : Page > ( e . value ( k ) ) ;
if ( currentPage = = getPageWidget ( p ) ) return p ;
}
return Page : : Start ;
}
void MainWindow : : setupUiConnections ( )
{
connect ( ui - > pushButton_close , & QPushButton : : clicked , this , [ this ] ( ) {
if ( currentPage ( ) = = Page : : Start | | currentPage ( ) = = Page : : NewServer ) qApp - > quit ( ) ;
else hide ( ) ;
} ) ;
2021-02-18 15:00:41 +03:00
connect ( ui - > pushButton_vpn_add_site , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Sites ) ; } ) ;
2021-01-09 19:55:16 +03:00
2021-04-20 02:09:47 +03:00
2021-05-18 15:50:52 +03:00
QVector < QPushButton * > backButtons {
ui - > pushButton_back_from_sites ,
2021-05-20 15:59:58 +03:00
ui - > pushButton_back_from_general_settings ,
2021-05-18 15:50:52 +03:00
ui - > pushButton_back_from_start ,
ui - > pushButton_back_from_new_server ,
ui - > pushButton_back_from_new_server_protocols ,
ui - > pushButton_back_from_setup_wizard ,
ui - > pushButton_back_from_setup_wizard_high ,
ui - > pushButton_back_from_setup_wizard_low ,
ui - > pushButton_back_from_setup_wizard_medium ,
ui - > pushButton_back_from_setup_wizard_vpn_mode ,
ui - > pushButton_back_from_app_settings ,
ui - > pushButton_back_from_network_settings ,
ui - > pushButton_back_from_server_settings ,
ui - > pushButton_back_from_servers ,
ui - > pushButton_back_from_share ,
ui - > pushButton_back_from_server_vpn_protocols ,
ui - > pushButton_back_from_openvpn_settings ,
ui - > pushButton_back_from_cloak_settings ,
ui - > pushButton_back_from_shadowsocks_settings
} ;
2021-04-20 02:09:47 +03:00
2021-05-18 15:50:52 +03:00
for ( QPushButton * b : backButtons ) {
connect ( b , & QPushButton : : clicked , this , [ this ] ( ) { closePage ( ) ; } ) ;
}
2021-01-09 19:55:16 +03:00
2021-05-11 17:04:04 +03:00
}
2021-05-18 15:50:52 +03:00
void MainWindow : : setupNewServerConnections ( )
{
connect ( ui - > pushButton_new_server_get_info , & QPushButton : : clicked , this , [ ] ( ) {
QDesktopServices : : openUrl ( QUrl ( " https://amnezia.org " ) ) ;
} ) ;
connect ( ui - > pushButton_new_server_connect , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonNewServerConnect ( bool ) ) ) ;
connect ( ui - > pushButton_new_server_import , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonNewServerImport ( bool ) ) ) ;
connect ( ui - > pushButton_new_server_connect_configure , & QPushButton : : clicked , this , [ this ] ( ) {
installServer ( getInstallConfigsFromProtocolsPage ( ) ) ;
} ) ;
}
void MainWindow : : setupWizardConnections ( )
{
connect ( ui - > pushButton_new_server_wizard , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Wizard ) ; } ) ;
connect ( ui - > pushButton_new_server_advanced , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : NewServerProtocols ) ; } ) ;
connect ( ui - > pushButton_setup_wizard_next , & QPushButton : : clicked , this , [ this ] ( ) {
if ( ui - > radioButton_setup_wizard_high - > isChecked ( ) ) goToPage ( Page : : WizardHigh ) ;
2021-05-19 00:15:40 +03:00
else if ( ui - > radioButton_setup_wizard_medium - > isChecked ( ) ) goToPage ( Page : : WizardMedium ) ;
else if ( ui - > radioButton_setup_wizard_low - > isChecked ( ) ) goToPage ( Page : : WizardLow ) ;
2021-05-18 15:50:52 +03:00
} ) ;
connect ( ui - > pushButton_setup_wizard_high_next , & QPushButton : : clicked , this , [ this ] ( ) {
QString domain = ui - > lineEdit_setup_wizard_high_website_masking - > text ( ) ;
if ( domain . isEmpty ( ) | | ! domain . contains ( " . " ) ) return ;
goToPage ( Page : : WizardVpnMode ) ;
} ) ;
connect ( ui - > lineEdit_setup_wizard_high_website_masking , & QLineEdit : : textEdited , this , [ this ] ( ) {
QString text = ui - > lineEdit_setup_wizard_high_website_masking - > text ( ) ;
text . replace ( " http:// " , " " ) ;
text . replace ( " https:// " , " " ) ;
if ( text . isEmpty ( ) ) return ;
text = text . split ( " / " ) . first ( ) ;
ui - > lineEdit_setup_wizard_high_website_masking - > setText ( text ) ;
} ) ;
connect ( ui - > pushButton_setup_wizard_medium_next , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : WizardVpnMode ) ; } ) ;
connect ( ui - > pushButton_setup_wizard_vpn_mode_finish , & QPushButton : : clicked , this , [ this ] ( ) {
installServer ( getInstallConfigsFromWizardPage ( ) ) ;
2021-05-27 22:18:36 +03:00
if ( ui - > checkBox_setup_wizard_vpn_mode - > isChecked ( ) ) m_settings . setRouteMode ( Settings : : VpnOnlyForwardSites ) ;
else m_settings . setRouteMode ( Settings : : VpnAllSites ) ;
2021-05-18 15:50:52 +03:00
} ) ;
connect ( ui - > pushButton_setup_wizard_low_finish , & QPushButton : : clicked , this , [ this ] ( ) {
installServer ( getInstallConfigsFromWizardPage ( ) ) ;
} ) ;
2021-05-19 00:15:40 +03:00
connect ( ui - > lineEdit_setup_wizard_high_website_masking , & QLineEdit : : returnPressed , this , [ this ] ( ) {
ui - > pushButton_setup_wizard_high_next - > click ( ) ;
} ) ;
2021-05-18 15:50:52 +03:00
}
2021-05-27 22:18:36 +03:00
void MainWindow : : setupVpnPageConnections ( )
{
connect ( ui - > radioButton_vpn_mode_all_sites , & QRadioButton : : toggled , ui - > pushButton_vpn_add_site , & QPushButton : : setDisabled ) ;
connect ( ui - > radioButton_vpn_mode_all_sites , & QRadioButton : : toggled , this , [ this ] ( bool toggled ) {
m_settings . setRouteMode ( Settings : : VpnAllSites ) ;
} ) ;
connect ( ui - > radioButton_vpn_mode_forward_sites , & QRadioButton : : toggled , this , [ this ] ( bool toggled ) {
m_settings . setRouteMode ( Settings : : VpnOnlyForwardSites ) ;
} ) ;
connect ( ui - > radioButton_vpn_mode_except_sites , & QRadioButton : : toggled , this , [ this ] ( bool toggled ) {
m_settings . setRouteMode ( Settings : : VpnAllExceptSites ) ;
} ) ;
}
void MainWindow : : setupSitesPageConnections ( )
{
connect ( ui - > pushButton_sites_add_custom , & QPushButton : : clicked , this , [ this ] ( ) { onPushButtonAddCustomSitesClicked ( ) ; } ) ;
connect ( ui - > lineEdit_sites_add_custom , & QLineEdit : : returnPressed , [ & ] ( ) {
ui - > pushButton_sites_add_custom - > click ( ) ;
} ) ;
connect ( ui - > pushButton_sites_delete , & QPushButton : : clicked , this , [ this ] ( ) {
Settings : : RouteMode mode = m_settings . routeMode ( ) ;
QItemSelectionModel * selection = ui - > tableView_sites - > selectionModel ( ) ;
if ( ! selection ) return ;
2021-06-01 18:18:09 +03:00
{
QModelIndexList indexesSites = selection - > selectedRows ( 0 ) ;
2021-05-27 22:18:36 +03:00
2021-06-01 18:18:09 +03:00
QStringList sites ;
for ( const QModelIndex & index : indexesSites ) {
sites . append ( index . data ( ) . toString ( ) ) ;
}
m_settings . removeVpnSites ( mode , sites ) ;
2021-05-27 22:18:36 +03:00
}
2021-06-01 18:18:09 +03:00
if ( m_vpnConnection - > connectionState ( ) = = VpnProtocol : : Connected ) {
QModelIndexList indexesIps = selection - > selectedRows ( 1 ) ;
2021-05-27 22:18:36 +03:00
2021-06-01 18:18:09 +03:00
QStringList ips ;
for ( const QModelIndex & index : indexesIps ) {
2021-06-05 20:55:57 +03:00
if ( index . data ( ) . toString ( ) . isEmpty ( ) ) {
ips . append ( index . sibling ( index . row ( ) , 0 ) . data ( ) . toString ( ) ) ;
}
else {
ips . append ( index . data ( ) . toString ( ) ) ;
}
2021-06-01 18:18:09 +03:00
}
2021-05-27 22:18:36 +03:00
2021-06-01 18:18:09 +03:00
m_vpnConnection - > deleteRoutes ( ips ) ;
m_vpnConnection - > flushDns ( ) ;
}
updateSitesPage ( ) ;
2021-05-27 22:18:36 +03:00
} ) ;
connect ( ui - > pushButton_sites_import , & QPushButton : : clicked , this , [ this ] ( ) {
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Import IP addresses " ) ,
QStandardPaths : : writableLocation ( QStandardPaths : : DocumentsLocation ) ) ;
QFile file ( fileName ) ;
if ( ! file . open ( QIODevice : : ReadOnly ) ) return ;
Settings : : RouteMode mode = m_settings . routeMode ( ) ;
QStringList ips ;
while ( ! file . atEnd ( ) ) {
QString line = file . readLine ( ) ;
int pos = 0 ;
QRegExp rx = Utils : : ipAddressWithSubnetRegExp ( ) ;
while ( ( pos = rx . indexIn ( line , pos ) ) ! = - 1 ) {
ips < < rx . cap ( 0 ) ;
pos + = rx . matchedLength ( ) ;
}
}
m_settings . addVpnIps ( mode , ips ) ;
2021-06-01 18:18:09 +03:00
m_vpnConnection - > addRoutes ( QStringList ( ) < < ips ) ;
m_vpnConnection - > flushDns ( ) ;
2021-05-27 22:18:36 +03:00
updateSitesPage ( ) ;
} ) ;
}
2021-05-11 17:04:04 +03:00
void MainWindow : : setupAppSettingsConnections ( )
{
connect ( ui - > checkBox_app_settings_autostart , & QCheckBox : : stateChanged , this , [ this ] ( int state ) {
2021-02-24 21:58:32 +03:00
if ( state = = Qt : : Unchecked ) {
2021-05-11 17:04:04 +03:00
ui - > checkBox_app_settings_autoconnect - > setChecked ( false ) ;
2021-02-24 21:58:32 +03:00
}
Autostart : : setAutostart ( state = = Qt : : Checked ) ;
} ) ;
2021-05-11 17:04:04 +03:00
connect ( ui - > checkBox_app_settings_autoconnect , & QCheckBox : : stateChanged , this , [ this ] ( int state ) {
2021-02-24 21:58:32 +03:00
m_settings . setAutoConnect ( state = = Qt : : Checked ) ;
} ) ;
2021-05-11 17:04:04 +03:00
connect ( ui - > checkBox_app_settings_start_minimized , & QCheckBox : : stateChanged , this , [ this ] ( int state ) {
m_settings . setStartMinimized ( state = = Qt : : Checked ) ;
2021-02-24 21:58:32 +03:00
} ) ;
2021-05-11 17:04:04 +03:00
connect ( ui - > pushButton_app_settings_check_for_updates , & QPushButton : : clicked , this , [ this ] ( ) {
QDesktopServices : : openUrl ( QUrl ( " https://github.com/amnezia-vpn/desktop-client/releases/latest " ) ) ;
2021-02-24 21:58:32 +03:00
} ) ;
2021-05-18 15:50:52 +03:00
connect ( ui - > pushButton_app_settings_open_logs , & QPushButton : : clicked , this , [ this ] ( ) {
Debug : : openLogsFolder ( ) ;
2021-06-16 19:48:38 +03:00
//QDesktopServices::openUrl(QUrl::fromLocalFile(Utils::systemLogPath()));
2021-05-18 15:50:52 +03:00
} ) ;
}
void MainWindow : : setupGeneralSettingsConnections ( )
{
2021-05-20 15:59:58 +03:00
connect ( ui - > pushButton_general_settings_exit , & QPushButton : : clicked , this , [ & ] ( ) { qApp - > quit ( ) ; } ) ;
2021-05-18 15:50:52 +03:00
connect ( ui - > pushButton_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
2021-05-20 15:59:58 +03:00
connect ( ui - > pushButton_general_settings_app_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : AppSettings ) ; } ) ;
connect ( ui - > pushButton_general_settings_network_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : NetworkSettings ) ; } ) ;
connect ( ui - > pushButton_general_settings_server_settings , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-18 15:50:52 +03:00
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
goToPage ( Page : : ServerSettings ) ;
} ) ;
2021-05-20 15:59:58 +03:00
connect ( ui - > pushButton_general_settings_servers_list , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : ServersList ) ; } ) ;
connect ( ui - > pushButton_general_settings_share_connection , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-18 15:50:52 +03:00
selectedServerIndex = m_settings . defaultServerIndex ( ) ;
selectedDockerContainer = m_settings . defaultContainer ( selectedServerIndex ) ;
updateSharingPage ( selectedServerIndex , m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer ) ;
goToPage ( Page : : ShareConnection ) ;
} ) ;
connect ( ui - > pushButton_general_settings_add_server , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Start ) ; } ) ;
2021-05-11 17:04:04 +03:00
}
2021-02-24 21:58:32 +03:00
2021-05-11 17:04:04 +03:00
void MainWindow : : setupNetworkSettingsConnections ( )
{
2021-02-24 21:58:32 +03:00
connect ( ui - > lineEdit_network_settings_dns1 , & QLineEdit : : textEdited , this , [ this ] ( const QString & newText ) {
if ( m_ipAddressValidator . regExp ( ) . exactMatch ( newText ) ) {
m_settings . setPrimaryDns ( newText ) ;
}
} ) ;
connect ( ui - > lineEdit_network_settings_dns2 , & QLineEdit : : textEdited , this , [ this ] ( const QString & newText ) {
if ( m_ipAddressValidator . regExp ( ) . exactMatch ( newText ) ) {
m_settings . setSecondaryDns ( newText ) ;
}
} ) ;
2021-05-11 17:04:04 +03:00
connect ( ui - > pushButton_network_settings_resetdns1 , & QPushButton : : clicked , this , [ this ] ( ) {
m_settings . setPrimaryDns ( m_settings . cloudFlareNs1 ) ;
updateAppSettingsPage ( ) ;
2021-04-26 22:54:31 +03:00
} ) ;
2021-05-11 17:04:04 +03:00
connect ( ui - > pushButton_network_settings_resetdns2 , & QPushButton : : clicked , this , [ this ] ( ) {
m_settings . setSecondaryDns ( m_settings . cloudFlareNs2 ) ;
updateAppSettingsPage ( ) ;
2021-04-26 22:54:31 +03:00
} ) ;
2021-04-20 02:09:47 +03:00
}
2021-04-26 22:54:31 +03:00
void MainWindow : : setupProtocolsPageConnections ( )
2021-04-20 02:09:47 +03:00
{
2021-04-26 22:54:31 +03:00
QJsonObject openvpnConfig ;
2021-05-07 23:28:37 +03:00
2021-06-12 11:59:36 +03:00
// all containers
2021-05-07 23:28:37 +03:00
QList < DockerContainer > containers {
DockerContainer : : OpenVpn ,
DockerContainer : : OpenVpnOverShadowSocks ,
2021-06-12 11:59:36 +03:00
DockerContainer : : OpenVpnOverCloak ,
DockerContainer : : WireGuard
2021-05-07 23:28:37 +03:00
} ;
// default buttons
QList < QPushButton * > defaultButtons {
ui - > pushButton_proto_openvpn_cont_default ,
ui - > pushButton_proto_ss_openvpn_cont_default ,
2021-06-12 11:59:36 +03:00
ui - > pushButton_proto_cloak_openvpn_cont_default ,
ui - > pushButton_proto_wireguard_cont_default
2021-05-07 23:28:37 +03:00
} ;
for ( int i = 0 ; i < containers . size ( ) ; + + i ) {
connect ( defaultButtons . at ( i ) , & QPushButton : : clicked , this , [ this , containers , i ] ( ) {
m_settings . setDefaultContainer ( selectedServerIndex , containers . at ( i ) ) ;
updateProtocolsPage ( ) ;
} ) ;
}
// install buttons
QList < QPushButton * > installButtons {
ui - > pushButton_proto_openvpn_cont_install ,
ui - > pushButton_proto_ss_openvpn_cont_install ,
2021-06-12 11:59:36 +03:00
ui - > pushButton_proto_cloak_openvpn_cont_install ,
ui - > pushButton_proto_wireguard_cont_install
2021-05-07 23:28:37 +03:00
} ;
for ( int i = 0 ; i < containers . size ( ) ; + + i ) {
QPushButton * button = installButtons . at ( i ) ;
DockerContainer container = containers . at ( i ) ;
connect ( button , & QPushButton : : clicked , this , [ this , container , button ] ( bool checked ) {
if ( checked ) {
ErrorCode e = doInstallAction ( [ this , container ] ( ) {
return ServerController : : setupContainer ( m_settings . serverCredentials ( selectedServerIndex ) , container ) ;
} ,
ui - > page_server_protocols , ui - > progressBar_protocols_container_reinstall ,
nullptr , nullptr ) ;
if ( ! e ) {
m_settings . setContainerConfig ( selectedServerIndex , container , QJsonObject ( ) ) ;
2021-05-10 20:51:38 +03:00
m_settings . setDefaultContainer ( selectedServerIndex , container ) ;
2021-05-07 23:28:37 +03:00
}
}
else {
button - > setEnabled ( false ) ;
ErrorCode e = ServerController : : removeContainer ( m_settings . serverCredentials ( selectedServerIndex ) , container ) ;
m_settings . removeContainerConfig ( selectedServerIndex , container ) ;
button - > setEnabled ( true ) ;
2021-05-10 20:51:38 +03:00
if ( m_settings . defaultContainer ( selectedServerIndex ) = = container ) {
const auto & c = m_settings . containers ( selectedServerIndex ) ;
if ( c . isEmpty ( ) ) m_settings . setDefaultContainer ( selectedServerIndex , DockerContainer : : None ) ;
else m_settings . setDefaultContainer ( selectedServerIndex , c . keys ( ) . first ( ) ) ;
}
2021-05-07 23:28:37 +03:00
}
updateProtocolsPage ( ) ;
} ) ;
}
2021-05-10 02:33:31 +03:00
// share buttons
QList < QPushButton * > shareButtons {
ui - > pushButton_proto_openvpn_cont_share ,
ui - > pushButton_proto_ss_openvpn_cont_share ,
2021-06-12 11:59:36 +03:00
ui - > pushButton_proto_cloak_openvpn_cont_share ,
ui - > pushButton_proto_wireguard_cont_share
2021-05-10 02:33:31 +03:00
} ;
for ( int i = 0 ; i < containers . size ( ) ; + + i ) {
QPushButton * button = shareButtons . at ( i ) ;
DockerContainer container = containers . at ( i ) ;
connect ( button , & QPushButton : : clicked , this , [ this , button , container ] ( ) {
updateSharingPage ( selectedServerIndex , m_settings . serverCredentials ( selectedServerIndex ) , container ) ;
goToPage ( Page : : ShareConnection ) ;
} ) ;
}
2021-05-07 23:28:37 +03:00
// settings buttons
// settings openvpn container
2021-04-20 02:09:47 +03:00
connect ( ui - > pushButton_proto_openvpn_cont_openvpn_config , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-07 23:28:37 +03:00
selectedDockerContainer = DockerContainer : : OpenVpn ;
2021-05-10 02:33:31 +03:00
updateOpenVpnPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : OpenVpn ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-04-20 02:09:47 +03:00
goToPage ( Page : : OpenVpnSettings ) ;
} ) ;
2021-05-07 23:28:37 +03:00
// settings shadowsocks container
2021-04-20 02:09:47 +03:00
connect ( ui - > pushButton_proto_ss_openvpn_cont_openvpn_config , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-07 23:28:37 +03:00
selectedDockerContainer = DockerContainer : : OpenVpnOverShadowSocks ;
2021-05-10 02:33:31 +03:00
updateOpenVpnPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : OpenVpn ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-04-20 02:09:47 +03:00
goToPage ( Page : : OpenVpnSettings ) ;
} ) ;
2021-05-07 23:28:37 +03:00
connect ( ui - > pushButton_proto_ss_openvpn_cont_ss_config , & QPushButton : : clicked , this , [ this ] ( ) {
selectedDockerContainer = DockerContainer : : OpenVpnOverShadowSocks ;
2021-05-10 02:33:31 +03:00
updateShadowSocksPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : ShadowSocks ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-05-07 23:28:37 +03:00
goToPage ( Page : : ShadowSocksSettings ) ;
} ) ;
// settings cloak container
2021-04-20 02:09:47 +03:00
connect ( ui - > pushButton_proto_cloak_openvpn_cont_openvpn_config , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-07 23:28:37 +03:00
selectedDockerContainer = DockerContainer : : OpenVpnOverCloak ;
2021-05-10 02:33:31 +03:00
updateOpenVpnPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : OpenVpn ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-04-20 02:09:47 +03:00
goToPage ( Page : : OpenVpnSettings ) ;
} ) ;
2021-05-07 23:28:37 +03:00
connect ( ui - > pushButton_proto_cloak_openvpn_cont_ss_config , & QPushButton : : clicked , this , [ this ] ( ) {
selectedDockerContainer = DockerContainer : : OpenVpnOverCloak ;
2021-05-10 02:33:31 +03:00
updateShadowSocksPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : ShadowSocks ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-05-07 23:28:37 +03:00
goToPage ( Page : : ShadowSocksSettings ) ;
2021-04-20 02:09:47 +03:00
} ) ;
2021-05-07 23:28:37 +03:00
connect ( ui - > pushButton_proto_cloak_openvpn_cont_cloak_config , & QPushButton : : clicked , this , [ this ] ( ) {
selectedDockerContainer = DockerContainer : : OpenVpnOverCloak ;
2021-05-10 02:33:31 +03:00
updateCloakPage ( m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : Cloak ) ,
2021-05-10 14:19:36 +03:00
selectedDockerContainer , m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-05-07 23:28:37 +03:00
goToPage ( Page : : CloakSettings ) ;
2021-04-20 02:09:47 +03:00
} ) ;
2021-05-07 23:28:37 +03:00
///
// Protocols pages
connect ( ui - > checkBox_proto_openvpn_auto_encryption , & QCheckBox : : stateChanged , this , [ this ] ( ) {
ui - > comboBox_proto_openvpn_cipher - > setDisabled ( ui - > checkBox_proto_openvpn_auto_encryption - > isChecked ( ) ) ;
ui - > comboBox_proto_openvpn_hash - > setDisabled ( ui - > checkBox_proto_openvpn_auto_encryption - > isChecked ( ) ) ;
2021-04-20 02:09:47 +03:00
} ) ;
2021-05-07 23:28:37 +03:00
connect ( ui - > pushButton_proto_openvpn_save , & QPushButton : : clicked , this , [ this ] ( ) {
QJsonObject protocolConfig = m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : OpenVpn ) ;
protocolConfig = getOpenVpnConfigFromPage ( protocolConfig ) ;
QJsonObject containerConfig = m_settings . containerConfig ( selectedServerIndex , selectedDockerContainer ) ;
QJsonObject newContainerConfig = containerConfig ;
newContainerConfig . insert ( config_key : : openvpn , protocolConfig ) ;
ErrorCode e = doInstallAction ( [ this , containerConfig , newContainerConfig ] ( ) {
return ServerController : : updateContainer ( m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer , containerConfig , newContainerConfig ) ;
} ,
ui - > page_proto_openvpn , ui - > progressBar_proto_openvpn_reset ,
ui - > pushButton_proto_openvpn_save , ui - > label_proto_openvpn_info ) ;
if ( ! e ) {
m_settings . setContainerConfig ( selectedServerIndex , selectedDockerContainer , newContainerConfig ) ;
m_settings . clearLastConnectionConfig ( selectedServerIndex , selectedDockerContainer ) ;
}
2021-05-10 02:33:31 +03:00
qDebug ( ) < < " Protocol saved with code: " < < e < < " for " < < selectedServerIndex < < selectedDockerContainer ;
} ) ;
connect ( ui - > pushButton_proto_shadowsocks_save , & QPushButton : : clicked , this , [ this ] ( ) {
QJsonObject protocolConfig = m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : ShadowSocks ) ;
protocolConfig = getShadowSocksConfigFromPage ( protocolConfig ) ;
QJsonObject containerConfig = m_settings . containerConfig ( selectedServerIndex , selectedDockerContainer ) ;
QJsonObject newContainerConfig = containerConfig ;
newContainerConfig . insert ( config_key : : shadowsocks , protocolConfig ) ;
ErrorCode e = doInstallAction ( [ this , containerConfig , newContainerConfig ] ( ) {
return ServerController : : updateContainer ( m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer , containerConfig , newContainerConfig ) ;
} ,
ui - > page_proto_shadowsocks , ui - > progressBar_proto_shadowsocks_reset ,
ui - > pushButton_proto_shadowsocks_save , ui - > label_proto_shadowsocks_info ) ;
if ( ! e ) {
m_settings . setContainerConfig ( selectedServerIndex , selectedDockerContainer , newContainerConfig ) ;
m_settings . clearLastConnectionConfig ( selectedServerIndex , selectedDockerContainer ) ;
}
qDebug ( ) < < " Protocol saved with code: " < < e < < " for " < < selectedServerIndex < < selectedDockerContainer ;
} ) ;
connect ( ui - > pushButton_proto_cloak_save , & QPushButton : : clicked , this , [ this ] ( ) {
QJsonObject protocolConfig = m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , Protocol : : Cloak ) ;
protocolConfig = getCloakConfigFromPage ( protocolConfig ) ;
QJsonObject containerConfig = m_settings . containerConfig ( selectedServerIndex , selectedDockerContainer ) ;
QJsonObject newContainerConfig = containerConfig ;
newContainerConfig . insert ( config_key : : cloak , protocolConfig ) ;
ErrorCode e = doInstallAction ( [ this , containerConfig , newContainerConfig ] ( ) {
return ServerController : : updateContainer ( m_settings . serverCredentials ( selectedServerIndex ) , selectedDockerContainer , containerConfig , newContainerConfig ) ;
} ,
ui - > page_proto_cloak , ui - > progressBar_proto_cloak_reset ,
ui - > pushButton_proto_cloak_save , ui - > label_proto_cloak_info ) ;
if ( ! e ) {
m_settings . setContainerConfig ( selectedServerIndex , selectedDockerContainer , newContainerConfig ) ;
m_settings . clearLastConnectionConfig ( selectedServerIndex , selectedDockerContainer ) ;
}
qDebug ( ) < < " Protocol saved with code: " < < e < < " for " < < selectedServerIndex < < selectedDockerContainer ;
2021-05-07 23:28:37 +03:00
} ) ;
2021-07-23 12:42:08 +03:00
connect ( ui - > pushButton_proto_tor_web_site_cont_install , & QPushButton : : clicked , this , [ this ] ( bool checked ) {
DockerContainer container = DockerContainer : : WebSiteInTor ;
if ( checked ) {
ErrorCode e ;
e = ServerController : : runScript ( ServerController : : sshParams ( m_settings . serverCredentials ( selectedServerIndex ) ) ,
" sudo docker stop amnezia-tor " ) ;
e = ServerController : : runScript ( ServerController : : sshParams ( m_settings . serverCredentials ( selectedServerIndex ) ) ,
" sudo docker rm -f amnezia-tor " ) ;
e = ServerController : : runScript ( ServerController : : sshParams ( m_settings . serverCredentials ( selectedServerIndex ) ) ,
" sudo docker stop amnezia-wp-tor " ) ;
e = ServerController : : runScript ( ServerController : : sshParams ( m_settings . serverCredentials ( selectedServerIndex ) ) ,
" sudo docker rm -f amnezia-wp-tor " ) ;
e = doInstallAction ( [ this , container ] ( ) {
return ServerController : : setupContainer ( m_settings . serverCredentials ( selectedServerIndex ) , container ) ;
} ,
ui - > page_server_protocols , ui - > progressBar_protocols_container_reinstall ,
nullptr , nullptr ) ;
QString stdOut ;
auto cbReadStdOut = [ & ] ( const QString & data , QSharedPointer < QSsh : : SshRemoteProcess > proc ) {
stdOut + = data + " \n " ;
} ;
auto cbReadStdErr = [ & ] ( const QString & data , QSharedPointer < QSsh : : SshRemoteProcess > proc ) {
stdOut + = data + " \n " ;
} ;
e = ServerController : : runScript ( ServerController : : sshParams ( m_settings . serverCredentials ( selectedServerIndex ) ) ,
" sudo docker exec -i amnezia-tor onions " ,
cbReadStdOut , cbReadStdErr ) ;
qDebug ( ) < < " amnezia-tor onions " < < stdOut ;
QStringList l = stdOut . split ( " , " ) ;
for ( QString s : l ) {
if ( s . contains ( " :80 " ) ) {
ui - > label_tor_web_site - > setText ( s ) ;
}
}
}
else {
ui - > pushButton_proto_tor_web_site_cont_install - > setEnabled ( false ) ;
ErrorCode e = ServerController : : removeContainer ( m_settings . serverCredentials ( selectedServerIndex ) , container ) ;
m_settings . removeContainerConfig ( selectedServerIndex , container ) ;
ui - > pushButton_proto_tor_web_site_cont_install - > setEnabled ( true ) ;
}
//updateProtocolsPage();
} ) ;
2021-01-09 19:55:16 +03:00
}
2021-04-26 22:54:31 +03:00
void MainWindow : : setupNewServerPageConnections ( )
{
connect ( ui - > pushButton_connect , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonConnectClicked ( bool ) ) ) ;
connect ( ui - > pushButton_start_switch_page , & QPushButton : : toggled , this , [ this ] ( bool toggled ) {
if ( toggled ) {
ui - > stackedWidget_start - > setCurrentWidget ( ui - > page_start_new_server ) ;
ui - > pushButton_start_switch_page - > setText ( tr ( " Import connection " ) ) ;
}
else {
ui - > stackedWidget_start - > setCurrentWidget ( ui - > page_start_import ) ;
ui - > pushButton_start_switch_page - > setText ( tr ( " Set up your own server " ) ) ;
}
//goToPage(Page::NewServer);
} ) ;
connect ( ui - > pushButton_new_server_connect_key , & QPushButton : : toggled , this , [ this ] ( bool checked ) {
ui - > label_new_server_password - > setText ( checked ? tr ( " Private key " ) : tr ( " Password " ) ) ;
ui - > pushButton_new_server_connect_key - > setText ( checked ? tr ( " Connect using SSH password " ) : tr ( " Connect using SSH key " ) ) ;
ui - > lineEdit_new_server_password - > setVisible ( ! checked ) ;
ui - > textEdit_new_server_ssh_key - > setVisible ( checked ) ;
} ) ;
connect ( ui - > pushButton_new_server_settings_cloak , & QPushButton : : toggled , this , [ this ] ( bool toggle ) {
ui - > frame_new_server_settings_cloak - > setMaximumHeight ( toggle * 200 ) ;
if ( toggle )
ui - > frame_new_server_settings_parent_cloak - > layout ( ) - > addWidget ( ui - > frame_new_server_settings_cloak ) ;
else
ui - > frame_new_server_settings_parent_cloak - > layout ( ) - > removeWidget ( ui - > frame_new_server_settings_cloak ) ;
} ) ;
connect ( ui - > pushButton_new_server_settings_ss , & QPushButton : : toggled , this , [ this ] ( bool toggle ) {
ui - > frame_new_server_settings_ss - > setMaximumHeight ( toggle * 200 ) ;
if ( toggle )
ui - > frame_new_server_settings_parent_ss - > layout ( ) - > addWidget ( ui - > frame_new_server_settings_ss ) ;
else
ui - > frame_new_server_settings_parent_ss - > layout ( ) - > removeWidget ( ui - > frame_new_server_settings_ss ) ;
} ) ;
connect ( ui - > pushButton_new_server_settings_openvpn , & QPushButton : : toggled , this , [ this ] ( bool toggle ) {
ui - > frame_new_server_settings_openvpn - > setMaximumHeight ( toggle * 200 ) ;
if ( toggle )
ui - > frame_new_server_settings_parent_openvpn - > layout ( ) - > addWidget ( ui - > frame_new_server_settings_openvpn ) ;
else
ui - > frame_new_server_settings_parent_openvpn - > layout ( ) - > removeWidget ( ui - > frame_new_server_settings_ss ) ;
} ) ;
}
2021-05-10 02:33:31 +03:00
void MainWindow : : setupServerSettingsPageConnections ( )
{
2021-05-27 22:18:36 +03:00
connect ( ui - > pushButton_servers_add_new , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Start ) ; } ) ;
2021-05-10 02:33:31 +03:00
connect ( ui - > pushButton_server_settings_protocols , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : ServerVpnProtocols ) ; } ) ;
2021-05-27 22:18:36 +03:00
connect ( ui - > pushButton_server_settings_share_full , & QPushButton : : clicked , this , [ this ] ( ) {
updateSharingPage ( selectedServerIndex , m_settings . serverCredentials ( selectedServerIndex ) , DockerContainer : : None ) ;
goToPage ( Page : : ShareConnection ) ;
} ) ;
2021-05-10 02:33:31 +03:00
connect ( ui - > pushButton_server_settings_clear , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonClearServer ( bool ) ) ) ;
connect ( ui - > pushButton_server_settings_forget , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonForgetServer ( bool ) ) ) ;
connect ( ui - > pushButton_server_settings_clear_client_cache , & QPushButton : : clicked , this , [ this ] ( ) {
ui - > pushButton_server_settings_clear_client_cache - > setText ( tr ( " Cache cleared " ) ) ;
const auto & containers = m_settings . containers ( selectedServerIndex ) ;
for ( DockerContainer container : containers . keys ( ) ) {
m_settings . clearLastConnectionConfig ( selectedServerIndex , container ) ;
}
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_server_settings_clear_client_cache - > setText ( tr ( " Clear client cached profile " ) ) ;
} ) ;
} ) ;
2021-05-27 22:18:36 +03:00
connect ( ui - > lineEdit_server_settings_description , & QLineEdit : : editingFinished , this , [ this ] ( ) {
const QString & newText = ui - > lineEdit_server_settings_description - > text ( ) ;
QJsonObject server = m_settings . server ( selectedServerIndex ) ;
server . insert ( config_key : : description , newText ) ;
m_settings . editServer ( selectedServerIndex , server ) ;
updateServersListPage ( ) ;
} ) ;
connect ( ui - > lineEdit_server_settings_description , & QLineEdit : : returnPressed , this , [ this ] ( ) {
ui - > lineEdit_server_settings_description - > clearFocus ( ) ;
} ) ;
2021-05-10 02:33:31 +03:00
}
void MainWindow : : setupSharePageConnections ( )
{
connect ( ui - > pushButton_share_full_copy , & QPushButton : : clicked , this , [ this ] ( ) {
QGuiApplication : : clipboard ( ) - > setText ( ui - > textEdit_share_full_code - > toPlainText ( ) ) ;
ui - > pushButton_share_full_copy - > setText ( tr ( " Copied " ) ) ;
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_share_full_copy - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
2021-06-16 19:48:38 +03:00
connect ( ui - > pushButton_share_full_save , & QPushButton : : clicked , this , [ this ] ( ) {
if ( ui - > textEdit_share_full_code - > toPlainText ( ) . isEmpty ( ) ) return ;
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save AmneziaVPN config " ) ,
QStandardPaths : : writableLocation ( QStandardPaths : : DocumentsLocation ) , " *.amnezia " ) ;
QSaveFile save ( fileName ) ;
save . open ( QIODevice : : WriteOnly ) ;
save . write ( ui - > textEdit_share_full_code - > toPlainText ( ) . toUtf8 ( ) ) ;
save . commit ( ) ;
} ) ;
2021-05-10 02:33:31 +03:00
connect ( ui - > pushButton_share_amnezia_copy , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-22 16:14:26 +03:00
if ( ui - > textEdit_share_amnezia_code - > toPlainText ( ) . isEmpty ( ) ) return ;
2021-05-10 02:33:31 +03:00
QGuiApplication : : clipboard ( ) - > setText ( ui - > textEdit_share_amnezia_code - > toPlainText ( ) ) ;
ui - > pushButton_share_amnezia_copy - > setText ( tr ( " Copied " ) ) ;
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_share_amnezia_copy - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
2021-05-22 16:14:26 +03:00
connect ( ui - > pushButton_share_amnezia_save , & QPushButton : : clicked , this , [ this ] ( ) {
if ( ui - > textEdit_share_amnezia_code - > toPlainText ( ) . isEmpty ( ) ) return ;
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save AmneziaVPN config " ) ,
QStandardPaths : : writableLocation ( QStandardPaths : : DocumentsLocation ) , " *.amnezia " ) ;
QSaveFile save ( fileName ) ;
save . open ( QIODevice : : WriteOnly ) ;
save . write ( ui - > textEdit_share_amnezia_code - > toPlainText ( ) . toUtf8 ( ) ) ;
save . commit ( ) ;
} ) ;
2021-05-10 02:33:31 +03:00
connect ( ui - > pushButton_share_openvpn_copy , & QPushButton : : clicked , this , [ this ] ( ) {
QGuiApplication : : clipboard ( ) - > setText ( ui - > textEdit_share_openvpn_code - > toPlainText ( ) ) ;
ui - > pushButton_share_openvpn_copy - > setText ( tr ( " Copied " ) ) ;
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_share_openvpn_copy - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
connect ( ui - > pushButton_share_ss_copy , & QPushButton : : clicked , this , [ this ] ( ) {
QGuiApplication : : clipboard ( ) - > setText ( ui - > lineEdit_share_ss_string - > text ( ) ) ;
ui - > pushButton_share_ss_copy - > setText ( tr ( " Copied " ) ) ;
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_share_ss_copy - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
2021-06-16 19:48:38 +03:00
connect ( ui - > pushButton_share_cloak_copy , & QPushButton : : clicked , this , [ this ] ( ) {
QGuiApplication : : clipboard ( ) - > setText ( ui - > plainTextEdit_share_cloak - > toPlainText ( ) ) ;
ui - > pushButton_share_cloak_copy - > setText ( tr ( " Copied " ) ) ;
2021-05-10 02:33:31 +03:00
2021-06-16 19:48:38 +03:00
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
ui - > pushButton_share_cloak_copy - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
2021-05-10 02:33:31 +03:00
connect ( ui - > pushButton_share_amnezia_generate , & QPushButton : : clicked , this , [ this ] ( ) {
ui - > pushButton_share_amnezia_generate - > setEnabled ( false ) ;
ui - > pushButton_share_amnezia_copy - > setEnabled ( false ) ;
ui - > pushButton_share_amnezia_generate - > setText ( tr ( " Generating... " ) ) ;
qApp - > processEvents ( ) ;
ServerCredentials credentials = m_settings . serverCredentials ( selectedServerIndex ) ;
2021-05-11 17:04:04 +03:00
QJsonObject containerConfig = m_settings . containerConfig ( selectedServerIndex , selectedDockerContainer ) ;
2021-05-10 02:33:31 +03:00
containerConfig . insert ( config_key : : container , containerToString ( selectedDockerContainer ) ) ;
2021-05-10 20:51:38 +03:00
ErrorCode e = ErrorCode : : NoError ;
2021-05-10 02:33:31 +03:00
for ( Protocol p : amnezia : : protocolsForContainer ( selectedDockerContainer ) ) {
QJsonObject protoConfig = m_settings . protocolConfig ( selectedServerIndex , selectedDockerContainer , p ) ;
QString cfg = VpnConfigurator : : genVpnProtocolConfig ( credentials , selectedDockerContainer , containerConfig , p , & e ) ;
if ( e ) {
cfg = " Error generating config " ;
break ;
}
protoConfig . insert ( config_key : : last_config , cfg ) ;
containerConfig . insert ( protoToString ( p ) , protoConfig ) ;
}
2021-05-10 20:51:38 +03:00
QByteArray ba ;
if ( ! e ) {
QJsonObject serverConfig = m_settings . server ( selectedServerIndex ) ;
serverConfig . remove ( config_key : : userName ) ;
serverConfig . remove ( config_key : : password ) ;
serverConfig . remove ( config_key : : port ) ;
serverConfig . insert ( config_key : : containers , QJsonArray { containerConfig } ) ;
serverConfig . insert ( config_key : : defaultContainer , containerToString ( selectedDockerContainer ) ) ;
2021-05-10 02:33:31 +03:00
2021-05-10 20:51:38 +03:00
ba = QJsonDocument ( serverConfig ) . toJson ( ) . toBase64 ( QByteArray : : Base64UrlEncoding | QByteArray : : OmitTrailingEquals ) ;
ui - > textEdit_share_amnezia_code - > setPlainText ( QString ( " vpn://%1 " ) . arg ( QString ( ba ) ) ) ;
}
else {
ui - > textEdit_share_amnezia_code - > setPlainText ( tr ( " Error while generating connection profile " ) ) ;
}
2021-05-10 02:33:31 +03:00
ui - > pushButton_share_amnezia_generate - > setEnabled ( true ) ;
ui - > pushButton_share_amnezia_copy - > setEnabled ( true ) ;
ui - > pushButton_share_amnezia_generate - > setText ( tr ( " Generate config " ) ) ;
} ) ;
connect ( ui - > pushButton_share_openvpn_generate , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-11 17:04:04 +03:00
ui - > pushButton_share_openvpn_generate - > setEnabled ( false ) ;
2021-05-10 02:33:31 +03:00
ui - > pushButton_share_openvpn_copy - > setEnabled ( false ) ;
ui - > pushButton_share_openvpn_save - > setEnabled ( false ) ;
2021-05-11 17:04:04 +03:00
ui - > pushButton_share_openvpn_generate - > setText ( tr ( " Generating... " ) ) ;
ServerCredentials credentials = m_settings . serverCredentials ( selectedServerIndex ) ;
const QJsonObject & containerConfig = m_settings . containerConfig ( selectedServerIndex , selectedDockerContainer ) ;
2021-05-10 02:33:31 +03:00
ErrorCode e = ErrorCode : : NoError ;
QString cfg = OpenVpnConfigurator : : genOpenVpnConfig ( credentials , selectedDockerContainer , containerConfig , & e ) ;
2021-05-20 15:59:58 +03:00
cfg = OpenVpnConfigurator : : processConfigWithExportSettings ( cfg ) ;
2021-05-10 02:33:31 +03:00
ui - > textEdit_share_openvpn_code - > setPlainText ( cfg ) ;
2021-05-11 17:04:04 +03:00
ui - > pushButton_share_openvpn_generate - > setEnabled ( true ) ;
2021-05-10 02:33:31 +03:00
ui - > pushButton_share_openvpn_copy - > setEnabled ( true ) ;
ui - > pushButton_share_openvpn_save - > setEnabled ( true ) ;
2021-05-11 17:04:04 +03:00
ui - > pushButton_share_openvpn_generate - > setText ( tr ( " Generate config " ) ) ;
2021-05-10 02:33:31 +03:00
} ) ;
connect ( ui - > pushButton_share_openvpn_save , & QPushButton : : clicked , this , [ this ] ( ) {
2021-05-22 16:14:26 +03:00
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save OpenVPN config " ) ,
QStandardPaths : : writableLocation ( QStandardPaths : : DocumentsLocation ) , " *.ovpn " ) ;
2021-05-10 02:33:31 +03:00
QSaveFile save ( fileName ) ;
save . open ( QIODevice : : WriteOnly ) ;
save . write ( ui - > textEdit_share_openvpn_code - > toPlainText ( ) . toUtf8 ( ) ) ;
save . commit ( ) ;
} ) ;
}
2021-01-09 19:55:16 +03:00
void MainWindow : : setTrayState ( VpnProtocol : : ConnectionState state )
{
QString resourcesPath = " :/images/tray/%1 " ;
2021-06-01 18:18:09 +03:00
m_trayActionDisconnect - > setEnabled ( state = = VpnProtocol : : Connected ) ;
m_trayActionConnect - > setEnabled ( state = = VpnProtocol : : Disconnected ) ;
2021-01-09 19:55:16 +03:00
switch ( state ) {
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Disconnected :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Preparing :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Connecting :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Connected :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( ConnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Disconnecting :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Reconnecting :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Error :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( ErrorTrayIconName ) ) ;
break ;
2021-06-01 18:18:09 +03:00
case VpnProtocol : : Unknown :
2021-01-09 19:55:16 +03:00
default :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
}
2021-01-10 16:50:38 +03:00
//#ifdef Q_OS_MAC
// // Get theme from current user (note, this app can be launched as root application and in this case this theme can be different from theme of real current user )
// bool darkTaskBar = MacOSFunctions::instance().isMenuBarUseDarkTheme();
// darkTaskBar = forceUseBrightIcons ? true : darkTaskBar;
// resourcesPath = ":/images_mac/tray_icon/%1";
// useIconName = useIconName.replace(".png", darkTaskBar ? "@2x.png" : " dark@2x.png");
//#endif
2021-01-09 19:55:16 +03:00
}
void MainWindow : : onTrayActivated ( QSystemTrayIcon : : ActivationReason reason )
{
2021-06-26 23:09:48 +00:00
# if defined Q_OS_MACX || defined Q_OS_LINUX
2021-01-09 19:55:16 +03:00
if ( reason = = QSystemTrayIcon : : DoubleClick | | reason = = QSystemTrayIcon : : Trigger ) {
show ( ) ;
raise ( ) ;
setWindowState ( Qt : : WindowActive ) ;
}
2021-01-16 21:04:16 +03:00
# endif
2021-01-09 19:55:16 +03:00
}
void MainWindow : : onConnect ( )
2021-05-07 23:28:37 +03:00
{
int serverIndex = m_settings . defaultServerIndex ( ) ;
ServerCredentials credentials = m_settings . serverCredentials ( serverIndex ) ;
DockerContainer container = m_settings . defaultContainer ( serverIndex ) ;
2021-05-10 20:51:38 +03:00
if ( m_settings . containers ( serverIndex ) . isEmpty ( ) ) {
ui - > label_error_text - > setText ( tr ( " VPN Protocols is not installed. \n Please install VPN container at first " ) ) ;
ui - > pushButton_connect - > setChecked ( false ) ;
return ;
}
if ( container = = DockerContainer : : None ) {
ui - > label_error_text - > setText ( tr ( " VPN Protocol not choosen " ) ) ;
ui - > pushButton_connect - > setChecked ( false ) ;
return ;
}
2021-05-07 23:28:37 +03:00
const QJsonObject & containerConfig = m_settings . containerConfig ( serverIndex , container ) ;
onConnectWorker ( serverIndex , credentials , container , containerConfig ) ;
}
void MainWindow : : onConnectWorker ( int serverIndex , const ServerCredentials & credentials , DockerContainer container , const QJsonObject & containerConfig )
2021-01-09 19:55:16 +03:00
{
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > clear ( ) ;
2021-01-09 19:55:16 +03:00
ui - > pushButton_connect - > setChecked ( true ) ;
qApp - > processEvents ( ) ;
2021-06-01 18:18:09 +03:00
ErrorCode errorCode = m_vpnConnection - > connectToVpn (
serverIndex , credentials , container , containerConfig
) ;
2021-01-09 19:55:16 +03:00
if ( errorCode ) {
//ui->pushButton_connect->setChecked(false);
QMessageBox : : critical ( this , APPLICATION_NAME , errorString ( errorCode ) ) ;
return ;
2020-12-26 15:03:51 +03:00
}
2021-01-15 23:36:35 +03:00
2021-01-09 19:55:16 +03:00
ui - > pushButton_connect - > setEnabled ( false ) ;
}
void MainWindow : : onDisconnect ( )
{
ui - > pushButton_connect - > setChecked ( false ) ;
m_vpnConnection - > disconnectFromVpn ( ) ;
2020-12-16 06:02:22 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onTrayActionConnect ( )
2021-01-03 19:14:54 +03:00
{
2021-01-09 19:55:16 +03:00
if ( m_trayActionConnect - > text ( ) = = tr ( " Connect " ) ) {
onConnect ( ) ;
} else if ( m_trayActionConnect - > text ( ) = = tr ( " Disconnect " ) ) {
onDisconnect ( ) ;
}
2021-01-03 19:14:54 +03:00
}
2021-01-09 19:55:16 +03:00
2021-01-26 15:01:15 +03:00
void MainWindow : : onPushButtonAddCustomSitesClicked ( )
{
2021-05-27 22:18:36 +03:00
if ( ui - > radioButton_vpn_mode_all_sites - > isChecked ( ) ) return ;
Settings : : RouteMode mode = m_settings . routeMode ( ) ;
2021-01-26 15:01:15 +03:00
QString newSite = ui - > lineEdit_sites_add_custom - > text ( ) ;
if ( newSite . isEmpty ( ) ) return ;
if ( ! newSite . contains ( " . " ) ) return ;
2021-05-27 22:18:36 +03:00
if ( ! Utils : : ipAddressWithSubnetRegExp ( ) . exactMatch ( newSite ) ) {
// get domain name if it present
newSite . replace ( " https:// " , " " ) ;
newSite . replace ( " http:// " , " " ) ;
newSite . replace ( " ftp:// " , " " ) ;
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
newSite = newSite . split ( " / " , QString : : SkipEmptyParts ) . first ( ) ;
}
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
const auto & cbProcess = [ this , mode ] ( const QString & newSite , const QString & ip ) {
m_settings . addVpnSite ( mode , newSite , ip ) ;
2021-01-26 15:01:15 +03:00
2021-06-05 20:55:57 +03:00
if ( ! ip . isEmpty ( ) ) {
m_vpnConnection - > addRoutes ( QStringList ( ) < < ip ) ;
m_vpnConnection - > flushDns ( ) ;
}
2021-06-12 11:59:36 +03:00
else if ( Utils : : ipAddressWithSubnetRegExp ( ) . exactMatch ( newSite ) ) {
m_vpnConnection - > addRoutes ( QStringList ( ) < < newSite ) ;
m_vpnConnection - > flushDns ( ) ;
}
2021-01-26 15:01:15 +03:00
2021-05-10 20:51:38 +03:00
updateSitesPage ( ) ;
2021-05-27 22:18:36 +03:00
} ;
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
const auto & cbResolv = [ this , cbProcess ] ( const QHostInfo & hostInfo ) {
const QList < QHostAddress > & addresses = hostInfo . addresses ( ) ;
2021-06-03 20:23:44 +03:00
QString ipv4Addr ;
for ( const QHostAddress & addr : hostInfo . addresses ( ) ) {
if ( addr . protocol ( ) = = QAbstractSocket : : NetworkLayerProtocol : : IPv4Protocol ) {
2021-06-05 20:55:57 +03:00
cbProcess ( hostInfo . hostName ( ) , addr . toString ( ) ) ;
2021-06-03 20:23:44 +03:00
break ;
}
2021-05-27 22:18:36 +03:00
}
} ;
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
ui - > lineEdit_sites_add_custom - > clear ( ) ;
2021-01-26 15:01:15 +03:00
2021-05-27 22:18:36 +03:00
if ( Utils : : ipAddressWithSubnetRegExp ( ) . exactMatch ( newSite ) ) {
2021-06-05 20:55:57 +03:00
cbProcess ( newSite , " " ) ;
2021-05-27 22:18:36 +03:00
return ;
}
else {
cbProcess ( newSite , " " ) ;
updateSitesPage ( ) ;
2021-06-03 20:23:44 +03:00
QHostInfo : : lookupHost ( newSite , this , cbResolv ) ;
2021-02-18 15:00:41 +03:00
}
2021-01-26 15:01:15 +03:00
}
2021-05-10 20:51:38 +03:00
void MainWindow : : updateStartPage ( )
2021-05-07 23:28:37 +03:00
{
2021-05-10 20:51:38 +03:00
ui - > lineEdit_start_existing_code - > clear ( ) ;
ui - > textEdit_new_server_ssh_key - > clear ( ) ;
ui - > lineEdit_new_server_ip - > clear ( ) ;
ui - > lineEdit_new_server_password - > clear ( ) ;
ui - > textEdit_new_server_ssh_key - > clear ( ) ;
ui - > lineEdit_new_server_login - > setText ( " root " ) ;
ui - > label_new_server_wait_info - > hide ( ) ;
ui - > label_new_server_wait_info - > clear ( ) ;
ui - > progressBar_new_server_connection - > setMinimum ( 0 ) ;
ui - > progressBar_new_server_connection - > setMaximum ( 300 ) ;
ui - > pushButton_back_from_start - > setVisible ( ! pagesStack . isEmpty ( ) ) ;
2021-05-07 23:28:37 +03:00
2021-05-10 20:51:38 +03:00
ui - > pushButton_new_server_connect - > setVisible ( true ) ;
2021-05-07 23:28:37 +03:00
}
void MainWindow : : updateSitesPage ( )
{
2021-05-27 22:18:36 +03:00
Settings : : RouteMode m = m_settings . routeMode ( ) ;
if ( m = = Settings : : VpnAllSites ) return ;
2021-06-03 20:23:44 +03:00
if ( m = = Settings : : VpnOnlyForwardSites ) ui - > label_sites_add_custom - > setText ( tr ( " These sites will be opened using VPN " ) ) ;
if ( m = = Settings : : VpnAllExceptSites ) ui - > label_sites_add_custom - > setText ( tr ( " These sites will be excepted from VPN " ) ) ;
2021-05-27 22:18:36 +03:00
ui - > tableView_sites - > setModel ( sitesModels . value ( m ) ) ;
sitesModels . value ( m ) - > resetCache ( ) ;
2021-05-07 23:28:37 +03:00
}
void MainWindow : : updateVpnPage ( )
2021-01-26 15:01:15 +03:00
{
2021-05-27 22:18:36 +03:00
Settings : : RouteMode mode = m_settings . routeMode ( ) ;
ui - > radioButton_vpn_mode_all_sites - > setChecked ( mode = = Settings : : VpnAllSites ) ;
ui - > radioButton_vpn_mode_forward_sites - > setChecked ( mode = = Settings : : VpnOnlyForwardSites ) ;
ui - > radioButton_vpn_mode_except_sites - > setChecked ( mode = = Settings : : VpnAllExceptSites ) ;
ui - > pushButton_vpn_add_site - > setEnabled ( mode ! = Settings : : VpnAllSites ) ;
2021-05-07 23:28:37 +03:00
}
2021-02-24 21:58:32 +03:00
2021-05-07 23:28:37 +03:00
void MainWindow : : updateAppSettingsPage ( )
{
2021-05-11 17:04:04 +03:00
ui - > checkBox_app_settings_autostart - > setChecked ( Autostart : : isAutostart ( ) ) ;
ui - > checkBox_app_settings_autoconnect - > setChecked ( m_settings . isAutoConnect ( ) ) ;
ui - > checkBox_app_settings_start_minimized - > setChecked ( m_settings . isStartMinimized ( ) ) ;
2021-02-24 21:58:32 +03:00
ui - > lineEdit_network_settings_dns1 - > setText ( m_settings . primaryDns ( ) ) ;
ui - > lineEdit_network_settings_dns2 - > setText ( m_settings . secondaryDns ( ) ) ;
2021-06-01 18:18:09 +03:00
QString ver = QString ( " %1: %2 (%3) " )
. arg ( tr ( " Software version " ) )
. arg ( QString ( APP_MAJOR_VERSION ) )
. arg ( __DATE__ ) ;
ui - > label_app_settings_version - > setText ( ver ) ;
2021-05-07 23:28:37 +03:00
}
2021-02-25 21:16:00 +03:00
2021-05-20 15:59:58 +03:00
void MainWindow : : updateGeneralSettingPage ( )
{
ui - > pushButton_general_settings_share_connection - > setEnabled ( m_settings . haveAuthData ( m_settings . defaultServerIndex ( ) ) ) ;
}
2021-05-07 23:28:37 +03:00
void MainWindow : : updateServerPage ( )
{
ui - > label_server_settings_wait_info - > hide ( ) ;
ui - > label_server_settings_wait_info - > clear ( ) ;
2021-05-10 14:19:36 +03:00
ui - > pushButton_server_settings_clear - > setVisible ( m_settings . haveAuthData ( selectedServerIndex ) ) ;
ui - > pushButton_server_settings_clear_client_cache - > setVisible ( m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-06-03 20:23:44 +03:00
ui - > pushButton_server_settings_share_full - > setVisible ( m_settings . haveAuthData ( selectedServerIndex ) ) ;
2021-05-10 14:19:36 +03:00
2021-05-07 23:28:37 +03:00
QJsonObject server = m_settings . server ( selectedServerIndex ) ;
QString port = server . value ( config_key : : port ) . toString ( ) ;
ui - > label_server_settings_server - > setText ( QString ( " %1@%2%3%4 " )
. arg ( server . value ( config_key : : userName ) . toString ( ) )
. arg ( server . value ( config_key : : hostName ) . toString ( ) )
. arg ( port . isEmpty ( ) ? " " : " : " )
. arg ( port ) ) ;
ui - > lineEdit_server_settings_description - > setText ( server . value ( config_key : : description ) . toString ( ) ) ;
2021-04-20 02:09:47 +03:00
QString selectedContainerName = m_settings . defaultContainerName ( selectedServerIndex ) ;
ui - > label_server_settings_current_vpn_protocol - > setText ( tr ( " Protocol: " ) + selectedContainerName ) ;
2021-05-07 23:28:37 +03:00
//qDebug() << "DefaultContainer(selectedServerIndex)" << selectedServerIndex << containerToString(m_settings.defaultContainer(selectedServerIndex));
2021-01-26 15:01:15 +03:00
}
2021-05-07 23:28:37 +03:00
void MainWindow : : updateServersListPage ( )
2021-04-26 22:54:31 +03:00
{
ui - > listWidget_servers - > clear ( ) ;
const QJsonArray & servers = m_settings . serversArray ( ) ;
int defaultServer = m_settings . defaultServerIndex ( ) ;
ui - > listWidget_servers - > setUpdatesEnabled ( false ) ;
for ( int i = 0 ; i < servers . size ( ) ; i + + ) {
makeServersListItem ( ui - > listWidget_servers , servers . at ( i ) . toObject ( ) , i = = defaultServer , i ) ;
}
ui - > listWidget_servers - > setUpdatesEnabled ( true ) ;
}
2021-05-07 23:28:37 +03:00
void MainWindow : : updateProtocolsPage ( )
{
ui - > progressBar_protocols_container_reinstall - > hide ( ) ;
auto containers = m_settings . containers ( selectedServerIndex ) ;
2021-06-12 11:59:36 +03:00
DockerContainer defaultContainer = m_settings . defaultContainer ( selectedServerIndex ) ;
2021-05-10 14:19:36 +03:00
bool haveAuthData = m_settings . haveAuthData ( selectedServerIndex ) ;
2021-06-12 11:59:36 +03:00
// all containers
QList < DockerContainer > allContainers {
DockerContainer : : OpenVpn ,
DockerContainer : : OpenVpnOverShadowSocks ,
DockerContainer : : OpenVpnOverCloak ,
DockerContainer : : WireGuard
} ;
// install buttons
QList < QPushButton * > installButtons {
ui - > pushButton_proto_openvpn_cont_install ,
ui - > pushButton_proto_ss_openvpn_cont_install ,
ui - > pushButton_proto_cloak_openvpn_cont_install ,
ui - > pushButton_proto_wireguard_cont_install
} ;
2021-05-07 23:28:37 +03:00
2021-06-12 11:59:36 +03:00
// default buttons
QList < QPushButton * > defaultButtons {
ui - > pushButton_proto_openvpn_cont_default ,
ui - > pushButton_proto_ss_openvpn_cont_default ,
ui - > pushButton_proto_cloak_openvpn_cont_default ,
ui - > pushButton_proto_wireguard_cont_default
} ;
2021-05-07 23:28:37 +03:00
2021-06-12 11:59:36 +03:00
// share buttons
QList < QPushButton * > shareButtons {
ui - > pushButton_proto_openvpn_cont_share ,
ui - > pushButton_proto_ss_openvpn_cont_share ,
ui - > pushButton_proto_cloak_openvpn_cont_share ,
ui - > pushButton_proto_wireguard_cont_share
} ;
2021-05-07 23:28:37 +03:00
2021-06-12 11:59:36 +03:00
// frames
QList < QFrame * > frames {
ui - > frame_openvpn_settings ,
ui - > frame_openvpn_ss_settings ,
ui - > frame_openvpn_ss_cloak_settings ,
ui - > frame_wireguard_settings
} ;
2021-05-07 23:28:37 +03:00
2021-06-12 11:59:36 +03:00
for ( int i = 0 ; i < allContainers . size ( ) ; + + i ) {
defaultButtons . at ( i ) - > setChecked ( defaultContainer = = allContainers . at ( i ) ) ;
defaultButtons . at ( i ) - > setVisible ( haveAuthData & & containers . contains ( allContainers . at ( i ) ) ) ;
shareButtons . at ( i ) - > setVisible ( haveAuthData & & containers . contains ( allContainers . at ( i ) ) ) ;
installButtons . at ( i ) - > setChecked ( containers . contains ( allContainers . at ( i ) ) ) ;
installButtons . at ( i ) - > setEnabled ( haveAuthData ) ;
frames . at ( i ) - > setVisible ( containers . contains ( allContainers . at ( i ) ) ) ;
2021-05-10 14:19:36 +03:00
2021-06-12 11:59:36 +03:00
}
2021-05-07 23:28:37 +03:00
}
2021-05-10 14:19:36 +03:00
void MainWindow : : updateOpenVpnPage ( const QJsonObject & openvpnConfig , DockerContainer container , bool haveAuthData )
2021-05-07 23:28:37 +03:00
{
2021-05-10 14:19:36 +03:00
ui - > widget_proto_openvpn - > setEnabled ( haveAuthData ) ;
ui - > pushButton_proto_openvpn_save - > setVisible ( haveAuthData ) ;
ui - > progressBar_proto_openvpn_reset - > setVisible ( haveAuthData ) ;
2021-05-07 23:28:37 +03:00
ui - > radioButton_proto_openvpn_udp - > setEnabled ( true ) ;
ui - > radioButton_proto_openvpn_tcp - > setEnabled ( true ) ;
ui - > lineEdit_proto_openvpn_subnet - > setText ( openvpnConfig . value ( config_key : : subnet_address ) .
2021-06-12 11:59:36 +03:00
toString ( protocols : : openvpn : : defaultSubnetAddress ) ) ;
2021-05-07 23:28:37 +03:00
QString trasnsport = openvpnConfig . value ( config_key : : transport_proto ) .
toString ( protocols : : openvpn : : defaultTransportProto ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
ui - > radioButton_proto_openvpn_udp - > setChecked ( trasnsport = = protocols : : openvpn : : defaultTransportProto ) ;
ui - > radioButton_proto_openvpn_tcp - > setChecked ( trasnsport ! = protocols : : openvpn : : defaultTransportProto ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
ui - > comboBox_proto_openvpn_cipher - > setCurrentText ( openvpnConfig . value ( config_key : : cipher ) .
toString ( protocols : : openvpn : : defaultCipher ) ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
ui - > comboBox_proto_openvpn_hash - > setCurrentText ( openvpnConfig . value ( config_key : : hash ) .
toString ( protocols : : openvpn : : defaultHash ) ) ;
2021-04-26 22:54:31 +03:00
2021-05-07 23:28:37 +03:00
bool blockOutsideDns = openvpnConfig . value ( config_key : : block_outside_dns ) . toBool ( protocols : : openvpn : : defaultBlockOutsideDns ) ;
ui - > checkBox_proto_openvpn_block_dns - > setChecked ( blockOutsideDns ) ;
bool isNcpDisabled = openvpnConfig . value ( config_key : : ncp_disable ) . toBool ( protocols : : openvpn : : defaultNcpDisable ) ;
ui - > checkBox_proto_openvpn_auto_encryption - > setChecked ( ! isNcpDisabled ) ;
2021-05-18 15:50:52 +03:00
bool isTlsAuth = openvpnConfig . value ( config_key : : tls_auth ) . toBool ( protocols : : openvpn : : defaultTlsAuth ) ;
ui - > checkBox_proto_openvpn_tls_auth - > setChecked ( isTlsAuth ) ;
2021-05-07 23:28:37 +03:00
if ( container = = DockerContainer : : OpenVpnOverShadowSocks ) {
ui - > radioButton_proto_openvpn_udp - > setEnabled ( false ) ;
ui - > radioButton_proto_openvpn_tcp - > setEnabled ( false ) ;
ui - > radioButton_proto_openvpn_tcp - > setChecked ( true ) ;
}
2021-05-10 02:33:31 +03:00
ui - > lineEdit_proto_openvpn_port - > setText ( openvpnConfig . value ( config_key : : port ) .
toString ( protocols : : openvpn : : defaultPort ) ) ;
ui - > lineEdit_proto_openvpn_port - > setEnabled ( container = = DockerContainer : : OpenVpn ) ;
}
2021-05-10 14:19:36 +03:00
void MainWindow : : updateShadowSocksPage ( const QJsonObject & ssConfig , DockerContainer container , bool haveAuthData )
2021-05-10 02:33:31 +03:00
{
2021-05-10 14:19:36 +03:00
ui - > widget_proto_ss - > setEnabled ( haveAuthData ) ;
ui - > pushButton_proto_shadowsocks_save - > setVisible ( haveAuthData ) ;
ui - > progressBar_proto_shadowsocks_reset - > setVisible ( haveAuthData ) ;
2021-05-10 02:33:31 +03:00
ui - > comboBox_proto_shadowsocks_cipher - > setCurrentText ( ssConfig . value ( config_key : : cipher ) .
toString ( protocols : : shadowsocks : : defaultCipher ) ) ;
ui - > lineEdit_proto_shadowsocks_port - > setText ( ssConfig . value ( config_key : : port ) .
toString ( protocols : : shadowsocks : : defaultPort ) ) ;
ui - > lineEdit_proto_shadowsocks_port - > setEnabled ( container = = DockerContainer : : OpenVpnOverShadowSocks ) ;
}
2021-05-10 14:19:36 +03:00
void MainWindow : : updateCloakPage ( const QJsonObject & ckConfig , DockerContainer container , bool haveAuthData )
2021-05-10 02:33:31 +03:00
{
2021-05-10 14:19:36 +03:00
ui - > widget_proto_cloak - > setEnabled ( haveAuthData ) ;
ui - > pushButton_proto_cloak_save - > setVisible ( haveAuthData ) ;
ui - > progressBar_proto_cloak_reset - > setVisible ( haveAuthData ) ;
2021-05-10 02:33:31 +03:00
ui - > comboBox_proto_cloak_cipher - > setCurrentText ( ckConfig . value ( config_key : : cipher ) .
toString ( protocols : : cloak : : defaultCipher ) ) ;
ui - > lineEdit_proto_cloak_site - > setText ( ckConfig . value ( config_key : : site ) .
toString ( protocols : : cloak : : defaultRedirSite ) ) ;
ui - > lineEdit_proto_cloak_port - > setText ( ckConfig . value ( config_key : : port ) .
toString ( protocols : : cloak : : defaultPort ) ) ;
ui - > lineEdit_proto_cloak_port - > setEnabled ( container = = DockerContainer : : OpenVpnOverCloak ) ;
}
void MainWindow : : updateSharingPage ( int serverIndex , const ServerCredentials & credentials ,
DockerContainer container )
{
selectedDockerContainer = container ;
selectedServerIndex = serverIndex ;
2021-06-01 18:18:09 +03:00
//const QJsonObject &containerConfig = m_settings.containerConfig(serverIndex, container);
2021-05-10 02:33:31 +03:00
2021-05-27 22:18:36 +03:00
for ( QWidget * page : {
ui - > page_share_amnezia ,
ui - > page_share_openvpn ,
ui - > page_share_shadowsocks ,
ui - > page_share_cloak ,
ui - > page_share_full_access } ) {
2021-05-10 02:33:31 +03:00
ui - > toolBox_share_connection - > removeItem ( ui - > toolBox_share_connection - > indexOf ( page ) ) ;
page - > hide ( ) ;
}
if ( container = = DockerContainer : : OpenVpn ) {
2021-05-27 22:18:36 +03:00
ui - > toolBox_share_connection - > addItem ( ui - > page_share_amnezia , tr ( " Share for Amnezia client " ) ) ;
2021-05-10 02:33:31 +03:00
ui - > toolBox_share_connection - > addItem ( ui - > page_share_openvpn , tr ( " Share for OpenVPN client " ) ) ;
2021-05-27 22:18:36 +03:00
QString cfg = tr ( " Press Generate config " ) ;
ui - > textEdit_share_openvpn_code - > setPlainText ( cfg ) ;
ui - > pushButton_share_openvpn_copy - > setEnabled ( false ) ;
ui - > pushButton_share_openvpn_save - > setEnabled ( false ) ;
2021-05-18 15:50:52 +03:00
ui - > toolBox_share_connection - > setCurrentWidget ( ui - > page_share_openvpn ) ;
2021-05-10 02:33:31 +03:00
}
2021-06-16 19:48:38 +03:00
if ( container = = DockerContainer : : OpenVpnOverShadowSocks | |
container = = DockerContainer : : OpenVpnOverCloak ) {
2021-05-27 22:18:36 +03:00
ui - > toolBox_share_connection - > addItem ( ui - > page_share_amnezia , tr ( " Share for Amnezia client " ) ) ;
2021-05-10 02:33:31 +03:00
ui - > toolBox_share_connection - > addItem ( ui - > page_share_shadowsocks , tr ( " Share for ShadowSocks client " ) ) ;
QJsonObject protoConfig = m_settings . protocolConfig ( serverIndex , container , Protocol : : ShadowSocks ) ;
QString cfg = protoConfig . value ( config_key : : last_config ) . toString ( ) ;
if ( cfg . isEmpty ( ) ) {
const QJsonObject & containerConfig = m_settings . containerConfig ( serverIndex , container ) ;
ErrorCode e = ErrorCode : : NoError ;
cfg = ShadowSocksConfigurator : : genShadowSocksConfig ( credentials , container , containerConfig , & e ) ;
ui - > pushButton_share_ss_copy - > setEnabled ( true ) ;
}
QJsonObject ssConfig = QJsonDocument : : fromJson ( cfg . toUtf8 ( ) ) . object ( ) ;
QString ssString = QString ( " %1:%2@%3:%4 " )
. arg ( ssConfig . value ( " method " ) . toString ( ) )
. arg ( ssConfig . value ( " password " ) . toString ( ) )
. arg ( ssConfig . value ( " server " ) . toString ( ) )
. arg ( ssConfig . value ( " server_port " ) . toString ( ) ) ;
ssString = " ss:// " + ssString . toUtf8 ( ) . toBase64 ( ) ;
ui - > lineEdit_share_ss_string - > setText ( ssString ) ;
updateQRCodeImage ( ssString , ui - > label_share_ss_qr_code ) ;
ui - > label_share_ss_server - > setText ( ssConfig . value ( " server " ) . toString ( ) ) ;
ui - > label_share_ss_port - > setText ( ssConfig . value ( " server_port " ) . toString ( ) ) ;
ui - > label_share_ss_method - > setText ( ssConfig . value ( " method " ) . toString ( ) ) ;
ui - > label_share_ss_password - > setText ( ssConfig . value ( " password " ) . toString ( ) ) ;
2021-05-18 15:50:52 +03:00
ui - > toolBox_share_connection - > setCurrentWidget ( ui - > page_share_shadowsocks ) ;
ui - > page_share_shadowsocks - > show ( ) ;
ui - > page_share_shadowsocks - > raise ( ) ;
qDebug ( ) < < ui - > page_share_shadowsocks - > size ( ) ;
ui - > toolBox_share_connection - > layout ( ) - > update ( ) ;
2021-05-10 02:33:31 +03:00
}
2021-05-27 22:18:36 +03:00
if ( container = = DockerContainer : : OpenVpnOverCloak ) {
2021-06-16 19:48:38 +03:00
//ui->toolBox_share_connection->addItem(ui->page_share_amnezia, tr(" Share for Amnezia client"));
ui - > toolBox_share_connection - > addItem ( ui - > page_share_cloak , tr ( " Share for Cloak client " ) ) ;
ui - > plainTextEdit_share_cloak - > setPlainText ( QString ( " " ) ) ;
QJsonObject protoConfig = m_settings . protocolConfig ( serverIndex , container , Protocol : : Cloak ) ;
QString cfg = protoConfig . value ( config_key : : last_config ) . toString ( ) ;
if ( cfg . isEmpty ( ) ) {
const QJsonObject & containerConfig = m_settings . containerConfig ( serverIndex , container ) ;
ErrorCode e = ErrorCode : : NoError ;
cfg = CloakConfigurator : : genCloakConfig ( credentials , container , containerConfig , & e ) ;
ui - > pushButton_share_cloak_copy - > setEnabled ( true ) ;
}
QJsonObject cloakConfig = QJsonDocument : : fromJson ( cfg . toUtf8 ( ) ) . object ( ) ;
cloakConfig . remove ( config_key : : transport_proto ) ;
cloakConfig . insert ( " ProxyMethod " , " shadowsocks " ) ;
ui - > plainTextEdit_share_cloak - > setPlainText ( QJsonDocument ( cloakConfig ) . toJson ( ) ) ;
2021-05-27 22:18:36 +03:00
}
// Full access
if ( container = = DockerContainer : : None ) {
ui - > toolBox_share_connection - > addItem ( ui - > page_share_full_access , tr ( " Share server full access " ) ) ;
const QJsonObject & server = m_settings . server ( selectedServerIndex ) ;
QByteArray ba = QJsonDocument ( server ) . toJson ( ) . toBase64 ( QByteArray : : Base64UrlEncoding | QByteArray : : OmitTrailingEquals ) ;
ui - > textEdit_share_full_code - > setText ( QString ( " vpn://%1 " ) . arg ( QString ( ba ) ) ) ;
ui - > toolBox_share_connection - > setCurrentWidget ( ui - > page_share_full_access ) ;
}
//ui->toolBox_share_connection->addItem(ui->page_share_amnezia, tr(" Share for Amnezia client"));
2021-05-10 02:33:31 +03:00
// Amnezia sharing
2021-05-10 14:19:36 +03:00
// QJsonObject exportContainer;
// for (Protocol p: protocolsForContainer(container)) {
// QJsonObject protocolConfig = containerConfig.value(protoToString(p)).toObject();
// protocolConfig.remove(config_key::last_config);
// exportContainer.insert(protoToString(p), protocolConfig);
// }
// exportContainer.insert(config_key::container, containerToString(container));
// ui->textEdit_share_amnezia_code->setPlainText(QJsonDocument(exportContainer).toJson());
2021-05-10 02:33:31 +03:00
2021-05-10 14:19:36 +03:00
ui - > textEdit_share_amnezia_code - > setPlainText ( tr ( " " ) ) ;
2021-02-25 21:16:00 +03:00
}
2021-03-17 03:45:38 +03:00
2021-04-20 02:09:47 +03:00
void MainWindow : : makeServersListItem ( QListWidget * listWidget , const QJsonObject & server , bool isDefault , int index )
{
QSize size ( 310 , 70 ) ;
ServerWidget * widget = new ServerWidget ( server , isDefault ) ;
2021-04-26 22:54:31 +03:00
2021-04-20 02:09:47 +03:00
widget - > resize ( size ) ;
connect ( widget - > ui - > pushButton_default , & QPushButton : : clicked , this , [ this , index ] ( ) {
m_settings . setDefaultServer ( index ) ;
2021-05-07 23:28:37 +03:00
updateServersListPage ( ) ;
2021-04-20 02:09:47 +03:00
} ) ;
2021-05-10 02:33:31 +03:00
// connect(widget->ui->pushButton_share, &QPushButton::clicked, this, [this, index](){
// goToPage(Page::ShareConnection);
// // update share page
// });
2021-04-20 02:09:47 +03:00
connect ( widget - > ui - > pushButton_settings , & QPushButton : : clicked , this , [ this , index ] ( ) {
selectedServerIndex = index ;
goToPage ( Page : : ServerSettings ) ;
} ) ;
QListWidgetItem * item = new QListWidgetItem ( listWidget ) ;
item - > setSizeHint ( size ) ;
listWidget - > setItemWidget ( item , widget ) ;
widget - > setStyleSheet ( styleSheet ( ) ) ;
}
2021-03-17 03:45:38 +03:00
void MainWindow : : updateQRCodeImage ( const QString & text , QLabel * label )
{
int levelIndex = 1 ;
int versionIndex = 0 ;
bool bExtent = true ;
int maskIndex = - 1 ;
m_qrEncode . EncodeData ( levelIndex , versionIndex , bExtent , maskIndex , text . toUtf8 ( ) . data ( ) ) ;
int qrImageSize = m_qrEncode . m_nSymbleSize ;
int encodeImageSize = qrImageSize + ( QR_MARGIN * 2 ) ;
QImage encodeImage ( encodeImageSize , encodeImageSize , QImage : : Format_Mono ) ;
encodeImage . fill ( 1 ) ;
for ( int i = 0 ; i < qrImageSize ; i + + )
for ( int j = 0 ; j < qrImageSize ; j + + )
if ( m_qrEncode . m_byModuleData [ i ] [ j ] )
encodeImage . setPixel ( i + QR_MARGIN , j + QR_MARGIN , 0 ) ;
label - > setPixmap ( QPixmap : : fromImage ( encodeImage . scaledToWidth ( label - > width ( ) ) ) ) ;
}
2021-05-07 23:28:37 +03:00
QJsonObject MainWindow : : getOpenVpnConfigFromPage ( QJsonObject oldConfig )
{
oldConfig . insert ( config_key : : subnet_address , ui - > lineEdit_proto_openvpn_subnet - > text ( ) ) ;
oldConfig . insert ( config_key : : transport_proto , ui - > radioButton_proto_openvpn_udp - > isChecked ( ) ? protocols : : UDP : protocols : : TCP ) ;
oldConfig . insert ( config_key : : ncp_disable , ! ui - > checkBox_proto_openvpn_auto_encryption - > isChecked ( ) ) ;
oldConfig . insert ( config_key : : cipher , ui - > comboBox_proto_openvpn_cipher - > currentText ( ) ) ;
oldConfig . insert ( config_key : : hash , ui - > comboBox_proto_openvpn_hash - > currentText ( ) ) ;
oldConfig . insert ( config_key : : block_outside_dns , ui - > checkBox_proto_openvpn_block_dns - > isChecked ( ) ) ;
2021-05-10 02:33:31 +03:00
oldConfig . insert ( config_key : : port , ui - > lineEdit_proto_openvpn_port - > text ( ) ) ;
2021-05-18 15:50:52 +03:00
oldConfig . insert ( config_key : : tls_auth , ui - > checkBox_proto_openvpn_tls_auth - > isChecked ( ) ) ;
2021-05-10 02:33:31 +03:00
return oldConfig ;
}
QJsonObject MainWindow : : getShadowSocksConfigFromPage ( QJsonObject oldConfig )
{
oldConfig . insert ( config_key : : cipher , ui - > comboBox_proto_shadowsocks_cipher - > currentText ( ) ) ;
oldConfig . insert ( config_key : : port , ui - > lineEdit_proto_shadowsocks_port - > text ( ) ) ;
return oldConfig ;
}
QJsonObject MainWindow : : getCloakConfigFromPage ( QJsonObject oldConfig )
{
oldConfig . insert ( config_key : : cipher , ui - > comboBox_proto_cloak_cipher - > currentText ( ) ) ;
oldConfig . insert ( config_key : : site , ui - > lineEdit_proto_cloak_site - > text ( ) ) ;
oldConfig . insert ( config_key : : port , ui - > lineEdit_proto_cloak_port - > text ( ) ) ;
2021-05-07 23:28:37 +03:00
return oldConfig ;
}