Skip to content

Commit b19e91a

Browse files
committed
fix: allow pre/post-2024.3 proxy settings (redhat-developer#242)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 71b135b commit b19e91a

File tree

2 files changed

+347
-49
lines changed

2 files changed

+347
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.intellij.common.utils;
12+
13+
import java.lang.reflect.Constructor;
14+
import java.lang.reflect.Method;
15+
import java.net.Authenticator;
16+
import java.net.InetAddress;
17+
import java.net.PasswordAuthentication;
18+
import java.net.Proxy;
19+
import java.net.ProxySelector;
20+
import java.net.URI;
21+
import java.net.URISyntaxException;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
public class IdeProxyAdapter {
26+
27+
public static ProxySelector getProxySelector() {
28+
ProxySelector selector = Pre243.getProxySelector();
29+
if (selector != null) {
30+
return selector;
31+
} else {
32+
return Post243.getProxySelector();
33+
}
34+
}
35+
36+
public static Authenticator getAuthenticator() {
37+
Authenticator authenticator = Pre243.getAuthenticator();
38+
if (authenticator != null) {
39+
return authenticator;
40+
} else {
41+
return Post243.getAuthenticator();
42+
}
43+
}
44+
45+
public static List<Proxy> getProxies(String url) {
46+
List<Proxy> proxies = Pre243.getProxies(url);
47+
if (proxies != null
48+
&& !proxies.isEmpty()) {
49+
return proxies;
50+
} else {
51+
return Post243.getProxies(url);
52+
}
53+
}
54+
55+
public static PasswordAuthentication getPasswordAuthentication() {
56+
PasswordAuthentication authentication = Pre243.getPasswordAuthentication();
57+
if (authentication != null) {
58+
return authentication;
59+
} else {
60+
return Post243.getPasswordAuthentication();
61+
}
62+
}
63+
64+
private static class Pre243 {
65+
66+
static ProxySelector getProxySelector() {
67+
Object httpConfigurable = httpConfigurable_getInstance();
68+
if (httpConfigurable == null) {
69+
return null;
70+
}
71+
return (ProxySelector) ideaWideProxySelector_newInstance(httpConfigurable);
72+
}
73+
74+
/**
75+
* new IdeaWideAuthenticator(HttpConfigurable)
76+
*/
77+
static Authenticator getAuthenticator() {
78+
Object httpConfigurable = httpConfigurable_getInstance();
79+
if (httpConfigurable == null) {
80+
return null;
81+
}
82+
return (Authenticator) ideaWideAuthenticator_newInstance(httpConfigurable);
83+
}
84+
85+
static List<Proxy> getProxies(String url) {
86+
List<Proxy> proxies = new ArrayList<>();
87+
try {
88+
Object httpConfigurable = httpConfigurable_getInstance();
89+
if (httpConfigurable != null) {
90+
Object ideaWideProxySelector = ideaWideProxySelector_newInstance(httpConfigurable);
91+
if (ideaWideProxySelector != null) {
92+
proxies = ideaWideProxySelector_select(ideaWideProxySelector, new URI(url));
93+
}
94+
}
95+
} catch (URISyntaxException e) {
96+
// swallow only
97+
}
98+
return proxies;
99+
}
100+
101+
/**
102+
* new IdeaWideAuthenticator(HttpConfigurable.getInstance()).getPasswordAuthentication()
103+
*/
104+
static private PasswordAuthentication getPasswordAuthentication() {
105+
PasswordAuthentication authentication = null;
106+
Object httpConfigurable = httpConfigurable_getInstance();
107+
if (httpConfigurable != null) {
108+
authentication = httpConfigurable_getPromptedAuthentication("*", httpConfigurable);
109+
}
110+
return authentication;
111+
}
112+
113+
/**
114+
* IdeaWideProxySelector.select(URI)
115+
*/
116+
@SuppressWarnings("unchecked")
117+
private static List<Proxy> ideaWideProxySelector_select(Object ideaWideProxySelector, URI uri) {
118+
try {
119+
Method method = ideaWideProxySelector.getClass().getDeclaredMethod("select", URI.class);
120+
return (List<Proxy>) method.invoke(ideaWideProxySelector, uri);
121+
} catch (ReflectiveOperationException | IllegalArgumentException e) {
122+
return null;
123+
}
124+
}
125+
126+
/**
127+
* new IdeaWideProxySelector()
128+
*/
129+
@SuppressWarnings("JavaReflectionMemberAccess")
130+
private static Object ideaWideProxySelector_newInstance(Object httpConfigurable) {
131+
try {
132+
Class<?> clazz = Class.forName("com.intellij.util.net.IdeaWideProxySelector");
133+
Constructor<?> constructor = clazz.getConstructor(httpConfigurable.getClass());
134+
return constructor.newInstance(httpConfigurable);
135+
} catch (ReflectiveOperationException | IllegalArgumentException e) {
136+
return null;
137+
}
138+
}
139+
140+
/**
141+
* new IdeaWideAuthenticator(HttpConfigurable)
142+
*/
143+
@SuppressWarnings("JavaReflectionMemberAccess")
144+
private static Object ideaWideAuthenticator_newInstance(Object httpConfigurable) {
145+
try {
146+
Class<?> clazz = Class.forName("com.intellij.util.net.IdeaWideAuthenticator");
147+
Constructor<?> constructor = clazz.getConstructor(httpConfigurable.getClass());
148+
return constructor.newInstance(httpConfigurable);
149+
} catch (Exception e) {
150+
return null;
151+
}
152+
}
153+
154+
/**
155+
* IdeaWideAuthenticator.getPromptedAuthentication()
156+
*/
157+
private static PasswordAuthentication httpConfigurable_getPromptedAuthentication(String url, Object httpConfigurable) {
158+
try {
159+
Method method = httpConfigurable.getClass().getMethod("getPromptedAuthentication", String.class, String.class);
160+
return (PasswordAuthentication) method.invoke(httpConfigurable, url, null);
161+
} catch (Exception e) {
162+
return null;
163+
}
164+
}
165+
166+
/**
167+
* HttpConfigurable.getInstance()
168+
*/
169+
private static Object httpConfigurable_getInstance() {
170+
try {
171+
Class<?> clazz = Class.forName("com.intellij.util.net.HttpConfigurable");
172+
Method getInstanceMethod = clazz.getDeclaredMethod("getInstance");
173+
return getInstanceMethod.invoke(null);
174+
} catch (ReflectiveOperationException e) {
175+
return null;
176+
}
177+
}
178+
}
179+
180+
private static class Post243 {
181+
182+
/**
183+
* JdkProxyProvider.getInstance().getProxySelector()
184+
*/
185+
static ProxySelector getProxySelector() {
186+
Object provider = jdkProxyProvider_getInstance();
187+
if (provider == null) {
188+
return null;
189+
}
190+
return (ProxySelector) jdkProxyProvider_getProxySelector(provider);
191+
}
192+
193+
/**
194+
* JdkProxyProvider.getInstance().getAuthenticator()
195+
*/
196+
public static Authenticator getAuthenticator() {
197+
Object provider = jdkProxyProvider_getInstance();
198+
if (provider == null) {
199+
return null;
200+
}
201+
return (Authenticator) jdkProxyProvider_getAuthenticator(provider);
202+
}
203+
204+
/**
205+
* JdkProxyProvider.getInstance().getProxySelector().select(URI.create(url))
206+
*/
207+
static List<Proxy> getProxies(String url) {
208+
List<Proxy> proxies = new ArrayList<>();
209+
try {
210+
ProxySelector selector = getProxySelector();
211+
if (selector != null) {
212+
proxies = proxySelector_select(selector, new URI(url));
213+
}
214+
} catch (Exception e) {
215+
// swallow only
216+
}
217+
return proxies;
218+
}
219+
220+
/**
221+
* JdkProxyProvider.getInstance().getAuthenticator().requestPasswordAuthentication(
222+
* null,
223+
* 0,
224+
* null,
225+
* null,
226+
* null
227+
* );
228+
*/
229+
private static PasswordAuthentication getPasswordAuthentication() {
230+
PasswordAuthentication authentication = null;
231+
Object provider = jdkProxyProvider_getInstance();
232+
if (provider != null) {
233+
Object authenticator = jdkProxyProvider_getAuthenticator(provider);
234+
if (authenticator != null) {
235+
authentication = authenticator_requestPasswordAuthentication(authenticator);
236+
}
237+
}
238+
return authentication;
239+
}
240+
241+
/**
242+
* ProxySelector.select(URI)
243+
*/
244+
@SuppressWarnings("unchecked")
245+
private static List<Proxy> proxySelector_select(Object proxySelector, URI uri) {
246+
try {
247+
Method method = proxySelector.getClass().getDeclaredMethod("select", URI.class);
248+
method.setAccessible(true);
249+
return (List<Proxy>) method.invoke(proxySelector, uri);
250+
} catch (Exception e) {
251+
return null;
252+
}
253+
}
254+
255+
/**
256+
* Authenticator.requestPasswordAuthentication()
257+
*/
258+
private static PasswordAuthentication authenticator_requestPasswordAuthentication(Object authenticator) {
259+
try {
260+
Method method = authenticator.getClass().getMethod("requestPasswordAuthentication",
261+
InetAddress.class,
262+
Integer.TYPE,
263+
String.class,
264+
String.class,
265+
String.class);
266+
return (PasswordAuthentication) method.invoke(authenticator,
267+
null,
268+
0,
269+
null,
270+
null,
271+
null
272+
);
273+
} catch (Exception e) {
274+
return null;
275+
}
276+
}
277+
278+
/**
279+
* JdkProxyProvider.getProxySelector()
280+
*/
281+
private static Object jdkProxyProvider_getProxySelector(Object jdkProxyProvider) {
282+
try {
283+
Method getInstanceMethod = jdkProxyProvider.getClass().getDeclaredMethod("getProxySelector");
284+
return getInstanceMethod.invoke(jdkProxyProvider);
285+
} catch (Exception e) {
286+
return null;
287+
}
288+
}
289+
290+
/**
291+
* JdkProxyProvider.getAuthenticator()
292+
*/
293+
private static Object jdkProxyProvider_getAuthenticator(Object jdkProxyProvider) {
294+
try {
295+
Method getAuthenticatorMethod = jdkProxyProvider.getClass().getDeclaredMethod("getAuthenticator");
296+
return getAuthenticatorMethod.invoke(jdkProxyProvider);
297+
} catch (Exception e) {
298+
return null;
299+
}
300+
}
301+
302+
/**
303+
* JdkProxyProvider.getInstance()
304+
*/
305+
private static Object jdkProxyProvider_getInstance() {
306+
try {
307+
Class<?> clazz = Class.forName("com.intellij.util.net.JdkProxyProvider");
308+
Method getInstanceMethod = clazz.getDeclaredMethod("getInstance");
309+
return getInstanceMethod.invoke(null);
310+
} catch (Exception e) {
311+
return null;
312+
}
313+
}
314+
}
315+
}

0 commit comments

Comments
 (0)