-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSysProxy.cpp
102 lines (87 loc) · 2.94 KB
/
SysProxy.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
/*
* Copyright (C) 2020 Richard Yu <[email protected]>
*
* This file is part of ClashXW.
*
* ClashXW is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ClashXW is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ClashXW. If not, see <https://www.gnu.org/licenses/>.
*/
// This file contains code from
// https://github.com/Noisyfox/sysproxy/blob/master/sysproxy/main.c
// which is licensed under the Apache License 2.0.
// See licenses/sysproxy for more information.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wininet.h>
#include <ras.h>
#include <raserror.h>
#include <vector>
// wil
#ifndef _DEBUG
#define RESULT_DIAGNOSTICS_LEVEL 1
#endif
#include <wil/result.h>
void SetSystemProxy(const wchar_t* server, const wchar_t* bypass = L"<local>")
{
INTERNET_PER_CONN_OPTIONW options[3];
INTERNET_PER_CONN_OPTION_LISTW optList = {
.dwSize = sizeof(optList),
.dwOptionCount = 1,
.pOptions = options
};
if (!server) // off
{
options[0] = {
.dwOption = INTERNET_PER_CONN_FLAGS,
.Value = { .dwValue = PROXY_TYPE_DIRECT }
};
}
else // global
{
optList.dwOptionCount = 3;
options[0] = {
.dwOption = INTERNET_PER_CONN_FLAGS,
.Value = { .dwValue = PROXY_TYPE_PROXY | PROXY_TYPE_DIRECT }
};
options[1] = {
.dwOption = INTERNET_PER_CONN_PROXY_SERVER,
.Value = { .pszValue = const_cast<LPWSTR>(server) }
};
options[2] = {
.dwOption = INTERNET_PER_CONN_PROXY_BYPASS,
.Value = { .pszValue = const_cast<LPWSTR>(bypass) }
};
}
THROW_IF_WIN32_BOOL_FALSE(InternetSetOptionW(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &optList, sizeof(optList)));
RASENTRYNAMEW entry;
entry.dwSize = sizeof(entry);
std::vector<RASENTRYNAMEW> entries;
DWORD size = sizeof(entry), count;
LPRASENTRYNAMEW entryAddr = &entry;
auto ret = RasEnumEntriesW(nullptr, nullptr, entryAddr, &size, &count);
if (ret == ERROR_BUFFER_TOO_SMALL)
{
entries.resize(count);
entries[0].dwSize = sizeof(RASENTRYNAMEW);
entryAddr = entries.data();
ret = RasEnumEntriesW(nullptr, nullptr, entryAddr, &size, &count);
}
THROW_IF_WIN32_ERROR(ret);
for (size_t i = 0; i < count; ++i)
{
optList.pszConnection = entryAddr[i].szEntryName;
THROW_IF_WIN32_BOOL_FALSE(InternetSetOptionW(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &optList, sizeof(optList)));
}
THROW_IF_WIN32_BOOL_FALSE(InternetSetOptionW(nullptr, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, nullptr, 0));
THROW_IF_WIN32_BOOL_FALSE(InternetSetOptionW(nullptr, INTERNET_OPTION_REFRESH, nullptr, 0));
}