| 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 | 0 | private URLConnectionFactory() { |
| 44 | 0 | } |
| 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 | 0 | HttpURLConnection conn = null; |
| 57 | 0 | final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER); |
| 58 | |
|
| 59 | |
try { |
| 60 | 0 | if (proxyUrl != null && !matchNonProxy(url)) { |
| 61 | 0 | final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT); |
| 62 | 0 | final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort); |
| 63 | |
|
| 64 | 0 | final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME); |
| 65 | 0 | final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD); |
| 66 | |
|
| 67 | 0 | if (username != null && password != null) { |
| 68 | 0 | final Authenticator auth = new Authenticator() { |
| 69 | |
@Override |
| 70 | |
public PasswordAuthentication getPasswordAuthentication() { |
| 71 | 0 | if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) { |
| 72 | 0 | return new PasswordAuthentication(username, password.toCharArray()); |
| 73 | |
} |
| 74 | 0 | return super.getPasswordAuthentication(); |
| 75 | |
} |
| 76 | |
}; |
| 77 | 0 | Authenticator.setDefault(auth); |
| 78 | |
} |
| 79 | |
|
| 80 | 0 | final Proxy proxy = new Proxy(Proxy.Type.HTTP, address); |
| 81 | 0 | conn = (HttpURLConnection) url.openConnection(proxy); |
| 82 | 0 | } else { |
| 83 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 84 | |
} |
| 85 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000); |
| 86 | 0 | conn.setConnectTimeout(timeout); |
| 87 | 0 | conn.setInstanceFollowRedirects(true); |
| 88 | 0 | } catch (IOException ex) { |
| 89 | 0 | if (conn != null) { |
| 90 | |
try { |
| 91 | 0 | conn.disconnect(); |
| 92 | |
} finally { |
| 93 | 0 | conn = null; |
| 94 | 0 | } |
| 95 | |
} |
| 96 | 0 | throw new URLConnectionFailureException("Error getting connection.", ex); |
| 97 | 0 | } |
| 98 | 0 | return conn; |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private static boolean matchNonProxy(final URL url) { |
| 108 | 0 | final String host = url.getHost(); |
| 109 | |
|
| 110 | |
|
| 111 | 0 | final String nonProxyHosts = Settings.getString(Settings.KEYS.PROXY_NON_PROXY_HOSTS); |
| 112 | 0 | if (null != nonProxyHosts) { |
| 113 | 0 | final String[] nonProxies = nonProxyHosts.split("(,)|(;)|(\\|)"); |
| 114 | 0 | for (final String nonProxyHost : nonProxies) { |
| 115 | |
|
| 116 | 0 | if (null != nonProxyHost && nonProxyHost.contains("*")) { |
| 117 | |
|
| 118 | 0 | final int pos = nonProxyHost.indexOf('*'); |
| 119 | 0 | final String nonProxyHostPrefix = nonProxyHost.substring(0, pos); |
| 120 | 0 | final String nonProxyHostSuffix = nonProxyHost.substring(pos + 1); |
| 121 | |
|
| 122 | 0 | if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && StringUtils.isEmpty(nonProxyHostSuffix)) { |
| 123 | 0 | return true; |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | if (StringUtils.isEmpty(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix) && host.endsWith(nonProxyHostSuffix)) { |
| 127 | 0 | return true; |
| 128 | |
} |
| 129 | |
|
| 130 | 0 | if (!StringUtils.isEmpty(nonProxyHostPrefix) && host.startsWith(nonProxyHostPrefix) && !StringUtils.isEmpty(nonProxyHostSuffix) |
| 131 | 0 | && host.endsWith(nonProxyHostSuffix)) { |
| 132 | 0 | return true; |
| 133 | |
} |
| 134 | 0 | } else if (host.equals(nonProxyHost)) { |
| 135 | 0 | return true; |
| 136 | |
} |
| 137 | |
} |
| 138 | |
} |
| 139 | 0 | 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 | 0 | if (proxy) { |
| 153 | 0 | return createHttpURLConnection(url); |
| 154 | |
} |
| 155 | 0 | HttpURLConnection conn = null; |
| 156 | |
try { |
| 157 | 0 | conn = (HttpURLConnection) url.openConnection(); |
| 158 | 0 | final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000); |
| 159 | 0 | conn.setConnectTimeout(timeout); |
| 160 | 0 | conn.setInstanceFollowRedirects(true); |
| 161 | 0 | } catch (IOException ioe) { |
| 162 | 0 | throw new URLConnectionFailureException("Error getting connection.", ioe); |
| 163 | 0 | } |
| 164 | 0 | return conn; |
| 165 | |
} |
| 166 | |
} |