Skip to content

Commit

Permalink
Send notifications via proxy if globally configured
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hugo authored and Ryudo302 committed Apr 1, 2024
1 parent b4ec00e commit a4bb6a2
Showing 1 changed file with 54 additions and 6 deletions.
@@ -1,19 +1,27 @@
package jenkins.plugins.googlechat;

import hudson.ProxyConfiguration;
import io.cnaik.Messages;
import io.cnaik.model.google.MessageReplyOption;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;

import io.cnaik.Messages;
import io.cnaik.model.google.MessageReplyOption;

public class StandardGoogleChatService implements GoogleChatService {

private static final Logger logger = Logger.getLogger(StandardGoogleChatService.class.getName());
Expand Down Expand Up @@ -99,7 +107,47 @@ private boolean call(String urlDetail, GoogleChatRequest request) {
}

protected HttpClient getHttpClient() {
return HttpClient.newHttpClient();
HttpClient.Builder builder = HttpClient.newBuilder();
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins != null) {
ProxyConfiguration proxy = jenkins.proxy;
if (proxy != null) {
builder.proxy(getProxySelector(proxy));
if (proxy.getUserName() != null) {
builder.authenticator(getProxyAuthenticator(proxy));
}
}
}
return builder.build();
}

private static ProxySelector getProxySelector(ProxyConfiguration proxy) {
return new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (proxy.getNoProxyHostPatterns().stream().anyMatch(pattern -> pattern.matcher(uri.getHost()).matches())) {
return List.of(Proxy.NO_PROXY);
}
return List.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port)));
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {

}
};
}

private static Authenticator getProxyAuthenticator(ProxyConfiguration proxy) {
return new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY && getRequestingHost().equalsIgnoreCase(proxy.name) && getRequestingPort() == proxy.port) {
return new PasswordAuthentication(proxy.getUserName(), proxy.getSecretPassword().getPlainText().toCharArray());
}
return null;

Check warning on line 148 in src/main/java/jenkins/plugins/googlechat/StandardGoogleChatService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 110-148 are not covered by tests
}
};
}

private boolean checkIfValidURL(String url) {
Expand Down

0 comments on commit a4bb6a2

Please sign in to comment.