Remove ipv6 scope from forwarded addresses

Affects forwarding when epoll enabled
This commit is contained in:
md_5 2021-05-25 18:42:06 +10:00
parent c96628b72e
commit 7ec1f487c1
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 47 additions and 1 deletions

View File

@ -51,6 +51,7 @@ import net.md_5.bungee.protocol.packet.ScoreboardObjective;
import net.md_5.bungee.protocol.packet.ScoreboardScore;
import net.md_5.bungee.protocol.packet.SetCompression;
import net.md_5.bungee.protocol.packet.ViewDistance;
import net.md_5.bungee.util.AddressUtil;
import net.md_5.bungee.util.BufUtil;
import net.md_5.bungee.util.QuietException;
@ -102,7 +103,7 @@ public class ServerConnector extends PacketHandler
if ( BungeeCord.getInstance().config.isIpForward() && user.getSocketAddress() instanceof InetSocketAddress )
{
String newHost = copiedHandshake.getHost() + "\00" + user.getAddress().getHostString() + "\00" + user.getUUID();
String newHost = copiedHandshake.getHost() + "\00" + AddressUtil.sanitizeAddress( user.getAddress() ) + "\00" + user.getUUID();
LoginResult profile = user.getPendingConnection().getLoginProfile();
if ( profile != null && profile.getProperties() != null && profile.getProperties().length > 0 )

View File

@ -0,0 +1,26 @@
package net.md_5.bungee.util;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AddressUtil
{
public static String sanitizeAddress(InetSocketAddress addr)
{
String string = addr.getHostString();
// Remove IPv6 scope if present
if ( addr.getAddress() instanceof Inet6Address )
{
int strip = string.indexOf( '%' );
return ( strip == -1 ) ? string : string.substring( 0, strip );
} else
{
return string;
}
}
}

View File

@ -0,0 +1,19 @@
package net.md_5.bungee.util;
import java.net.InetSocketAddress;
import org.junit.Assert;
import org.junit.Test;
public class AddressUtilTest
{
@Test
public void testScope()
{
InetSocketAddress addr = new InetSocketAddress( "0:0:0:0:0:0:0:1%0", 25577 );
Assert.assertEquals( "0:0:0:0:0:0:0:1", AddressUtil.sanitizeAddress( addr ) );
InetSocketAddress addr2 = new InetSocketAddress( "0:0:0:0:0:0:0:1", 25577 );
Assert.assertEquals( "0:0:0:0:0:0:0:1", AddressUtil.sanitizeAddress( addr2 ) );
}
}