-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRelayHandler.java
More file actions
96 lines (83 loc) · 3.65 KB
/
RelayHandler.java
File metadata and controls
96 lines (83 loc) · 3.65 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.arloor.forwardproxy;
import com.arloor.forwardproxy.util.SocksServerUtils;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.Objects;
public final class RelayHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(RelayHandler.class);
private final Channel relayChannel;
public RelayHandler(Channel relayChannel) {
this.relayChannel = relayChannel;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
boolean canWrite = ctx.channel().isWritable();
relayChannel.config().setAutoRead(canWrite);
super.channelWritabilityChanged(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {//删除代理特有的请求头
HttpRequest request = (HttpRequest) msg;
request.headers().remove("Proxy-Authorization");
String proxyConnection = request.headers().get("Proxy-Connection");
if (Objects.nonNull(proxyConnection)) {
request.headers().set("Connection", proxyConnection);
request.headers().remove("Proxy-Connection");
}
//获取Host和port
String hostAndPortStr = request.headers().get("Host");
String[] hostPortArray = hostAndPortStr.split(":");
String host = hostPortArray[0];
String portStr = hostPortArray.length == 2 ? hostPortArray[1] : "80";
int port = Integer.parseInt(portStr);
try {
String url = request.uri();
// log.info("relay request " + url);
int index = url.indexOf(host) + host.length();
url = url.substring(index);
if (url.startsWith(":")) {
url = url.substring(1 + String.valueOf(port).length());
}
request.setUri(url);
} catch (Exception e) {
System.err.println("无法获取url:" + request.uri() + " " + host);
}
}
if (relayChannel.isActive()) {
relayChannel.writeAndFlush(msg).addListener(future -> {
if (!future.isSuccess()) {
// System.out.println("FAILED "+ctx.channel().remoteAddress()+" >>>>> "+relayChannel.remoteAddress()+" "+msg.getClass().getSimpleName());
log.error("relay error!", future.cause());
} else {
// System.out.println("SUCCESS "+ctx.channel().remoteAddress()+" >>>>>>> "+relayChannel.remoteAddress()+" "+msg.getClass().getSimpleName());
}
});
} else {
ReferenceCountUtil.release(msg);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
if (relayChannel.isActive()) {
SocksServerUtils.closeOnFlush(relayChannel);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
String clientHostname = ((InetSocketAddress) ctx.channel().remoteAddress()).getHostString();
log.info("[EXCEPTION]["+clientHostname+"] "+ cause.getMessage());
ctx.close();
}
}