【fix】授权调整

This commit is contained in:
wujiawei
2025-07-16 21:30:09 +08:00
parent 841c24a6ed
commit 50b2b1866c
6 changed files with 37 additions and 26 deletions

View File

@@ -81,7 +81,7 @@ public class NettyClientSocketApplicationListener implements ApplicationListener
authenticationToken.setUsedByClientId(clientId); authenticationToken.setUsedByClientId(clientId);
authenticationToken.setAppKey(appKey); authenticationToken.setAppKey(appKey);
authenticationToken.setAppSecret(appSecret); authenticationToken.setAppSecret(appSecret);
AuthenticationTokenContext.setAuthenticationToken(authenticationToken); AuthenticationTokenContext.setAuthenticationToken(clientId, appKey, appSecret);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -1,14 +1,7 @@
package org.framework.lazy.cloud.network.heartbeat.protocol.auth; package org.framework.lazy.cloud.network.heartbeat.protocol.auth;
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
public abstract class AbstractNettyPasswordAuth implements NettyPasswordAuth { public abstract class AbstractNettyPasswordAuth implements NettyPasswordAuth {
private final ProtocolProxyProperties protocolProxyProperties;
protected AbstractNettyPasswordAuth(ProtocolProxyProperties protocolProxyProperties) {
this.protocolProxyProperties = protocolProxyProperties;
}
public abstract boolean doVerify(String username, String password); public abstract boolean doVerify(String username, String password);
@@ -21,9 +14,6 @@ public abstract class AbstractNettyPasswordAuth implements NettyPasswordAuth {
*/ */
@Override @Override
public boolean verify(String username, String password) { public boolean verify(String username, String password) {
if (protocolProxyProperties.getAuthentication()) { return doVerify(username, password);
return doVerify(username, password);
}
return true;
} }
} }

View File

@@ -12,10 +12,6 @@ import java.util.concurrent.ConcurrentHashMap;
@Component @Component
public class DefaultNettyPasswordAuth extends AbstractNettyPasswordAuth { public class DefaultNettyPasswordAuth extends AbstractNettyPasswordAuth {
protected DefaultNettyPasswordAuth(ProtocolProxyProperties protocolProxyProperties) {
super(protocolProxyProperties);
}
@Override @Override
public boolean doVerify(String username, String password) { public boolean doVerify(String username, String password) {
boolean verify = AuthenticationTokenContext.verify(username, password); boolean verify = AuthenticationTokenContext.verify(username, password);

View File

@@ -9,6 +9,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelT
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer; import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.protocol.auth.NettyPasswordAuth; import org.framework.lazy.cloud.network.heartbeat.protocol.auth.NettyPasswordAuth;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.*; import org.framework.lazy.cloud.network.heartbeat.protocol.handler.*;
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
@@ -23,10 +24,12 @@ import java.util.List;
public class NettyTcpProxyFilter extends DebugChannelInitializer<SocketChannel> { public class NettyTcpProxyFilter extends DebugChannelInitializer<SocketChannel> {
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList; private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
private final NettyPasswordAuth nettyPasswordAuth; private final NettyPasswordAuth nettyPasswordAuth;
private final ProtocolProxyProperties protocolProxyProperties;
public NettyTcpProxyFilter(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList, NettyPasswordAuth nettyPasswordAuth) { public NettyTcpProxyFilter(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList, NettyPasswordAuth nettyPasswordAuth, ProtocolProxyProperties protocolProxyProperties) {
this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList; this.handleChannelTypeAdvancedList = handleChannelTypeAdvancedList;
this.nettyPasswordAuth = nettyPasswordAuth; this.nettyPasswordAuth = nettyPasswordAuth;
this.protocolProxyProperties = protocolProxyProperties;
} }
@Override @Override
@@ -42,12 +45,7 @@ public class NettyTcpProxyFilter extends DebugChannelInitializer<SocketChannel>
pipeline.addLast(Socks5ServerEncoder.DEFAULT); pipeline.addLast(Socks5ServerEncoder.DEFAULT);
// 初始化连接 // 初始化连接
pipeline.addLast(new Socks5InitialRequestDecoder()); pipeline.addLast(new Socks5InitialRequestDecoder());
pipeline.addLast(new NettySocks5InitialRequestHandler()); pipeline.addLast(new NettySocks5InitialRequestHandler(protocolProxyProperties,nettyPasswordAuth));
// 认证
pipeline.addLast(new Socks5PasswordAuthRequestDecoder());
pipeline.addLast(new NettySocks5PasswordAuthRequestInboundHandler(nettyPasswordAuth));
// 连接请求 // 连接请求

View File

@@ -4,9 +4,20 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.socksx.v5.*; import io.netty.handler.codec.socksx.v5.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.protocol.auth.NettyPasswordAuth;
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
@Slf4j @Slf4j
public class NettySocks5InitialRequestHandler extends SimpleChannelInboundHandler<Socks5InitialRequest> { public class NettySocks5InitialRequestHandler extends SimpleChannelInboundHandler<Socks5InitialRequest> {
private final ProtocolProxyProperties protocolProxyProperties;
private final NettyPasswordAuth nettyPasswordAuth;
public NettySocks5InitialRequestHandler(ProtocolProxyProperties protocolProxyProperties, NettyPasswordAuth nettyPasswordAuth) {
this.protocolProxyProperties = protocolProxyProperties;
this.nettyPasswordAuth = nettyPasswordAuth;
}
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, Socks5InitialRequest msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, Socks5InitialRequest msg) throws Exception {
boolean failure = msg.decoderResult().isFailure(); boolean failure = msg.decoderResult().isFailure();
@@ -18,9 +29,23 @@ public class NettySocks5InitialRequestHandler extends SimpleChannelInboundHandle
} }
log.info("初始化socket连接"); log.info("初始化socket连接");
// 验证账号密码 if(protocolProxyProperties.getAuthentication()){
Socks5InitialResponse socks5InitialResponse = new DefaultSocks5InitialResponse(Socks5AuthMethod.PASSWORD); // 验证账号密码
ctx.writeAndFlush(socks5InitialResponse); Socks5InitialResponse socks5InitialResponse =
new DefaultSocks5InitialResponse(Socks5AuthMethod.PASSWORD);
ctx.writeAndFlush(socks5InitialResponse);
// 认证
ctx.pipeline().addLast(new Socks5PasswordAuthRequestDecoder());
ctx.pipeline().addLast(new NettySocks5PasswordAuthRequestInboundHandler(nettyPasswordAuth));
}else {
// 不验证账号密码
Socks5InitialResponse socks5InitialResponse =
new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH);
ctx.writeAndFlush(socks5InitialResponse);
}
ctx.pipeline().remove(this); ctx.pipeline().remove(this);
ctx.pipeline().remove(Socks5InitialRequestDecoder.class); ctx.pipeline().remove(Socks5InitialRequestDecoder.class);

View File

@@ -28,6 +28,8 @@ spring:
netty: netty:
protocol: protocol:
proxy: proxy:
authentication: true
enable-proxy-log: true
socket-protocol-proxy: socket-protocol-proxy:
port: 9002 port: 9002
http-protocol-proxy: http-protocol-proxy: