Skip to content

Instantly share code, notes, and snippets.

@sdww2348115
Created April 3, 2019 16:39
Show Gist options
  • Save sdww2348115/c6f9573d0d122db02148c7924e8fe941 to your computer and use it in GitHub Desktop.
Save sdww2348115/c6f9573d0d122db02148c7924e8fe941 to your computer and use it in GitHub Desktop.
package com.sdww8591.proxy;
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 lombok.Getter;
import lombok.SneakyThrows;
public class NettyProxy {
private static final Integer LISTEN = 8777;
private static final String HOST = "localhost";
private static final Integer PORT = 80;
@Getter
private static EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup(10);
@SneakyThrows
public static void startProxy() {
ServerBootstrap serverBootstrap = new ServerBootstrap();
EventLoopGroup acceptEventLoopGroup = new NioEventLoopGroup(1);
serverBootstrap.group(acceptEventLoopGroup, workerEventLoopGroup)
.channel(NioServerSocketChannel.class)
.localAddress(LISTEN)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProxyChannelHandler(HOST, PORT));
}
});
ChannelFuture future = serverBootstrap.bind().sync();
future.channel().closeFuture().sync();
}
public static void main(String[] args) {
startProxy();
}
}
package com.sdww8591.proxy;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.concurrent.TimeUnit;
public class ProxyChannelHandler extends ChannelInboundHandlerAdapter {
private String host;
private Integer port;
private Channel targetChannel;
public ProxyChannelHandler(String host, Integer port) {
this.host = host;
this.port = port;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ChannelFuture future = buildTargetChannel(ctx.channel());
targetChannel = future.channel();
future.get(30, TimeUnit.SECONDS);
}
private ChannelFuture buildTargetChannel(Channel srcChannel) {
Bootstrap bootstrap = new Bootstrap();
return bootstrap.group(NettyProxy.getWorkerEventLoopGroup())
.channel(NioSocketChannel.class)
.remoteAddress(host, port)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
srcChannel.writeAndFlush(msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
srcChannel.pipeline().fireChannelInactive();
}
});
}
}).connect();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
targetChannel.writeAndFlush(msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
targetChannel.pipeline().fireChannelInactive();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment