(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. new和delete的深层次剖析(C++)

    c++ 是公司开发最常用的语言之一, 那new和delete 这两个函数是所有开发者即爱又恨的函数.由new 和delete引发的bug , coredump , 让多少程序员加了多少班. 1. 遇到 ...

  2. [Note]后缀自动机

    后缀自动机 代码 #include <cstdio> #include <algorithm> #include <cstring> const int M = 1 ...

  3. python 序列 倒着取元素

    当要倒着取元素时,用s[-2]只能取一个, 如果取多个时用s[-9:-1],注意,最后一个-1是不取出来的. 此时要用s[-9:] 最后一个空着就可以取出来了.

  4. docker删除镜像时报错解决办法

    报错信息 [root@kvm ~]# docker rmi 4950a54ede5a Error response from daemon: conflict: unable to delete 49 ...

  5. 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH

    参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...

  6. Logarithmic-Trigonometric积分系列(二)

    \[\Large\displaystyle \int_0^{\pi/2}\ln^2(\sin x)\ln(\cos x)\tan x \,{\rm d}x\] \(\Large\mathbf{Solu ...

  7. Navicat Premium破解

    1.正常下载安装 2.把PatchNavicat.exe文件放到Navicat安装目录下 3.以管理员身份启动PatchNavicat.exe 4.运行Navicat即可

  8. C4K Power supply failed?

    故障log: %C4K_IOSMODPORTMAN-4-POWERSUPPLYBAD: Power supply 2 has failed or been turned off 在单机的情况下,我们可 ...

  9. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  10. Jmeter注册100个账户的三个方法

    Jmeter注册账户比如注册成千上万个账户,如何快速实现呢? 三种方法分别举例注册5个账户 1)添加CSV data config_txt 2)添加CSV data config_csv 3)函数助手 ...