add server banned status and blocked endpoint
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Failing after 21s

This commit is contained in:
Lee
2024-04-10 11:39:17 +01:00
parent 23bcb1d76e
commit 5ad2f438d1
12 changed files with 420 additions and 138 deletions

View File

@ -1,40 +0,0 @@
package cc.fascinated.service;
import cc.fascinated.common.WebRequest;
import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.mojang.MojangUsernameToUuid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service @Log4j2
public class MojangAPIService {
@Value("${mojang.session-server}")
private String mojangSessionServerUrl;
@Value("${mojang.api}")
private String mojangApiUrl;
/**
* Gets the Session Server profile of the
* player with the given UUID.
*
* @param id the uuid or name of the player
* @return the profile
*/
public MojangProfile getProfile(String id) {
return WebRequest.getAsEntity(mojangSessionServerUrl + "/session/minecraft/profile/" + id, MojangProfile.class);
}
/**
* Gets the UUID of the player using
* the name of the player.
*
* @param id the name of the player
* @return the profile
*/
public MojangUsernameToUuid getUuidFromUsername(String id) {
return WebRequest.getAsEntity(mojangApiUrl + "/users/profiles/minecraft/" + id, MojangUsernameToUuid.class);
}
}

View File

@ -0,0 +1,166 @@
package cc.fascinated.service;
import cc.fascinated.common.ExpiringSet;
import cc.fascinated.common.WebRequest;
import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.mojang.MojangUsernameToUuid;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.hash.Hashing;
import io.micrometer.common.lang.NonNull;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import net.jodah.expiringmap.ExpirationPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service @Log4j2
public class MojangService {
private static final String SESSION_SERVER_ENDPOINT = "https://sessionserver.mojang.com";
private static final String API_ENDPOINT = "https://api.mojang.com";
private static final String FETCH_BLOCKED_SERVERS = SESSION_SERVER_ENDPOINT + "/blockedservers";
private static final Splitter DOT_SPLITTER = Splitter.on('.');
private static final Joiner DOT_JOINER = Joiner.on('.');
/**
* A list of banned server hashes provided by Mojang.
* <p>
* This is periodically fetched from Mojang, see
* {@link #fetchBlockedServers()} for more info.
* </p>
*
* @see <a href="https://wiki.vg/Mojang_API#Blocked_Servers">Mojang API</a>
*/
private List<String> bannedServerHashes;
/**
* A cache of blocked server hostnames.
*
* @see #isServerHostnameBlocked(String) for more
*/
private final ExpiringSet<String> blockedServersCache = new ExpiringSet<>(ExpirationPolicy.CREATED, 10L, TimeUnit.MINUTES);
public MojangService() {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
fetchBlockedServers();
}
}, 0L, 60L * 15L * 1000L);
}
/**
* Fetch a list of blocked servers from Mojang.
*/
@SneakyThrows
private void fetchBlockedServers() {
try (
InputStream inputStream = new URL(FETCH_BLOCKED_SERVERS).openStream();
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8).useDelimiter("\n");
) {
List<String> hashes = new ArrayList<>();
while (scanner.hasNext()) {
hashes.add(scanner.next());
}
bannedServerHashes = Collections.synchronizedList(hashes);
log.info("Fetched {} banned server hashes", bannedServerHashes.size());
}
}
/**
* Check if the server with the
* given hostname is blocked by Mojang.
*
* @param hostname the server hostname to check
* @return whether the hostname is blocked
*/
public boolean isServerBlocked(@NonNull String hostname) {
// Remove trailing dots
while (hostname.charAt(hostname.length() - 1) == '.') {
hostname = hostname.substring(0, hostname.length() - 1);
}
// Is the hostname banned?
if (isServerHostnameBlocked(hostname)) {
return true;
}
List<String> splitDots = Lists.newArrayList(DOT_SPLITTER.split(hostname)); // Split the hostname by dots
boolean isIp = splitDots.size() == 4; // Is it an IP address?
if (isIp) {
for (String element : splitDots) {
try {
int part = Integer.parseInt(element);
if (part >= 0 && part <= 255) { // Ensure the part is within the valid range
continue;
}
} catch (NumberFormatException ignored) {
// Safely ignore, not a number
}
isIp = false;
break;
}
}
// Check if the hostname is blocked
if (!isIp && isServerHostnameBlocked("*." + hostname)) {
return true;
}
// Additional checks for the hostname
while (splitDots.size() > 1) {
splitDots.remove(isIp ? splitDots.size() - 1 : 0);
String starredPart = isIp ? DOT_JOINER.join(splitDots) + ".*" : "*." + DOT_JOINER.join(splitDots);
if (isServerHostnameBlocked(starredPart)) {
return true;
}
}
return false;
}
/**
* Check if the hash for the given
* hostname is in the blocked server list.
*
* @param hostname the hostname to check
* @return whether the hostname is blocked
*/
private boolean isServerHostnameBlocked(@NonNull String hostname) {
// Check the cache first for the hostname
if (blockedServersCache.contains(hostname)) {
return true;
}
String hashed = Hashing.sha1().hashBytes(hostname.toLowerCase().getBytes(StandardCharsets.ISO_8859_1)).toString();
boolean blocked = bannedServerHashes.contains(hashed); // Is the hostname blocked?
if (blocked) { // Cache the blocked hostname
blockedServersCache.add(hostname);
}
return blocked;
}
/**
* Gets the Session Server profile of the
* player with the given UUID.
*
* @param id the uuid or name of the player
* @return the profile
*/
public MojangProfile getProfile(String id) {
return WebRequest.getAsEntity(SESSION_SERVER_ENDPOINT + "/session/minecraft/profile/" + id, MojangProfile.class);
}
/**
* Gets the UUID of the player using
* the name of the player.
*
* @param id the name of the player
* @return the profile
*/
public MojangUsernameToUuid getUuidFromUsername(String id) {
return WebRequest.getAsEntity(API_ENDPOINT + "/users/profiles/minecraft/" + id, MojangUsernameToUuid.class);
}
}

View File

@ -24,12 +24,12 @@ import java.util.UUID;
@Service @Log4j2
public class PlayerService {
private final MojangAPIService mojangAPIService;
private final MojangService mojangAPIService;
private final PlayerCacheRepository playerCacheRepository;
private final PlayerNameCacheRepository playerNameCacheRepository;
@Autowired
public PlayerService(MojangAPIService mojangAPIService, PlayerCacheRepository playerCacheRepository, PlayerNameCacheRepository playerNameCacheRepository) {
public PlayerService(MojangService mojangAPIService, PlayerCacheRepository playerCacheRepository, PlayerNameCacheRepository playerNameCacheRepository) {
this.mojangAPIService = mojangAPIService;
this.playerCacheRepository = playerCacheRepository;
this.playerNameCacheRepository = playerNameCacheRepository;

View File

@ -20,10 +20,12 @@ import java.util.Optional;
public class ServerService {
private static final String DEFAULT_SERVER_ICON = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAASFBMVEWwsLBBQUE9PT1JSUlFRUUuLi5MTEyzs7M0NDQ5OTlVVVVQUFAmJia5ubl+fn5zc3PFxcVdXV3AwMCJiYmUlJRmZmbQ0NCjo6OL5p+6AAAFVklEQVRYw+1W67K0KAzkJnIZdRAZ3/9NtzvgXM45dX7st1VbW7XBUVDSdEISRqn/5R+T82/+nsr/XZn/SHm/3x9/ArA/IP8qwPK433d44VubZ/XT6/cJy0L792VZfnDrcRznr86d748u92X5vtaxOe228zcCy+MSMpg/5SwRopsYMv8oigCwngbQhE/rzhwAYMpxnvMvHhgy/8AgByJolzb5pPqEbvtgMBBmtvkbgxKmaaIZ5TyPum6Viue6te241N+s+W6nOlucgjEx6Nay9zZta1XVxejW+Q5ZhhkDS31lgOTegjUBor33CQilbC2GYGy9y9bN8ytevjE4a2stajHDAgAcUkoYwzO6zQi8ZflC+XO0+exiuNa3OQtIJOCk13neUjv7VO7Asu/3LwDFeg37sQtQhy4lAQH6IR9ztca0E3oI5PtDAlJ1tHGplrJ12jjrrXPWYvXsU042Bl/qUr3B9qzPSKaovpvjgglYL2F1x+Zs7gIvpLYuq46wr3H5/RJxyvM6sXOY762oU4YZ3mAz1lpc9O3Y30VJUM/iWhBIib63II/LA4COEMxcSmrH4ddl/wTYe3RIO0vK2VI9wQy6AxRsJpb3AAALvXb6TxvUCYSdOQo5Mh0GySkJc7rB405GUEfzbbl/iFpPoNQVNUQAZG06nkI6RCABRqRA9IimH6Up5Mhybtu2IlewB2Sf6AmQ4ZU9rfBELvyA23Yub6LWWtUBgK3OB79L7FILLDKWd4wpxmMRAMoLQR1ItLoiWUmhFtjptab7LQDgR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private final MinecraftServerCacheRepository serverCacheRepository;
@Autowired
public ServerService(MinecraftServerCacheRepository serverCacheRepository) {
public ServerService(MojangService mojangService, MinecraftServerCacheRepository serverCacheRepository) {
this.mojangService = mojangService;
this.serverCacheRepository = serverCacheRepository;
}
@ -61,6 +63,12 @@ public class ServerService {
platform.getPinger().ping(hostname, port),
System.currentTimeMillis()
);
if (platform == MinecraftServer.Platform.JAVA) { // Check if the server is blocked by Mojang
JavaMinecraftServer javaServer = (JavaMinecraftServer) server.getServer();
javaServer.setMojangBanned(mojangService.isServerBlocked(hostname));
}
log.info("Found server: {}:{}", hostname, port);
serverCacheRepository.save(server);
server.setCached(-1); // Indicate that the server is not cached

View File

@ -51,15 +51,7 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
JavaPacketStatusInStart packetStatusInStart = new JavaPacketStatusInStart();
packetStatusInStart.process(inputStream, outputStream);
JavaServerStatusToken token = Main.GSON.fromJson(packetStatusInStart.getResponse(), JavaServerStatusToken.class);
return new JavaMinecraftServer(
hostname,
ip,
port,
MinecraftServer.MOTD.create(token.getDescription()),
token.getVersion().detailedCopy(),
token.getPlayers(),
JavaMinecraftServer.Favicon.create(token.getFavicon(), ServerUtils.getAddress(hostname, port))
);
return JavaMinecraftServer.create(hostname, ip, port, token);
}
} catch (IOException ex) {
if (ex instanceof UnknownHostException) {