From 914ab071cda9943fb43d8544cd75d971f2e699cb Mon Sep 17 00:00:00 2001
From: mohammed jasem alaajel <34905970+ham1255@users.noreply.github.com>
Date: Mon, 14 Jun 2021 22:27:22 +0400
Subject: [PATCH] Added old commits from forked repo Fastlogin

commits: \nAuto-add - character for offline mode users\nFix empty database getting users marked as cracked falsely\nDon't save offline mode user to database\nalways use false to fix an error\nsome debug stuff...\nadd GovindasPreSecondAttemptEvent\nshade sqlite so we can stop using mariadb for fastlogin

diff --git a/bungee/pom.xml b/bungee/pom.xml
index 7cbdc04..eafeda5 100644
--- a/bungee/pom.xml
+++ b/bungee/pom.xml
@@ -176,5 +176,11 @@
             <optional>true</optional>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.xerial</groupId>
+            <artifactId>sqlite-jdbc</artifactId>
+            <version>3.34.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java
index 8777a60..fef86f5 100644
--- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java
+++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java
@@ -40,6 +40,7 @@ import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodHandles.Lookup;
 import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
 import java.util.UUID;
 
 import net.md_5.bungee.api.connection.PendingConnection;
@@ -157,6 +158,52 @@ public class ConnectListener implements Listener {
                 }
             }
         }
+        // LimeLogin start
+        //start of Govindas code to auto-add "-" character for offline mode users differentiation from online mode
+        else {
+            if (!connection.getName().contains("-")) {
+                InitialHandler initialHandler = (InitialHandler) loginEvent.getConnection();
+                //TODO add bedrock check of player and don't trigger this for bedrock users
+                LoginSession session = plugin.getSession().get(connection);
+                String username = connection.getName();
+
+                //for bedrock support, this opens up vulnerability, so cracked players with * can join without auth
+                //but that isn't a problem as we're handling * in LimeworkProxy instead
+                //but it would be better to handle it here, just need to debug it further
+
+                //debug, we can remove after
+                UUID checkUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + connection.getName()).getBytes(StandardCharsets.UTF_8));
+                System.out.println("Profile uuid: " + session.getProfile().getId());
+                System.out.println("Session uuid: " + session.getUuid());
+                System.out.println("Offline uuid: " + checkUUID);
+                if (username.contains("*")) {
+
+                    if (checkUUID.equals(session.getProfile().getId())) {
+                        System.out.println("IT EQUALS!");
+                    }
+                    return;
+                }
+                if (username.length() >= 16)  {
+                    username = username.substring(0, 15);
+                }
+
+                session.setVerifiedUsername("-" + username);
+                initialHandler.getLoginRequest().setData("-" + username);
+                UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + connection.getName()).getBytes(StandardCharsets.UTF_8));
+                plugin.getLog().info("Govindas system has converted username to contain - character: " + username);
+                try {
+                    uniqueIdSetter.invokeExact(initialHandler, uuid);
+                } catch (Throwable throwable) {
+                    throwable.printStackTrace();
+                }
+                session.getProfile().setPremium(false);
+                session.getProfile().setPlayerName(connection.getName());
+                session.setUuid(uuid);
+                session.getProfile().setId(uuid);
+            }
+        }
+        //end of Govindas code
+        // LimeLogin end
     }
 
     private void setOfflineId(InitialHandler connection, String username) {
diff --git a/bungee/src/main/resources/bungee.yml b/bungee/src/main/resources/bungee.yml
index c52f41a..1d1c8d9 100644
--- a/bungee/src/main/resources/bungee.yml
+++ b/bungee/src/main/resources/bungee.yml
@@ -3,10 +3,10 @@
 # This make it easy to combine BungeeCord and Bukkit support in one plugin
 name: ${project.parent.name}
 # ${-} will be automatically replaced by Maven
-main: ${project.groupId}.${project.artifactId}.${project.name}
+main: com.github.games647.fastlogin.bungee.FastLoginBungee
 
-version: ${project.version}-${git.commit.id.abbrev}
-author: games647, https://github.com/games647/FastLogin/graphs/contributors
+version: ${project.version}
+author: games647, https://github.com/games647/FastLogin/graphs/contributors, Limework.net
 
 softDepends:
     # BungeeCord auth plugins
diff --git a/core/pom.xml b/core/pom.xml
index 17535a4..46fc0a9 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -81,7 +81,7 @@
         <dependency>
             <groupId>net.md-5</groupId>
             <artifactId>bungeecord-config</artifactId>
-            <version>1.12-SNAPSHOT</version>
+            <version>1.16-R0.5-SNAPSHOT</version>
             <exclusions>
                 <exclusion>
                     <groupId>*</groupId>
@@ -90,6 +90,13 @@
             </exclusions>
         </dependency>
 
+        <dependency>
+            <groupId>net.md-5</groupId>
+            <artifactId>bungeecord-proxy</artifactId>
+            <version>1.16-R0.5-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+
         <!--Floodgate for Xbox Live Authentication-->
         <dependency>
             <groupId>org.geysermc.floodgate</groupId>
diff --git a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java
index 94500cb..316445c 100644
--- a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java
+++ b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java
@@ -142,9 +142,8 @@ public class AuthStorage {
                 + "`Name` VARCHAR(16) NOT NULL, "
                 + "`Premium` BOOLEAN NOT NULL, "
                 + "`LastIp` VARCHAR(255) NOT NULL, "
-                + "`LastLogin` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
+                + "`LastLogin` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAM"
                 //the premium shouldn't steal the cracked account by changing the name
-                + "UNIQUE (`Name`) "
                 + ')';
 
         if (dataSource.getJdbcUrl().contains("sqlite")) {
@@ -208,7 +207,13 @@ public class AuthStorage {
     public void save(StoredProfile playerProfile) {
         try (Connection con = dataSource.getConnection()) {
             String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
+            // LimeLogin start
 
+            //start of Govindas code
+            if (!playerProfile.getName().contains("-")) { playerProfile.setPremium(true); }
+            //end of Govindas code
+
+            // LimeLogin end
             playerProfile.getSaveLock().lock();
             try {
                 if (playerProfile.isSaved()) {
@@ -222,6 +227,13 @@ public class AuthStorage {
                         saveStmt.execute();
                     }
                 } else {
+                    // LimeLogin start
+
+                    //start of Govindas code
+                    if (!playerProfile.getName().contains("-")) { playerProfile.setPremium(true); }
+                    //end of Govindas code
+
+                    // LimeLogin end
                     try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
                         saveStmt.setString(1, uuid);
 
diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java
index 7efb157..feac10d 100644
--- a/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java
+++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java
@@ -92,7 +92,13 @@ public abstract class ForceLoginManagement<P extends C, C, L extends LoginSessio
                 //cracked player
                 playerProfile.setId(null);
                 playerProfile.setPremium(false);
-                storage.save(playerProfile);
+                // LimeLogin start
+
+                //Govindas comment - don't save cracked player profiles, I found it to be useless
+                //storage.save(playerProfile);
+                //end of Govindas comment
+
+                // LimeLogin end
             }
         } catch (Exception ex) {
             core.getPlugin().getLog().warn("ERROR ON FORCE LOGIN of {}", getName(player), ex);
diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java
index ec129a8..3f05090 100644
--- a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java
+++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java
@@ -34,6 +34,8 @@ import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent;
 
 import java.util.Optional;
 
+import com.github.games647.fastlogin.core.shared.event.GovindasPreSecondAttemptEvent;
+import net.md_5.bungee.api.ProxyServer;
 import org.geysermc.floodgate.api.player.FloodgatePlayer;
 
 import net.md_5.bungee.config.Configuration;
@@ -80,11 +82,16 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
                 }
             } else {
                 if (core.getPendingLogin().remove(ip + username) != null && config.get("secondAttemptCracked", false)) {
-                    core.getPlugin().getLog().info("Second attempt login -> cracked {}", username);
-
-                    //first login request failed so make a cracked session
-                    startCrackedSession(source, profile, username);
-                    return;
+                    // LimeLogin start
+                    GovindasPreSecondAttemptEvent event = new GovindasPreSecondAttemptEvent(ip, username);
+                    ProxyServer.getInstance().getPluginManager().callEvent(event);
+                    if (!event.isCancelled()) {
+                        core.getPlugin().getLog().info("Second attempt login -> cracked {}", username);
+                        //first login request failed so make a cracked session
+                        startCrackedSession(source, profile, username);
+                        return;
+                    }
+                    // LimeLogin start
                 }
 
                 Optional<Profile> premiumUUID = Optional.empty();
@@ -116,8 +123,17 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
 
     private boolean checkPremiumName(S source, String username, StoredProfile profile) throws Exception {
         core.getPlugin().getLog().info("GameProfile {} uses a premium username", username);
-        if (core.getConfig().get("autoRegister", false) && (authHook == null || !authHook.isRegistered(username))) {
-            requestPremiumLogin(source, profile, username, false);
+        // LimeLogin start
+
+        //start of Govindas comment, THIS IS NEEDED to be commented if FastLogin database gets emptied, to not get premium users counted as cracked
+        //if (core.getConfig().get("autoRegister", false) && (authHook == null || !authHook.isRegistered(username))) {
+        //end of Govindas comment
+
+        // LimeLogin end
+        if (core.getConfig().get("autoRegister", false) && (authHook == null)) {
+            // LimeLogin start
+            requestPremiumLogin(source, profile, username, false); //Comment: always use false to fix an error XD
+            // LimeLogin end
             return true;
         }
 
diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/event/GovindasPreSecondAttemptEvent.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/event/GovindasPreSecondAttemptEvent.java
new file mode 100644
index 0000000..5ac3cc9
--- /dev/null
+++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/event/GovindasPreSecondAttemptEvent.java
@@ -0,0 +1,36 @@
+// LimeLogin start
+package com.github.games647.fastlogin.core.shared.event;
+
+import net.md_5.bungee.api.plugin.Cancellable;
+import net.md_5.bungee.api.plugin.Event;
+
+public class GovindasPreSecondAttemptEvent extends Event implements Cancellable {
+
+    private final String ip;
+    private final String username;
+    private boolean cancelled;
+
+    public GovindasPreSecondAttemptEvent(String ip, String username) {
+        this.ip = ip;
+        this.username = username;
+    }
+    public String getIP() {
+        return ip;
+    }
+
+
+    public String getUsername() {
+        return username;
+    }
+
+    @Override
+    public boolean isCancelled() {
+        return cancelled;
+    }
+
+    @Override
+    public void setCancelled(boolean cancelled) {
+        this.cancelled = cancelled;
+    }
+}
+// LimeLogin end
\ No newline at end of file
diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml
index 8e04896..b495031 100644
--- a/core/src/main/resources/config.yml
+++ b/core/src/main/resources/config.yml
@@ -246,7 +246,7 @@ autoRegisterFloodgate: false
 # Recommended is the use of MariaDB (a better version of MySQL)
 
 # Single file SQLite database
-driver: 'org.sqlite.JDBC'
+driver: 'fastlogin.sqlite.JDBC'
 # File location
 database: '{pluginDir}/FastLogin.db'
 
-- 
2.25.1

