Repository navigation
Expand file tree
/
Copy pathwindows.c
More file actions
135 lines (122 loc) · 3.84 KB
/
Copy pathwindows.c
File metadata and controls
135 lines (122 loc) · 3.84 KB
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
#include <stdlib.h>
#include <windows.h>
#include <Wininet.h>
#include <ras.h>
#include <tchar.h>
#include <stdio.h>
#include "common.h"
void reportWindowsError(const char* action) {
LPTSTR pErrMsg = NULL;
DWORD errCode = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
errCode,
LANG_NEUTRAL,
pErrMsg,
0,
NULL);
fprintf(stderr, "Error %s: %lu %s\n", action, errCode, pErrMsg);
}
// Stolen from https://github.com/getlantern/winproxy
// Figure out which Dial-Up or VPN connection is active; in a normal LAN connection, this should
// return NULL. NOTE: For some reason this method fails when compiled in Debug mode but works
// every time in Release mode.
LPTSTR FindActiveConnection() {
DWORD dwCb = sizeof(RASCONN);
DWORD dwErr = ERROR_SUCCESS;
DWORD dwRetries = 5;
DWORD dwConnections = 0;
RASCONN* lpRasConn = NULL;
RASCONNSTATUS rasconnstatus;
rasconnstatus.dwSize = sizeof(RASCONNSTATUS);
//
// Loop through in case the information from RAS changes between calls.
//
while (dwRetries--) {
// If the memory is allocated, free it.
if (NULL != lpRasConn) {
HeapFree(GetProcessHeap(), 0, lpRasConn);
lpRasConn = NULL;
}
// Allocate the size needed for the RAS structure.
lpRasConn = (RASCONN*)HeapAlloc(GetProcessHeap(), 0, dwCb);
if (NULL == lpRasConn) {
dwErr = ERROR_NOT_ENOUGH_MEMORY;
break;
}
// Set the structure size for version checking purposes.
lpRasConn->dwSize = sizeof(RASCONN);
// Call the RAS API then exit the loop if we are successful or an unknown
// error occurs.
dwErr = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
if (ERROR_INSUFFICIENT_BUFFER != dwErr) {
break;
}
}
//
// In the success case, print the names of the connections.
//
if (ERROR_SUCCESS == dwErr) {
DWORD i;
for (i = 0; i < dwConnections; i++) {
RasGetConnectStatus(lpRasConn[i].hrasconn, &rasconnstatus);
if (rasconnstatus.rasconnstate == RASCS_Connected){
return lpRasConn[i].szEntryName;
}
}
}
return NULL; // Couldn't find an active dial-up/VPN connection; return NULL
}
int togglePac(bool turnOn, const char* pacUrl)
{
int ret = RET_NO_ERROR;
INTERNET_PER_CONN_OPTION_LIST options;
DWORD dwBufferSize = sizeof(options);
options.dwSize = dwBufferSize;
options.pszConnection = FindActiveConnection();
options.dwOptionCount = 2;
options.pOptions = (INTERNET_PER_CONN_OPTION*)calloc(2, sizeof(INTERNET_PER_CONN_OPTION));
if(!options.pOptions) {
return NO_MEMORY;
}
options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
options.pOptions[1].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
if (turnOn) {
options.pOptions[0].Value.dwValue = PROXY_TYPE_AUTO_PROXY_URL;
wchar_t url[150];
if (swprintf(url, 150, L"%hs", pacUrl) == -1) {
printf("Unable to convert pacUrl to wchar_t %s\n", strerror(errno));
ret = PAC_URL_CONVERSION_ERROR;
goto cleanup;
}
options.pOptions[1].Value.pszValue = (char*)pacUrl;
}
else {
options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT;
options.pOptions[1].Value.pszValue = "";
}
BOOL result;
result = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &options, dwBufferSize);
if (!result) {
reportWindowsError("setting options");
ret = SYSCALL_FAILED;
goto cleanup;
}
result = InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
if (!result) {
reportWindowsError("propagating changes");
ret = SYSCALL_FAILED;
goto cleanup;
}
result = InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
if (!result) {
reportWindowsError("refreshing");
ret = SYSCALL_FAILED;
goto cleanup;
}
cleanup:
free(options.pOptions);
return ret;
}