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

netty代理http&https请求的更多相关文章

  1. nginx反向代理 强制https请求

    upstream emove_pools { server ; check interval= rise= fall= timeout=; } #强制使用https跳转 server { listen ...

  2. nginx反向代理 强制https请求 + 非root用户起80,443端口

    1. #强制使用https跳转 return 301 https://$server_name$request_uri;rewrite ^(.*)$ https://${server_name}$1 ...

  3. 如何用 fiddler 捕获 https 请求

    安装完 Fiddler 后,我们每次打开浏览器输入 url,Fiddler 便会捕获到我们的 http 请求(Fiddler 是以代理 web 服务器的形式工作的,它使用代理地址:127.0.0.1, ...

  4. Charles 查看https请求数据 Mac/ android

    Charles_v4.0.1_Mac_破解版下载地址:https://pan.baidu.com/s/1c23VPuS 1.在Mac电脑上安装Charles的根证书 打开Charles->菜单H ...

  5. 如何使用查尔斯代理抓取https请求

    首先 查尔斯代理是一个很不错的抓包工具 有适合各种系统的版本 最近http的请求几乎铺天盖地的已经变为了https了  其中的好处有很多  更加安全(http://www.cnblogs.com/lo ...

  6. 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

    目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...

  7. Window下通过charles代理抓取iphone/android手机Https请求乱码问题处理

    Window下通过charles代理抓取iphone手机Https请求乱码问题 如果保持默认设置,https的reqeust和response都是乱码,设置完之后https就可以抓包了 手机端操作: ...

  8. fiddle2 代理HTTPS请求无效?解决方法。

    fiddle2: 捕获的https请求结尾跟着443,是因为没有开启HTTPS捕获. 解决方案,开启HTTPS捕获:         然后你就看到能正常捕获HTTPS请求了:    

  9. charles4抓https请求的注意事项

    最近升级charles4.0后发现抓不了https请求了,但很奇怪ssl证书一样,记得以前用3.0就可以,今天仔细研究了一下,发现4.0的ssl代理设置中有一段说明(可能3.0也有但没注意): 直接上 ...

随机推荐

  1. Laravel 部署到阿里云 / 腾讯云

    首先你需要一台阿里云/腾讯云服务器 安装系统选择 ubuntu 16.04 然后通过 ssh 登录远程服务器按下列步骤进行配置: 更新列表 apt-get update 安装语言包 sudo apt- ...

  2. 201771010135-杨蓉庆 实验一 软件工程准备—用Markdown写构建之法

    项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh   博客园  https://www.cnblogs.com/nwnu-daizh/p/12369881.h ...

  3. React 实现input输入框的防抖和节流

    1.为什么使用防抖和节流对于频繁触发的事件 比如keydown keyup事件 当频繁点击时候 会多次触发事件 页面出现卡顿 影响性能 2.函数防抖(debounce):间隔时间内只执行一次   函数 ...

  4. MySQL 学习(三)事务学习

    事务隔离级别     SQL标准的事务隔离级别包括:读未提交(read uncommitted).读提交(read committed).可重复读(repeatable read)和串行化(seria ...

  5. tableSizeFor()函数在java8和Java13的差别

    java8 static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >&g ...

  6. 谷歌play上APK的下载

    https://apkcombo.com/tw-hk/ 在google搜索 APK DOWNLAOD就有专门下载谷歌PLAY的APK的

  7. PHP 文件上传之如何识别文件伪装?——PHP的fileinfo扩展可!

    问题:文件上传时候需要验证上传的文件是否合法,文件伪装如何识别? 一个简单测试:把txt文件后缀直接改成jpg;上传 <!DOCTYPE html> <html> <ti ...

  8. Cesium 基于MapBox底图加载3DTiles 模型

    3DTiles 模型采用   CATIA V5 R22 --->3dxml --->GLB--->B3DM var extent = Cesium.Rectangle.fromDeg ...

  9. reStructuredText语法

    reStructuredText 除了makedown语法这还存在另一种语法reStructuredText 相对Markdown来说,在写书方面更有优势: 使用sphnix能够自动生成目录和索引文件 ...

  10. mybatis中条件查询大于等于和小于等于写法

    原符号 < <= > >= & ' "替换符号 < <= > >= & &apos; " createDat ...