Skip to content

Commit

Permalink
Made connect timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jul 24, 2024
1 parent 6cd2510 commit 193ce90
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
private final OptionSpec<String> optionTargetAddress;
private final OptionSpec<ProtocolVersion> optionTargetVersion;
private final OptionSpec<Boolean> optionProxyOnlineMode;
private final OptionSpec<Integer> optionConnectTimeout;
private final OptionSpec<AuthMethod> optionAuthMethod;
private final OptionSpec<Integer> optionMinecraftAccountIndex;
private final OptionSpec<Boolean> optionBetacraftAuth;
Expand All @@ -79,6 +80,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
private SocketAddress bindAddress = AddressUtil.parse("0.0.0.0:25568", null);
private SocketAddress targetAddress = AddressUtil.parse("127.0.0.1:25565", null);
private ProtocolVersion targetVersion = ProtocolTranslator.AUTO_DETECT_PROTOCOL;
private int connectTimeout = 8000;
private boolean proxyOnlineMode = false;
private AuthMethod authMethod = AuthMethod.NONE;
private Account account = null;
Expand Down Expand Up @@ -108,6 +110,7 @@ public ViaProxyConfig(final File configFile) {
this.optionTargetAddress = this.optionParser.accepts("target-address").withRequiredArg().ofType(String.class).defaultsTo(AddressUtil.toString(this.targetAddress));
this.optionTargetVersion = this.optionParser.accepts("target-version").withRequiredArg().withValuesConvertedBy(new ProtocolVersionConverter()).defaultsTo(this.targetVersion);
this.optionProxyOnlineMode = this.optionParser.accepts("proxy-online-mode").withRequiredArg().ofType(Boolean.class).defaultsTo(this.proxyOnlineMode);
this.optionConnectTimeout = this.optionParser.accepts("connect-timeout").withRequiredArg().ofType(Integer.class).defaultsTo(this.connectTimeout);
this.optionAuthMethod = this.optionParser.accepts("auth-method").withRequiredArg().ofType(AuthMethod.class).defaultsTo(this.authMethod);
this.optionMinecraftAccountIndex = this.optionParser.accepts("minecraft-account-index").withRequiredArg().ofType(Integer.class).defaultsTo(0);
this.optionBetacraftAuth = this.optionParser.accepts("betacraft-auth").withRequiredArg().ofType(Boolean.class).defaultsTo(this.betacraftAuth);
Expand Down Expand Up @@ -136,6 +139,7 @@ public void reload() {
this.targetVersion = ProtocolVersion.getClosest(this.getString("target-version", this.targetVersion.getName()));
this.checkTargetVersion();
this.targetAddress = AddressUtil.parse(this.getString("target-address", AddressUtil.toString(this.targetAddress)), this.targetVersion);
this.connectTimeout = this.getInt("connect-timeout", this.connectTimeout);
this.proxyOnlineMode = this.getBoolean("proxy-online-mode", this.proxyOnlineMode);
this.authMethod = AuthMethod.byName(this.getString("auth-method", this.authMethod.name()));
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
Expand Down Expand Up @@ -174,6 +178,7 @@ public void loadFromArguments(final String[] args) throws IOException {
this.targetVersion = options.valueOf(this.optionTargetVersion);
this.checkTargetVersion();
this.targetAddress = AddressUtil.parse(options.valueOf(this.optionTargetAddress), this.targetVersion);
this.connectTimeout = options.valueOf(this.optionConnectTimeout);
this.proxyOnlineMode = options.valueOf(this.optionProxyOnlineMode);
this.authMethod = options.valueOf(this.optionAuthMethod);
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
Expand Down Expand Up @@ -259,6 +264,15 @@ public void setTargetVersion(final ProtocolVersion targetVersion) {
this.set("target-version", targetVersion.getName());
}

public int getConnectTimeout() {
return this.connectTimeout;
}

public void setConnectTimeout(final int connectTimeout) {
this.connectTimeout = connectTimeout;
this.set("connect-timeout", connectTimeout);
}

public boolean isProxyOnlineMode() {
return this.proxyOnlineMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;
import net.raphimc.vialoader.netty.VLPipeline;
import net.raphimc.vialoader.netty.viabedrock.PingEncapsulationCodec;
import net.raphimc.viaproxy.ViaProxy;
import org.cloudburstmc.netty.channel.raknet.RakChannelFactory;
import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption;

Expand All @@ -53,11 +54,11 @@ public void initialize(final ChannelType channelType, final Bootstrap bootstrap)
bootstrap
.group(channelType.clientEventLoopGroup().get())
.channelFactory(RakChannelFactory.client(channelClass))
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ViaProxy.getConfig().getConnectTimeout())
.option(RakChannelOption.RAK_PROTOCOL_VERSION, ProtocolConstants.BEDROCK_RAKNET_PROTOCOL_VERSION)
.option(RakChannelOption.RAK_COMPATIBILITY_MODE, true)
.option(RakChannelOption.RAK_CLIENT_INTERNAL_ADDRESSES, 20)
.option(RakChannelOption.RAK_CONNECT_TIMEOUT, 4_000L)
.option(RakChannelOption.RAK_CONNECT_TIMEOUT, (long) ViaProxy.getConfig().getConnectTimeout())
.option(RakChannelOption.RAK_SESSION_TIMEOUT, 30_000L)
.option(RakChannelOption.RAK_GUID, ThreadLocalRandom.current().nextLong())
.attr(ProxyConnection.PROXY_CONNECTION_ATTRIBUTE_KEY, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.netty.util.AttributeKey;
import net.raphimc.netminecraft.netty.connection.NetClient;
import net.raphimc.netminecraft.util.ChannelType;
import net.raphimc.viaproxy.ViaProxy;

import java.net.SocketAddress;
import java.util.function.Function;
Expand All @@ -45,7 +46,7 @@ public static LegacyProxyConnection fromChannel(final Channel channel) {

@Override
public void initialize(final ChannelType channelType, final Bootstrap bootstrap) {
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ViaProxy.getConfig().getConnectTimeout());
bootstrap.attr(LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY, this);
super.initialize(channelType, bootstrap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.raphimc.netminecraft.packet.impl.status.S2CStatusResponsePacket;
import net.raphimc.netminecraft.packet.registry.PacketRegistryUtil;
import net.raphimc.netminecraft.util.ChannelType;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.ConsoleFormatter;
import net.raphimc.viaproxy.proxy.packethandler.PacketHandler;
import net.raphimc.viaproxy.proxy.util.CloseAndReturn;
Expand Down Expand Up @@ -100,7 +101,7 @@ public ChannelFuture connect(final SocketAddress address) {

@Override
public void initialize(final ChannelType channelType, final Bootstrap bootstrap) {
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ViaProxy.getConfig().getConnectTimeout());
bootstrap.attr(PROXY_CONNECTION_ATTRIBUTE_KEY, this);
super.initialize(channelType, bootstrap);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/viaproxy/viaproxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ target-address: "127.0.0.1:25565"
# The version ViaProxy should translate to. (See ViaProxy GUI for a list of versions)
target-version: "Auto Detect (1.7+ servers)"
#
# The connect timeout for backend server connections in milliseconds.
connect-timeout: 8000
#
# Proxy Online Mode allows you to see skins on online mode servers and use the signed chat features.
# Enabling Proxy Online Mode requires your client to have a valid minecraft account.
proxy-online-mode: false
Expand Down

0 comments on commit 193ce90

Please sign in to comment.