forked from quickfix-j/quickfixj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocksProxyServer.java
More file actions
74 lines (61 loc) · 2.47 KB
/
Copy pathSocksProxyServer.java
File metadata and controls
74 lines (61 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package quickfix.mina;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.example.socksproxy.SocksServerInitializer;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.apache.mina.util.DaemonThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
/**
* Simple SOCKS proxy server based on Netty examples. Only SOCKS protocols are currently supported.
* The implementation performs the proxy handshake, but it doesn't perform any user authentication.
*/
public class SocksProxyServer {
private static final Logger LOGGER = LoggerFactory.getLogger(SocksProxyServer.class);
private static final ThreadFactory THREAD_FACTORY = new DaemonThreadFactory();
private final ServerBootstrap bootstrap;
private final int port;
private Channel channel;
public SocksProxyServer(int port) {
this.bootstrap = new ServerBootstrap();
this.bootstrap.group(new NioEventLoopGroup(THREAD_FACTORY), new NioEventLoopGroup(THREAD_FACTORY))
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new SocksServerInitializer());
this.port = port;
}
public synchronized void start() {
if (channel != null) {
throw new IllegalStateException("SOCKS proxy server is running already");
}
try {
channel = bootstrap.bind(port)
.sync()
.channel();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
LOGGER.info("SOCKS proxy server started at port: {}", port);
}
public synchronized void stop() {
if (channel == null) {
throw new IllegalStateException("SOCKS proxy server is not running");
}
try {
channel.close().sync();
channel = null;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Failed to close SOCKS proxy server");
}
LOGGER.info("SOCKS proxy server stopped at port {}", port);
}
public int getPort() {
return port;
}
}