Skip to content

Commit

Permalink
Merge pull request #176 from ukiuni/feature/AuthProxyWithPassword
Browse files Browse the repository at this point in the history
Uses proxy with password authentication.
  • Loading branch information
gluck authored May 10, 2017
2 parents a3f7fee + 73b474c commit 0c58385
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -25,6 +35,12 @@ public WebSocketContainer getWebSocketContainer()
{
clientManager.getProperties().put(ClientProperties.PROXY_URI, "http://" + proxyAddress + ":" + proxyPort);
}
if (proxyUser != null)
{
Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -243,18 +250,20 @@ private <E extends SlackEvent, L extends SlackEventListener<E>> 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();
}

Expand Down Expand Up @@ -805,7 +814,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
{
Expand Down

0 comments on commit 0c58385

Please sign in to comment.