java版本的http代理服务器
https://gitee.com/lixl/proxyee.git
基于socket实现
使用socket转发http请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//连接到目标服务器
proxySocket = new Socket(host, port);
proxyInput = proxySocket.getInputStream();
proxyOutput = proxySocket.getOutputStream();
//根据HTTP method来判断是https还是http请求
if ("CONNECT".equalsIgnoreCase(type)) {//https先建立隧道
clientOutput.write("HTTP/1.1 200 Connection Established\r\n\r\n".getBytes());
clientOutput.flush();
} else {//http直接将请求头转发
proxyOutput.write(headStr.toString().getBytes());
}
//新开线程转发客户端请求至目标服务器
new ProxyHandle(Thread.currentThread().getName(),clientInput, proxyOutput).start();
//转发目标服务器响应至客户端
while (true) {
clientOutput.write(proxyInput.read());
}使用socket转发https请求
创建SocketServer监听端口,根据http请求头方法如果是CONNECT就是HTTPS请求否则都为HTTP请求,
接着根据HOST头建立代理服务器与目标服务器的连接,然后转发数据。
HTTPS请求需要特殊处理,因为CONNECT请求并不需要转发,
要返回一个HTTP 200的响应建立隧道,之后才进行转发。http请求头
1
2
3
4
5
6
7
8GET /wiki/show HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1https请求头
基于nio实现
基于netty实现
客户端配置示例
java客户端通过代理服务器访问网络
1 | package com.lixl.socket; |
socket基于实现http请求
jmeter通过代理服务器访问网络
1 | package org.shirdrn.java.communications.nio; |
1 | package org.shirdrn.java.communications.nio; |