(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. 安装 Navicat for MySQL

    安装 Navicat for MySQL 下载地址:https://www.pcsoft.com.cn/soft/20832.html

  2. Redis非关系型缓存数据库集群部署、参数、命令工具

    <关系型数据库与非关系型数据库> 关系数据库:mysql.oracle.DB2.SQL Server非关系数据库:Redis(缓存数据库).MongodDB(处理海量数据).Memcach ...

  3. Visual Studio 配置 fftw 库

    前提条件: 1.vs 2010 +(我的是2019): 2.下载 fftw. 先将vs 的 msvc 编译器的位置添加到path,一般在下面这个目录下: Microsoft Visual Studio ...

  4. bugku come_game

    首先打开游戏发现就是一个单纯的游戏但是当你闯过一关去的时候会发现会多一个文件 看一下玩游戏前 与玩游戏之后有什么变化 然后发现一个文件叫temp 然后用hxd进行分析一下然后尝试着将01改成02然后就 ...

  5. CDH的坑之Deploy Client Configuration Failed

    Deploy Client Configuration Failed 1.问题描述 当使用CDH增添spark服务的时候,出现了以下错误: Faile to deploy client configu ...

  6. 概率 lightoj 1027

    题意 : 在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花费Xi时间后出去 , 如果Xi是负数的话你会花费-Xi时间后回到老地方,并且忘记了刚才的选择, 选择一扇门的概率是等概的 ...

  7. Codeforces 1315B Homecoming (二分)

    After a long party Petya decided to return home, but he turned out to be at the opposite end of the ...

  8. eureka-获取服务列表(各种状态)

    在刚开始做的时候也搜了下搜到的大多是下面的第一种方法,这种方法很简单,但并不是Eureka展示的那个服务列表,他只包括了注册证成功的,或者说eureka中状态为“Up”的实例列表,对于down掉的实例 ...

  9. 什么是this指针?this的几种指向

    在JavaScript中,this指针是在创建时,由系统默认生成的两个隐式参数之一(另一个是arguments). this指针指向与该函数调用进行隐式关联的一个对象,该对象被称为“函数上下文”. t ...

  10. yii2自定义报错页面

    在Yii2版本的advanced高级模板环境中:设置404自定义页面的方法 1.config/main.php文件 'errorHandler' => [ 'errorAction' => ...