-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathl2tpmode.cpp
263 lines (226 loc) · 11 KB
/
l2tpmode.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <QMessageBox>
#include <QTimer>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "utils/utils.h"
void MainWindow::setModeToL2tp()
{
mode = "有线网 L2TP";
clearLog();
networkDetector->start();
ui->interfaceLabel->hide();
ui->refreshInterfaceButton->hide();
ui->interfaceComboBox->hide();
ui->tunCheckBox->hide();
// 清除系统代理
if (isSystemProxySet)
{
Utils::clearSystemProxy();
isSystemProxySet = false;
addLog("已清除系统代理设置");
}
disconnect(ui->pushButton1, &QPushButton::clicked, nullptr, nullptr);
if (!isL2tpLinked)
{
ui->pushButton1->setText("连接 L2TP");
}
else
{
ui->pushButton1->setText("断开 L2TP");
}
ui->pushButton1->setEnabled(false);
disconnect(ui->pushButton2, &QPushButton::clicked, nullptr, nullptr);
ui->pushButton2->hide();
addLog("工作模式设置为:有线网 L2TP");
// 检查是否存在 VPN
disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr);
connect(processForL2tp, &QProcess::finished, this, [&]()
{
QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput()).trimmed();
if (output.isEmpty())
{
addLog("警告:在系统中未发现 VPN 配置!请使用高级 - 创建 L2TP VPN 创建 VPN");
}
else
{
QString line;
QTextStream stream(&output);
bool isVpnInSettingsExist = false;
while (!stream.atEnd())
{
line = stream.readLine();
if (line.contains("Name") && line.contains(settings->value("L2TP/Name", "ZJUVPN").toString()))
{
auto vpnName = line.split(":")[1].trimmed();
if (vpnName == settings->value("L2TP/Name", "ZJUVPN").toString())
{
isVpnInSettingsExist = true;
break;
}
}
}
if (!isVpnInSettingsExist)
{
addLog(
"警告:在系统中未发现名称为 "
+ settings->value("L2TP/Name", "ZJUVPN").toString()
+ " 的 VPN,请在设置中修改 VPN 名称"
);
}
}
ui->pushButton1->setEnabled(true);
emit SetModeFinished();
});
processForL2tp->start("powershell", QStringList() << "-command" << "Get-VpnConnection");
connect(ui->pushButton1, &QPushButton::clicked,
[&]()
{
ui->pushButton1->setEnabled(false);
ui->modeComboBox->setEnabled(false);
ui->createL2tpAction->setEnabled(false);
QString l2tpName = settings->value("L2TP/Name", "ZJUVPN").toString();
if (l2tpName.isEmpty())
{
QMessageBox::critical(this, "错误", "L2TP VPN 名称不能为空");
return;
}
if (!isL2tpLinked)
{
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;
}
disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr);
connect(processForL2tp, &QProcess::finished, this, [&]()
{
QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput()).trimmed();
if (output.contains("命令已完成") || output.contains("Command completed successfully"))
{
isL2tpLinked = true;
ui->pushButton1->setText("断开 L2TP");
addLog("连接成功!");
showNotification("有线网 L2TP", "连接成功!");
if (settings->value("L2TP/AutoReconnect", false).toBool())
{
l2tpCheckTimer = new QTimer(this);
connect(l2tpCheckTimer, &QTimer::timeout, this, [&]()
{
disconnect(processForL2tpCheck, &QProcess::finished, nullptr, nullptr);
connect(processForL2tpCheck, &QProcess::finished, this, [&]()
{
QString output = Utils::ConsoleOutputToQString(
processForL2tpCheck->readAllStandardOutput()
).trimmed();
if (output.contains("(0%") && !output.contains("unreachable") &&
!output.contains("无法"))
{
addLog("自动检测结果:L2TP VPN 连接正常");
}
else
{
addLog("自动检测结果:L2TP VPN 连接异常,正在重连...");
showNotification(
"有线网 L2TP",
"自动检测结果:L2TP VPN 连接异常,正在重连...",
QSystemTrayIcon::MessageIcon::Warning
);
isL2tpReconnecting = true;
ui->pushButton1->click();
}
});
processForL2tpCheck->start(
"ping",
QStringList()
<< "-n"
<< "1"
<< settings->value("L2TP/CheckIp", "223.5.5.5").toString()
);
});
l2tpCheckTimer->start(settings->value("L2TP/CheckTime", 600).toInt() * 1000);
}
}
else
{
addLog("连接失败!");
if (output.contains("623"))
{
output = "请检查 VPN 名称是否设置正确\n"
"如果你没有创建过 VPN,请使用高级-创建 L2TP VPN\n"
"如果你创建过 VPN,请打开系统设置-网络-VPN,将 VPN 名称填写在本软件设置-L2TP 中\n"
"详细信息为:\n" + output;
}
else if (output.contains("691"))
{
output = "请检查设置中的网络账号和密码是否设置正确!\n详细信息为:\n" + output;
}
else if (output.contains("789"))
{
output = "连接失败!这个问题通常是注册表造成的\n"
"建议删除现有 VPN,然后使用高级-创建 L2TP VPN\n"
"如果您刚刚使用本程序创建了 VPN,请重启电脑\n详细信息为:\n" + output;
}
else if (output.contains("868"))
{
output = "请检查系统 DNS 是否设置正确!\n详细信息为:\n" + output;
}
addLog(output);
ui->modeComboBox->setEnabled(true);
ui->createL2tpAction->setEnabled(true);
QMessageBox::critical(this, "错误", output);
}
ui->pushButton1->setEnabled(true);
});
processForL2tp->start(
"rasdial",
QStringList()
<< l2tpName
<< settings->value("Common/Username").toString()
<< QByteArray::fromBase64(settings->value("Common/Password").toString().toUtf8())
);
addLog("正在连接 L2TP VPN: " + l2tpName);
}
else
{
if (l2tpCheckTimer != nullptr)
{
disconnect(processForL2tpCheck, &QProcess::finished, nullptr, nullptr);
l2tpCheckTimer->stop();
delete l2tpCheckTimer;
l2tpCheckTimer = nullptr;
}
disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr);
connect(processForL2tp, &QProcess::finished, this, [&]()
{
QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput());
if (output.contains("命令已完成") || output.contains("Command completed successfully"))
{
isL2tpLinked = false;
ui->pushButton1->setText("连接 L2TP");
addLog("断开成功!");
}
else
{
addLog("断开失败!");
addLog(output);
QMessageBox::critical(this, "错误", output);
}
ui->pushButton1->setEnabled(true);
ui->modeComboBox->setEnabled(true);
ui->createL2tpAction->setEnabled(true);
if (isL2tpReconnecting)
{
isL2tpReconnecting = false;
ui->pushButton1->click();
}
});
processForL2tp->start("rasdial", QStringList() << l2tpName << "/DISCONNECT");
addLog("正在断开 L2TP VPN: " + l2tpName);
}
});
}