Add beta support for binding bungee to unix socket addresses

This commit is contained in:
md_5
2020-01-16 10:36:13 +11:00
parent 701391f232
commit b70cb01413
22 changed files with 223 additions and 44 deletions

View File

@@ -23,6 +23,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
@@ -304,7 +305,7 @@ public class BungeeCord extends ProxyServer
{
if ( info.isProxyProtocol() )
{
getLogger().log( Level.WARNING, "Using PROXY protocol for listener {0}, please ensure this listener is adequately firewalled.", info.getHost() );
getLogger().log( Level.WARNING, "Using PROXY protocol for listener {0}, please ensure this listener is adequately firewalled.", info.getSocketAddress() );
if ( connectionThrottle != null )
{
@@ -321,24 +322,26 @@ public class BungeeCord extends ProxyServer
if ( future.isSuccess() )
{
listeners.add( future.channel() );
getLogger().log( Level.INFO, "Listening on {0}", info.getHost() );
getLogger().log( Level.INFO, "Listening on {0}", info.getSocketAddress() );
} else
{
getLogger().log( Level.WARNING, "Could not bind to host " + info.getHost(), future.cause() );
getLogger().log( Level.WARNING, "Could not bind to host " + info.getSocketAddress(), future.cause() );
}
}
};
new ServerBootstrap()
.channel( PipelineUtils.getServerChannel() )
.channel( PipelineUtils.getServerChannel( info.getSocketAddress() ) )
.option( ChannelOption.SO_REUSEADDR, true ) // TODO: Move this elsewhere!
.childAttr( PipelineUtils.LISTENER, info )
.childHandler( PipelineUtils.SERVER_CHILD )
.group( eventLoops )
.localAddress( info.getHost() )
.localAddress( info.getSocketAddress() )
.bind().addListener( listener );
if ( info.isQueryEnabled() )
{
Preconditions.checkArgument( info.getSocketAddress() instanceof InetSocketAddress, "Can only create query listener on UDP address" );
ChannelFutureListener bindListener = new ChannelFutureListener()
{
@Override
@@ -350,7 +353,7 @@ public class BungeeCord extends ProxyServer
getLogger().log( Level.INFO, "Started query on {0}", future.channel().localAddress() );
} else
{
getLogger().log( Level.WARNING, "Could not bind to host " + info.getHost(), future.cause() );
getLogger().log( Level.WARNING, "Could not bind to host " + info.getSocketAddress(), future.cause() );
}
}
};
@@ -643,6 +646,12 @@ public class BungeeCord extends ProxyServer
@Override
public ServerInfo constructServerInfo(String name, InetSocketAddress address, String motd, boolean restricted)
{
return constructServerInfo( name, (SocketAddress) address, motd, restricted );
}
@Override
public ServerInfo constructServerInfo(String name, SocketAddress address, String motd, boolean restricted)
{
return new BungeeServerInfo( name, address, motd, restricted );
}

View File

@@ -6,6 +6,7 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -41,7 +42,7 @@ public class BungeeServerInfo implements ServerInfo
@Getter
private final String name;
@Getter
private final InetSocketAddress address;
private final SocketAddress socketAddress;
private final Collection<ProxiedPlayer> players = new ArrayList<>();
@Getter
private final String motd;
@@ -91,7 +92,7 @@ public class BungeeServerInfo implements ServerInfo
@Override
public int hashCode()
{
return address.hashCode();
return socketAddress.hashCode();
}
@Override
@@ -122,6 +123,12 @@ public class BungeeServerInfo implements ServerInfo
}
}
@Override
public InetSocketAddress getAddress()
{
return (InetSocketAddress) socketAddress;
}
@Override
public void ping(final Callback<ServerPing> callback)
{
@@ -147,11 +154,11 @@ public class BungeeServerInfo implements ServerInfo
}
};
new Bootstrap()
.channel( PipelineUtils.getChannel() )
.channel( PipelineUtils.getChannel( socketAddress ) )
.group( BungeeCord.getInstance().eventLoops )
.handler( PipelineUtils.BASE )
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable
.remoteAddress( getAddress() )
.remoteAddress( socketAddress )
.connect()
.addListener( listener );
}

View File

@@ -6,6 +6,8 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
public class ConnectionThrottle
@@ -38,14 +40,26 @@ public class ConnectionThrottle
this.throttleLimit = throttleLimit;
}
public void unthrottle(InetAddress address)
public void unthrottle(SocketAddress socketAddress)
{
if ( !( socketAddress instanceof InetSocketAddress ) )
{
return;
}
InetAddress address = ( (InetSocketAddress) socketAddress ).getAddress();
int throttleCount = throttle.getUnchecked( address ) - 1;
throttle.put( address, throttleCount );
}
public boolean throttle(InetAddress address)
public boolean throttle(SocketAddress socketAddress)
{
if ( !( socketAddress instanceof InetSocketAddress ) )
{
return false;
}
InetAddress address = ( (InetSocketAddress) socketAddress ).getAddress();
int throttleCount = throttle.getUnchecked( address ) + 1;
throttle.put( address, throttleCount );

View File

@@ -2,6 +2,7 @@ package net.md_5.bungee;
import com.google.common.base.Preconditions;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@@ -65,6 +66,12 @@ public class ServerConnection implements Server
@Override
public InetSocketAddress getAddress()
{
return (InetSocketAddress) getSocketAddress();
}
@Override
public SocketAddress getSocketAddress()
{
return getInfo().getAddress();
}

View File

@@ -3,6 +3,7 @@ package net.md_5.bungee;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.net.InetSocketAddress;
import java.util.Locale;
import java.util.Queue;
import java.util.Set;
@@ -96,7 +97,7 @@ public class ServerConnector extends PacketHandler
Handshake originalHandshake = user.getPendingConnection().getHandshake();
Handshake copiedHandshake = new Handshake( originalHandshake.getProtocolVersion(), originalHandshake.getHost(), originalHandshake.getPort(), 2 );
if ( BungeeCord.getInstance().config.isIpForward() )
if ( BungeeCord.getInstance().config.isIpForward() && user.getSocketAddress() instanceof InetSocketAddress )
{
String newHost = copiedHandshake.getHost() + "\00" + user.getAddress().getHostString() + "\00" + user.getUUID();

View File

@@ -10,6 +10,7 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.util.internal.PlatformDependent;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -359,13 +360,13 @@ public final class UserConnection implements ProxiedPlayer
}
};
Bootstrap b = new Bootstrap()
.channel( PipelineUtils.getChannel() )
.channel( PipelineUtils.getChannel( target.getAddress() ) )
.group( ch.getHandle().eventLoop() )
.handler( initializer )
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, request.getConnectTimeout() )
.remoteAddress( target.getAddress() );
// Windows is bugged, multi homed users will just have to live with random connecting IPs
if ( getPendingConnection().getListener().isSetLocalAddress() && !PlatformDependent.isWindows() )
if ( getPendingConnection().getListener().isSetLocalAddress() && !PlatformDependent.isWindows() && getPendingConnection().getListener().getSocketAddress() instanceof InetSocketAddress )
{
b.localAddress( getPendingConnection().getListener().getHost().getHostString(), 0 );
}
@@ -497,6 +498,12 @@ public final class UserConnection implements ProxiedPlayer
@Override
public InetSocketAddress getAddress()
{
return (InetSocketAddress) getSocketAddress();
}
@Override
public SocketAddress getSocketAddress()
{
return ch.getRemoteAddress();
}

View File

@@ -26,7 +26,7 @@ public class CommandIP extends PlayerCommand
sender.sendMessage( ProxyServer.getInstance().getTranslation( "user_not_online" ) );
} else
{
sender.sendMessage( ProxyServer.getInstance().getTranslation( "command_ip", args[0], user.getAddress() ) );
sender.sendMessage( ProxyServer.getInstance().getTranslation( "command_ip", args[0], user.getSocketAddress() ) );
}
}
}

View File

@@ -6,7 +6,7 @@ import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -215,7 +215,7 @@ public class YamlConfig implements ConfigurationAdapter
String addr = get( "address", "localhost:25565", val );
String motd = ChatColor.translateAlternateColorCodes( '&', get( "motd", "&1Just another BungeeCord - Forced Host", val ) );
boolean restricted = get( "restricted", false, val );
InetSocketAddress address = Util.getAddr( addr );
SocketAddress address = Util.getAddr( addr );
ServerInfo info = ProxyServer.getInstance().constructServerInfo( name, address, motd, restricted );
ret.put( name, info );
}
@@ -246,7 +246,7 @@ public class YamlConfig implements ConfigurationAdapter
boolean forceDefault = get( "force_default_server", false, val );
String host = get( "host", "0.0.0.0:25577", val );
int tabListSize = get( "tab_size", 60, val );
InetSocketAddress address = Util.getAddr( host );
SocketAddress address = Util.getAddr( host );
Map<String, String> forced = new CaseInsensitiveMap<>( get( "forced_hosts", forcedDef, val ) );
String tabListName = get( "tab_list", "GLOBAL_PING", val );
DefaultTabList value = DefaultTabList.valueOf( tabListName.toUpperCase( Locale.ROOT ) );

View File

@@ -15,7 +15,9 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.unix.DomainSocketAddress;
import java.io.DataInput;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -359,8 +361,15 @@ public class DownstreamBridge extends PacketHandler
if ( subChannel.equals( "IP" ) )
{
out.writeUTF( "IP" );
out.writeUTF( con.getAddress().getHostString() );
out.writeInt( con.getAddress().getPort() );
if ( con.getSocketAddress() instanceof InetSocketAddress )
{
out.writeUTF( con.getAddress().getHostString() );
out.writeInt( con.getAddress().getPort() );
} else
{
out.writeUTF( "unix://" + ( (DomainSocketAddress) con.getSocketAddress() ).path() );
out.writeInt( 0 );
}
}
if ( subChannel.equals( "PlayerCount" ) )
{

View File

@@ -5,6 +5,7 @@ import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.List;
@@ -236,7 +237,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
unsafe.sendPacket( new StatusResponse( gson.toJson( pingResult.getResponse() ) ) );
if ( bungee.getConnectionThrottle() != null )
{
bungee.getConnectionThrottle().unthrottle( getAddress().getAddress() );
bungee.getConnectionThrottle().unthrottle( getSocketAddress() );
}
}
};
@@ -418,7 +419,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
}
String encodedHash = URLEncoder.encode( new BigInteger( sha.digest() ).toString( 16 ), "UTF-8" );
String preventProxy = ( ( BungeeCord.getInstance().config.isPreventProxyConnections() ) ? "&ip=" + URLEncoder.encode( getAddress().getAddress().getHostAddress(), "UTF-8" ) : "" );
String preventProxy = ( BungeeCord.getInstance().config.isPreventProxyConnections() && getSocketAddress() instanceof InetSocketAddress ) ? "&ip=" + URLEncoder.encode( getAddress().getAddress().getHostAddress(), "UTF-8" ) : "";
String authURL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + encName + "&serverId=" + encodedHash + preventProxy;
Callback<String> handler = new Callback<String>()
@@ -591,6 +592,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override
public InetSocketAddress getAddress()
{
return (InetSocketAddress) getSocketAddress();
}
@Override
public SocketAddress getSocketAddress()
{
return ch.getRemoteAddress();
}
@@ -625,7 +632,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override
public String toString()
{
return "[" + ( ( getName() != null ) ? getName() : getAddress() ) + "] <-> InitialHandler";
return "[" + ( ( getName() != null ) ? getName() : getSocketAddress() ) + "] <-> InitialHandler";
}
@Override

View File

@@ -92,7 +92,7 @@ public class HttpClient
}
};
new Bootstrap().channel( PipelineUtils.getChannel() ).group( eventLoop ).handler( new HttpInitializer( callback, ssl, uri.getHost(), port ) ).
new Bootstrap().channel( PipelineUtils.getChannel( null ) ).group( eventLoop ).handler( new HttpInitializer( callback, ssl, uri.getHost(), port ) ).
option( ChannelOption.CONNECT_TIMEOUT_MILLIS, TIMEOUT ).remoteAddress( inetHost, port ).connect().addListener( future );
}
}

View File

@@ -5,7 +5,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.Setter;
@@ -23,7 +23,7 @@ public class ChannelWrapper
private final Channel ch;
@Getter
@Setter
private InetSocketAddress remoteAddress;
private SocketAddress remoteAddress;
@Getter
private volatile boolean closed;
@Getter
@@ -32,7 +32,7 @@ public class ChannelWrapper
public ChannelWrapper(ChannelHandlerContext ctx)
{
this.ch = ctx.channel();
this.remoteAddress = (InetSocketAddress) this.ch.remoteAddress();
this.remoteAddress = ( this.ch.remoteAddress() == null ) ? this.ch.parent().localAddress() : this.ch.remoteAddress();
}
public void setProtocol(Protocol protocol)

View File

@@ -1,5 +1,6 @@
package net.md_5.bungee.netty;
import com.google.common.base.Preconditions;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
@@ -10,18 +11,22 @@ import io.netty.channel.ServerChannel;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerDomainSocketChannel;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.AttributeKey;
import io.netty.util.internal.PlatformDependent;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
@@ -51,7 +56,7 @@ public class PipelineUtils
@Override
protected void initChannel(Channel ch) throws Exception
{
if ( BungeeCord.getInstance().getConnectionThrottle() != null && BungeeCord.getInstance().getConnectionThrottle().throttle( ( (InetSocketAddress) ch.remoteAddress() ).getAddress() ) )
if ( BungeeCord.getInstance().getConnectionThrottle() != null && BungeeCord.getInstance().getConnectionThrottle().throttle( ch.remoteAddress() ) )
{
ch.close();
return;
@@ -109,17 +114,31 @@ public class PipelineUtils
return epoll ? new EpollEventLoopGroup( threads, factory ) : new NioEventLoopGroup( threads, factory );
}
public static Class<? extends ServerChannel> getServerChannel()
public static Class<? extends ServerChannel> getServerChannel(SocketAddress address)
{
if ( address instanceof DomainSocketAddress )
{
Preconditions.checkState( epoll, "Epoll required to have UNIX sockets" );
return EpollServerDomainSocketChannel.class;
}
return epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
}
public static Class<? extends Channel> getChannel()
public static Class<? extends Channel> getChannel(SocketAddress address)
{
if ( address instanceof DomainSocketAddress )
{
Preconditions.checkState( epoll, "Epoll required to have UNIX sockets" );
return EpollDomainSocketChannel.class;
}
return epoll ? EpollSocketChannel.class : NioSocketChannel.class;
}
public static Class<? extends Channel> getDatagramChannel()
public static Class<? extends DatagramChannel> getDatagramChannel()
{
return epoll ? EpollDatagramChannel.class : NioDatagramChannel.class;
}

View File

@@ -2,6 +2,7 @@ package net.md_5.bungee;
import com.google.common.base.Ticker;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
@@ -27,14 +28,14 @@ public class ThrottleTest
{
FixedTicker ticker = new FixedTicker();
ConnectionThrottle throttle = new ConnectionThrottle( ticker, 10, 3 );
InetAddress address;
InetSocketAddress address;
try
{
address = InetAddress.getLocalHost();
address = new InetSocketAddress( InetAddress.getLocalHost(), 0 );
} catch ( UnknownHostException ex )
{
address = InetAddress.getByName( null );
address = new InetSocketAddress( InetAddress.getByName( null ), 0 );
}
Assert.assertFalse( "Address should not be throttled", throttle.throttle( address ) ); // 1