| 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 | |
import java.net.URLConnection; |
| 32 | |
import java.security.KeyManagementException; |
| 33 | |
import java.security.NoSuchAlgorithmException; |
| 34 | |
import javax.net.ssl.HttpsURLConnection; |
| 35 | |
import org.slf4j.Logger; |
| 36 | |
import org.slf4j.LoggerFactory; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public final class URLConnectionFactory { |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(URLConnectionFactory.class); |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | private URLConnectionFactory() { |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Just being extra safe") |
| 68 | |
public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException { |
| 69 | 0 | HttpURLConnection conn = null; |
| 70 | 0 | final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER); |
| 71 | |
|
| 72 | |
try { |
| 73 | 0 | if (proxyUrl != null && !matchNonProxy(url)) { |
| 74 | 0 | final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT); |
| 75 | 0 | final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort); |
| 76 | |
|
| 77 | 0 | final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME); |
| 78 | 0 | final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD); |
| 79 | |
|
| 80 | 0 | if (username != null && password != null) { |
| 81 | 0 | final Authenticator auth = new Authenticator() { |
| 82 | |
@Override |
| 83 | |
public PasswordAuthentication getPasswordAuthentication() { |
| 84 | 0 | if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) { |
| 85 | 0 | return new PasswordAuthentication(username, password.toCharArray()); |
| 86 | |
} |
| 87 | 0 | return super.getPasswordAuthentication(); |
| 88 | |
} |
| 89 | |
}; |
| 90 | 0 | Authenticator.setDefault(auth); |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | final Proxy proxy = new Proxy(Proxy.Type.HTTP, address); |
| 94 | 0 | conn = (HttpURLConnection) url.openConnection(proxy); |
| 95 | 0 | } else { |
| 96 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 97 | |
} |
| 98 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000); |
| 99 | 0 | conn.setConnectTimeout(timeout); |
| 100 | 0 | conn.setInstanceFollowRedirects(true); |
| 101 | 0 | } catch (IOException ex) { |
| 102 | 0 | if (conn != null) { |
| 103 | |
try { |
| 104 | 0 | conn.disconnect(); |
| 105 | |
} finally { |
| 106 | 0 | conn = null; |
| 107 | 0 | } |
| 108 | |
} |
| 109 | 0 | throw new URLConnectionFailureException("Error getting connection.", ex); |
| 110 | 0 | } |
| 111 | 0 | configureTLS(url, conn); |
| 112 | 0 | return conn; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
private static boolean matchNonProxy(final URL url) { |
| 122 | 0 | final String host = url.getHost(); |
| 123 | |
|
| 124 | |
|
| 125 | 0 | final String nonProxyHosts = Settings.getString(Settings.KEYS.PROXY_NON_PROXY_HOSTS); |
| 126 | 0 | if (null != nonProxyHosts) { |
| 127 | 0 | final String[] nonProxies = nonProxyHosts.split("(,)|(;)|(\\|)"); |
| 128 | 0 | for (final String nonProxyHost : nonProxies) { |
| 129 | |
|
| 130 | 0 | if (null != nonProxyHost && nonProxyHost.contains("*")) { |
| 131 | |
|
| 132 | 0 | final int pos = nonProxyHost.indexOf('*'); |
| 133 | 0 | final String nonProxyHostPrefix = nonProxyHost.substring(0, pos); |
| 134 | 0 | final String nonProxyHostSuffix = nonProxyHost.substring(pos + 1); |
| 135 | |
|
| 136 | 0 | if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && StringUtils.isEmpty(nonProxyHostSuffix)) { |
| 137 | 0 | return true; |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | if (StringUtils.isEmpty(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix) && host.endsWith(nonProxyHostSuffix)) { |
| 141 | 0 | return true; |
| 142 | |
} |
| 143 | |
|
| 144 | 0 | if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix) |
| 145 | 0 | && host.endsWith(nonProxyHostSuffix)) { |
| 146 | 0 | return true; |
| 147 | |
} |
| 148 | 0 | } else if (host.equals(nonProxyHost)) { |
| 149 | 0 | return true; |
| 150 | |
} |
| 151 | |
} |
| 152 | |
} |
| 153 | 0 | return false; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public static HttpURLConnection createHttpURLConnection(URL url, boolean proxy) throws URLConnectionFailureException { |
| 168 | 0 | if (proxy) { |
| 169 | 0 | return createHttpURLConnection(url); |
| 170 | |
} |
| 171 | 0 | HttpURLConnection conn = null; |
| 172 | |
try { |
| 173 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 174 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000); |
| 175 | 0 | conn.setConnectTimeout(timeout); |
| 176 | 0 | conn.setInstanceFollowRedirects(true); |
| 177 | 0 | } catch (IOException ioe) { |
| 178 | 0 | throw new URLConnectionFailureException("Error getting connection.", ioe); |
| 179 | 0 | } |
| 180 | 0 | configureTLS(url, conn); |
| 181 | 0 | return conn; |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
private static void configureTLS(URL url, URLConnection conn) { |
| 193 | 0 | if ("https".equals(url.getProtocol())) { |
| 194 | |
try { |
| 195 | 0 | final HttpsURLConnection secCon = (HttpsURLConnection) conn; |
| 196 | 0 | final SSLSocketFactoryEx factory = new SSLSocketFactoryEx(); |
| 197 | 0 | secCon.setSSLSocketFactory(factory); |
| 198 | 0 | } catch (NoSuchAlgorithmException ex) { |
| 199 | 0 | LOGGER.debug("Unsupported algorithm in SSLSocketFactoryEx", ex); |
| 200 | 0 | } catch (KeyManagementException ex) { |
| 201 | 0 | LOGGER.debug("Key management exception in SSLSocketFactoryEx", ex); |
| 202 | 0 | } |
| 203 | |
} |
| 204 | 0 | } |
| 205 | |
} |