我正在使用小代理https://github.com/adamfisk/LittleProxy来创建一个测试代理,该代理为给定的网址返回一些预定义的html内容。代码相同:
HttpProxyServer server =
DefaultHttpProxyServer.bootstrap()
.withPort(8080)
.withFiltersSource(new HttpFiltersSourceAdapter() {
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
// My custom implementataion to return the locally stored content based in input url
String content = getContent(request.getUri())
final ByteBuf buffer = Unpooled.copiedBuffer(content, CharsetUtil.UTF_8);
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buffer);
HttpHeaders.setKeepAlive(response, false);
HttpHeaders.setHeader(response, "Referrer-Policy", "no-referrer");
return response;
}
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
return httpObject;
}
};
}
})
.start();
我使用以下代码调用url的代理:
public HttpResponse performHttpGet(String url, String proxyHost, int port) throws Exception {
CloseableHttpClient http = HttpClients.custom().setProxy(new HttpHost(proxyHost, port)).build();
HttpGet get = new HttpGet(url);
return http.execute(get);
}
对于http网址,http get调用正常,但对于https网址,调用失败,因为它发出了对https网址的CONNECT请求。有没有办法在小代理中为 https 网址返回相同的响应?
如何在这个用例中使用MITM,我想为https网址发送一些预定义内容,而不是实际调用网络服务器。