goauth

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

AuthStore.java (2595B)


      1 package cz.bain.plugins.goauth;
      2 
      3 import com.google.gson.Gson;
      4 import com.google.gson.reflect.TypeToken;
      5 import net.minecraft.network.ClientConnection;
      6 import net.minecraft.server.network.ServerPlayerEntity;
      7 import org.apache.logging.log4j.Logger;
      8 
      9 import java.io.*;
     10 import java.lang.reflect.Type;
     11 import java.nio.charset.StandardCharsets;
     12 import java.security.MessageDigest;
     13 import java.security.NoSuchAlgorithmException;
     14 import java.util.Base64;
     15 import java.util.HashMap;
     16 
     17 public class AuthStore {
     18     private final File file;
     19     private HashMap<String, String> entries = new HashMap<>();
     20     private final Logger logger;
     21 
     22     public AuthStore(Logger logger, File file) {
     23         this.file = file;
     24         this.logger = logger;
     25     }
     26 
     27     public void load() {
     28         if (!this.file.exists()) {
     29             save();
     30             return;
     31         }
     32         try (FileReader reader = new FileReader(this.file)) {
     33             Gson gson = new Gson();
     34             Type type = new TypeToken<HashMap<String, String>>(){}.getType();
     35             entries = gson.fromJson(reader, type);
     36         } catch (IOException e) {
     37             logger.error("IOException while loading auth db!");
     38         }
     39     }
     40 
     41     public void save() {
     42         try (FileWriter writer = new FileWriter(this.file)) {
     43             Gson gson = new Gson();
     44             gson.toJson(entries, writer);
     45         } catch (IOException e) {
     46             logger.error("IOException while saving auth db!");
     47         }
     48     }
     49 
     50     public String hash(String ip) {
     51         try {
     52             MessageDigest digest = MessageDigest.getInstance("SHA-256");
     53             byte[] hash = digest.digest(ip.getBytes(StandardCharsets.UTF_8));
     54             return Base64.getEncoder().encodeToString(hash);
     55         } catch (NoSuchAlgorithmException e) {
     56             e.printStackTrace();
     57             return "";
     58         }
     59     }
     60 
     61     public void put(ServerPlayerEntity player) {
     62         String username = player.getGameProfile().getName();
     63         String ipHash = hash(player.getIp());
     64         entries.put(username, ipHash);
     65         save();
     66     }
     67 
     68     public boolean remove(String name) {
     69         String val = entries.remove(name);
     70         if (val != null) save();
     71         return val != null;
     72     }
     73 
     74     public boolean allowed(ServerPlayerEntity player, ClientConnection connection) {
     75         String ipHash = entries.get(player.getGameProfile().getName());
     76         String ip = connection.getAddress().toString();
     77         ip = ip.substring(ip.indexOf("/") + 1);
     78         ip = ip.substring(0, ip.indexOf(":"));
     79         return ipHash != null && ipHash.equals(hash(ip));
     80     }
     81 
     82 }