diff --git a/README.MD b/README.MD index 191010f..050058c 100644 --- a/README.MD +++ b/README.MD @@ -1,27 +1,24 @@ -# Gradle Template -A template project for my gradle based repos. +# ViaProxyWhitelist +Only allow specified players to join the ViaProxy server. -## Maven Server Setup -To use the maven servers in this template you need to enter your credentials into your `gradle.properties` files.\ -The file can be found in the `.gradle` folder in your home directory (create it if it doesn't exist). -```properties -reposiliteUsername=user -reposilitePassword=123456 +## Usage +### Installation +Download the latest release from the [releases page](https://github.com/ViaVersionAddons/ViaProxyWhitelist/releases) and place it in the `plugins` folder of the ViaProxy server.\ +The minimum required version of ViaProxy is `3.3.4`. -ossrhUsername=user -ossrhPassword=123456 -``` +### Configuration +You must enable the `proxy-online-mode` option in the config!\ +The plugin does not work without this option enabled. -## Signing Setup -To sign your artifacts you need to enter your signing credentials into your `gradle.properties` files.\ -The file can be found in the `.gradle` folder in your home directory (create it if it doesn't exist). -```properties -signing.keyId=ABC12345 -signing.password=123456 -signing.secretKeyRingFile=secretkey.gpg -``` +After starting the server with the plugin, a `whitelist.yml` file will be created in the server folder.\ +It contains the kick message and the list of players that are allowed to join the server. + +```yaml +#The message that will be displayed when a player is not whitelisted +KickMessage: You are not whitelisted on this server! -The secretkey can be exported using the `gpg` command: -```bash -gpg --export-secret-key > secretkey.gpg +#The list of players that are allowed to join the server +Whitelist: +- Player1 +- Player2 ``` diff --git a/build.gradle b/build.gradle index ba844f4..ace796f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,10 @@ plugins { id "java" - id "maven-publish" - id "signing" id "checkstyle" - id "net.raphimc.class-token-replacer" version "1.1.3" } base { - java.toolchain.languageVersion = JavaLanguageVersion.of(8) + java.toolchain.languageVersion = JavaLanguageVersion.of(17) compileJava.options.encoding = compileTestJava.options.encoding = javadoc.options.encoding = "UTF-8" archivesName = project.maven_name ?: rootProject.maven_name @@ -15,67 +12,19 @@ base { version = project.maven_version ?: rootProject.maven_version } -configurations { - include - - implementation.extendsFrom include - api.extendsFrom include -} - repositories { mavenCentral() maven { - name = "lenni0451" - url = "https://maven.lenni0451.net/everything" - } - maven { - name = "jitpack" - url = "https://jitpack.io" + name = "viaversion" + url = "https://repo.viaversion.com/everything" } - maven { - name = "sonatype snapshots" - url "https://oss.sonatype.org/content/repositories/snapshots/" - } - - //ivy { //include "waterfall:1.19:504@jar" - // url "https://papermc.io/api/v2/projects" - // patternLayout { - // artifact "/[organisation]/versions/[module]/builds/[revision]/downloads/[organisation]-[module]-[revision].[ext]" - // } - // metadataSources { - // artifact() - // } - //} } dependencies { - if (new File(projectDir, "libs").exists()) { //Include all jars in the libs folder - include fileTree(include: ["*.jar"], dir: "libs") - } - compileOnly "com.google.code.findbugs:jsr305:3.0.2" compileOnly "org.jetbrains:annotations:25.0.0" compileOnly(annotationProcessor("org.projectlombok:lombok:1.18.34")) - //include "net.lenni0451:Reflect:1.0.0" - - testImplementation(platform("org.junit:junit-bom:5.11.1")) - testImplementation "org.junit.jupiter:junit-jupiter" - testRuntimeOnly "org.junit.platform:junit-platform-launcher" - testCompileOnly(testAnnotationProcessor("org.projectlombok:lombok:1.18.34")) -} - -sourceSets { - main { - classTokenReplacer { - //Replace all instances of '${version}' with the project version - property("\${version}", project.version) - } - } -} - -java { - withSourcesJar() - withJavadocJar() + compileOnly "net.raphimc:ViaProxy:3.3.4-SNAPSHOT" } processResources { @@ -84,115 +33,16 @@ processResources { "version": project.maven_version ) - filesMatching("config.yml") { + filesMatching("viaproxy.yml") { expand( "version": project.maven_version ) } } -artifacts { - archives javadocJar, sourcesJar -} - -jar { - dependsOn configurations.include - from { - duplicatesStrategy = DuplicatesStrategy.EXCLUDE - configurations.include.collect { - zipTree(it) - } - } { - exclude "META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA" - } - - manifest { - attributes( - "Main-Class": "net.lenni0451.gradletemplate.Main", - "Multi-Release": "true" - ) - } -} - -test { - useJUnitPlatform() - testLogging { - events "passed", "skipped", "failed" - } - maxParallelForks Runtime.runtime.availableProcessors() -} - -publishing { - repositories { - maven { - name = "reposilite" - def releasesUrl = "https://maven.lenni0451.net/releases" - def snapshotsUrl = "https://maven.lenni0451.net/snapshots" - url = project.maven_version.endsWith("SNAPSHOT") ? snapshotsUrl : releasesUrl - - credentials(PasswordCredentials) - authentication { - basic(BasicAuthentication) - } - } - maven { - name = "ossrh" - def releasesUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" - def snapshotsUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/" - url = project.maven_version.endsWith("SNAPSHOT") ? snapshotsUrl : releasesUrl - - credentials(PasswordCredentials) - authentication { - basic(BasicAuthentication) - } - } - } - publications { - maven(MavenPublication) { - artifactId = project.maven_name ?: rootProject.maven_name - groupId = rootProject.maven_group - version = rootProject.maven_version - - from components.java - - pom { - name = rootProject.name - description = rootProject.maven_description - url = "https://github.com/" + rootProject.github_repo - licenses { - license { - name = "MIT License" - url = "https://github.com/" + rootProject.github_repo + "/blob/main/LICENSE" - } - } - developers { - developer { - id = "Lenni0451" - } - } - scm { - connection = "scm:git:git://github.com/" + rootProject.github_repo + ".git" - developerConnection = "scm:git:ssh://github.com/" + rootProject.github_repo + ".git" - url = "github.com/" + rootProject.github_repo - } - } - } - } -} - checkstyle { toolVersion = "10.16.0" //Latest version for Java 8: 9.3 configFile = rootProject.file("config/checkstyle/checkstyle.xml") } -signing { - setRequired false - sign configurations.archives - sign publishing.publications.maven -} - -project.tasks.withType(PublishToMavenRepository).forEach { - it.dependsOn(project.tasks.withType(Sign)) -} -build.dependsOn(test) build.dependsOn(check) diff --git a/gradle.properties b/gradle.properties index 9573198..e87f215 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,9 +4,9 @@ org.gradle.parallel=true org.gradle.configureondemand=true #Maven settings -maven_name=GradleTemplate +maven_name=ViaProxyWhitelist maven_group=net.lenni0451 maven_version=1.0.0 -maven_description=A template for gradle projects -github_repo=Lenni0451/GradleTemplate +maven_description=Only allow specified players to join the ViaProxy server +github_repo=ViaVersionAddons/ViaProxyWhitelist diff --git a/settings.gradle b/settings.gradle index 2320d90..f3de32e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,6 +9,4 @@ plugins { id "org.gradle.toolchains.foojay-resolver-convention" version "0.8.0" } -rootProject.name = "GradleTemplate" - -//include(":subproject") +rootProject.name = "ViaProxyWhitelist" diff --git a/src/.gitkeep b/src/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/net/lenni0451/whitelist/LoginListener.java b/src/main/java/net/lenni0451/whitelist/LoginListener.java new file mode 100644 index 0000000..25eabe2 --- /dev/null +++ b/src/main/java/net/lenni0451/whitelist/LoginListener.java @@ -0,0 +1,27 @@ +package net.lenni0451.whitelist; + +import io.netty.channel.ChannelFutureListener; +import net.raphimc.netminecraft.packet.Packet; +import net.raphimc.netminecraft.packet.impl.login.C2SLoginHelloPacket; +import net.raphimc.viaproxy.proxy.packethandler.PacketHandler; +import net.raphimc.viaproxy.proxy.session.ProxyConnection; + +import java.util.List; + +public class LoginListener extends PacketHandler { + + public LoginListener(final ProxyConnection proxyConnection) { + super(proxyConnection); + } + + @Override + public boolean handleC2P(Packet packet, List listeners) { + if (packet instanceof C2SLoginHelloPacket loginHello) { + if (!WhitelistConfig.whitelist.contains(loginHello.name)) { + this.proxyConnection.kickClient(WhitelistConfig.kickMessage); + } + } + return true; + } + +} diff --git a/src/main/java/net/lenni0451/whitelist/Main.java b/src/main/java/net/lenni0451/whitelist/Main.java new file mode 100644 index 0000000..caab775 --- /dev/null +++ b/src/main/java/net/lenni0451/whitelist/Main.java @@ -0,0 +1,33 @@ +package net.lenni0451.whitelist; + +import net.lenni0451.lambdaevents.EventHandler; +import net.raphimc.viaproxy.ViaProxy; +import net.raphimc.viaproxy.plugins.ViaProxyPlugin; +import net.raphimc.viaproxy.plugins.events.ConnectEvent; +import net.raphimc.viaproxy.plugins.events.ViaProxyLoadedEvent; +import net.raphimc.viaproxy.util.logging.Logger; + +public class Main extends ViaProxyPlugin { + + @Override + public void onEnable() { + WhitelistConfig.load(); + ViaProxy.EVENT_MANAGER.register(this); + } + + @EventHandler + public void onViaProxyLoaded(final ViaProxyLoadedEvent event) { + if (!ViaProxy.getConfig().isProxyOnlineMode()) { + Logger.LOGGER.error("Proxy online mode is disabled, please enable it to use the whitelist plugin!"); + Logger.LOGGER.error("Without online mode the whitelist plugin would be effectively useless"); + Logger.LOGGER.error("Shutting down..."); + System.exit(0); + } + } + + @EventHandler(priority = Integer.MIN_VALUE) + public void onConnect(final ConnectEvent event) { + event.getProxyConnection().getPacketHandlers().add(0, new LoginListener(event.getProxyConnection())); + } + +} diff --git a/src/main/java/net/lenni0451/whitelist/WhitelistConfig.java b/src/main/java/net/lenni0451/whitelist/WhitelistConfig.java new file mode 100644 index 0000000..5c16bf4 --- /dev/null +++ b/src/main/java/net/lenni0451/whitelist/WhitelistConfig.java @@ -0,0 +1,35 @@ +package net.lenni0451.whitelist; + +import net.lenni0451.optconfig.ConfigLoader; +import net.lenni0451.optconfig.annotations.Description; +import net.lenni0451.optconfig.annotations.OptConfig; +import net.lenni0451.optconfig.annotations.Option; +import net.lenni0451.optconfig.provider.ConfigProvider; +import net.raphimc.viaproxy.util.logging.Logger; + +import java.io.File; +import java.util.List; + +@OptConfig +public class WhitelistConfig { + + @Option("KickMessage") + @Description("The message that will be displayed when a player is not whitelisted") + public static String kickMessage = "You are not whitelisted on this server!"; + + @Option("Whitelist") + @Description("The list of players that are allowed to join the server") + public static List whitelist = List.of("Player1", "Player2"); + + public static void load() { + try { + ConfigLoader configLoader = new ConfigLoader<>(WhitelistConfig.class); + configLoader.getConfigOptions().setResetInvalidOptions(true); + configLoader.loadStatic(ConfigProvider.file(new File("whitelist.yml"))); + } catch (Throwable t) { + Logger.LOGGER.error("Failed to load the whitelist configuration!", t); + System.exit(-1); + } + } + +} diff --git a/src/main/resources/viaproxy.yml b/src/main/resources/viaproxy.yml new file mode 100644 index 0000000..c5f1879 --- /dev/null +++ b/src/main/resources/viaproxy.yml @@ -0,0 +1,5 @@ +name: "Whitelist" +version: "${version}" +author: "Lenni0451" +main: "net.lenni0451.whitelist.Main" +min-version: "3.3.4"