1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-08 14:11:07 +01:00

Make Socks5TestProxy a subclass of Socks5Proxy

to reduce the duplicated code. This also means we are now testing the
real implementation.
This commit is contained in:
Florian Schmaus 2019-06-11 10:18:29 +02:00
parent 6e1193edaf
commit 9bb36fc63c
2 changed files with 42 additions and 236 deletions

View file

@ -72,7 +72,7 @@ import org.jivesoftware.smack.util.CloseableUtil;
*
* @author Henning Staib
*/
public final class Socks5Proxy {
public class Socks5Proxy {
private static final Logger LOGGER = Logger.getLogger(Socks5Proxy.class.getName());
private static final List<Socks5Proxy> RUNNING_PROXIES = new CopyOnWriteArrayList<>();
@ -95,7 +95,7 @@ public final class Socks5Proxy {
private int localSocks5ProxyPort = -7777;
/* reusable implementation of a SOCKS5 proxy server process */
private Socks5ServerProcess serverProcess;
private final Socks5ServerProcess serverProcess;
/* thread running the SOCKS5 server process */
private Thread serverThread;
@ -111,12 +111,20 @@ public final class Socks5Proxy {
private final Set<InetAddress> localAddresses = new LinkedHashSet<>(4);
/**
* If set to <code>true</code>, then all connections are allowed and the digest is not verified. Should be set to
* <code>false</code> for production usage and <code>true</code> for (unit) testing purposes.
*/
private final boolean allowAllConnections;
/**
* Private constructor.
*/
Socks5Proxy() {
this.serverProcess = new Socks5ServerProcess();
allowAllConnections = false;
Enumeration<NetworkInterface> networkInterfaces;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
@ -136,6 +144,22 @@ public final class Socks5Proxy {
replaceLocalAddresses(localAddresses);
}
/**
* Constructor a Socks5Proxy with the given socket. Used for unit test purposes.
*
* @param serverSocket the server socket to use
*/
protected Socks5Proxy(ServerSocket serverSocket) {
this.serverProcess = new Socks5ServerProcess();
this.serverSocket = serverSocket;
allowAllConnections = true;
startServerThread();
}
/**
* Returns true if the local Socks5 proxy should be started. Default is true.
*
@ -229,12 +253,7 @@ public final class Socks5Proxy {
}
if (this.serverSocket != null) {
this.serverThread = new Thread(this.serverProcess);
this.serverThread.setName("Smack Local SOCKS5 Proxy [" + this.serverSocket + ']');
this.serverThread.setDaemon(true);
RUNNING_PROXIES.add(this);
this.serverThread.start();
startServerThread();
}
}
catch (IOException e) {
@ -245,6 +264,15 @@ public final class Socks5Proxy {
return this.serverSocket;
}
private synchronized void startServerThread() {
this.serverThread = new Thread(this.serverProcess);
this.serverThread.setName("Smack Local SOCKS5 Proxy [" + this.serverSocket + ']');
this.serverThread.setDaemon(true);
RUNNING_PROXIES.add(this);
this.serverThread.start();
}
/**
* Stops the local SOCKS5 proxy server. If it is not running this method does nothing.
*/
@ -490,7 +518,7 @@ public final class Socks5Proxy {
String responseDigest = new String(connectionRequest, 5, connectionRequest[4], StandardCharsets.UTF_8);
// return error if digest is not allowed
if (!Socks5Proxy.this.allowedConnections.contains(responseDigest)) {
if (!allowAllConnections && !Socks5Proxy.this.allowedConnections.contains(responseDigest)) {
connectionRequest[1] = (byte) 0x05; // set return status to 5 (connection refused)
out.write(connectionRequest);
out.flush();