public Proxy createProxy(String host) { return createProxy(host, name, port, noProxyHost); }
/** * @deprecated * Use {@link #createProxy(String)} */ @Deprecated public Proxy createProxy() { return createProxy(null); }
public static InputStream getInputStream(URL url) throws IOException { final ProxyConfiguration p = get(); if (p == null) return new RetryableHttpStream(url); Proxy proxy = p.createProxy(url.getHost()); InputStream is = new RetryableHttpStream(url, proxy); if (p.getUserName() != null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(p.authenticator); p.jenkins48775workaround(proxy, url); } return is; }
/** * This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly. */ public static URLConnection open(URL url) throws IOException { final ProxyConfiguration p = get(); URLConnection con; if(p==null) { con = url.openConnection(); } else { Proxy proxy = p.createProxy(url.getHost()); con = url.openConnection(proxy); if(p.getUserName()!=null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(p.authenticator); p.jenkins48775workaround(proxy, url); } } if(DEFAULT_CONNECT_TIMEOUT_MILLIS > 0) { con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS); } if (JenkinsJVM.isJenkinsJVM()) { // this code may run on a slave decorate(con); } return con; }
Proxy proxy = httpProxyConfig.createProxy(getHost(url)); if (proxy.type() == HTTP) { InetSocketAddress address = (InetSocketAddress) proxy.address();
public Proxy createProxy(String host) { return createProxy(host, name, port, noProxyHost); }
/** * @deprecated * Use {@link #createProxy(String)} */ @Deprecated public Proxy createProxy() { return createProxy(null); }
/** * Uses proxy if configured on pluginManager/advanced page * * @param apiUrl GitHub's url to build proxy to * * @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour */ @Nonnull private Proxy getProxy(String apiUrl) { Jenkins jenkins = GitHubWebHook.getJenkinsInstance(); if (jenkins.proxy == null) { return Proxy.NO_PROXY; } else { try { return jenkins.proxy.createProxy(new URL(apiUrl).getHost()); } catch (MalformedURLException e) { return jenkins.proxy.createProxy(apiUrl); } } }
/** * Uses proxy if configured on pluginManager/advanced page * * @param host GitHub's hostname to build proxy to * * @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour */ @Nonnull private static Proxy getProxy(@Nonnull String host) { Jenkins jenkins = GitHubWebHook.getJenkinsInstance(); if (jenkins.proxy == null) { return Proxy.NO_PROXY; } else { return jenkins.proxy.createProxy(host); } }
private static HttpURLConnection openConnection(UriTemplate template) throws IOException { URL url = new URL(template.expand()); Jenkins jenkins = Jenkins.getInstance(); if (jenkins == null || jenkins.proxy == null) { return (HttpURLConnection) url.openConnection(); } return (HttpURLConnection) url.openConnection(jenkins.proxy.createProxy(url.getHost())); }
private HttpHost getProxy(HttpUriRequest method) throws URIException { ProxyConfiguration proxy = Jenkins.getInstance().proxy; if (proxy == null) return null; Proxy p = proxy.createProxy(method.getURI().getHost()); switch (p.type()) { case DIRECT: return null; case HTTP: InetSocketAddress sa = (InetSocketAddress) p.address(); return new HttpHost(sa.getHostName(), sa.getPort()); case SOCKS: default: return null; } } }
/** * Returns an HTTP url connection given a url object. Supports jenkins configured proxy. * * @param url - a URL object containing the URL to open a connection to. * @return a HttpURLConnection object. * @throws IOException if HttpURLConnection fails to open connection */ public static HttpURLConnection getHttpURLConnection(final URL url) throws IOException { HttpURLConnection conn = null; ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; /* Attempt to use proxy */ if (proxyConfig != null) { Proxy proxy = proxyConfig.createProxy(url.getHost()); if (proxy != null && proxy.type() == Proxy.Type.HTTP) { logger.fine("Attempting to use the Jenkins proxy configuration"); conn = (HttpURLConnection) url.openConnection(proxy); if (conn == null) { logger.fine("Failed to use the Jenkins proxy configuration"); } } } else { logger.fine("Jenkins proxy configuration not found"); } /* If proxy fails, use HttpURLConnection */ if (conn == null) { conn = (HttpURLConnection) url.openConnection(); logger.fine("Using HttpURLConnection, without proxy"); } return conn; }
public URLConnection openUrl(URL url) throws IOException { if (name == null) { URLConnection urlConnection = url.openConnection(); connect(urlConnection); return urlConnection; } if (noProxyFor != null) { StringTokenizer tokenizer = new StringTokenizer(noProxyFor, ","); while (tokenizer.hasMoreTokens()) { String noProxyHost = tokenizer.nextToken().trim(); if (noProxyHost.contains("*")) { if (url.getHost().trim().contains(noProxyHost.replaceAll("\\*", ""))) { return url.openConnection(Proxy.NO_PROXY); } } else if (url.getHost().trim().equals(noProxyHost)) { return url.openConnection(Proxy.NO_PROXY); } } } URLConnection urlConnection = url.openConnection(createProxy()); if (isAuthNeeded()) { String credentials = getUserName() + ":" + getPassword(); String encoded = new String(Base64.encodeBase64(credentials.getBytes())); urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded); } connect(urlConnection); return urlConnection; }
con = url.openConnection(); } else { con = url.openConnection(p.createProxy(url.getHost())); if(p.getUserName()!=null) {
public static InputStream getInputStream(URL url) throws IOException { final ProxyConfiguration p = get(); if (p == null) return new RetryableHttpStream(url); InputStream is = new RetryableHttpStream(url, p.createProxy(url.getHost())); if (p.getUserName() != null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { if (getRequestorType() != RequestorType.PROXY) { return null; } return new PasswordAuthentication(p.getUserName(), p.getPassword().toCharArray()); } }); } return is; }
private void configureProxy(HttpClientBuilder builder, URL url) { Jenkins jenkins = Jenkins.getInstance(); if (jenkins == null) { return; } ProxyConfiguration proxyConfig = jenkins.proxy; if (proxyConfig == null) { return; } Proxy proxy = proxyConfig.createProxy(url.getHost()); if (proxy == null || proxy.type() != Proxy.Type.HTTP) { return; } SocketAddress addr = proxy.address(); if (addr == null || !(addr instanceof InetSocketAddress)) { return; } InetSocketAddress proxyAddr = (InetSocketAddress) addr; HttpHost proxyHost = new HttpHost(proxyAddr.getAddress().getHostAddress(), proxyAddr.getPort()); builder.setProxy(proxyHost); String proxyUser = proxyConfig.getUserName(); if (proxyUser != null) { String proxyPass = proxyConfig.getPassword(); BasicCredentialsProvider cred = new BasicCredentialsProvider(); cred.setCredentials(new AuthScope(proxyHost), new org.apache.http.auth.UsernamePasswordCredentials(proxyUser, proxyPass)); builder.setDefaultCredentialsProvider(cred) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } }
final Proxy proxy = proxyConfig.createProxy(new URL(urlString).getHost()); if (proxy == null || proxy.type() != Proxy.Type.HTTP) { logger.println("Not using proxy, not HTTP.");
public static ChartRepo getChartRepoData(String chartsRepoUrl, String credentialsId) { Authentication authData = PluginHelper.getAuthenticationData(credentialsId); ChartRepo chartRepo = new ChartRepo(chartsRepoUrl, authData); final ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; if (proxyConfig != null) { chartRepo.setProxy(proxyConfig.createProxy(chartsRepoUrl) ); if (StringUtils.isNotEmpty(proxyConfig.getUserName() )) { UserAndPasswordAuthentication proxyAuth = new UserAndPasswordAuthentication(proxyConfig.getUserName(), proxyConfig.getPassword() ); chartRepo.setProxyAuthentication(proxyAuth); } } return chartRepo; } }
public static ClientConfiguration createClientConfiguration(final String host) { ClientConfiguration config = new ClientConfiguration(); config.setMaxErrorRetry(16); // Default retry limit (3) is low and often // cause problems. Raise it a bit. // See: https://issues.jenkins-ci.org/browse/JENKINS-26800 config.setSignerOverride("AWS4SignerType"); ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; Proxy proxy = proxyConfig == null ? Proxy.NO_PROXY : proxyConfig.createProxy(host); if (!proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); config.setProxyHost(address.getHostName()); config.setProxyPort(address.getPort()); if (null != proxyConfig.getUserName()) { config.setProxyUsername(proxyConfig.getUserName()); config.setProxyPassword(proxyConfig.getPassword()); } } return config; }
/*** * Connect to an EC2 instance. * @return {@link AmazonEC2} client */ public synchronized static AmazonEC2 connect(AWSCredentialsProvider credentialsProvider, URL endpoint) { awsCredentialsProvider = credentialsProvider; ClientConfiguration config = new ClientConfiguration(); config.setMaxErrorRetry(16); // Default retry limit (3) is low and often cause problems. Raise it a bit. // See: https://issues.jenkins-ci.org/browse/JENKINS-26800 config.setSignerOverride("QueryStringSignerType"); ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; Proxy proxy = proxyConfig == null ? Proxy.NO_PROXY : proxyConfig.createProxy(endpoint.getHost()); if (! proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); config.setProxyHost(address.getHostName()); config.setProxyPort(address.getPort()); if(null != proxyConfig.getUserName()) { config.setProxyUsername(proxyConfig.getUserName()); config.setProxyPassword(proxyConfig.getPassword()); } } AmazonEC2 client = new AmazonEC2Client(credentialsProvider, config); client.setEndpoint(endpoint.toString()); return client; }