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

最新文章

  1. Oracle迁移:Linux-&gt;Windows
  2. ZooKeeper:Quick Start
  3. HTML5 的web储存: localStorage &amp; sessionStorage
  4. VS2010在64位系统中连接64位Oracle出现的问题和解决方法
  5. selenium and win32api
  6. Python on VS Code
  7. POJ 1035题目描述
  8. sublime3 注册码
  9. MVC 笔记(二)
  10. A Very Easy Triangle Counting Game
  11. OpenStack Pike超详细搭建文档 LinuxBridge版
  12. BSA Network Shell系列-nsh命令
  13. Mac安装opencv指南
  14. 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)
  15. H5——while循环,for循环
  16. (转)git stash使用
  17. Oracle单机Rman笔记[1]---环境准备
  18. 兼容IE-FireFox-Chrome的背景音乐播放
  19. System帐户!我使用你登陆
  20. OAF_OAF Framework状态分析(概念)

热门文章

  1. Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
  2. 搭建Springboot监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)
  3. 使用js处理后台返回的Date类型的数据
  4. PHP导出身份证号科学计数法
  5. vue+axios安装
  6. 其他 - 阻塞 &amp; 同步 的基本认识
  7. 【C语言】用指针描述数组,实现冒泡法排序
  8. git相关项目迁移
  9. C++记录(一)
  10. allegro 16.6 铜皮显示问题