Skip to content

Commit

Permalink
#15: handle default authentication handler properly, and also added s…
Browse files Browse the repository at this point in the history
…ome nullability annotations. Also start and stop methods don't need to return the instance, as there is no usecase for chaining these calls.
  • Loading branch information
bbottema committed Oct 2, 2024
1 parent 40322b1 commit e1286c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
38 changes: 13 additions & 25 deletions src/main/java/org/bbottema/javasocksproxyserver/SocksServer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.bbottema.javasocksproxyserver;

import org.bbottema.javasocksproxyserver.auth.Authenticator;
import org.bbottema.javasocksproxyserver.auth.DefaultAuthenticator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,52 +17,38 @@ public class SocksServer {
private static final Logger LOGGER = LoggerFactory.getLogger(SocksServer.class);

private volatile boolean stopped = false;
private int listenPort;
private final int listenPort;

private ServerSocketFactory factory;
private Authenticator authenticator = null;
@NotNull private ServerSocketFactory factory;
@NotNull private Authenticator authenticator;

public SocksServer() {
listenPort = 1080;
factory = ServerSocketFactory.getDefault();
this(1080);
}

public SocksServer(int listenPort) {
this.listenPort = listenPort;
factory = ServerSocketFactory.getDefault();
this.factory = ServerSocketFactory.getDefault();
this.authenticator = new DefaultAuthenticator();
}

public SocksServer(int listenPort, ServerSocketFactory factory) {
this.listenPort = listenPort;
public synchronized SocksServer setFactory(@NotNull ServerSocketFactory factory) {
this.factory = factory;
return this;
}

public synchronized SocksServer setAuthenticator(Authenticator authenticator) {
public synchronized SocksServer setAuthenticator(@NotNull Authenticator authenticator) {
this.authenticator = authenticator;
return this;
}

@Deprecated
public synchronized void start(int port) {
start(port, ServerSocketFactory.getDefault());
}

@Deprecated
public synchronized void start(int port, ServerSocketFactory factory) {
listenPort = port;
this.factory = factory;
start();
}

public synchronized SocksServer start() {
public synchronized void start() {
stopped = false;
new Thread(new ServerProcess(listenPort, factory, authenticator)).start();
return this;
}

public synchronized SocksServer stop() {
public synchronized void stop() {
stopped = true;
return this;
}

private class ServerProcess implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
public class SockServerExtension implements BeforeAllCallback, AfterAllCallback {

private final SocksServer socksServer;
private final int port;
private final ServerSocketFactory serverSocketFactory;

public SockServerExtension(@NotNull Integer port) {
this(port, ServerSocketFactory.getDefault());
}

public SockServerExtension(@NotNull Integer port, @NotNull ServerSocketFactory serverSocketFactory) {
this.socksServer = new SocksServer(port, serverSocketFactory);
this.port = port;
this.serverSocketFactory = serverSocketFactory;
this.socksServer = new SocksServer(port).setFactory(serverSocketFactory);
}

@Override
Expand Down

0 comments on commit e1286c2

Please sign in to comment.