forked from Mythologyli/ZJU-Connect-for-Windows
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zjuconnectmode.cpp
228 lines (203 loc) · 9.5 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "loginwindow/loginwindow.h"
#include "utils/utils.h"
void MainWindow::setModeToZjuConnect()
{
clearLog();
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();
// 连接服务器
connect(zjuConnectController, &ZjuConnectController::outputRead, this,
[&](const QString &output)
{
ui->logPlainTextEdit->appendPlainText(output.trimmed());
});
connect(zjuConnectController, &ZjuConnectController::error, this,
[&](ZJU_ERROR err)
{
if (zjuConnectError == ZJU_ERROR::NONE)
{
zjuConnectError = err;
}
});
connect(zjuConnectController, &ZjuConnectController::finished, this, [&]()
{
if (
zjuConnectError != ZJU_ERROR::NONE &&
zjuConnectError != ZJU_ERROR::OTHER &&
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("VPN 断开!");
showNotification("VPN", "VPN 断开!", QSystemTrayIcon::MessageIcon::Warning);
isZjuConnectLinked = false;
ui->pushButton1->setText("连接服务器");
if (isSystemProxySet)
{
ui->pushButton2->click();
}
ui->pushButton2->hide();
switch (zjuConnectError)
{
case ZJU_ERROR::INVALID_DETAIL:
QMessageBox::critical(this, "错误", "登录失败!\n请检查设置中的网络账号和密码是否设置正确。");
break;
case ZJU_ERROR::BRUTE_FORCE:
QMessageBox::critical(this, "错误", "登录失败!\n登录尝试过于频繁,IP 被风控,请稍后重试或换用 EasyConnect。");
break;
case ZJU_ERROR::OTHER_LOGIN_FAILED:
QMessageBox::critical(this, "错误", "登录失败!\n未知原因,可将日志反馈给开发者以便调查。");
break;
case ZJU_ERROR::ACCESS_DENIED:
QMessageBox::critical(this, "错误", "权限不足!\n请关闭程序,点击右键以管理员身份运行。");
break;
case ZJU_ERROR::LISTEN_FAILED:
QMessageBox::critical(this, "错误", "监听失败!\n请关闭占用端口的程序(如残留的 zju-connect.exe),或者监听其它端口。");
break;
case ZJU_ERROR::CLIENT_FAILED:
QMessageBox::critical(this, "错误", "连接失败!\n可能是响应超时,请检查本地网络配置是否正常,服务器设置是否正确。");
break;
case ZJU_ERROR::OTHER:
QMessageBox::critical(this, "错误", "其它错误!\n未知原因,可将日志反馈给开发者以便调查。");
break;
case ZJU_ERROR::NONE:
default:
break;
}
zjuConnectError = ZJU_ERROR::NONE;
});
connect(ui->pushButton1, &QPushButton::clicked,
[&]()
{
if (!isZjuConnectLinked)
{
if (settings->contains("ZJUConnect/ServerAddress") &&
settings->value("ZJUConnect/ServerAddress").toString().isEmpty())
{
QMessageBox::critical(this, "错误", "服务器地址不能为空");
return;
}
QString username_ = settings->value("Common/Username", "").toString();
QString password_ = QByteArray::fromBase64(settings->value("Common/Password", "").toString().toUtf8());
auto startZjuConnect = [this](const QString& username, const QString& password)
{
QString bind_prefix = settings->value("ZJUConnect/OutsideAccess", false).toBool() ? "0.0.0.0:" : "127.0.0.1:";
zjuConnectController->start(
"zju-connect.exe",
username,
password,
settings->value("ZJUConnect/ServerAddress", "vpn.hitsz.edu.cn").toString(),
settings->value("ZJUConnect/ServerPort", 443).toInt(),
settings->value("ZJUConnect/DNS").toString(),
settings->value("ZJUConnect/SecondaryDNS", "").toString(),
!settings->value("ZJUConnect/MultiLine", true).toBool(),
!settings->value("ZJUConnect/KeepAlive", true).toBool(),
settings->value("ZJUConnect/ProxyAll", false).toBool(),
bind_prefix + QString::number(settings->value("ZJUConnect/Socks5Port", 11080).toInt()),
bind_prefix + QString::number(settings->value("ZJUConnect/HttpPort", 11081).toInt()),
settings->value("ZJUConnect/ShadowsocksUrl", "").toString(),
settings->value("ZJUConnect/TunMode", false).toBool(),
settings->value("ZJUConnect/Route", false).toBool(),
settings->value("ZJUConnect/DNSHijack", false).toBool(),
settings->value("ZJUConnect/Debug", false).toBool(),
settings->value("ZJUConnect/TcpPortForwarding", "").toString(),
settings->value("ZJUConnect/UdpPortForwarding", "").toString()
);
isZjuConnectLinked = true;
ui->pushButton1->setText("断开服务器");
ui->pushButton2->show();
if (settings->value("ZJUConnect/AutoSetProxy", true).toBool())
{
ui->pushButton2->click();
}
};
if (username_.isEmpty() || password_.isEmpty())
{
login_window = new LoginWindow(this);
login_window->setDetail(username_, password_);
connect(login_window, &LoginWindow::login, this,
[&, startZjuConnect](const QString& username, const QString& password, bool saveDetail)
{
if (saveDetail)
{
settings->setValue("Common/Username", username);
settings->setValue("Common/Password", QString(password.toUtf8().toBase64()));
settings->sync();
}
startZjuConnect(username, password);
}
);
login_window->show();
}
else
{
startZjuConnect(username_, password_);
}
}
else
{
isZjuConnectLinked = false;
zjuConnectController->stop();
if (isSystemProxySet)
{
ui->pushButton2->click();
}
ui->pushButton1->setText("连接服务器");
ui->pushButton2->hide();
}
});
// 设置系统代理
connect(ui->pushButton2, &QPushButton::clicked,
[&]()
{
if (!isSystemProxySet)
{
QSettings proxySettings(
R"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings)",
QSettings::NativeFormat
);
if (proxySettings.value("ProxyEnable", 0).toInt() == 1)
{
int rtn = QMessageBox::warning(this, "警告",
"当前已存在系统代理配置(可能是 Clash 或其它代理软件)\n是否覆盖当前系统代理配置?",
QMessageBox::Yes | QMessageBox::No);
if (rtn == QMessageBox::No)
{
return;
}
}
Utils::setSystemProxy(settings->value("ZJUConnect/HttpPort", 11081).toInt());
ui->pushButton2->setText("清除系统代理");
isSystemProxySet = true;
}
else
{
Utils::clearSystemProxy();
ui->pushButton2->setText("设置系统代理");
isSystemProxySet = false;
if (!isZjuConnectLinked)
{
ui->pushButton2->hide();
}
}
});
emit SetModeFinished();
}