proxy support
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package io.socket.engineio.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author Eugene.Kudelevsky
|
||||
*/
|
||||
public interface HttpConnectionProvider {
|
||||
HttpURLConnection openConnection(URL url) throws IOException;
|
||||
}
|
||||
@@ -128,6 +128,11 @@ public class Socket extends Emitter {
|
||||
private Future pingIntervalTimer;
|
||||
private SSLContext sslContext;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
public String proxyHost;
|
||||
public int proxyPort = -1;
|
||||
public String proxyLogin;
|
||||
public String proxyPassword;
|
||||
private HttpConnectionProvider httpConnectionProvider;
|
||||
|
||||
private ReadyState readyState;
|
||||
private ScheduledExecutorService heartbeatScheduler;
|
||||
@@ -205,6 +210,11 @@ public class Socket extends Emitter {
|
||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||
this.rememberUpgrade = opts.rememberUpgrade;
|
||||
this.hostnameVerifier = opts.hostnameVerifier != null ? opts.hostnameVerifier : defaultHostnameVerifier;
|
||||
this.proxyHost = opts.proxyHost;
|
||||
this.proxyPort = opts.proxyPort;
|
||||
this.proxyLogin = opts.proxyLogin;
|
||||
this.proxyPassword = opts.proxyPassword;
|
||||
this.httpConnectionProvider = opts.httpConnectionProvider;
|
||||
}
|
||||
|
||||
public static void setDefaultSSLContext(SSLContext sslContext) {
|
||||
@@ -271,6 +281,11 @@ public class Socket extends Emitter {
|
||||
opts.policyPort = this.policyPort;
|
||||
opts.socket = this;
|
||||
opts.hostnameVerifier = this.hostnameVerifier;
|
||||
opts.proxyHost = this.proxyHost;
|
||||
opts.proxyPort = this.proxyPort;
|
||||
opts.proxyLogin = this.proxyLogin;
|
||||
opts.proxyPassword = this.proxyPassword;
|
||||
opts.httpConnectionProvider = this.httpConnectionProvider;
|
||||
|
||||
Transport transport;
|
||||
if (WebSocket.NAME.equals(name)) {
|
||||
|
||||
@@ -43,6 +43,11 @@ public abstract class Transport extends Emitter {
|
||||
protected SSLContext sslContext;
|
||||
protected Socket socket;
|
||||
protected HostnameVerifier hostnameVerifier;
|
||||
protected String proxyHost;
|
||||
protected int proxyPort;
|
||||
protected String proxyLogin;
|
||||
protected String proxyPassword;
|
||||
protected HttpConnectionProvider httpConnectionProvider;
|
||||
|
||||
protected ReadyState readyState;
|
||||
|
||||
@@ -57,6 +62,11 @@ public abstract class Transport extends Emitter {
|
||||
this.sslContext = opts.sslContext;
|
||||
this.socket = opts.socket;
|
||||
this.hostnameVerifier = opts.hostnameVerifier;
|
||||
this.proxyHost = opts.proxyHost;
|
||||
this.proxyPort = opts.proxyPort;
|
||||
this.proxyLogin = opts.proxyLogin;
|
||||
this.proxyPassword = opts.proxyPassword;
|
||||
this.httpConnectionProvider = opts.httpConnectionProvider;
|
||||
}
|
||||
|
||||
protected Transport onError(String msg, Exception desc) {
|
||||
@@ -152,5 +162,10 @@ public abstract class Transport extends Emitter {
|
||||
public SSLContext sslContext;
|
||||
public HostnameVerifier hostnameVerifier;
|
||||
protected Socket socket;
|
||||
public String proxyHost;
|
||||
public int proxyPort = -1;
|
||||
public String proxyLogin;
|
||||
public String proxyPassword;
|
||||
public HttpConnectionProvider httpConnectionProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ package io.socket.engineio.client.transports;
|
||||
|
||||
|
||||
import io.socket.emitter.Emitter;
|
||||
import io.socket.thread.EventThread;
|
||||
import io.socket.engineio.client.HttpConnectionProvider;
|
||||
import io.socket.engineio.client.Transport;
|
||||
import io.socket.thread.EventThread;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
@@ -37,6 +38,7 @@ public class PollingXHR extends Polling {
|
||||
opts.uri = this.uri();
|
||||
opts.sslContext = this.sslContext;
|
||||
opts.hostnameVerifier = this.hostnameVerifier;
|
||||
opts.httpConnectionProvider = this.httpConnectionProvider;
|
||||
|
||||
Request req = new Request(opts);
|
||||
|
||||
@@ -149,6 +151,7 @@ public class PollingXHR extends Polling {
|
||||
private SSLContext sslContext;
|
||||
private HttpURLConnection xhr;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
private HttpConnectionProvider httpConnectionProvider;
|
||||
|
||||
public Request(Options opts) {
|
||||
this.method = opts.method != null ? opts.method : "GET";
|
||||
@@ -156,6 +159,7 @@ public class PollingXHR extends Polling {
|
||||
this.data = opts.data;
|
||||
this.sslContext = opts.sslContext;
|
||||
this.hostnameVerifier = opts.hostnameVerifier;
|
||||
this.httpConnectionProvider = opts.httpConnectionProvider;
|
||||
}
|
||||
|
||||
public void create() {
|
||||
@@ -163,7 +167,12 @@ public class PollingXHR extends Polling {
|
||||
try {
|
||||
logger.fine(String.format("xhr open %s: %s", this.method, this.uri));
|
||||
URL url = new URL(this.uri);
|
||||
xhr = (HttpURLConnection)url.openConnection();
|
||||
if (httpConnectionProvider != null) {
|
||||
xhr = httpConnectionProvider.openConnection(url);
|
||||
}
|
||||
if (xhr == null) {
|
||||
xhr = (HttpURLConnection) url.openConnection();
|
||||
}
|
||||
xhr.setRequestMethod(this.method);
|
||||
} catch (IOException e) {
|
||||
this.onError(e);
|
||||
@@ -322,6 +331,7 @@ public class PollingXHR extends Polling {
|
||||
public byte[] data;
|
||||
public SSLContext sslContext;
|
||||
public HostnameVerifier hostnameVerifier;
|
||||
public HttpConnectionProvider httpConnectionProvider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,15 @@ import io.socket.parseqs.ParseQS;
|
||||
import io.socket.thread.EventThread;
|
||||
import io.socket.utf8.UTF8Exception;
|
||||
import io.socket.yeast.Yeast;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.*;
|
||||
import okhttp3.ws.WebSocketCall;
|
||||
import okhttp3.ws.WebSocketListener;
|
||||
import okio.Buffer;
|
||||
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -61,6 +59,21 @@ public class WebSocket extends Transport {
|
||||
if (this.hostnameVerifier != null) {
|
||||
clientBuilder.hostnameVerifier(this.hostnameVerifier);
|
||||
}
|
||||
if (proxyHost != null && !proxyHost.isEmpty() && proxyPort >= 0) {
|
||||
clientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
|
||||
}
|
||||
if (proxyLogin != null && !proxyLogin.isEmpty()) {
|
||||
final String credentials = Credentials.basic(proxyLogin, proxyPassword);
|
||||
|
||||
clientBuilder.proxyAuthenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
return response.request().newBuilder()
|
||||
.header("Proxy-Authorization", credentials)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
}
|
||||
Request.Builder builder = new Request.Builder().url(uri());
|
||||
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
for (String v : entry.getValue()) {
|
||||
|
||||
Reference in New Issue
Block a user