ShadowSocksServer.java
2.1 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
package org.shadowsocks;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpServerCodec;
import org.shadowsocks.config.Config;
import org.shadowsocks.crypto.CryptoFactory;
import org.shadowsocks.handler.server.AddressHandler;
import org.shadowsocks.handler.server.StringHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShadowSocksServer {
private static Logger logger = LoggerFactory.getLogger(ShadowSocksServer.class);
Config config;
public ShadowSocksServer(Config config){
this.config = config;
}
public void start() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(2);
ServerBootstrap bootstrap = new ServerBootstrap();
try {
bootstrap.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(config.getServerPort())
.option(ChannelOption.SO_TIMEOUT, config.getTimeout())
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new StringHandler(CryptoFactory.create(config.getMethod(),
config.getPassword())));
}
});
ChannelFuture channelFuture = bootstrap.bind().sync();
logger.info("started and listen on " + channelFuture.channel().localAddress());
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}