1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00

Added support for Java's Proxy class in ProxyInfo

This commit is contained in:
Vshnv 2021-03-29 19:01:42 +05:30
parent d75cd2acb8
commit 525db827a9
2 changed files with 41 additions and 13 deletions

View file

@ -16,6 +16,10 @@
*/ */
package org.jivesoftware.smack.proxy; 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, * Class which stores proxy information such as proxy type, host, port,
* authentication etc. * authentication etc.
@ -44,19 +48,10 @@ public class ProxyInfo {
this.proxyPort = pPort; this.proxyPort = pPort;
this.proxyUsername = pUser; this.proxyUsername = pUser;
this.proxyPassword = pPass; this.proxyPassword = pPass;
switch (proxyType) { this.proxySocketConnection =
case HTTP: ProxySocketConnection
proxySocketConnection = new HTTPProxySocketConnection(this); .forProxyType(proxyType)
break; .apply(this);
case SOCKS4:
proxySocketConnection = new Socks4ProxySocketConnection(this);
break;
case SOCKS5:
proxySocketConnection = new Socks5ProxySocketConnection(this);
break;
default:
throw new IllegalStateException();
}
} }
public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser, 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); 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() { public ProxyType getProxyType() {
return proxyType; return proxyType;
} }
@ -97,4 +104,11 @@ public class ProxyInfo {
public ProxySocketConnection getProxySocketConnection() { public ProxySocketConnection getProxySocketConnection() {
return proxySocketConnection; return proxySocketConnection;
} }
public Proxy toJavaProxy() {
final SocketAddress proxySocketAddress = new InetSocketAddress(proxyAddress, proxyPort);
final Proxy.Type type = getJavaProxyType();
return new Proxy(type, proxySocketAddress);
}
} }

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smack.proxy;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import org.jivesoftware.smack.util.Function;
public interface ProxySocketConnection { public interface ProxySocketConnection {
/** /**
@ -34,4 +36,16 @@ public interface ProxySocketConnection {
void connect(Socket socket, String host, int port, int timeout) void connect(Socket socket, String host, int port, int timeout)
throws IOException; throws IOException;
static Function<ProxySocketConnection, ProxyInfo> 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);
}
}
} }