-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVpnThread.cpp
82 lines (71 loc) · 1.81 KB
/
VpnThread.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
#include <process.h>
#include "VpnDialup/VpnDialup.h"
#include "Exception/Exception.h"
#include "Logging/Logging.h"
#include "ExitWindows/ExitWindows.h"
unsigned __stdcall VpnThreadProc(void* p);
HRASCONN g_hRasConn = 0; //VPN connection.
struct param_t
{
bool bQuiet;
HWND hMainWindow;
bool bFirstConnect;
};
extern param_t g_params;
UINT WMU_VPNCONNECTED =
RegisterWindowMessageW(L"RUNSISI_HUST_VPNCONNECTED");
unsigned __stdcall VpnThreadProc(void* p)
{
using namespace RUNSISI_HUST;
try
{
RASENTRYW RasEntry = {0};
LONG lRet = ERROR_SUCCESS;
lRet = DisableIpSec();
if (lRet != ERROR_SUCCESS)
{
if (lRet != ERROR_ALREADY_EXISTS)
{
throw CRunsisiExceptionW(L"Failed to disable IPSec, code: %d.",
lRet);
}
}
else //Reboot.
{
int iRet = ::MessageBoxW(0, L"Reboot is needed, reboot it now?",
L"REBOOT", MB_YESNO | MB_ICONINFORMATION);
(iRet == IDYES) ? ExitWindowsSystem(
ExitWindowsType::ExitWindowsType(
ExitWindowsType::REBOOT | ExitWindowsType::FORCE)) :
(throw CRunsisiExceptionW(
L"Reboot canceled, it will take effect next time."));
}
lRet = InitVpnEntry(RasEntry);
if (lRet != ERROR_SUCCESS)
{
throw CRunsisiExceptionW(L"Failed to initialize VPN entry, code: %d.",
lRet);
}
lRet = CreateVpnEntry(RasEntry);
if (lRet != ERROR_SUCCESS)
{
throw CRunsisiExceptionW(L"Failed to create VPN entry, code: %d.",
lRet);
}
lRet = ConnectVpn(g_hRasConn, g_params.bQuiet);
if (lRet != ERROR_SUCCESS)
{
//Notify main window.
PostMessageW(g_params.hMainWindow, WMU_VPNCONNECTED, 0, lRet);
throw CRunsisiExceptionW(L"Failed to connect VPN server, code: %d.",
lRet);
}
//Notify main window.
PostMessageW(g_params.hMainWindow, WMU_VPNCONNECTED, 1, 0);
}
catch (CRunsisiExceptionW& e)
{
ERRW(e.what());
}
return 0;
}