From 73b474c7dd8b58a00b83839dea5fe656f6b41ff5 Mon Sep 17 00:00:00 2001 From: ukiuni Date: Sat, 15 Apr 2017 16:19:21 +0900 Subject: [PATCH] Uses proxy with password authentication. --- .../DefaultWebSocketContainerProvider.java | 18 ++++++++++++- .../impl/SlackSessionFactory.java | 13 ++++++++- .../impl/SlackWebSocketSessionImpl.java | 27 ++++++++++++++++--- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/DefaultWebSocketContainerProvider.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/DefaultWebSocketContainerProvider.java index 5fd604de..94829e92 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/DefaultWebSocketContainerProvider.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/DefaultWebSocketContainerProvider.java @@ -1,8 +1,14 @@ package com.ullink.slack.simpleslackapi.impl; import com.ullink.slack.simpleslackapi.WebSocketContainerProvider; + import org.glassfish.tyrus.client.ClientManager; import org.glassfish.tyrus.client.ClientProperties; +import org.glassfish.tyrus.core.Base64Utils; + +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; import javax.websocket.WebSocketContainer; @@ -11,10 +17,14 @@ public class DefaultWebSocketContainerProvider implements WebSocketContainerProv private String proxyAddress; private int proxyPort; + private String proxyUser; + private String proxyPassword; - DefaultWebSocketContainerProvider(String proxyAdress, int proxyPort) { + DefaultWebSocketContainerProvider(String proxyAdress, int proxyPort, String proxyUser, String proxyPassword) { this.proxyAddress = proxyAdress; this.proxyPort = proxyPort; + this.proxyUser = proxyUser; + this.proxyPassword = proxyPassword; } @Override @@ -25,6 +35,12 @@ public WebSocketContainer getWebSocketContainer() { clientManager.getProperties().put(ClientProperties.PROXY_URI, "http://" + proxyAddress + ":" + proxyPort); } + if (proxyUser != null) + { + Map headers = new HashMap<>(); + headers.put("Proxy-Authorization", "Basic " + Base64Utils.encodeToString((proxyUser + ":" + proxyPassword).getBytes(Charset.forName("UTF-8")), false)); + clientManager.getProperties().put(ClientProperties.PROXY_HEADERS, headers); + } return clientManager; } } diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackSessionFactory.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackSessionFactory.java index 3ef3d904..7b96cc35 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackSessionFactory.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackSessionFactory.java @@ -22,6 +22,8 @@ public static class SlackSessionFactoryBuilder { private Proxy.Type proxyType; private String proxyAddress; private int proxyPort; + private String proxyUser; + private String proxyPassword; private int heartbeat; private TimeUnit unit; private WebSocketContainerProvider provider; @@ -38,6 +40,15 @@ public SlackSessionFactoryBuilder withProxy(Proxy.Type proxyType, String proxyAd return this; } + public SlackSessionFactoryBuilder withProxy(Proxy.Type proxyType, String proxyAddress, int proxyPort, String proxyUser, String proxyPassword) { + this.proxyType = proxyType; + this.proxyAddress = proxyAddress; + this.proxyPort = proxyPort; + this.proxyUser = proxyUser; + this.proxyPassword = proxyPassword; + return this; + } + public SlackSessionFactoryBuilder withConnectionHeartbeat(int heartbeat, TimeUnit unit) { this.heartbeat = heartbeat; this.unit = unit; @@ -56,7 +67,7 @@ public SlackSessionFactoryBuilder withAutoreconnectOnDisconnection(boolean autor public SlackSession build() { - return new SlackWebSocketSessionImpl(provider,authToken, proxyType, proxyAddress, proxyPort, autoreconnection,heartbeat,unit); + return new SlackWebSocketSessionImpl(provider, authToken, proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword, autoreconnection, heartbeat,unit); } } } diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java index a1d95b47..0429fe7a 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java @@ -21,13 +21,18 @@ import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.DefaultProxyRoutePlanner; import org.apache.http.message.BasicNameValuePair; @@ -103,6 +108,8 @@ class SlackWebSocketSessionImpl extends AbstractSlackSessionImpl implements Slac private String proxyAddress; private int proxyPort = -1; HttpHost proxyHost; + private String proxyUser; + private String proxyPassword; private volatile long lastPingSent; private volatile long lastPingAck; @@ -243,18 +250,20 @@ private > void dispatchImp this.authToken = authToken; this.reconnectOnDisconnection = reconnectOnDisconnection; this.heartbeat = heartbeat != 0 ? unit.toMillis(heartbeat) : 30000; - this.webSocketContainerProvider = webSocketContainerProvider != null ? webSocketContainerProvider : new DefaultWebSocketContainerProvider(null,0); + this.webSocketContainerProvider = webSocketContainerProvider != null ? webSocketContainerProvider : new DefaultWebSocketContainerProvider(null, 0, null, null); addInternalListeners(); } - SlackWebSocketSessionImpl(WebSocketContainerProvider webSocketContainerProvider, String authToken, Proxy.Type proxyType, String proxyAddress, int proxyPort, boolean reconnectOnDisconnection, long heartbeat, TimeUnit unit) { + SlackWebSocketSessionImpl(WebSocketContainerProvider webSocketContainerProvider, String authToken, Proxy.Type proxyType, String proxyAddress, int proxyPort, String proxyUser, String proxyPassword, boolean reconnectOnDisconnection, long heartbeat, TimeUnit unit) { this.authToken = authToken; this.proxyAddress = proxyAddress; this.proxyPort = proxyPort; this.proxyHost = new HttpHost(proxyAddress, proxyPort); this.reconnectOnDisconnection = reconnectOnDisconnection; this.heartbeat = heartbeat != 0 ? unit.toMillis(heartbeat) : DEFAULT_HEARTBEAT_IN_MILLIS; - this.webSocketContainerProvider = webSocketContainerProvider != null ? webSocketContainerProvider : new DefaultWebSocketContainerProvider(proxyAddress,proxyPort); + this.webSocketContainerProvider = webSocketContainerProvider != null ? webSocketContainerProvider : new DefaultWebSocketContainerProvider(proxyAddress, proxyPort, proxyUser, proxyPassword); + this.proxyUser = proxyUser; + this.proxyPassword = proxyPassword; addInternalListeners(); } @@ -798,7 +807,17 @@ private HttpClient getHttpClient() { HttpClient client; if (proxyHost != null) { - client = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxyHost)).build(); + if(null == this.proxyUser) + { + client = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxyHost)).build(); + } + else + { + RequestConfig config = RequestConfig.custom().setProxy(this.proxyHost).build(); + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(this.proxyHost), new UsernamePasswordCredentials(this.proxyUser, this.proxyPassword)); + client = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(config).build(); + } } else {