121 lines
2.9 KiB
Java
121 lines
2.9 KiB
Java
package server.network;
|
|
|
|
import java.io.File;
|
|
import java.security.PublicKey;
|
|
import java.util.Map;
|
|
|
|
import common.log.Log;
|
|
import common.network.IPlayer;
|
|
import common.tags.TagObject;
|
|
import common.util.EncryptUtil;
|
|
import common.util.Pair;
|
|
|
|
public class User {
|
|
private final String user;
|
|
private boolean admin;
|
|
private Pair<byte[], byte[]> password;
|
|
private PublicKey pubkey;
|
|
|
|
public User(String user) {
|
|
this.user = user;
|
|
}
|
|
|
|
public boolean isOnline() {
|
|
return false;
|
|
}
|
|
|
|
public String getUser() {
|
|
return this.user;
|
|
}
|
|
|
|
public void setAdmin(boolean admin) {
|
|
this.admin = admin;
|
|
}
|
|
|
|
public boolean isAdmin() {
|
|
return this.admin;
|
|
}
|
|
|
|
public void setPasswordHash(Pair<byte[], byte[]> hash) {
|
|
this.password = hash;
|
|
}
|
|
|
|
public Pair<byte[], byte[]> getPasswordHash() {
|
|
return this.password;
|
|
}
|
|
|
|
public void setPubkey(PublicKey key) {
|
|
this.pubkey = key;
|
|
}
|
|
|
|
public PublicKey getPubkey() {
|
|
return this.pubkey;
|
|
}
|
|
|
|
public void copyFrom(User user) {
|
|
this.admin = user.admin;
|
|
this.password = user.password;
|
|
this.pubkey = user.pubkey;
|
|
}
|
|
|
|
public void readUserTags(TagObject tag) {
|
|
this.admin = tag.getBool("admin");
|
|
if(tag.hasByteArray("passwordHash") && tag.hasByteArray("passwordSalt"))
|
|
this.password = new Pair<byte[], byte[]>(tag.getByteArray("passwordHash"), tag.getByteArray("passwordSalt"));
|
|
if(tag.hasByteArray("pubkey"))
|
|
this.pubkey = EncryptUtil.decodePublicKey(tag.getByteArray("pubkey"));
|
|
}
|
|
|
|
public void writeUserTags(TagObject tag) {
|
|
if(this.admin)
|
|
tag.setBool("admin", this.admin);
|
|
if(this.password != null) {
|
|
tag.setByteArray("passwordHash", this.password.first());
|
|
tag.setByteArray("passwordSalt", this.password.second());
|
|
}
|
|
if(this.pubkey != null)
|
|
tag.setByteArray("pubkey", this.pubkey.getEncoded());
|
|
}
|
|
|
|
public static void saveDatabase(Map<String, User> map) {
|
|
TagObject tag = new TagObject();
|
|
for(User user : map.values()) {
|
|
TagObject data = new TagObject();
|
|
user.writeUserTags(data);
|
|
tag.setObject(user.getUser(), data);
|
|
}
|
|
File nfile = new File("users.cdt.tmp");
|
|
File lfile = new File("users.cdt");
|
|
try {
|
|
TagObject.writeGZip(tag, nfile);
|
|
if(lfile.exists())
|
|
lfile.delete();
|
|
nfile.renameTo(lfile);
|
|
}
|
|
catch(Exception e) {
|
|
Log.IO.error(e, "Fehler beim Schreiben von " + nfile);
|
|
}
|
|
}
|
|
|
|
public static void loadDatabase(Map<String, User> map) {
|
|
File file = new File("users.cdt");
|
|
if(!file.exists())
|
|
file = new File("users.cdt.tmp");
|
|
if(file.exists()) {
|
|
try {
|
|
TagObject tag = TagObject.readGZip(file);
|
|
for(String key : tag.keySet()) {
|
|
if(IPlayer.isValidUser(key) && tag.hasObject(key)) {
|
|
User user = new User(key);
|
|
user.readUserTags(tag.getObject(key));
|
|
map.put(key, user);
|
|
}
|
|
}
|
|
}
|
|
catch(Exception e) {
|
|
Log.IO.error(e, "Fehler beim Lesen von " + file);
|
|
map.clear();
|
|
}
|
|
}
|
|
}
|
|
}
|