Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/windows/native/libnet/DefaultProxySelector.c
67723 views
1
/*
2
* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <windows.h>
27
#include <Winhttp.h>
28
29
#include "jni.h"
30
#include "jni_util.h"
31
#include "jvm.h"
32
33
#include "proxy_util.h"
34
35
#include "sun_net_spi_DefaultProxySelector.h"
36
37
/*
38
* These functions are used by the sun.net.spi.DefaultProxySelector class
39
* to access some platform specific settings.
40
* On Windows use WinHTTP functions to get the system settings.
41
*/
42
43
/* Keep one static session for all requests. */
44
static HINTERNET session = NULL;
45
46
/*
47
* Class: sun_net_spi_DefaultProxySelector
48
* Method: init
49
* Signature: ()Z
50
*/
51
JNIEXPORT jboolean JNICALL
52
Java_sun_net_spi_DefaultProxySelector_init(JNIEnv *env, jclass clazz) {
53
54
/*
55
* Get one WinHTTP session handle to initialize the WinHTTP internal data
56
* structures. Keep and use only this one for the whole life time.
57
*/
58
session = WinHttpOpen(L"Only used internal", /* we need no real agent string here */
59
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
60
WINHTTP_NO_PROXY_NAME,
61
WINHTTP_NO_PROXY_BYPASS,
62
0);
63
if (session == NULL) {
64
return JNI_FALSE;
65
}
66
67
if (!initJavaClass(env)) {
68
return JNI_FALSE;
69
}
70
71
return JNI_TRUE;
72
}
73
74
75
#define MAX_STR_LEN 1024
76
77
/* A linked list element for a proxy */
78
typedef struct list_item {
79
wchar_t *host;
80
int port;
81
struct list_item *next;
82
} list_item;
83
84
/* Free the linked list */
85
static void freeList(list_item *head) {
86
list_item *next = NULL;
87
list_item *current = head;
88
while (current != NULL) {
89
next = current->next;
90
free(current->host);
91
free(current);
92
current = next;
93
}
94
}
95
96
97
/*
98
* Creates a linked list of list_item elements that has to be freed later on.
99
* Returns the size of the array as int.
100
*/
101
static int createProxyList(LPWSTR win_proxy, const WCHAR *pproto, list_item **head) {
102
static const wchar_t separators[] = L"\t\r\n ;";
103
list_item *current = NULL;
104
int nr_elems = 0;
105
wchar_t *context = NULL;
106
wchar_t *current_proxy = NULL;
107
108
/*
109
* The proxy server list contains one or more of the following strings
110
* separated by semicolons or whitespace:
111
* ([<scheme>=][<scheme>"://"]<server>[":"<port>])
112
*/
113
current_proxy = wcstok_s(win_proxy, separators, &context);
114
while (current_proxy != NULL) {
115
LPWSTR pport;
116
LPWSTR phost;
117
int portVal = 0;
118
list_item *proxy = NULL;
119
wchar_t* pos = NULL;
120
121
/* Filter based on the scheme, if there is one */
122
pos = wcschr(current_proxy, L'=');
123
if (pos) {
124
*pos = L'\0';
125
if (wcscmp(current_proxy, pproto) != 0) {
126
current_proxy = wcstok_s(NULL, separators, &context);
127
continue;
128
}
129
current_proxy = pos + 1;
130
}
131
132
/* Let's check for a scheme and ignore it. */
133
if ((phost = wcsstr(current_proxy, L"://")) != NULL) {
134
phost += 3;
135
} else {
136
phost = current_proxy;
137
}
138
139
/* Get the port */
140
pport = wcschr(phost, L':');
141
if (pport != NULL) {
142
*pport = 0;
143
pport++;
144
swscanf(pport, L"%d", &portVal);
145
}
146
147
proxy = (list_item *)malloc(sizeof(list_item));
148
if (proxy != NULL) {
149
proxy->next = NULL;
150
proxy->port = portVal;
151
proxy->host = _wcsdup(phost);
152
153
if (proxy->host != NULL) {
154
if (*head == NULL) {
155
*head = proxy; /* first elem */
156
}
157
if (current != NULL) {
158
current->next = proxy;
159
}
160
current = proxy;
161
nr_elems++;
162
} else {
163
free(proxy); /* cleanup */
164
}
165
}
166
/* goto next proxy if available... */
167
current_proxy = wcstok_s(NULL, separators, &context);
168
}
169
return nr_elems;
170
}
171
172
173
174
/*
175
* Class: sun_net_spi_DefaultProxySelector
176
* Method: getSystemProxies
177
* Signature: ([Ljava/lang/String;Ljava/lang/String;)[Ljava/net/Proxy;
178
*/
179
JNIEXPORT jobjectArray JNICALL
180
Java_sun_net_spi_DefaultProxySelector_getSystemProxies(JNIEnv *env,
181
jobject this,
182
jstring proto,
183
jstring host)
184
{
185
jobjectArray proxy_array = NULL;
186
jobject type_proxy = NULL;
187
LPCWSTR lpProto;
188
LPCWSTR lpHost;
189
list_item *head = NULL;
190
191
BOOL use_auto_proxy = FALSE;
192
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_proxy_config;
193
WINHTTP_AUTOPROXY_OPTIONS auto_proxy_options;
194
WINHTTP_PROXY_INFO proxy_info;
195
LPWSTR win_proxy = NULL;
196
LPWSTR win_bypass_proxy = NULL;
197
198
memset(&ie_proxy_config, 0, sizeof(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG));
199
memset(&auto_proxy_options, 0, sizeof(WINHTTP_AUTOPROXY_OPTIONS));
200
memset(&proxy_info, 0, sizeof(WINHTTP_PROXY_INFO));
201
202
lpHost = (*env)->GetStringChars(env, host, NULL);
203
if (lpHost == NULL) {
204
if (!(*env)->ExceptionCheck(env))
205
JNU_ThrowOutOfMemoryError(env, NULL);
206
return NULL;
207
}
208
209
lpProto = (*env)->GetStringChars(env, proto, NULL);
210
if (lpProto == NULL) {
211
(*env)->ReleaseStringChars(env, host, lpHost);
212
if (!(*env)->ExceptionCheck(env))
213
JNU_ThrowOutOfMemoryError(env, NULL);
214
return NULL;
215
}
216
217
if (WinHttpGetIEProxyConfigForCurrentUser(&ie_proxy_config) == FALSE) {
218
/* cleanup and exit */
219
(*env)->ReleaseStringChars(env, host, lpHost);
220
(*env)->ReleaseStringChars(env, proto, lpProto);
221
return NULL;
222
}
223
224
if (ie_proxy_config.fAutoDetect) {
225
/* Windows uses WPAD */
226
auto_proxy_options.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP |
227
WINHTTP_AUTO_DETECT_TYPE_DNS_A;
228
auto_proxy_options.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
229
auto_proxy_options.fAutoLogonIfChallenged = TRUE;
230
use_auto_proxy = TRUE;
231
} else if (ie_proxy_config.lpszAutoConfigUrl != NULL) {
232
/* Windows uses PAC file */
233
auto_proxy_options.lpszAutoConfigUrl = ie_proxy_config.lpszAutoConfigUrl;
234
auto_proxy_options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
235
use_auto_proxy = TRUE;
236
} else if (ie_proxy_config.lpszProxy != NULL) {
237
/* Windows uses manually entered proxy. */
238
use_auto_proxy = FALSE;
239
win_bypass_proxy = ie_proxy_config.lpszProxyBypass;
240
win_proxy = ie_proxy_config.lpszProxy;
241
}
242
243
if (use_auto_proxy) {
244
WCHAR url[MAX_STR_LEN];
245
/* Create url for WinHttpGetProxyForUrl */
246
_snwprintf(url, sizeof(url) - 1, L"%s://%s", lpProto, lpHost);
247
/* Get proxy for URL from Windows */
248
use_auto_proxy = WinHttpGetProxyForUrl(session, &url[0], &auto_proxy_options, &proxy_info);
249
if (use_auto_proxy) {
250
win_proxy = proxy_info.lpszProxy;
251
win_bypass_proxy = proxy_info.lpszProxyBypass;
252
}
253
}
254
255
/* Check the bypass entry. */
256
if (NULL != win_bypass_proxy) {
257
/*
258
* From MSDN:
259
* The proxy bypass list contains one or more server names separated by
260
* semicolons or whitespace. The proxy bypass list can also contain the
261
* string "<local>" to indicate that all local intranet sites are
262
* bypassed. Local intranet sites are considered to be all servers that
263
* do not contain a period in their name.
264
*/
265
wchar_t *context = NULL;
266
LPWSTR s = wcstok_s(win_bypass_proxy, L"; ", &context);
267
268
while (s != NULL) {
269
size_t maxlen = wcslen(s);
270
if (wcsncmp(s, lpHost, maxlen) == 0) {
271
/*
272
* The URL host name matches with one of the prefixes, use a
273
* direct connection.
274
*/
275
goto noproxy;
276
}
277
if (wcsncmp(s, L"<local>", maxlen) == 0) {
278
/*
279
* All local intranet sites are bypassed - Microsoft consider all
280
* servers that do not contain a period in their name to be local.
281
*/
282
if (wcschr(lpHost, '.') == NULL) {
283
goto noproxy;
284
}
285
}
286
s = wcstok_s(NULL, L"; ", &context);
287
}
288
}
289
290
if (win_proxy != NULL) {
291
int defport = 0;
292
int nr_elems = 0;
293
294
/* Set the default port value & proxy type from protocol. */
295
if ((wcscmp(lpProto, L"http") == 0) ||
296
(wcscmp(lpProto, L"ftp") == 0))
297
defport = 80;
298
if (wcscmp(lpProto, L"https") == 0)
299
defport = 443;
300
if (wcscmp(lpProto, L"socks") == 0) {
301
defport = 1080;
302
type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_socksID);
303
} else {
304
type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_httpID);
305
}
306
if (type_proxy == NULL || (*env)->ExceptionCheck(env)) {
307
goto noproxy;
308
}
309
310
nr_elems = createProxyList(win_proxy, lpProto, &head);
311
if (nr_elems != 0 && head != NULL) {
312
int index = 0;
313
list_item *current = head;
314
proxy_array = (*env)->NewObjectArray(env, nr_elems, proxy_class, NULL);
315
if (proxy_array == NULL || (*env)->ExceptionCheck(env)) {
316
goto noproxy;
317
}
318
while (current != NULL && index < nr_elems) {
319
jstring jhost;
320
jobject isa;
321
jobject proxy;
322
323
if (current->host != NULL && proxy_array != NULL) {
324
/* Let's create the appropriate Proxy object then. */
325
if (current->port == 0) {
326
current->port = defport;
327
}
328
jhost = (*env)->NewString(env, current->host, (jsize)wcslen(current->host));
329
if (jhost == NULL || (*env)->ExceptionCheck(env)) {
330
proxy_array = NULL;
331
}
332
isa = (*env)->CallStaticObjectMethod(env, isaddr_class,
333
isaddr_createUnresolvedID, jhost,
334
current->port);
335
if (isa == NULL || (*env)->ExceptionCheck(env)) {
336
proxy_array = NULL;
337
}
338
proxy = (*env)->NewObject(env, proxy_class, proxy_ctrID, type_proxy, isa);
339
if (proxy == NULL || (*env)->ExceptionCheck(env)) {
340
proxy_array = NULL;
341
}
342
(*env)->SetObjectArrayElement(env, proxy_array, index, proxy);
343
if ((*env)->ExceptionCheck(env)) {
344
proxy_array = NULL;
345
}
346
index++;
347
}
348
current = current->next;
349
}
350
}
351
}
352
353
noproxy:
354
if (head != NULL) {
355
freeList(head);
356
}
357
if (proxy_info.lpszProxy != NULL)
358
GlobalFree(proxy_info.lpszProxy);
359
if (proxy_info.lpszProxyBypass != NULL)
360
GlobalFree(proxy_info.lpszProxyBypass);
361
if (ie_proxy_config.lpszAutoConfigUrl != NULL)
362
GlobalFree(ie_proxy_config.lpszAutoConfigUrl);
363
if (ie_proxy_config.lpszProxy != NULL)
364
GlobalFree(ie_proxy_config.lpszProxy);
365
if (ie_proxy_config.lpszProxyBypass != NULL)
366
GlobalFree(ie_proxy_config.lpszProxyBypass);
367
if (lpHost != NULL)
368
(*env)->ReleaseStringChars(env, host, lpHost);
369
if (lpProto != NULL)
370
(*env)->ReleaseStringChars(env, proto, lpProto);
371
372
return proxy_array;
373
}
374
375