1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21 import org.apache.commons.lang3.StringUtils;
22
23 import java.io.IOException;
24 import java.net.Authenticator;
25 import java.net.HttpURLConnection;
26 import java.net.InetSocketAddress;
27 import java.net.PasswordAuthentication;
28 import java.net.Proxy;
29 import java.net.SocketAddress;
30 import java.net.URL;
31
32
33
34
35
36
37
38 public final class URLConnectionFactory {
39
40
41
42
43 private URLConnectionFactory() {
44 }
45
46
47
48
49
50
51
52
53
54 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Just being extra safe")
55 public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException {
56 HttpURLConnection conn = null;
57 final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER);
58
59 try {
60 if (proxyUrl != null && !matchNonProxy(url)) {
61 final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
62 final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort);
63
64 final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME);
65 final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD);
66
67 if (username != null && password != null) {
68 final Authenticator auth = new Authenticator() {
69 @Override
70 public PasswordAuthentication getPasswordAuthentication() {
71 if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) {
72 return new PasswordAuthentication(username, password.toCharArray());
73 }
74 return super.getPasswordAuthentication();
75 }
76 };
77 Authenticator.setDefault(auth);
78 }
79
80 final Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
81 conn = (HttpURLConnection) url.openConnection(proxy);
82 } else {
83 conn = (HttpURLConnection) url.openConnection();
84 }
85 final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000);
86 conn.setConnectTimeout(timeout);
87 conn.setInstanceFollowRedirects(true);
88 } catch (IOException ex) {
89 if (conn != null) {
90 try {
91 conn.disconnect();
92 } finally {
93 conn = null;
94 }
95 }
96 throw new URLConnectionFailureException("Error getting connection.", ex);
97 }
98 return conn;
99 }
100
101
102
103
104
105
106
107 private static boolean matchNonProxy(final URL url) {
108 final String host = url.getHost();
109
110
111 final String nonProxyHosts = Settings.getString(Settings.KEYS.PROXY_NON_PROXY_HOSTS);
112 if (null != nonProxyHosts) {
113 final String[] nonProxies = nonProxyHosts.split("(,)|(;)|(\\|)");
114 for (final String nonProxyHost : nonProxies) {
115
116 if (null != nonProxyHost && nonProxyHost.contains("*")) {
117
118 final int pos = nonProxyHost.indexOf('*');
119 final String nonProxyHostPrefix = nonProxyHost.substring(0, pos);
120 final String nonProxyHostSuffix = nonProxyHost.substring(pos + 1);
121
122 if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && StringUtils.isEmpty(nonProxyHostSuffix)) {
123 return true;
124 }
125
126 if (StringUtils.isEmpty(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix) && host.endsWith(nonProxyHostSuffix)) {
127 return true;
128 }
129
130 if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix)
131 && host.endsWith(nonProxyHostSuffix)) {
132 return true;
133 }
134 } else if (host.equals(nonProxyHost)) {
135 return true;
136 }
137 }
138 }
139 return false;
140 }
141
142
143
144
145
146
147
148
149
150
151 public static HttpURLConnection createHttpURLConnection(URL url, boolean proxy) throws URLConnectionFailureException {
152 if (proxy) {
153 return createHttpURLConnection(url);
154 }
155 HttpURLConnection conn = null;
156 try {
157 conn = (HttpURLConnection) url.openConnection();
158 final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000);
159 conn.setConnectTimeout(timeout);
160 conn.setInstanceFollowRedirects(true);
161 } catch (IOException ioe) {
162 throw new URLConnectionFailureException("Error getting connection.", ioe);
163 }
164 return conn;
165 }
166 }