-
Notifications
You must be signed in to change notification settings - Fork 18
/
zjuconnectmode.cpp
199 lines (168 loc) · 7.15 KB
/
zjuconnectmode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "utils/utils.h"
void MainWindow::setModeToZjuConnect()
{
mode = "RVPN";
clearLog();
networkDetector->start();
ui->interfaceLabel->hide();
ui->refreshInterfaceButton->hide();
ui->interfaceComboBox->hide();
ui->tunCheckBox->show();
if (settings->contains("ZJUConnect/TunMode"))
{
if (settings->value("ZJUConnect/TunMode").toBool())
{
ui->tunCheckBox->setChecked(true);
}
}
zjuConnectController = new ZjuConnectController();
disconnect(ui->pushButton1, &QPushButton::clicked, nullptr, nullptr);
ui->pushButton1->setText("连接服务器");
disconnect(ui->pushButton2, &QPushButton::clicked, nullptr, nullptr);
ui->pushButton2->setText("设置系统代理");
ui->pushButton2->hide();
addLog("工作模式设置为:RVPN");
// 连接服务器
connect(zjuConnectController, &ZjuConnectController::outputRead, this, [&](const QString &output)
{
ui->logPlainTextEdit->appendPlainText(output);
});
connect(zjuConnectController, &ZjuConnectController::loginFailed, this, [&]()
{
isZjuConnectLoginError = true;
});
connect(zjuConnectController, &ZjuConnectController::accessDenied, this, [&]()
{
isZjuConnectAccessDenied = true;
});
connect(zjuConnectController, &ZjuConnectController::finished, this, [&]()
{
if (
!isZjuConnectLoginError &&
!isZjuConnectAccessDenied &&
settings->value("ZJUConnect/AutoReconnect", false).toBool() &&
isZjuConnectLinked
)
{
QTimer::singleShot(settings->value("ZJUConnect/ReconnectTime", 1).toInt() * 1000, this, [&]()
{
if (isZjuConnectLinked)
{
zjuConnectController->stop();
isZjuConnectLinked = false;
ui->pushButton1->click();
}
});
return;
}
addLog("RVPN 断开!");
showNotification("RVPN", "RVPN 断开!", QSystemTrayIcon::MessageIcon::Warning);
isZjuConnectLinked = false;
ui->pushButton1->setText("连接服务器");
ui->modeComboBox->setEnabled(true);
if (isZjuConnectLoginError)
{
isZjuConnectLoginError = false;
QMessageBox::critical(this, "错误", "登录失败!\n请检查设置中的网络账号和密码是否设置正确");
}
else if (isZjuConnectAccessDenied)
{
isZjuConnectAccessDenied = false;
QMessageBox::critical(this, "错误", "权限不足!\n请关闭程序,点击右键以管理员身份运行");
}
});
connect(ui->pushButton1, &QPushButton::clicked,
[&]()
{
if (!isZjuConnectLinked)
{
if (settings->value("ZJUConnect/ServerAddress", "rvpn.zju.edu.cn").toString().isEmpty())
{
QMessageBox::critical(this, "错误", "服务器地址不能为空");
return;
}
if (settings->value("Common/Username", "").toString().isEmpty())
{
QMessageBox::critical(this, "错误", "用户名不能为空");
return;
}
if (QByteArray::fromBase64(settings->value("Common/Password", "").toString().toUtf8()).isEmpty())
{
QMessageBox::critical(this, "错误", "密码不能为空");
return;
}
zjuConnectController->start(
"zju-connect.exe",
settings->value("Common/Username").toString(),
QByteArray::fromBase64(settings->value("Common/Password").toString().toUtf8()),
settings->value("ZJUConnect/ServerAddress").toString(),
settings->value("ZJUConnect/ServerPort", 443).toInt(),
!settings->value("ZJUConnect/MultiLine", true).toBool(),
settings->value("ZJUConnect/ProxyAll", false).toBool(),
"127.0.0.1:" + QString::number(settings->value("ZJUConnect/Socks5Port", 11080).toInt()),
"127.0.0.1:" + QString::number(settings->value("ZJUConnect/HttpPort", 11081).toInt()),
ui->tunCheckBox->isChecked(),
settings->value("ZJUConnect/Route", false).toBool(),
settings->value("ZJUConnect/Debug", false).toBool(),
settings->value("ZJUConnect/TcpPortForwarding", "").toString(),
settings->value("ZJUConnect/UdpPortForwarding", "").toString()
);
isZjuConnectLinked = true;
ui->modeComboBox->setEnabled(false);
ui->pushButton1->setText("断开服务器");
ui->pushButton2->show();
if (settings->value("ZJUConnect/AutoSetProxy", false).toBool())
{
ui->pushButton2->click();
}
}
else
{
isZjuConnectLinked = false;
zjuConnectController->stop();
if (isSystemProxySet)
{
ui->pushButton2->click();
}
ui->pushButton1->setText("连接服务器");
ui->pushButton2->hide();
ui->modeComboBox->setEnabled(true);
}
});
// 设置系统代理
connect(ui->pushButton2, &QPushButton::clicked,
[&]()
{
if (!isSystemProxySet)
{
if (networkDetectResult.isProxyEnabled)
{
QMessageBox messageBox(this);
messageBox.setWindowTitle("警告");
messageBox.setText(
"当前已存在系统代理配置\n是否覆盖当前系统代理配置?"
);
messageBox.addButton(QMessageBox::Yes)->setText("是");
messageBox.addButton(QMessageBox::No)->setText("否");
messageBox.setDefaultButton(QMessageBox::Yes);
if (messageBox.exec() == QMessageBox::No)
{
return;
}
}
Utils::setSystemProxy(settings->value("ZJUConnect/HttpPort", 11081).toInt());
ui->pushButton2->setText("清除系统代理");
isSystemProxySet = true;
}
else
{
Utils::clearSystemProxy();
ui->pushButton2->setText("设置系统代理");
isSystemProxySet = false;
}
});
emit SetModeFinished();
}