bin*_*rek 2 java https apache-camel http-proxy
我正在尝试使用 Apache Camel 实现一个简单的 HTTP 代理服务。我的代码如下所示:
from("jetty:http://localhost:80?matchOnUriPrefix=true")
.recipientList(simple("jetty:${in.header.CamelHttpUrl}?bridgeEndpoint=true&throwExceptionOnFailure=false&disableStreamCache=true"));
Run Code Online (Sandbox Code Playgroud)
它本质上是这样一个动态收件人列表,以支持多个目的地。我还必须添加该disableStreamCache=true位,否则我会得到路径重复的奇怪异常(就像/index.html会变成/index.html/index.html)。
尽管如此,它似乎有效。但仅限于 HTTP 请求。当我尝试访问 HTTPS 站点时,总是收到 404。
根据日志,jetty 组件似乎没有找到远程服务器。我不知道为什么。
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG org.eclipse.jetty.server.Server - REQUEST www.google.cz:443 on AsyncHttpConnection@6964b063,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=17,c=0},r=1
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG o.e.j.server.handler.ContextHandler - scope null||www.google.cz:443 @ o.e.j.s.ServletContextHandler{/,null}
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG o.e.j.server.handler.ContextHandler - context=null||www.google.cz:443 @ o.e.j.s.ServletContextHandler{/,null}
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG o.e.jetty.servlet.ServletHandler - servlet null||www.google.cz:443 -> null
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG o.e.jetty.servlet.ServletHandler - chain=null
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG o.e.jetty.servlet.ServletHandler - Not Found www.google.cz:443
01:36:37.495 [qtp85415531-22 - www.google.cz:443] DEBUG org.eclipse.jetty.server.Server - RESPONSE www.google.cz:443 200 handled=false
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能启用此 HTTPS 支持?甚至可以使用标准的 Camel 组件吗?
编辑:
我设法更新了我的路由定义以不使用收件人列表。我不知道这是否可以提高性能(是吗?),但感觉更好。不使用时,我也能够消除路径重复问题disableStreamCache=true。
from("jetty:http://localhost:80?matchOnUriPrefix=true")
.to("http4:dummy?bridgeEndpoint=true&urlRewrite=#urlRewrite&throwExceptionOnFailure=false");
Run Code Online (Sandbox Code Playgroud)
以及 URL 重写器实现:
UrlRewrite urlRewrite = new HttpServletUrlRewrite() {
@Override
public String rewrite(String url, String relativeUrl, Producer producer, HttpServletRequest request) throws Exception {
return request.getRequestURL().toString();
}
@Override
public String rewrite(String url, String relativeUrl, Producer producer) throws Exception {
return null;
}
};
Run Code Online (Sandbox Code Playgroud)
编辑2:
我可能应该提到我想拦截这些请求并读取/更改内容(实际上只是 HTTP 标头)。实际上,我想实现一个 MITM 代理。
编辑3:
我尝试用 替换目标组件log以查看请求是否通过:
from("jetty:http://localhost:80?matchOnUriPrefix=true")
.to("log:test")
Run Code Online (Sandbox Code Playgroud)
该消息在用作 HTTP 代理时被记录。当我用 替换 URIjetty:https://localhost?matchOnUriPrefix=true并尝试https://localhost直接在浏览器中打开时,它也会被记录。但是,当尝试将其用作 HTTPS 代理时,我无法将其记录下来。似乎 Jetty 组件不支持这种行为。这是正确的吗?
我还尝试使用具有类似结果的 Netty-http 组件(路由跟踪器记录了CONNECT请求,但消息没有传递到日志组件)
关键是为码头定义一个处理程序来处理该CONNECT方法:
from("jetty:http://localhost:80?matchOnUriPrefix=true&handlers=#connectHandler")
Run Code Online (Sandbox Code Playgroud)
在connectHandler这种情况下可以是码头ConnectHandler(用于正常HTTPS代理)或定制的一个(MITM代理):
class CustomConnectHandler extends ConnectHandler {
@Override
protected SocketChannel connect(HttpServletRequest request, String host, int port) throws IOException {
SocketChannel channel = SocketChannel.open();
channel.socket().setTcpNoDelay(true);
channel.socket().connect(new InetSocketAddress("localhost", 443), getConnectTimeout());
return channel;
}
}
Run Code Online (Sandbox Code Playgroud)
这个连接到本地主机上的端口 443 并在那里路由 SSL 数据。所以我们需要从那里定义另一条路线:
from("jetty:https://localhost:443?matchOnUriPrefix=true")
Run Code Online (Sandbox Code Playgroud)
(我们可以保留两条独立的路由,或者稍后通过使用 SEDA 组件加入它们)
要定义自定义 SSL 上下文参数(密钥库、信任库等),我们可以sslContextParametersRef在端点上使用该参数。
接下来,我们需要更改 URL。我们通过用完整的 URL 填充 URI 标头来做到这一点(默认情况下它只是相对路径):
.setHeader(Exchange.HTTP_URI, simple("${header.CamelHttpUrl}"))
Run Code Online (Sandbox Code Playgroud)
我们还需要删除 PATH 标头,因为 HTTP4 组件通过连接 URI 和 PATH 标头来构建最终 URL:
.removeHeader(Exchange.HTTP_PATH)
Run Code Online (Sandbox Code Playgroud)
最后,我们可以将消息转发到 HTTP4 组件。我们还应该设置throwExceptionOnFailure=false将错误转发给客户端并httpClient.redirectsEnabled=false转发重定向(否则这可能会干扰某些站点):
.to("http4:dummy?throwExceptionOnFailure=false&httpClient.redirectsEnabled=false");
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助那些希望使用 Camel 实现 HTTPS 代理并像我一样苦苦挣扎的人。
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |