diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 2e172a55..5fd1b70e 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -35,7 +35,7 @@ jobs: name: Artifacts path: | build/libs/ - module/**/build/libs/ + modules/**/build/libs/ - name: capture test reports on failure uses: actions/upload-artifact@v3 if: failure() diff --git a/.gitignore b/.gitignore index 42d56c63..7555e099 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .idea/ build/ out/ +mods/ proxyfox.db.properties systems.json .env \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 9d194ea4..40485cf8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,6 +20,7 @@ tasks { archiveClassifier.set("") } register("poolRelease") { + group = "build" for (proj in subprojects) { val shadowJar = proj.tasks.findByPath("shadowJar") if (shadowJar != null) { @@ -48,6 +49,9 @@ allprojects { mavenCentral() maven("https://libraries.minecraft.net/") maven("https://oss.sonatype.org/content/repositories/snapshots") + maven("https://maven.quiltmc.org/repository/release/") + maven("https://maven.quiltmc.org/repository/snapshot/") + maven("https://maven.fabricmc.net/") maven("https://jitpack.io") } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 7454180f..41d9927a 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa991fce..070cb702 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 744e882e..1b6c7873 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,67 +17,101 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +APP_BASE_NAME=${0##*/} # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MSYS* | MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -87,9 +121,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -98,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the @@ -106,80 +140,95 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=`expr $i + 1` + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' exec "$JAVACMD" "$@" diff --git a/modules/bot/src/main/kotlin/dev/proxyfox/bot/BotUtil.kt b/modules/bot/src/main/kotlin/dev/proxyfox/bot/BotUtil.kt index f8c6f755..aaf6b04a 100644 --- a/modules/bot/src/main/kotlin/dev/proxyfox/bot/BotUtil.kt +++ b/modules/bot/src/main/kotlin/dev/proxyfox/bot/BotUtil.kt @@ -33,7 +33,6 @@ import dev.kord.gateway.Intent import dev.kord.gateway.PrivilegedIntent import dev.kord.gateway.builder.Shards import dev.kord.rest.builder.message.EmbedBuilder -import dev.kord.rest.builder.message.create.embed import dev.kord.rest.request.KtorRequestException import dev.proxyfox.common.* import dev.proxyfox.database.database @@ -49,6 +48,7 @@ import kotlinx.datetime.Clock import kotlinx.datetime.Instant import java.lang.Integer.min import java.time.OffsetDateTime +import java.util.* import java.util.concurrent.Executors import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind @@ -61,6 +61,11 @@ const val UPLOAD_LIMIT = 1024 * 1024 * 8 val scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()) +private val idUrl = System.getenv("PROXYFOX_KEY").let { it.substring(0, it.indexOf('.')) } + +private val webhook = Regex("https?://(?:[^./]\\.)?discord(?:app)?\\.com/api/(v\\d+/)?webhooks/\\d+/\\S+") +private val token = Regex("$idUrl[a-z0-9=/+_-]*\\.[a-z0-9=/+_-]+\\.[a-z0-9=/+_-]", RegexOption.IGNORE_CASE) + lateinit var scope: CoroutineScope lateinit var kord: Kord lateinit var http: HttpClient @@ -185,12 +190,16 @@ suspend fun updatePresence() { suspend fun handleError(err: Throwable, message: MessageBehavior) { // Catch any errors and log them val timestamp = System.currentTimeMillis() - logger.warn(timestamp.toString()) - logger.warn(err.stackTraceToString()) - val reason = err.message + // Let the logger unwind the stacktrace. + logger.warn(timestamp.toString(), err) + // Do not leak webhook URL nor token in output. + // Note: The token here is a generic regex that only matches with the bot's + // ID and will make no attempt to verify it's the real one, purely for guarding + // the token from brute forcing of the replace method. + val reason = err.message?.replace(webhook, "[WEBHOOK]")?.replace(token, "[TOKEN]") var cause = "" err.stackTrace.forEach { - if (it.toString().startsWith("dev.proxyfox")) + if (it.className.startsWith("dev.proxyfox")) cause += " at $it\n" } message.channel.createMessage( @@ -200,17 +209,12 @@ suspend fun handleError(err: Throwable, message: MessageBehavior) { if (errorChannel == null && errorChannelId != null) errorChannel = kord.getChannel(errorChannelId) as TextChannel if (errorChannel != null) { - cause = "" - err.stackTrace.forEach { - if (cause.length > 2000) return@forEach - cause += "at $it\n\n" - } + // Prevent the log channel from also showing tokens, should it be public in any manner. + cause = err.stackTraceToString().replace(webhook, "[WEBHOOK]").replace(token, "[TOKEN]") + errorChannel!!.createMessage { content = "`$timestamp`" - embed { - title = "${err.javaClass.name}: $reason" - description = "```\n$cause```" - } + addFile("exception.log", cause.byteInputStream()) } } } diff --git a/modules/bot/src/main/kotlin/dev/proxyfox/bot/webhook/ProxyContext.kt b/modules/bot/src/main/kotlin/dev/proxyfox/bot/webhook/ProxyContext.kt index 2ab1b9dd..5dac012f 100644 --- a/modules/bot/src/main/kotlin/dev/proxyfox/bot/webhook/ProxyContext.kt +++ b/modules/bot/src/main/kotlin/dev/proxyfox/bot/webhook/ProxyContext.kt @@ -102,7 +102,7 @@ data class ProxyContext( val id = if (channel is ThreadChannelBehavior) channel.parentId.value else channel.id.value WebhookCache -= id } - throw RuntimeException("Failed to proxy your message.", e) + throw RuntimeException("Failed to proxy your message: $e", e) } if (newMessage.content != messageContent && messageContent.isNotBlank()) webhook.edit(newMessage.id, threadId) { diff --git a/modules/database/src/main/kotlin/dev/proxyfox/database/etc/exporter/Exporter.kt b/modules/database/src/main/kotlin/dev/proxyfox/database/etc/exporter/Exporter.kt index 2e7ecb43..99948a1d 100644 --- a/modules/database/src/main/kotlin/dev/proxyfox/database/etc/exporter/Exporter.kt +++ b/modules/database/src/main/kotlin/dev/proxyfox/database/etc/exporter/Exporter.kt @@ -10,24 +10,46 @@ package dev.proxyfox.database.etc.exporter import dev.proxyfox.database.Database import dev.proxyfox.database.database +import dev.proxyfox.database.etc.types.* import dev.proxyfox.database.gson -import dev.proxyfox.database.etc.types.PkMember -import dev.proxyfox.database.etc.types.PkProxy -import dev.proxyfox.database.etc.types.PkSwitch -import dev.proxyfox.database.etc.types.PkSystem +import dev.proxyfox.database.pkCompatibleIso8601 +import dev.proxyfox.database.records.member.MemberRecord +import dev.proxyfox.database.records.system.SystemSwitchRecord object Exporter { suspend inline fun export(userId: ULong) = export(database, userId) suspend fun export(database: Database, userId: ULong): String { val system = database.fetchSystemFromUser(userId) ?: return "" + val members = database.fetchMembersFromSystem(system.id) + val memberIds = members?.mapTo(HashSet(), MemberRecord::id) ?: setOf() + + // Nested function for mapping switches to PkSwitch exports. + fun toPkSwitch(record: SystemSwitchRecord): PkSwitch { + // Note, ArrayList is used here to retain order. + val existing = ArrayList(record.memberIds) + + // If retainAll modifies the list, take the slow route. + if (existing.retainAll(memberIds)) { + return PkSwitch( + timestamp = record.timestamp.pkCompatibleIso8601(), + members = existing.toList(), + + proxyfox = PfSwitchExtension( + allMembers = record.memberIds, + ) + ) + } + + return PkSwitch(record) + } val pkSystem = PkSystem( system, - members = database.fetchMembersFromSystem(system.id)?.map { + members = members?.map { PkMember(it, database.fetchProxiesFromSystemAndMember(system.id, it.id)?.mapTo(HashSet(), ::PkProxy)) }, - switches = database.fetchSwitchesFromSystem(system.id)?.map(::PkSwitch), + switches = database.fetchSwitchesFromSystem(system.id)?.map(::toPkSwitch), ) return gson.toJson(pkSystem) } diff --git a/modules/database/src/main/kotlin/dev/proxyfox/database/etc/types/PkTypes.kt b/modules/database/src/main/kotlin/dev/proxyfox/database/etc/types/PkTypes.kt index 971f48f1..1389bacf 100644 --- a/modules/database/src/main/kotlin/dev/proxyfox/database/etc/types/PkTypes.kt +++ b/modules/database/src/main/kotlin/dev/proxyfox/database/etc/types/PkTypes.kt @@ -11,14 +11,14 @@ package dev.proxyfox.database.etc.types import com.google.gson.annotations.SerializedName import dev.proxyfox.common.fromColorForExport import dev.proxyfox.database.* +import dev.proxyfox.database.etc.gson.NullValueProcessor +import dev.proxyfox.database.etc.gson.UnexpectedValueProcessor import dev.proxyfox.database.records.member.MemberProxyTagRecord import dev.proxyfox.database.records.member.MemberRecord import dev.proxyfox.database.records.misc.AutoProxyMode import dev.proxyfox.database.records.misc.TrustLevel import dev.proxyfox.database.records.system.SystemRecord import dev.proxyfox.database.records.system.SystemSwitchRecord -import dev.proxyfox.database.etc.gson.NullValueProcessor -import dev.proxyfox.database.etc.gson.UnexpectedValueProcessor import java.time.LocalDate import java.time.OffsetDateTime import java.time.format.DateTimeFormatter @@ -218,6 +218,9 @@ data class PkSwitch( val timestamp: String?, val members: List?, + /** Allows for storing missing member data */ + val proxyfox: PfSwitchExtension? = null, + // Ignored for PFv1 database imports @Deprecated("PFv1 database imports only") val id: Void? = null, @@ -329,6 +332,12 @@ data class PfMemberExtension( val autoProxy: Boolean?, ) +@JvmRecord +data class PfSwitchExtension( + /** Note: It is *not* possible to reimport this. */ + val allMembers: List?, +) + @Suppress("EnumEntryName") enum class PkPrivacyEnum { public, private diff --git a/modules/patch/build.gradle.kts b/modules/patch/build.gradle.kts new file mode 100644 index 00000000..94ef53e7 --- /dev/null +++ b/modules/patch/build.gradle.kts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +plugins { + alias(libs.plugins.kotlin.jvm) + alias(libs.plugins.shadow) +} + +configurations { + create("include") +} + +dependencies { + // This provides a patched Quilt Loader that can run in this environment. + // Version is pinned, avoiding having the API suddenly break due to an + // update that happens to effect what we depend on. + implementation("org.quiltmc:quilt-loader:0.18.1-beta.27-20230105.235638-8")?.let { "include"(it) } + + // Loader's dependencies; will not be required when QuiltMC/quilt-loader#190 + // is merged upstream. + implementation("net.fabricmc:sponge-mixin:0.11.4+mixin.0.8.5")?.let { "include"(it) } + implementation("net.fabricmc:tiny-mappings-parser:0.3.0+build.17")?.let { "include"(it) } + implementation("net.fabricmc:tiny-remapper:0.8.6")?.let { "include"(it) } + implementation("net.fabricmc:access-widener:2.1.0")?.let { "include"(it) } + implementation("org.quiltmc:quilt-json5:1.0.2")?.let { "include"(it) } + implementation("org.quiltmc:quilt-config:1.0.0-beta.6")?.let { "include"(it) } + implementation("org.ow2.asm:asm:9.4")?.let { "include"(it) } + implementation("org.ow2.asm:asm-analysis:9.4")?.let { "include"(it) } + implementation("org.ow2.asm:asm-commons:9.4")?.let { "include"(it) } + implementation("org.ow2.asm:asm-tree:9.4")?.let { "include"(it) } + implementation("org.ow2.asm:asm-util:9.4")?.let { "include"(it) } + + // Bot module to avoid shadowing in Quilt Loader into ProxyFox. + implementation(project(":modules:bot")) +} + +tasks { + compileKotlin { + kotlinOptions.freeCompilerArgs += listOf( + "-Xno-call-assertions", + "-Xno-receiver-assertions", + "-Xno-param-assertions" + ) + } + shadowJar { + configurations = listOf(project.configurations["include"]) + } +} \ No newline at end of file diff --git a/modules/patch/src/main/java/dev/proxyfox/patch/BudgetLibClassifier.java b/modules/patch/src/main/java/dev/proxyfox/patch/BudgetLibClassifier.java new file mode 100644 index 00000000..0b93acf1 --- /dev/null +++ b/modules/patch/src/main/java/dev/proxyfox/patch/BudgetLibClassifier.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package dev.proxyfox.patch;// Created 2023-03-01T01:42:32 + +import net.fabricmc.api.EnvType; +import org.quiltmc.loader.impl.util.log.Log; +import org.quiltmc.loader.impl.util.log.LogCategory; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.zip.ZipFile; + +/** + * @author KJP12 + * @since 2.0.8 + **/ +public class BudgetLibClassifier & Library> { + private final EnumMap origins; + private final Set unmatched; + private final Class clazz; + + public BudgetLibClassifier(Class clazz) { + this.clazz = clazz; + this.origins = new EnumMap<>(clazz); + this.unmatched = new HashSet<>(); + } + + public void process(final Collection paths, final EnvType envType) throws IOException { + final List values; + if (envType != null) { + final var tmp = new ArrayList(); + for (var value : clazz.getEnumConstants()) { + if (value.envType() == null || value.envType() == envType) { + tmp.add(value); + } + } + + values = List.copyOf(tmp); + } else { + values = List.of(clazz.getEnumConstants()); + } + + for (final var path : paths) { + boolean found = false; + if (Files.isDirectory(path)) { + for (final var lib : values) { + for (final var lpath : lib.paths()) + if (Files.exists(path.resolve(lpath))) { + found = true; + final var old = origins.putIfAbsent(lib, path); + if (old != null) { + Log.warn(LogCategory.GENERAL, "Found %s for %s but %s is already present!", path, lib, old); + } + } + } + } else if (Files.isRegularFile(path)) { + try (final var zipfile = new ZipFile(path.toFile())) { + for (final var lib : values) { + for (final var lpath : lib.paths()) + if (zipfile.getEntry(lpath) != null) { + found = true; + final var old = origins.putIfAbsent(lib, path); + if (old != null) { + Log.warn(LogCategory.GENERAL, "Found %s for %s but %s is already present!", path, lib, old); + } + } + } + } + } + if (!found) { + unmatched.add(path); + } + } + } + + public Path getOrigin(T library) { + return origins.get(library); + } + + public Set getUnmatched() { + return unmatched; + } + + public String getClassName(T library) { + if (!origins.containsKey(library)) { + return null; + } + + final var path = library.path(); + + if (path.endsWith(".class")) { + return path.substring(0, path.length() - 6).replace('/', '.'); + } + + return null; + } + + public boolean is(Path path, T library) { + return origins.get(library).equals(path); + } + + public boolean has(T library) { + return origins.containsKey(library); + } +} diff --git a/modules/patch/src/main/java/dev/proxyfox/patch/FakeKnot.kt b/modules/patch/src/main/java/dev/proxyfox/patch/FakeKnot.kt new file mode 100644 index 00000000..21daad23 --- /dev/null +++ b/modules/patch/src/main/java/dev/proxyfox/patch/FakeKnot.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +@file:JvmName("FakeKnot") + +package dev.proxyfox.patch + +import org.quiltmc.loader.impl.launch.knot.KnotServer + +// Created 2023-05-01T20:11:34 + +/** + * @author KJP12 + * @since 2.0.8 + **/ + +fun main(args: Array) = KnotServer.main(args) \ No newline at end of file diff --git a/modules/patch/src/main/java/dev/proxyfox/patch/Library.kt b/modules/patch/src/main/java/dev/proxyfox/patch/Library.kt new file mode 100644 index 00000000..ddcc8485 --- /dev/null +++ b/modules/patch/src/main/java/dev/proxyfox/patch/Library.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package dev.proxyfox.patch + +import net.fabricmc.api.EnvType + +// Created 2023-03-01T01:43:20 +/** + * @author KJP12 + * @since 2.0.8 + */ +interface Library { + fun envType(): EnvType? + fun paths(): List + fun path(): String? = paths()[0] +} \ No newline at end of file diff --git a/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxLibrary.kt b/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxLibrary.kt new file mode 100644 index 00000000..2c1109b0 --- /dev/null +++ b/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxLibrary.kt @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package dev.proxyfox.patch + +import net.fabricmc.api.EnvType + +// Created 2023-03-01T02:42:57 +/** + * @author KJP12 + * @since 2.0.8 + */ +enum class ProxyFoxLibrary : Library { + PROXY_FOX("dev/proxyfox/bot/BotMainKt.class"), + QUILT_LIBRARIES( + "org/quiltmc/loader/impl/launch/knot/Knot.class", + "net/fabricmc/loader/launch/server/FabricServerLauncher.class", + "org/quiltmc/json5/JsonReader.class", + "org/quiltmc/config/api/Config.class", + "org/objectweb/asm/util/ASMifier.class", + "org/objectweb/asm/tree/ClassNode.class", + "org/objectweb/asm/commons/JSRInlinerAdapter.class", + "org/objectweb/asm/tree/analysis/Frame.class", + "org/objectweb/asm/Opcodes.class", + "net/fabricmc/accesswidener/AccessWidener.class", + "org/spongepowered/asm/mixin/Debug.class", + "net/fabricmc/tinyremapper/AsmRemapper.class", + "net/fabricmc/mapping/tree/TinyTree.class" + ); + + private val envType: EnvType? + private val paths: List + + constructor(envType: EnvType, vararg paths: String) { + this.envType = envType + this.paths = java.util.List.of(*paths) + } + + constructor(vararg paths: String) { + envType = null + this.paths = java.util.List.of(*paths) + } + + override fun envType(): EnvType? { + return envType + } + + override fun paths(): List { + return paths + } +} \ No newline at end of file diff --git a/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxProvider.kt b/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxProvider.kt new file mode 100644 index 00000000..a7c64b46 --- /dev/null +++ b/modules/patch/src/main/java/dev/proxyfox/patch/ProxyFoxProvider.kt @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2023, The ProxyFox Group + * + * This Source Code is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package dev.proxyfox.patch + +import org.quiltmc.loader.api.Version +import org.quiltmc.loader.impl.FormattedException +import org.quiltmc.loader.impl.QuiltLoaderImpl +import org.quiltmc.loader.impl.entrypoint.GameTransformer +import org.quiltmc.loader.impl.game.GameProvider +import org.quiltmc.loader.impl.game.GameProvider.BuiltinMod +import org.quiltmc.loader.impl.launch.common.QuiltLauncher +import org.quiltmc.loader.impl.metadata.qmj.V1ModMetadataBuilder +import org.quiltmc.loader.impl.util.Arguments +import org.quiltmc.loader.impl.util.ExceptionUtil +import org.quiltmc.loader.impl.util.SystemProperties +import org.quiltmc.loader.impl.util.log.ConsoleLogHandler +import org.quiltmc.loader.impl.util.log.Log +import java.io.File +import java.io.IOException +import java.lang.reflect.InvocationTargetException +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.collections.List +import kotlin.collections.Set +import kotlin.io.path.notExists +import java.util.List as JList +import java.util.Set as JSet + +// Created 2023-05-01T19:51:55 + +/** + * Partial copy of the Minecraft [GameProvider] adapted for use with ProxyFox. + * + * @author KJP12 + * @since 2.0.8 + **/ +class ProxyFoxProvider : GameProvider { + private val TRANSFORMER = GameTransformer() + + private var arguments: Arguments? = null + private var entrypoint: String? = null + private var appJar: Path? = null + private var miscJars: Set? = null + + override fun getGameId() = "proxyfox" + + override fun getGameName() = "ProxyFox" + + override fun getRawGameVersion() = "0.0.0" + + override fun getNormalizedGameVersion() = "0.0.0" + + override fun getBuiltinMods(): Set = JSet.of(BuiltinMod(JList.of(appJar), V1ModMetadataBuilder().apply { + id = gameId + group = "builtin" + version = Version.of(normalizedGameVersion) + name = gameName + }.build())) + + override fun getEntrypoint() = entrypoint + + override fun getLaunchDirectory() = Path.of(".") + + override fun isObfuscated() = false + + override fun requiresUrlClassLoader() = false + + override fun isEnabled() = true + + override fun locateGame(launcher: QuiltLauncher, args: Array): Boolean { + this.arguments = Arguments().apply { parse(args) } + + try { + val jar = System.getProperty(SystemProperties.GAME_JAR_PATH) + val lookupPaths: List = if (jar != null) { + val path = Paths.get(jar).toAbsolutePath().normalize() + + if (path.notExists()) { + throw RuntimeException("ProxyFox jar $path configured through ${SystemProperties.GAME_JAR_PATH} system property doesn't exist!") + } + + + ArrayList(launcher.classPath.size + 1).apply { + add(path) + addAll(launcher.classPath) + } + } else { + launcher.classPath + } + + val classifier = BudgetLibClassifier(ProxyFoxLibrary::class.java) + classifier.process(lookupPaths, launcher.environmentType) + + appJar = classifier.getOrigin(ProxyFoxLibrary.PROXY_FOX) ?: return false + + miscJars = classifier.unmatched + + entrypoint = classifier.getClassName(ProxyFoxLibrary.PROXY_FOX) + } catch (e: IOException) { + throw ExceptionUtil.wrap(e) + } + + QuiltLoaderImpl.INSTANCE.objectShare.put("fabric-loader:inputGameJar", appJar) + + return true + } + + override fun initialize(launcher: QuiltLauncher?) { + // Ideally, use SLF4J, but we currently don't test for that. + Log.init(ConsoleLogHandler(), true) + + val path = Files.createTempFile("Hacky-Classpath", ".txt").toAbsolutePath() + val builder = StringBuilder("$appJar${File.pathSeparatorChar}") + miscJars?.forEach { builder.append(it).append(File.pathSeparatorChar) } + Files.writeString(path, builder) + + // This is honestly stupidly hacky. + // There's no other workaround available that I can see, so + // you will have to deal with this. + System.setProperty(SystemProperties.REMAP_CLASSPATH_FILE, path.toString()) + + TRANSFORMER.locateEntrypoints(launcher, appJar) + } + + override fun getEntrypointTransformer() = TRANSFORMER + + override fun unlockClassPath(launcher: QuiltLauncher) { + launcher.addToClassPath(appJar) + miscJars?.forEach(launcher::addToClassPath) + } + + override fun launch(loader: ClassLoader) { + try { + val c = loader.loadClass(entrypoint) + val m = c.getMethod("main", Array::class.java) + val args: Any = arguments?.toArray() ?: throw NullPointerException("arguments") + m.invoke(null, args) + } catch (e: InvocationTargetException) { + throw FormattedException("ProxyFox has crashed", e.cause) + } catch (e: ReflectiveOperationException) { + throw FormattedException("Failed to start ProxyFox", e) + } + } + + override fun getArguments() = arguments + + override fun getLaunchArguments(sanitize: Boolean): Array = arguments?.toArray() ?: emptyArray() +} \ No newline at end of file diff --git a/modules/patch/src/main/resources/META-INF/services/org.quiltmc.loader.impl.game.GameProvider b/modules/patch/src/main/resources/META-INF/services/org.quiltmc.loader.impl.game.GameProvider new file mode 100644 index 00000000..cba7238e --- /dev/null +++ b/modules/patch/src/main/resources/META-INF/services/org.quiltmc.loader.impl.game.GameProvider @@ -0,0 +1 @@ +dev.proxyfox.patch.ProxyFoxProvider \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 0f092266..be8b3d1d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -24,4 +24,5 @@ include(":modules:common") include(":modules:database") include(":modules:conversion") include(":modules:api") -include(":modules:api:server") \ No newline at end of file +include(":modules:api:server") +include(":modules:patch") \ No newline at end of file