~psiphon-inc/psiphon/trunk

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2010, Psiphon Inc.
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "proxysettings.h"
#include "psiclient.h"
#include "wininet.h"
#include "config.h"
#include "ras.h"
#include "raserror.h"

ProxySettings::ProxySettings(void)
{
    m_originalSettings.clear();
}

ProxySettings::~ProxySettings(void)
{
    Revert();
}

bool ProxySettings::Configure(void)
{
    TCHAR proxyAddress[24];
    memset(proxyAddress, 0, sizeof(proxyAddress));
    _stprintf_s(proxyAddress, _T("%hs:%d"), LOCAL_LISTEN_IP, LOCAL_LISTEN_PORT);

    // Get a list of connections, starting with the dial-up connections
    vector<tstring> connections = GetRasConnectionNames();
    
    // NULL indicates the default or LAN connection
    connections.push_back(_T(""));

    return (Save(connections) && SetConnectionsProxies(connections, proxyAddress));
}

bool ProxySettings::Revert(void)
{
    bool success = true;

    for (connection_proxy_iter ii = m_originalSettings.begin();
         ii != m_originalSettings.end();
         ++ii)
    {
        if (!SetConnectionProxy(*ii))
        {
            success = false;
            break;
        }
    }

    if (success)
    {
        m_originalSettings.clear();
    }

    return success;
}

// TODO: do this properly.
// ie. Save original proxy settings to a file, and remove the file on restoring settings.
// If the file exists when saving original settings, use the file's contents and don't save
// the existing settings.
void ProxySettings::PreviousCrashCheckHack(connection_proxy& proxySettings)
{
    TCHAR proxyAddress[24];
    memset(proxyAddress, 0, sizeof(proxyAddress));
    _stprintf_s(proxyAddress, _T("%hs:%d"), LOCAL_LISTEN_IP, LOCAL_LISTEN_PORT);

    // Don't save settings that are the same as we will be setting.
    // Instead, save cleared settings.
    if (   (proxySettings.flags == PROXY_TYPE_PROXY)
        && (proxySettings.proxy == proxyAddress))
    {
        proxySettings.flags = PROXY_TYPE_DIRECT;
        proxySettings.proxy = L"";
    }
}

bool ProxySettings::Save(const vector<tstring>& connections)
{
    if (!m_originalSettings.empty())
    {
        my_print(false, _T("Error: can't save Proxy Settings because they are already saved."));
        my_print(false, _T("Original proxy settings may not be restored correctly."));
        return false;
    }

    bool success = true;
    connection_proxy proxySettings;

    for (tstring_iter ii = connections.begin();
         ii != connections.end();
         ++ii)
    {
        proxySettings.name = *ii;
        if (GetConnectionProxy(proxySettings))
        {
            PreviousCrashCheckHack(proxySettings);
            m_originalSettings.push_back(proxySettings);
        }
        else
        {
            success = false;
            break;
        }
    }

    if (!success)
    {
        // If we failed to save any connection, discard everything.
        // Nothing should proceed.
        m_originalSettings.clear();
    }

    return success;
}

bool ProxySettings::SetConnectionsProxies(const vector<tstring>& connections, const tstring& proxyAddress)
{
    bool success = true;
    connection_proxy proxySettings;

    for (tstring_iter ii = connections.begin();
         ii != connections.end();
         ++ii)
    {
        // These are the new proxy settings we want to use
        proxySettings.name = *ii;
        proxySettings.flags = PROXY_TYPE_PROXY;
        proxySettings.proxy = proxyAddress;

        if (!SetConnectionProxy(proxySettings))
        {
            success = false;
            break;
        }
    }

    return success;
}

bool ProxySettings::SetConnectionProxy(const connection_proxy& setting)
{
    INTERNET_PER_CONN_OPTION_LIST list;
    INTERNET_PER_CONN_OPTION options[INTERNET_OPTIONS_NUMBER];
    list.dwSize = sizeof(list);
    // Pointer to a string that contains the name of the RAS connection
    // or NULL, which indicates the default or LAN connection, to set or query options on.
    list.pszConnection = setting.name.length() ? const_cast<TCHAR*>(setting.name.c_str()) : 0;
    list.dwOptionCount = sizeof(options)/sizeof(INTERNET_PER_CONN_OPTION);
    list.pOptions = options;

    list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
    list.pOptions[0].Value.dwValue = setting.flags;

    list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
    list.pOptions[1].Value.pszValue = const_cast<TCHAR*>(setting.proxy.c_str());

    bool success = (0 != InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, list.dwSize));

    if (success)
    {
        InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
        InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
    }
    else
    {
        my_print(false, _T("InternetSetOption error: %d"), GetLastError());
        // NOTE: We are calling the Unicode version of InternetSetOption.
        // In Microsoft Internet Explorer 5, only the ANSI versions of InternetQueryOption and InternetSetOption
        // will work with the INTERNET_PER_CONN_OPTION_LIST structure.
    }

    return success;
}

bool ProxySettings::GetConnectionProxy(connection_proxy& setting)
{
    INTERNET_PER_CONN_OPTION_LIST list;
    INTERNET_PER_CONN_OPTION options[INTERNET_OPTIONS_NUMBER];
    unsigned long length = sizeof(list);
    list.dwSize = length;
    // Pointer to a string that contains the name of the RAS connection
    // or NULL, which indicates the default or LAN connection, to set or query options on.
    list.pszConnection = setting.name.length() ? const_cast<TCHAR*>(setting.name.c_str()) : 0;
    list.dwOptionCount = sizeof(options)/sizeof(INTERNET_PER_CONN_OPTION);
    list.pOptions = options;

    options[0].dwOption = INTERNET_PER_CONN_FLAGS;
    options[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;

    if (0 == InternetQueryOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, &length))
    {
        my_print(false, _T("InternetQueryOption error: %d"), GetLastError());
        // NOTE: We are calling the Unicode version of InternetQueryOption.
        // In Microsoft Internet Explorer 5, only the ANSI versions of InternetQueryOption and InternetSetOption
        // will work with the INTERNET_PER_CONN_OPTION_LIST structure.
        return false;
    }

    setting.flags = options[0].Value.dwValue;
    setting.proxy = options[1].Value.pszValue ? options[1].Value.pszValue : _T("");

    // Cleanup
    if (options[1].Value.pszValue)
    {
        GlobalFree(options[1].Value.pszValue);
    }

    return true;
}

static DWORD GetRasEntries(LPRASENTRYNAME& rasEntryNames, DWORD& bufferSize, DWORD& entries)
{
    if (rasEntryNames)
    {
        //Deallocate memory for the entries buffer
        HeapFree(GetProcessHeap(), 0, rasEntryNames);
        rasEntryNames = 0;
    }

    rasEntryNames = (LPRASENTRYNAME)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);

    if (!rasEntryNames)
    {
        my_print(false, _T("HeapAlloc failed when trying to enumerate RAS connections"));
        throw 0;
    }

    // The first RASENTRYNAME structure in the array must contain the structure size
    rasEntryNames[0].dwSize = sizeof(RASENTRYNAME);

    // Call RasEnumEntries to enumerate all RAS entry names
    return RasEnumEntries(0, 0, rasEntryNames, &bufferSize, &entries);
}

vector<tstring> ProxySettings::GetRasConnectionNames(void)
{
    vector<tstring> connections;
    LPRASENTRYNAME rasEntryNames = 0;
    // The RasEnumEntries API requires that we pass in a buffer first
    // and if the buffer is too small, it tells us how big a buffer it needs.
    // For the first call we will pass in a single RASENTRYNAME struct.
    DWORD bufferSize = sizeof(RASENTRYNAME);
    DWORD entries = 0;
    DWORD returnCode = ERROR_SUCCESS;

    try
    {
        returnCode = GetRasEntries(rasEntryNames, bufferSize, entries);

        if (ERROR_BUFFER_TOO_SMALL == returnCode)
        {
            returnCode = GetRasEntries(rasEntryNames, bufferSize, entries);
        }

        if (ERROR_SUCCESS != returnCode)
        {
            my_print(false, _T("failed to enumerate RAS connections: %s"), _tcserror(returnCode));
            throw 0;
        }

        for (DWORD i = 0; i < entries; i++)
        {
            connections.push_back(rasEntryNames[i].szEntryName);
	    }
    }
    catch (...)
    {
    }
    // TODO: Don't throw 0.  Throw and catch a std::exception.
    // Maybe put the error message in the exception.

    // Clean up

    if (rasEntryNames)
    {
        //Deallocate memory for the entries buffer
        HeapFree(GetProcessHeap(), 0, rasEntryNames);
        rasEntryNames = 0;
    }

    return connections;
}