• netty代理http&https请求


    (1)关键代码

    package test;
    
    import java.security.cert.CertificateException;
    
    import javax.net.ssl.SSLException;
    
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.ssl.SslContext;
    import io.netty.handler.ssl.SslContextBuilder;
    import io.netty.handler.ssl.util.SelfSignedCertificate;
    
    public class ProxyServer {
        public static void main(String[] args) throws InterruptedException, CertificateException, SSLException {
            boolean SSL = false;//System.getProperty("ssl") != null;
            int PORT = Integer.parseInt(System.getProperty("port", SSL? "5688" : "8080"));
            
             final SslContext sslCtx;
             if (SSL) {
                 SelfSignedCertificate ssc = new SelfSignedCertificate();
                 sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
             } else {
                 sslCtx = null;
             }
             
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128)
                        .childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6000)
                        .childHandler(new ProxyServiceInit(sslCtx));
    
                ChannelFuture f = b.bind(PORT).sync();
                System.out.println("端口号:"+PORT);
                f.channel().closeFuture().sync();
            } finally {
                workGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }
        }
    }
    package test;
    
    import io.netty.channel.Channel;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelPipeline;
    import io.netty.handler.codec.http.HttpServerCodec;
    import io.netty.handler.ssl.SslContext;
    
    public class ProxyServiceInit extends ChannelInitializer<Channel> {
        private final SslContext sslCtx;
        
        public ProxyServiceInit(SslContext sslCtx) { 
            this.sslCtx = sslCtx;
        }
    
        @Override
        protected void initChannel(Channel channel) throws Exception {
            ChannelPipeline p = channel.pipeline();
            System.out.println("ProxyServiceInit");
            if (sslCtx != null) {
                p.addLast(sslCtx.newHandler(channel.alloc()));
            }
            p.addLast("httpcode", new HttpServerCodec());
            p.addLast("httpservice", new HttpService());
            
        }
    }
    package test;
    
    import java.security.cert.CertificateException;
    
    import javax.net.ssl.SSLException;
    
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.ssl.SslContext;
    import io.netty.handler.ssl.SslContextBuilder;
    import io.netty.handler.ssl.util.SelfSignedCertificate;
    
    public class ProxyServer {
        public static void main(String[] args) throws InterruptedException, CertificateException, SSLException {
            boolean SSL = false;//System.getProperty("ssl") != null;
            int PORT = Integer.parseInt(System.getProperty("port", SSL? "5688" : "8080"));
            
             final SslContext sslCtx;
             if (SSL) {
                 SelfSignedCertificate ssc = new SelfSignedCertificate();
                 sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
             } else {
                 sslCtx = null;
             }
             
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128)
                        .childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6000)
                        .childHandler(new ProxyServiceInit(sslCtx));
    
                ChannelFuture f = b.bind(PORT).sync();
                System.out.println("端口号:"+PORT);
                f.channel().closeFuture().sync();
            } finally {
                workGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }
        }
    }
  • 相关阅读:
    【算法系列学习】Dijkstra求最短路 [kuangbin带你飞]专题四 最短路练习 D
    【算法系列学习】Dijkstra算法变形 [kuangbin带你飞]专题四 最短路练习
    【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A
    【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G
    【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F
    【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 E
    【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D
    【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 C
    【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 B
    【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A
  • 原文地址:https://www.cnblogs.com/excellencesy/p/11262655.html
Copyright © 2020-2023  润新知