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;
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);
}
}

View File

@ -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<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);
}
}
}