goauth

Fabric plugin for enhanced whitelists
git clone git://git.bain.cz/goauth.git
Log | Files | Refs | README | LICENSE

Goauth.java (3358B)


      1 package cz.bain.plugins.goauth;
      2 
      3 import cz.bain.plugins.goauth.events.callbacks.OnPlayerConnectCallback;
      4 import cz.bain.plugins.goauth.events.callbacks.OnServerTickCallback;
      5 import cz.bain.plugins.goauth.events.listeners.OnPlayerConnectListener;
      6 import cz.bain.plugins.goauth.events.listeners.OnServerTickListener;
      7 import net.fabricmc.api.ModInitializer;
      8 import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
      9 import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
     10 import net.minecraft.server.network.ServerPlayerEntity;
     11 import org.apache.logging.log4j.LogManager;
     12 import org.apache.logging.log4j.Logger;
     13 
     14 import java.io.File;
     15 import java.io.IOException;
     16 import java.util.ArrayList;
     17 import java.util.HashMap;
     18 import java.util.List;
     19 
     20 
     21 public class Goauth implements ModInitializer {
     22 
     23     public final Logger logger = LogManager.getLogger("goauth");
     24     public final List<AuthUpdate> completed = new ArrayList<>();
     25     public final HashMap<String, ServerPlayerEntity> playersLimbo = new HashMap<>();
     26     public String authLink;
     27     public boolean running = true;
     28     public AuthStore authStore;
     29     public String authLinkText;
     30     public String welcomeText;
     31     public String reminderText;
     32 
     33     @Override
     34     public void onInitialize() {
     35         GoauthConfig config;
     36         try {
     37             config = new GoauthConfig(new File("goauth.properties"));
     38         } catch (IOException e) {
     39             this.logger.error(e.getMessage());
     40             return;
     41         }
     42         int port = Integer.parseInt(config.getRequired("port"));
     43         byte[] key;
     44         try {
     45             key = hexStringToByteArray(config.getRequired("key"));
     46         } catch (IllegalStateException e) {
     47             this.logger.error("Failed to parse communication key! Needs to be in hex.");
     48             return;
     49         }
     50         if (key.length != 32) {
     51             this.logger.error("Communication key is not 32 bytes long!");
     52             return;
     53         }
     54 
     55         this.welcomeText = config.get("welcome-text", "Welcome! Please authenticate: ");
     56         this.reminderText = config.get("reminder-text", "Please authenticate in chat");
     57         this.authLinkText = config.getRequired("auth-link-text");
     58         this.authLink = config.get("auth-link", "");
     59 
     60         this.authStore = new AuthStore(
     61                 this.logger,
     62                 new File(config.get("db-location", "authdb.json"))
     63         );
     64         this.authStore.load();
     65 
     66         OnPlayerConnectCallback.EVENT.register(new OnPlayerConnectListener(this));
     67         OnServerTickCallback.EVENT.register(new OnServerTickListener(this));
     68         ServerLifecycleEvents.SERVER_STOPPED.register(server -> running = false);
     69         CommandRegistrationCallback.EVENT.register(new CommandManager(this)::register);
     70         new Thread(new AuthUpdateRunner(key, this, port)).start();
     71         this.logger.info("Initialized!");
     72     }
     73 
     74     public static byte[] hexStringToByteArray(String s) throws IllegalStateException {
     75         int len = s.length();
     76         byte[] data = new byte[len / 2];
     77         for (int i = 0; i < len; i += 2) {
     78             int b1 = Character.digit(s.charAt(i), 16);
     79             int b2 = Character.digit(s.charAt(i+1), 16);
     80             if (b1 == -1 || b2 == -1) throw new IllegalStateException();
     81             data[i / 2] = (byte) ((b1 << 4) + b2);
     82         }
     83         return data;
     84     }
     85 }