From 525db827a9ba01d4811bc5a258283ab4072a35e6 Mon Sep 17 00:00:00 2001 From: Vshnv Date: Mon, 29 Mar 2021 19:01:42 +0530 Subject: [PATCH 1/2] Added support for Java's Proxy class in ProxyInfo --- .../jivesoftware/smack/proxy/ProxyInfo.java | 40 +++++++++++++------ .../smack/proxy/ProxySocketConnection.java | 14 +++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxyInfo.java b/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxyInfo.java index 005101190..402a106a4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxyInfo.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxyInfo.java @@ -16,6 +16,10 @@ */ package org.jivesoftware.smack.proxy; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.SocketAddress; + /** * Class which stores proxy information such as proxy type, host, port, * authentication etc. @@ -44,19 +48,10 @@ public class ProxyInfo { this.proxyPort = pPort; this.proxyUsername = pUser; this.proxyPassword = pPass; - switch (proxyType) { - case HTTP: - proxySocketConnection = new HTTPProxySocketConnection(this); - break; - case SOCKS4: - proxySocketConnection = new Socks4ProxySocketConnection(this); - break; - case SOCKS5: - proxySocketConnection = new Socks5ProxySocketConnection(this); - break; - default: - throw new IllegalStateException(); - } + this.proxySocketConnection = + ProxySocketConnection + .forProxyType(proxyType) + .apply(this); } public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser, @@ -74,6 +69,18 @@ public class ProxyInfo { return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass); } + public Proxy.Type getJavaProxyType() { + switch (proxyType) { + case HTTP: + return Proxy.Type.HTTP; + case SOCKS4: + case SOCKS5: + return Proxy.Type.SOCKS; + default: + throw new AssertionError("Invalid proxy type: " + proxyType); + } + } + public ProxyType getProxyType() { return proxyType; } @@ -97,4 +104,11 @@ public class ProxyInfo { public ProxySocketConnection getProxySocketConnection() { return proxySocketConnection; } + + public Proxy toJavaProxy() { + final SocketAddress proxySocketAddress = new InetSocketAddress(proxyAddress, proxyPort); + final Proxy.Type type = getJavaProxyType(); + return new Proxy(type, proxySocketAddress); + } + } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxySocketConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxySocketConnection.java index 7f31f94dc..beaf0ebad 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxySocketConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/proxy/ProxySocketConnection.java @@ -19,6 +19,8 @@ package org.jivesoftware.smack.proxy; import java.io.IOException; import java.net.Socket; +import org.jivesoftware.smack.util.Function; + public interface ProxySocketConnection { /** @@ -34,4 +36,16 @@ public interface ProxySocketConnection { void connect(Socket socket, String host, int port, int timeout) throws IOException; + static Function forProxyType(ProxyInfo.ProxyType proxyType) { + switch (proxyType) { + case HTTP: + return HTTPProxySocketConnection::new; + case SOCKS4: + return Socks4ProxySocketConnection::new; + case SOCKS5: + return Socks5ProxySocketConnection::new; + default: + throw new AssertionError("Unknown proxy type: " + proxyType); + } + } } From 4736d080a1c5d6d3a26d436ecf3d271da248bd35 Mon Sep 17 00:00:00 2001 From: Vshnv Date: Mon, 29 Mar 2021 19:01:58 +0530 Subject: [PATCH 2/2] Added proxy support for XEP-0363 --- .../httpfileupload/HttpFileUploadManager.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java index 23379a4cc..4e7f4a19e 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java @@ -27,7 +27,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Objects; import java.util.WeakHashMap; import java.util.logging.Level; @@ -37,6 +36,7 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; +import org.jivesoftware.smack.AbstractXMPPConnection; import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.Manager; @@ -46,6 +46,7 @@ import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.proxy.ProxyInfo; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.httpfileupload.UploadService.Version; @@ -429,15 +430,15 @@ public final class HttpFileUploadManager extends Manager { private void upload(InputStream iStream, long fileSize, Slot slot, UploadProgressListener listener) throws IOException { final URL putUrl = slot.getPutUrl(); - - final HttpURLConnection urlConnection = (HttpURLConnection) putUrl.openConnection(); + final XMPPConnection connection = connection(); + final HttpURLConnection urlConnection = createURLConnection(connection, putUrl); urlConnection.setRequestMethod("PUT"); urlConnection.setUseCaches(false); urlConnection.setDoOutput(true); urlConnection.setFixedLengthStreamingMode(fileSize); urlConnection.setRequestProperty("Content-Type", "application/octet-stream"); - for (Entry header : slot.getHeaders().entrySet()) { + for (Map.Entry header : slot.getHeaders().entrySet()) { urlConnection.setRequestProperty(header.getKey(), header.getValue()); } @@ -503,6 +504,30 @@ public final class HttpFileUploadManager extends Manager { } } + private static HttpURLConnection createURLConnection(XMPPConnection connection, URL putUrl) throws IOException { + Objects.requireNonNull(connection); + Objects.requireNonNull(putUrl); + ProxyInfo proxyInfo = fetchProxyInfo(connection); + if (proxyInfo != null) { + return createProxiedURLConnection(proxyInfo, putUrl); + } + return (HttpURLConnection) putUrl.openConnection(); + } + + private static HttpURLConnection createProxiedURLConnection(ProxyInfo proxyInfo, URL putUrl) throws IOException { + Objects.requireNonNull(proxyInfo); + Objects.requireNonNull(putUrl); + return (HttpURLConnection) putUrl.openConnection(proxyInfo.toJavaProxy()); + } + + private static ProxyInfo fetchProxyInfo(XMPPConnection connection) { + if (!(connection instanceof AbstractXMPPConnection)) { + return null; + } + AbstractXMPPConnection xmppConnection = (AbstractXMPPConnection) connection; + return xmppConnection.getConfiguration().getProxyInfo(); + } + public static UploadService.Version namespaceToVersion(String namespace) { UploadService.Version version; switch (namespace) {