Skip to content

Commit

Permalink
add possibility connection via proxy (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuliozor authored Jun 14, 2021
1 parent 30f8a9b commit 7a8b9c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.github.centrifugal.centrifuge.internal.protocol.Protocol;

import java8.util.concurrent.CompletableFuture;

import okhttp3.Credentials;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -172,7 +172,25 @@ private void _connect() {
if (this.ws != null) {
this.ws.cancel();
}
this.ws = (new OkHttpClient()).newWebSocket(request, new WebSocketListener() {

OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();

if (opts.getProxy() != null) {
okHttpBuilder.proxy(opts.getProxy());

if (this.opts.getProxyLogin() != null && this.opts.getProxyPassword() != null) {
okHttpBuilder.proxyAuthenticator((route, response) -> {
String credentials = Credentials.basic(opts.getProxyLogin(), opts.getProxyPassword());

return response.request()
.newBuilder()
.header("Proxy-Authorization", credentials)
.build();
});
}
}

this.ws = (okHttpBuilder.build()).newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.centrifugal.centrifuge;

import java.net.Proxy;
import java.util.Map;

/**
Expand Down Expand Up @@ -47,4 +48,30 @@ public int getPingInterval() {
public void setPingInterval(int pingInterval) {
this.pingInterval = pingInterval;
}

private Proxy proxy;

public void setProxy(Proxy proxy) {
this.proxy = proxy;
}

public Proxy getProxy() {
return proxy;
}

private String proxyLogin;
private String proxyPassword;

public void setProxyCredentials(String login, String password) {
this.proxyLogin = login;
this.proxyPassword = password;
}

public String getProxyLogin() {
return proxyLogin;
}

public String getProxyPassword() {
return proxyPassword;
}
}

0 comments on commit 7a8b9c0

Please sign in to comment.