Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
RockyLOMO committed Sep 26, 2023
1 parent ff08b0a commit 4fe5afd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ protected void channelRead0(ChannelHandlerContext inbound, DefaultSocks5CommandR
UnresolvedEndpoint dstEp = new UnresolvedEndpoint(msg.dstAddr(), msg.dstPort());
String dstEpHost = dstEp.getHost();
if (dstEpHost.endsWith(SocksSupport.FAKE_HOST_SUFFIX)) {
UnresolvedEndpoint realEp = SocksSupport.fakeDict()
.get(new BigInteger(dstEpHost.substring(0, dstEpHost.length() - SocksSupport.FAKE_HOST_SUFFIX.length())));
UnresolvedEndpoint realEp = SocksSupport.fakeDict().get(new BigInteger(dstEpHost.substring(0, dstEpHost.length() - SocksSupport.FAKE_HOST_SUFFIX.length())));
if (realEp == null) {
log.error("socks5[{}] recover dstEp {} fail", server.getConfig().getListenPort(), dstEp);
} else {
Expand All @@ -57,17 +56,16 @@ protected void channelRead0(ChannelHandlerContext inbound, DefaultSocks5CommandR
} else if (msg.type() == Socks5CommandType.UDP_ASSOCIATE) {
log.info("socks5[{}] UdpAssociate {}", server.getConfig().getListenPort(), msg);
pipeline.remove(ProxyChannelIdleHandler.class.getSimpleName());
Tasks.setTimeout(() -> {
log.info("UdpAssociate client close");
pipeline.addLast(new Socks5UdpAssociateHandler(Tasks.setTimeout(() -> {
log.info("UdpAssociate close by maxLife, tcp:{}", inbound.channel());
Sockets.closeOnFlushed(inbound.channel());
}, server.config.getUdpAssociateMaxLifeSeconds() * 1000L);
pipeline.addLast(Socks5UdpAssociateHandler.DEFAULT);
}, server.config.getUdpAssociateMaxLifeSeconds() * 1000L)));

InetSocketAddress bindEp = (InetSocketAddress) inbound.channel().localAddress();
Socks5AddressType bindAddrType = bindEp.getAddress() instanceof Inet6Address ? Socks5AddressType.IPv6 : Socks5AddressType.IPv4;
// Socks5AddressType bindAddrType = msg.dstAddrType();
// Socks5AddressType bindAddrType = bindEp.getAddress() instanceof Inet6Address ? Socks5AddressType.IPv6 : Socks5AddressType.IPv4;
Socks5AddressType bindAddrType = msg.dstAddrType();
// msg.dstAddr(), msg.dstPort() = 0.0.0.0:0
inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, bindAddrType, bindEp.getHostString(), bindEp.getPort()));
// inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, msg.dstAddrType(), msg.dstAddr(), msg.dstPort()));
} else {
log.warn("Command {} not support", msg.type());
inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.COMMAND_UNSUPPORTED, msg.dstAddrType())).addListener(ChannelFutureListener.CLOSE);
Expand All @@ -93,8 +91,7 @@ private void connect(Channel inbound, Socks5AddressType dstAddrType, SocksContex
return;
}
}
TraceHandler.INSTANCE.log("socks5[{}] connect {}[{}] fail", server.getConfig().getListenPort(),
e.getUpstream().getDestination(), e.firstDestination, f.cause());
TraceHandler.INSTANCE.log("socks5[{}] connect {}[{}] fail", server.getConfig().getListenPort(), e.getUpstream().getDestination(), e.firstDestination, f.cause());
inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, dstAddrType)).addListener(ChannelFutureListener.CLOSE);
return;
}
Expand Down Expand Up @@ -122,8 +119,7 @@ private void connect(Channel inbound, Socks5AddressType dstAddrType, SocksContex
});
}

private void relay(Channel inbound, Channel outbound, Socks5AddressType dstAddrType,
SocksContext e, StringBuilder extMsg) {
private void relay(Channel inbound, Channel outbound, Socks5AddressType dstAddrType, SocksContext e, StringBuilder extMsg) {
//initChannel may change dstEp
UnresolvedEndpoint dstEp = e.getUpstream().getDestination();
outbound.pipeline().addLast(BackendRelayHandler.DEFAULT);
Expand All @@ -145,8 +141,7 @@ private void relay(Channel inbound, Channel outbound, Socks5AddressType dstAddrT
// extMsg.append("[FRONTEND_AES] %s", Strings.join(inbound.channel().pipeline().names()));
extMsg.append("[FRONTEND_AES]");
}
log.info("socks5[{}] {} => {} connected, dstEp={}[{}] {}", config.getListenPort(),
inbound.localAddress(), outbound.remoteAddress(), dstEp, e.firstDestination, extMsg.toString());
log.info("socks5[{}] {} => {} connected, dstEp={}[{}] {}", config.getListenPort(), inbound.localAddress(), outbound.remoteAddress(), dstEp, e.firstDestination, extMsg.toString());
});
}
}
22 changes: 12 additions & 10 deletions rxlib/src/main/java/org/rx/net/socks/Socks5UdpAssociateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.rx.core.TimeoutFuture;

@Slf4j
@ChannelHandler.Sharable
//@ChannelHandler.Sharable
@RequiredArgsConstructor
public class Socks5UdpAssociateHandler extends ChannelInboundHandlerAdapter {
public static final Socks5UdpAssociateHandler DEFAULT = new Socks5UdpAssociateHandler();
// public static final Socks5UdpAssociateHandler DEFAULT = new Socks5UdpAssociateHandler();
final TimeoutFuture<?> maxLifeFn;

// public Socks5UdpAssociateHandler() {
//
//不会触发
// @Override
// public void channelActive(ChannelHandlerContext ctx) throws Exception {
// log.info("UdpAssociate {} active", ctx.channel().remoteAddress());
// super.channelActive(ctx);
// }

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.info("UdpAssociate {} active", ctx.channel().remoteAddress());
super.channelActive(ctx);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("UdpAssociate {} inactive", ctx.channel().remoteAddress());
maxLifeFn.cancel();
super.channelInactive(ctx);
}

Expand Down

0 comments on commit 4fe5afd

Please sign in to comment.