ShadowsocksLocal.java
2.2 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
package org.shadowsocks;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5ServerEncoder;
import org.shadowsocks.config.Config;
import org.shadowsocks.handler.local.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShadowsocksLocal {
private static Logger logger = LoggerFactory.getLogger(ShadowSocksServer.class);
private Config config;
public ShadowsocksLocal(Config config){
this.config = config;
}
public void start() throws InterruptedException{
EventLoopGroup worker = new NioEventLoopGroup(1);
EventLoopGroup boss = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
try{
bootstrap.group(worker,boss)
.localAddress(config.getLocalPort())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(Socks5ServerEncoder.DEFAULT);
ch.pipeline().addLast(new Socks5InitialRequestDecoder());
ch.pipeline().addLast(new Socks5InitialRequestHandler());
ch.pipeline().addLast(new Socks5CommandRequestDecoder());
ch.pipeline().addLast(new Socks5CmdRequesthandler(config));
}
});
ChannelFuture futrue = bootstrap.bind().sync();
logger.info("connected local port:" + config.getLocalPort());
futrue.channel().closeFuture().sync();
}
finally {
worker.shutdownGracefully();
boss.shutdownGracefully();
}
}
}