add pubkey auth

This commit is contained in:
Sen 2025-05-29 03:04:44 +02:00
parent bdf67a89f7
commit 5a394749bf
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
24 changed files with 822 additions and 107 deletions

View file

@ -121,9 +121,9 @@ public final class Server implements IThreadListener {
private final List<Dimension> unload = Lists.<Dimension>newArrayList();
private final Map<String, Position> warps = Maps.<String, Position>newTreeMap();
private final CommandEnvironment scriptEnv = new CommandEnvironment(this);
private final KeyPair keyPair;
private final boolean debug;
private KeyPair keyPair;
private WorldServer space;
private ChannelFuture endpoint;
@ -169,7 +169,7 @@ public final class Server implements IThreadListener {
Log.flushLog();
}
public static void saveServerConfig(long time) {
public static void saveServerConfig(long time, Server server) {
TagObject data = new TagObject();
data.setLong("Time", time);
data.setLong("LastAccess", System.currentTimeMillis());
@ -182,6 +182,10 @@ public final class Server implements IThreadListener {
}
data.setObject("Config", cfg);
data.setObject("Universe", UniverseRegistry.toTags());
if(server != null) {
data.setByteArray("PrivateKey", server.getPrivateKey().getEncoded());
data.setByteArray("PublicKey", server.getPublicKey().getEncoded());
}
File nfile = new File("server.cdt.tmp");
File lfile = new File("server.cdt");
try {
@ -195,7 +199,7 @@ public final class Server implements IThreadListener {
}
}
public static long loadServerConfig() {
public long loadServerConfig() {
Config.clear();
UniverseRegistry.clear();
File file = new File("server.cdt");
@ -216,6 +220,12 @@ public final class Server implements IThreadListener {
Log.IO.info("Version: %s", version);
Log.IO.info("Weltzeit: %d Ticks / %d Sekunden", time, time / 20L);
Log.IO.info("Zuletzt geladen: %s", new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date(lastPlayed)));
if(tag.hasByteArray("PrivateKey") && tag.hasByteArray("PublicKey")) {
PrivateKey key = EncryptUtil.decodePrivateKey(tag.getByteArray("PrivateKey"));
PublicKey pubkey = EncryptUtil.decodePublicKey(tag.getByteArray("PublicKey"));
if(key != null && pubkey != null)
this.keyPair = new KeyPair(pubkey, key);
}
return time;
}
catch(Exception e) {
@ -258,7 +268,6 @@ public final class Server implements IThreadListener {
}
}
}, "password");
this.keyPair = EncryptUtil.generateKeyPair();
}
public CommandEnvironment getScriptEnvironment() {
@ -283,7 +292,7 @@ public final class Server implements IThreadListener {
public void saveWorldInfo() {
if(!this.debug) {
saveServerConfig(this.space.getDayTime());
saveServerConfig(this.space.getDayTime(), this);
WorldServer.saveWarps(this.warps);
}
}
@ -452,7 +461,11 @@ public final class Server implements IThreadListener {
public void run(long time) {
if(!this.debug) {
Converter.convert();
long wtime = loadServerConfig();
long wtime = this.loadServerConfig();
if(this.keyPair == null) {
Log.SYSTEM.info("Generiere neues Schlüsselpaar");
this.keyPair = EncryptUtil.generateKeyPair();
}
// if(dtime == -1L) // {
// dtime = World.START_TIME;
//// Config.set("spawnDim", "1", null);
@ -485,6 +498,8 @@ public final class Server implements IThreadListener {
// }
}
else {
Log.SYSTEM.info("Generiere temporäres Schlüsselpaar");
this.keyPair = EncryptUtil.generateKeyPair();
Config.clear();
UniverseRegistry.clear();
Config.set("daylightCycle", "false", false);
@ -831,29 +846,41 @@ public final class Server implements IThreadListener {
radius > 0 ? 0.0f : Config.spawnPitch, world.dimension.getDimensionId());
}
public String addPlayer(NetConnection connection, String loginUser, String loginPass) {
public String addPlayer(NetConnection connection, String loginUser, String loginPass, PublicKey loginKey) {
TagObject tag = this.readPlayer(loginUser);
Player conn = new Player(this, connection, loginUser);
if(tag != null)
conn.readTags(tag);
if(Config.authenticate) {
if(conn.getPassword() == null && conn.getPubkey() == null) {
if(tag != null)
return loginKey != null ? "Falscher Pubkey" : "Falsches Passwort";
if(!Config.register)
return "Anmeldung neuer Accounts ist auf diesem Server deaktiviert (Whitelisted)";
if(Config.playerLimit > 0 && this.players.size() >= Config.playerLimit)
return String.format("Der Server ist voll (%d/%d)!", this.players.size(), Config.playerLimit);
if(loginKey != null) {
conn.setPubkey(loginKey);
Log.NETWORK.info(loginUser + " registrierte sich mit Pubkey");
}
else {
if(loginPass == null || loginPass.length() == 0)
return "Ein neues Passwort ist erforderlich um diesen Server zu betreten (mindestens " + Config.minPassLength + " Zeichen)";
if(loginPass.length() < Config.minPassLength)
return "Passwort ist zu kurz, mindestens " + Config.minPassLength + " Zeichen";
conn.setPassword(loginPass);
Log.NETWORK.info(loginUser + " registrierte sich mit Passwort");
}
}
else if(conn.getPubkey() != null ? !conn.getPubkey().equals(loginKey) : !conn.getPassword().equals(loginPass)) {
return loginKey != null ? "Falscher Pubkey" : "Falsches Passwort";
}
else {
Log.NETWORK.info(loginUser + " loggte sich mit " + (loginKey != null ? "Pubkey" : "Passwort") + " ein");
}
}
if(Config.playerLimit > 0 && this.players.size() >= Config.playerLimit && !conn.isAdmin())
return String.format("Der Server ist voll (%d/%d)!", this.players.size(), Config.playerLimit);
if(conn.getPassword() == null) {
if(!Config.register)
return "Anmeldung neuer Accounts ist auf diesem Server deaktiviert (Whitelisted)";
if(loginPass.length() == 0)
return "Ein neues Passwort ist erforderlich um diesen Server zu betreten (mindestens " + Config.minPassLength + " Zeichen)";
if(loginPass.length() < Config.minPassLength)
return "Passwort ist zu kurz, mindestens " + Config.minPassLength + " Zeichen";
conn.setPassword(loginPass);
Log.NETWORK.info(loginUser + " registrierte sich mit Passwort");
}
else if(!conn.getPassword().equals(loginPass)) {
return "Falsches Passwort";
}
else {
Log.NETWORK.info(loginUser + " loggte sich mit Passwort ein");
}
if(Config.compression >= 0) {
connection.sendPacket(new RPacketEnableCompression(Config.compression), new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {