Skip to content

Commit

Permalink
compat upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Feb 22, 2024
1 parent 70fab55 commit c30a7be
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ A list of small tweaks I made:
- Added proxy setting for authenticating player with `sessionserver.mojang.com`
- The proxy setting is in the `auth-proxy` section in `velocity.toml`, of course you know how to fill it
- Supported proxy types: `socks4`, `socks5`, `http`
- Due to [an issue](https://github.com/AsyncHttpClient/async-http-client/issues/1913) in the http library velocity uses, you can only use `http` proxy for now
- If enabled, velocity will firstly try authenticating with the given proxy, if failed it will try again without the proxy
- Implement UUID rewrite for TabList packets, like what bungeecord does

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ void start() {
this.cm.queryBind(configuration.getBind().getHostString(), configuration.getQueryPort());
}

// [fallen's fork] mojang auth proxy: create the proxied http client
cm.createProxiedHttpClient();

Metrics.VelocityMetrics.startMetrics(this, configuration.getMetrics());
}

Expand Down Expand Up @@ -603,8 +600,9 @@ public HttpClient createHttpClient() {
}

// [fallen's fork] mojang auth proxy
public AsyncHttpClient getAsyncProxiedHttpClient() {
return cm.getProxiedHttpClient();
@Nullable
public HttpClient createProxiedHttpClient() {
return cm.createProxiedHttpClient();
}

public Ratelimiter getIpAttemptLimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -215,14 +216,25 @@ public boolean handle(EncryptionResponsePacket packet) {
.uri(URI.create(url))
.build();
final HttpClient httpClient = server.createHttpClient();
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())

// [fallen's fork] mojang auth proxy: make the request progress reuseable
@SuppressWarnings("unchecked") final BiConsumer<HttpClient, Boolean>[] requester = new BiConsumer[1];
requester[0] = (client, retryable) ->
client.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
.whenCompleteAsync((response, throwable) -> {
if (mcConnection.isClosed()) {
// The player disconnected after we authenticated them.
return;
}

if (throwable != null) {
// [fallen's fork] mojang auth proxy: fail-able with proxy mode
if (retryable) {
logger.error("Unable to authenticate player (proxied), try without", throwable);
requester[0].accept(httpClient, false);
return;
}

logger.error("Unable to authenticate player", throwable);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
return;
Expand Down Expand Up @@ -259,6 +271,10 @@ public boolean handle(EncryptionResponsePacket packet) {
// Apparently an offline-mode user logged onto this online-mode proxy.
inbound.disconnect(
Component.translatable("velocity.error.online-mode-only", NamedTextColor.RED));
} else if (retryable) {
// [fallen's fork] mojang auth proxy: fail-able with proxy mode
logger.error("Error authenticating with proxy, http status code {}, try without", response.statusCode());
requester[0].accept(httpClient, false);
} else {
// Something else went wrong
logger.error(
Expand All @@ -278,6 +294,16 @@ public boolean handle(EncryptionResponsePacket packet) {
}
}
});

// [fallen's fork] mojang auth proxy starts
final HttpClient proxiedHttpClient = server.createProxiedHttpClient();
if (proxiedHttpClient != null) {
requester[0].accept(proxiedHttpClient, true);
} else {
requester[0].accept(httpClient, false);
}
// [fallen's fork] mojang auth proxy ends

} catch (GeneralSecurityException e) {
logger.error("Unable to enable encryption", e);
mcConnection.close(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@
import io.netty.channel.EventLoopGroup;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -64,11 +69,6 @@ public final class ConnectionManager {

private final SeparatePoolInetNameResolver resolver;

// [fallen's fork] mojang auth proxy starts
private AsyncHttpClient proxiedHttpClient;
private final Supplier<AsyncHttpClient> proxiedHttpClientSupplier;
// [fallen's fork] mojang auth proxy ends

/**
* Initializes the {@code ConnectionManager}.
*
Expand Down Expand Up @@ -247,15 +247,46 @@ public HttpClient createHttpClient() {
.build();
}

// [fallen's fork] mojang auth proxy starts
public AsyncHttpClient getProxiedHttpClient() {
return proxiedHttpClient;
}
// [fallen's fork] mojang auth proxy
@SuppressWarnings("checkstyle:MissingJavadocMethod")
@Nullable
public HttpClient createProxiedHttpClient() {
if (server.getConfiguration().isAuthProxyEnabled()) {
Proxy.Type proxyType = null;
String type = server.getConfiguration().getAuthProxyType();
switch (type) {
case "socks4":
case "socks5":
proxyType = Proxy.Type.SOCKS;
break;
case "http":
proxyType = Proxy.Type.HTTP;
break;
default:
LOGGER.error("Bad auth proxy type {}", type);
}
if (proxyType != null) {
var hostname = server.getConfiguration().getAuthProxyHostname();
var port = server.getConfiguration().getAuthProxyPort();
var finalProxyType = proxyType;
return HttpClient.newBuilder()
.proxy(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return List.of(new Proxy(finalProxyType, new InetSocketAddress(hostname, port)));
}

public void createProxiedHttpClient() {
proxiedHttpClient = proxiedHttpClientSupplier.get();
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// do nothing as what java.net.ProxySelector.of does
}
})
.executor(this.workerGroup)
.build();
}
}
return null;
}
// [fallen's fork] mojang auth proxy ends

public BackendChannelInitializerHolder getBackendChannelInitializer() {
return this.backendChannelInitializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package com.velocitypowered.proxy.tablist;

import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItem;
import com.velocitypowered.proxy.protocol.packet.RemovePlayerInfo;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfo;
import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItemPacket;
import com.velocitypowered.proxy.protocol.packet.RemovePlayerInfoPacket;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket;
import java.util.stream.Collectors;

/**
Expand All @@ -30,10 +30,10 @@ public class TabListUuidRewriter {
/**
* Rewrite uuid for a LegacyPlayerListItem packet.
*/
public static void rewrite(ConnectedPlayer player, LegacyPlayerListItem packet) {
public static void rewrite(ConnectedPlayer player, LegacyPlayerListItemPacket packet) {
packet.getItems().replaceAll(item -> {
if (player.getOfflineUuid().equals(item.getUuid())) {
var newItem = new LegacyPlayerListItem.Item(player.getUniqueId());
var newItem = new LegacyPlayerListItemPacket.Item(player.getUniqueId());

newItem.setName(item.getName());
newItem.setProperties(item.getProperties());
Expand All @@ -52,10 +52,10 @@ public static void rewrite(ConnectedPlayer player, LegacyPlayerListItem packet)
/**
* Rewrite uuid for a UpsertPlayerInfo packet.
*/
public static void rewrite(ConnectedPlayer player, UpsertPlayerInfo packet) {
public static void rewrite(ConnectedPlayer player, UpsertPlayerInfoPacket packet) {
packet.getEntries().replaceAll(entry -> {
if (player.getOfflineUuid().equals(entry.getProfileId())) {
var newEntry = new UpsertPlayerInfo.Entry(player.getUniqueId());
var newEntry = new UpsertPlayerInfoPacket.Entry(player.getUniqueId());

newEntry.setProfile(player.getGameProfile());
newEntry.setListed(entry.isListed());
Expand All @@ -74,7 +74,7 @@ public static void rewrite(ConnectedPlayer player, UpsertPlayerInfo packet) {
/**
* Rewrite uuid for a RemovePlayerInfo packet.
*/
public static void rewrite(ConnectedPlayer player, RemovePlayerInfo packet) {
public static void rewrite(ConnectedPlayer player, RemovePlayerInfoPacket packet) {
var newProfiles = packet.getProfilesToRemove().stream()
.map(uuid -> {
if (player.getOfflineUuid().equals(uuid)) {
Expand Down

0 comments on commit c30a7be

Please sign in to comment.