Skip to main content
summaryrefslogtreecommitdiffstats
blob: dfad67405b14438837dc16bf2d2e26995bd9db17 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2014, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.help.internal.base.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.internal.net.ProxyManager;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;


public class ProxyUtil {

	public static boolean isAuthConnSupported()
	{
		try {
			Class.forName("org.eclipse.core.net.proxy.IProxyService"); //$NON-NLS-1$
			return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static IProxyData getProxy(URL url)
	{
		if (!isAuthConnSupported())
			return null;

		IProxyService service = ProxyManager.getProxyManager();
		IProxyData data[];

		if (!service.isProxiesEnabled())
			return null;

		try {
			URI uri = url.toURI();
			if (shouldBypass(uri))
				return null;
			data = service.select(uri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		}

		if (data.length==0)
			return null;
		return data[0];
	}

	public static boolean shouldBypass(URI uri)
	{
		String host = uri.getHost();
		if (host==null)
			return true;

		List<String> hosts = getProxyBypassHosts();
		if (hosts.contains(host))
			return true;
		if ((host.equals("localhost") || host.equals("127.0.0.1")) && hosts.contains("<local>")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			return true;
		return false;
	}


	public static List<String> getProxyBypassHosts()
	{
		List<String> hosts = new ArrayList<>();
		if (!isAuthConnSupported())
			return hosts;

		IProxyService service = ProxyManager.getProxyManager();
		String manuals[] = service.getNonProxiedHosts();
		String natives[] = null;
		if (service instanceof ProxyManager)
			natives = ((ProxyManager)service).getNativeNonProxiedHosts();

		for (int m=0;m<manuals.length;m++)
			hosts.add(manuals[m]);
		if (natives!=null)
			for (int n=0;n<natives.length;n++)
				hosts.add(natives[n]);
		return hosts;
	}

	public static URLConnection getConnection(URL url) throws IOException
	{
		IProxyData data = getProxy(url);
		if (data==null)
			return url.openConnection();

		if (data.isRequiresAuthentication())
			Authenticator.setDefault(new ProxyAuthenticator(data.getUserId(),data.getPassword()));
		else
			Authenticator.setDefault(null);

		Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(data.getHost(), data.getPort()));
		return url.openConnection(proxy);
	}


	public static InputStream getStream(URL url) throws IOException
	{
		return getConnection(url).getInputStream();
	}


	private static class ProxyAuthenticator extends Authenticator {

		private String user, password;

		public ProxyAuthenticator(String user, String password) {
			this.user = user;
			this.password = password;
		}

		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(user, password.toCharArray());
		}
	}
}

Back to the top