Skip to content

Commit

Permalink
- cleanup code
Browse files Browse the repository at this point in the history
- test proxy authentication
  • Loading branch information
MCMicS committed Oct 3, 2023
1 parent 4e96a03 commit 67e967f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.codinjutsu.tools.jenkins.security;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.net.IdeHttpClientHelpers;
import com.intellij.util.net.ssl.CertificateManager;
Expand Down Expand Up @@ -102,8 +103,9 @@ class DefaultSecurityClient implements SecurityClient {
if (useProxySettings) {
this.configCreator = url -> {
final var configForUrl = RequestConfig.copy(defaultRequestConfig);
final var useSocks = JenkinsConnectionSocketFactory.INSTANCE.prepareContext(url, sslContext,
getHttpClientContext(), dnsResolver);
final var useSocks = ApplicationManager.getApplication()
.getService(JenkinsConnectionSocketFactory.class)
.prepareContext(url, sslContext, getHttpClientContext(), dnsResolver);
if (!useSocks) {
IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(configForUrl, url);
IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(credentialsProvider, url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.codinjutsu.tools.jenkins.security;

import com.intellij.openapi.components.Service;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.util.net.HttpConfigurable;
import com.intellij.util.net.IdeaWideProxySelector;
Expand All @@ -26,14 +27,13 @@
import java.util.Collections;
import java.util.List;

public class JenkinsConnectionSocketFactory {
@NotNull
public static final JenkinsConnectionSocketFactory INSTANCE = new JenkinsConnectionSocketFactory();
@Service
public final class JenkinsConnectionSocketFactory {

public @Nullable Registry<ConnectionSocketFactory> getRegistry(@NotNull String url, @NotNull SSLContext sslContext) {
public @NotNull Registry<ConnectionSocketFactory> getRegistry(@NotNull String url, @NotNull SSLContext sslContext) {
final Proxy proxy = getProxy(url);
if(proxy.type() == Proxy.Type.SOCKS) {
return createSocksRegistry(toUri(url), proxy, sslContext);
return createSocksRegistry(proxy, sslContext);
}
return createDefaultRegistry(sslContext);
}
Expand Down Expand Up @@ -71,12 +71,11 @@ public boolean prepareContext(@NotNull String url,
return false;
}

private Registry<ConnectionSocketFactory> createSocksRegistry(@Nullable URI uri,
@NotNull Proxy proxy,
@NotNull SSLContext sslContext) {
private Registry<ConnectionSocketFactory> createSocksRegistry(@NotNull Proxy proxy,
@NotNull SSLContext sslContext) {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new SocksPlainConnectionSocketFactory(proxy))
.register("https", new SocksSSLConnectionSocketFactory(uri != null ? uri.getHost() : null, sslContext, proxy))
.register("https", new SocksSSLConnectionSocketFactory(sslContext, proxy))
.build();
}

Expand Down Expand Up @@ -110,16 +109,13 @@ public SocksPlainConnectionSocketFactory(@NotNull Proxy proxy) {
this.proxy = proxy;
}

@NotNull
public Proxy getProxy() {
return this.proxy;
}

@Override
@NotNull
public Socket createSocket(@Nullable HttpContext context) {
return new Socket(this.proxy);
}

@Override
@NotNull
public Socket connectSocket(int connectTimeout,
@Nullable Socket socket,
Expand All @@ -133,27 +129,21 @@ public Socket connectSocket(int connectTimeout,
}

private static final class SocksSSLConnectionSocketFactory extends SSLConnectionSocketFactory {
@Nullable
private final String host;
@NotNull
private final Proxy proxy;

public SocksSSLConnectionSocketFactory(@Nullable String host, @NotNull SSLContext sslContext, @NotNull Proxy proxy) {
public SocksSSLConnectionSocketFactory(@NotNull SSLContext sslContext, @NotNull Proxy proxy) {
super(sslContext);
this.host = host;
this.proxy = proxy;
}

@NotNull
public Proxy getProxy() {
return this.proxy;
}

@Override
@NotNull
public Socket createSocket(@NotNull HttpContext context) {
return new Socket(this.proxy);
}

@Override
@NotNull
public Socket connectSocket(int connectTimeout,
@Nullable Socket socket,
Expand Down

0 comments on commit 67e967f

Please sign in to comment.