博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
netty代理http&https请求
阅读量:4363 次
发布时间:2019-06-07

本文共 4487 字,大约阅读时间需要 14 分钟。

(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
{ 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();        }    }}

 

转载于:https://www.cnblogs.com/excellencesy/p/11262655.html

你可能感兴趣的文章
前端UI框架选择区别对比推荐
查看>>
栈 队列 和 双向队列
查看>>
从垃圾回收看闭包
查看>>
Intel Core Microarchitecture Pipeline
查看>>
如何去除交叉表的子行(列)的小计?
查看>>
Web字体(链接)嵌入
查看>>
switch… case 语句的用法
查看>>
day07补充-数据类型总结及拷贝
查看>>
语言、数据和运算符
查看>>
正则表达式30分钟入门教程
查看>>
sqlserver try catch·
查看>>
怎么在三维世界里叙述五维故事
查看>>
1028: 可乐(2018年中南大学研究生复试机试题 )
查看>>
珍藏的最全的windows操作系统快捷键
查看>>
【DBAplus】SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
查看>>
二叉树结点删除操作
查看>>
图论-单源最短路-SPFA算法
查看>>
转换文件的字符集
查看>>
prometheus + grafana安装部署(centos6.8)
查看>>
Redis和Memcached的区别【转】
查看>>