Skip to content

Commit

Permalink
Added code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Sep 26, 2024
1 parent 5152644 commit 11b80dd
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 183 deletions.
41 changes: 19 additions & 22 deletions README.MD
Original file line number Diff line number Diff line change
@@ -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
```
160 changes: 5 additions & 155 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,81 +1,30 @@
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
group = project.maven_group ?: rootProject.maven_group
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 {
Expand All @@ -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)
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ plugins {
id "org.gradle.toolchains.foojay-resolver-convention" version "0.8.0"
}

rootProject.name = "GradleTemplate"

//include(":subproject")
rootProject.name = "ViaProxyWhitelist"
Empty file removed src/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions src/main/java/net/lenni0451/whitelist/LoginListener.java
Original file line number Diff line number Diff line change
@@ -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<ChannelFutureListener> listeners) {
if (packet instanceof C2SLoginHelloPacket loginHello) {
if (!WhitelistConfig.whitelist.contains(loginHello.name)) {
this.proxyConnection.kickClient(WhitelistConfig.kickMessage);
}
}
return true;
}

}
33 changes: 33 additions & 0 deletions src/main/java/net/lenni0451/whitelist/Main.java
Original file line number Diff line number Diff line change
@@ -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()));
}

}
35 changes: 35 additions & 0 deletions src/main/java/net/lenni0451/whitelist/WhitelistConfig.java
Original file line number Diff line number Diff line change
@@ -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<String> whitelist = List.of("Player1", "Player2");

public static void load() {
try {
ConfigLoader<WhitelistConfig> 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);
}
}

}
5 changes: 5 additions & 0 deletions src/main/resources/viaproxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: "Whitelist"
version: "${version}"
author: "Lenni0451"
main: "net.lenni0451.whitelist.Main"
min-version: "3.3.4"

0 comments on commit 11b80dd

Please sign in to comment.