Skip to content

Commit

Permalink
fixed role check
Browse files Browse the repository at this point in the history
  • Loading branch information
MeiNanziiii committed Oct 7, 2024
1 parent 6b41e6c commit 0cae670
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ runs/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

.kotlin
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ plugins {

loom {
serverOnlyMinecraftJar()

accessWidenerPath = file("src/main/resources/minekord.accesswidener")
}

val modVersion: String by project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@

@Mixin(ServerLoginNetworkHandler.class)
public abstract class ServerLoginNetworkHandlerMixin {
@Shadow public abstract void disconnect(Text text);
@Shadow
@Final
MinecraftServer server;

@Shadow @Nullable private GameProfile profile;
@Shadow
@Nullable
private GameProfile profile;

@Shadow protected abstract void sendSuccessPacket(GameProfile gameProfile);
@Shadow
public abstract void disconnect(Text text);

@Shadow @Final MinecraftServer server;
@Shadow
protected abstract void sendSuccessPacket(GameProfile gameProfile);

@Inject(method = "onHello", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;isOnlineMode()Z"), cancellable = true)
private void minekord$trueUuids(LoginHelloC2SPacket loginHelloC2SPacket, CallbackInfo ci) {
if (SnowflakeToUUID.INSTANCE.enabled()) {
if (this.server.isOnlineMode() && !SnowflakeToUUID.INSTANCE.allowOfflinePlayers() && !SnowflakeToUUID.INSTANCE.premiumPlayer(loginHelloC2SPacket.comp_907())) {
this.disconnect(Text.literal("test1"));
this.disconnect(Text.translatable("multiplayer.disconnect.generic"));
ci.cancel();
}

Expand All @@ -40,10 +46,19 @@ public abstract class ServerLoginNetworkHandlerMixin {
this.profile = new GameProfile(trueUuid, loginHelloC2SPacket.comp_765());
this.sendSuccessPacket(this.profile);
} else {
this.disconnect(Text.literal("test2"));
this.disconnect(Text.translatable("multiplayer.disconnect.generic"));
}

ci.cancel();
}
}

@Inject(method = "sendSuccessPacket", at = @At("HEAD"))
private void minekord$checkRoles(GameProfile gameProfile, CallbackInfo ci) {
if (!SnowflakeToUUID.INSTANCE.enabled()) {
if (SnowflakeToUUID.INSTANCE.getPlayer(gameProfile.getName()) == null) {
this.disconnect(Text.translatable("multiplayer.disconnect.generic"));
}
}
}
}
15 changes: 10 additions & 5 deletions src/main/kotlin/ua/mei/minekord/auth/SnowflakeToUUID.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import dev.kord.common.annotation.KordExperimental
import dev.kord.core.entity.Member
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.timeout
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull
Expand Down Expand Up @@ -35,12 +33,19 @@ object SnowflakeToUUID {
}

@OptIn(KordExperimental::class)
fun getPlayer(nickname: String): Member? {
return runBlocking {
MinekordBot.guild?.getMembers(nickname)?.filter { member ->
val trueRoleIds: List<ULong> = member.roleIds.map { it.value }
config[AuthSpec.requiredRoles].all { it in trueRoleIds }
}?.firstOrNull()
}
}

fun generateFromNickname(nickname: String): UUID? {
return runBlocking {
try {
val member: Member = MinekordBot.guild?.getMembers(nickname)?.filter {
it.roleIds.map { it.value }.all { it in config[AuthSpec.requiredRoles] }
}?.firstOrNull() ?: return@runBlocking null
val member: Member = getPlayer(nickname) ?: return@runBlocking null

generateFromId(member.id.value)
} catch (_: Throwable) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/ua/mei/minekord/config/AuthSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ object AuthSpec : ConfigSpec() {
val uuidFromSnowflake by required<Boolean>()
val allowOfflinePlayers by required<Boolean>()
val requiredRoles by required<List<ULong>>()
val loginByIp by required<Boolean>()
val ignorePremiumIp by required<Boolean>()
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"mixins": [
"minekord.mixins.json"
],
"accessWidener": "minekord.accesswidener",
"depends": {
"fabricloader": ">=${loader_version}",
"fabric-language-kotlin": ">=${kotlin_loader_version}",
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/minekord.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessWidener v2 named

accessible class net/minecraft/server/network/ServerLoginNetworkHandler$State
16 changes: 16 additions & 0 deletions src/main/resources/minekord.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,24 @@ chat = 0
uuidFromSnowflake = false

# If true, offline players can join server if online mode enabled.
# If online mode disabled, ignore.
# Made for servers which uses EasyAuth with premium-auto-login.
allowOfflinePlayers = false

# Roles which required for server join.
# Can be empty.
requiredRoles = []

# When player trying to join server, bot will send embed to player DM,
# embed contains IP from which player are joining, player can confirm
# is this ip are they, and then successful join server.
#
# Every new IP, bot will request confirmation.
# Player can join only with last confirmed IP.
#
# Option made for players, who are lazy to type /login and /register,
# or for best safety.
loginByIp = false

# Don't request IP confirmation to premium players.
ignorePremiumIp = false

0 comments on commit 0cae670

Please sign in to comment.