-
-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathTunnelProxyInitializer.java
More file actions
52 lines (45 loc) · 1.92 KB
/
TunnelProxyInitializer.java
File metadata and controls
52 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.github.monkeywie.proxyee.handler;
import com.github.monkeywie.proxyee.exception.HttpProxyExceptionHandle;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.proxy.ProxyHandler;
/**
* <div class="zh">http代理隧道,转发原始报文</div>
* <div class="en">http proxy tunnel, forwarding original packets</div>
*/
public class TunnelProxyInitializer extends ChannelInitializer {
private Channel clientChannel;
private ProxyHandler proxyHandler;
public TunnelProxyInitializer(Channel clientChannel,
ProxyHandler proxyHandler) {
this.clientChannel = clientChannel;
this.proxyHandler = proxyHandler;
}
@Override
protected void initChannel(Channel ch) throws Exception {
if (proxyHandler != null) {
ch.pipeline().addLast(proxyHandler);
}
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx0, Object msg0) throws Exception {
clientChannel.writeAndFlush(msg0);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx0) throws Exception {
ctx0.channel().close();
clientChannel.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx0, Throwable cause) throws Exception {
ctx0.channel().close();
clientChannel.close();
HttpProxyExceptionHandle exceptionHandle = ((HttpProxyServerHandler) clientChannel.pipeline()
.get("serverHandle")).getExceptionHandle();
exceptionHandle.afterCatch(clientChannel, ctx0.channel(), cause);
}
});
}
}