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();
}
}
}
netty代理http&https请求的更多相关文章
- nginx反向代理 强制https请求
upstream emove_pools { server ; check interval= rise= fall= timeout=; } #强制使用https跳转 server { listen ...
- nginx反向代理 强制https请求 + 非root用户起80,443端口
1. #强制使用https跳转 return 301 https://$server_name$request_uri;rewrite ^(.*)$ https://${server_name}$1 ...
- 如何用 fiddler 捕获 https 请求
安装完 Fiddler 后,我们每次打开浏览器输入 url,Fiddler 便会捕获到我们的 http 请求(Fiddler 是以代理 web 服务器的形式工作的,它使用代理地址:127.0.0.1, ...
- Charles 查看https请求数据 Mac/ android
Charles_v4.0.1_Mac_破解版下载地址:https://pan.baidu.com/s/1c23VPuS 1.在Mac电脑上安装Charles的根证书 打开Charles->菜单H ...
- 如何使用查尔斯代理抓取https请求
首先 查尔斯代理是一个很不错的抓包工具 有适合各种系统的版本 最近http的请求几乎铺天盖地的已经变为了https了 其中的好处有很多 更加安全(http://www.cnblogs.com/lo ...
- 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头
目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...
- Window下通过charles代理抓取iphone/android手机Https请求乱码问题处理
Window下通过charles代理抓取iphone手机Https请求乱码问题 如果保持默认设置,https的reqeust和response都是乱码,设置完之后https就可以抓包了 手机端操作: ...
- fiddle2 代理HTTPS请求无效?解决方法。
fiddle2: 捕获的https请求结尾跟着443,是因为没有开启HTTPS捕获. 解决方案,开启HTTPS捕获: 然后你就看到能正常捕获HTTPS请求了:
- charles4抓https请求的注意事项
最近升级charles4.0后发现抓不了https请求了,但很奇怪ssl证书一样,记得以前用3.0就可以,今天仔细研究了一下,发现4.0的ssl代理设置中有一段说明(可能3.0也有但没注意): 直接上 ...
随机推荐
- ffmpeg-- audio decoder
测试代码来源于:http://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html /* * Copyright (c) 2001 Fabrice ...
- Java引擎
import java.io.FileNotFoundException; import java.io.FileReader; import java.util.List; import javax ...
- NW.js构建桌面应用
最近要做个桌面的应用,用起来也方便.找了一圈发现NW.js挺容易上手,分享给大家. NW.js 官网https://nwjs.io/ 1.下载适合当前版本的js [这里下载的SDK版本,方便后续调试] ...
- stm32f103中freertos的tasks基本使用案例及备忘
基本实例 freetos的在stm32中使用踩了一些坑,事情做完了,就 做个备忘,希望能给后面的人一些借鉴. 先给出一个实际的例子吧. 启动代码 void task_create(void) { ...
- 【想见你】剧情解析byZlc
花两天时间刷完了想见你,精神有点恍惚. 要是刷题也能有这个尽头就好了... 下面给大家带来个人的剧(hu)情(bian)解(luan)析(zao) 穿越条件:一台老式随身听,一首last dance, ...
- IDE - IDEA - 代码缩进设置
1. 概述 Idea 的代码缩进设置 2. 场景 最近进场会从别的地方找到代码, 然后导入 Idea 时长出现 缩进的问题 会 弹出 以下提示 # 后面还有 3 个 可选按钮 this file in ...
- Hadoop 启动/停止集群和节点的命令
集群启动/停止Hadoop集群:start-all.sh stop-all.sh 1) 启动NameNode, DataNode 2) 启动JournalNode, JournalNode在hd ...
- JEECG弹出框提交表单
一.设备主页面(deviceMain.jsp) <t:dgToolBar title="编辑设备" icon="icon-edit" url=" ...
- Wx-小程序-图片预览、保存
点击图片预览 长按图片保存 点击按钮保存到手机相册 view: <!--wxml--> <text>点击图片预览.长按保存图片.点击按钮保存到系统相册</text> ...
- idea项目更改git地址
第一步:idea打开项目,菜单栏找VCS - Git - Remotes 点进去,弹出对话框,选中,点击编辑 弹出编辑框,更改地址,点击ok 弹出输入账号密码编辑框,输入自己的账号密码,点击确认 完成 ...