mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-20 11:06:46 +00:00
Merge pull request #177 from Wiewiogr/148-secret-key
Add secret key generator #148
This commit is contained in:
commit
e50bcdeab7
@ -1,12 +1,12 @@
|
|||||||
package net.simon987.server;
|
package net.simon987.server;
|
||||||
|
|
||||||
|
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.client.MongoCollection;
|
import com.mongodb.client.MongoCollection;
|
||||||
import com.mongodb.client.MongoCursor;
|
import com.mongodb.client.MongoCursor;
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import com.mongodb.client.model.ReplaceOptions;
|
import com.mongodb.client.model.ReplaceOptions;
|
||||||
import net.simon987.server.crypto.CryptoProvider;
|
import net.simon987.server.crypto.CryptoProvider;
|
||||||
|
import net.simon987.server.crypto.SecretKeyGenerator;
|
||||||
import net.simon987.server.event.GameEvent;
|
import net.simon987.server.event.GameEvent;
|
||||||
import net.simon987.server.event.GameEventDispatcher;
|
import net.simon987.server.event.GameEventDispatcher;
|
||||||
import net.simon987.server.event.TickEvent;
|
import net.simon987.server.event.TickEvent;
|
||||||
@ -54,6 +54,8 @@ public class GameServer implements Runnable {
|
|||||||
|
|
||||||
private GameRegistry gameRegistry;
|
private GameRegistry gameRegistry;
|
||||||
|
|
||||||
|
private String secretKey;
|
||||||
|
|
||||||
public GameServer() {
|
public GameServer() {
|
||||||
this.config = new ServerConfiguration("config.properties");
|
this.config = new ServerConfiguration("config.properties");
|
||||||
|
|
||||||
@ -76,6 +78,13 @@ public class GameServer implements Runnable {
|
|||||||
|
|
||||||
dayNightCycle = new DayNightCycle();
|
dayNightCycle = new DayNightCycle();
|
||||||
|
|
||||||
|
SecretKeyGenerator keyGenerator = new SecretKeyGenerator();
|
||||||
|
secretKey = config.getString("secret_key");
|
||||||
|
if (secretKey == null) {
|
||||||
|
secretKey = keyGenerator.generate();
|
||||||
|
config.setString("secret_key", secretKey);
|
||||||
|
}
|
||||||
|
|
||||||
//Load all plugins in plugins folder, if it doesn't exist, create it
|
//Load all plugins in plugins folder, if it doesn't exist, create it
|
||||||
File pluginDir = new File("plugins/");
|
File pluginDir = new File("plugins/");
|
||||||
File[] pluginDirListing = pluginDir.listFiles();
|
File[] pluginDirListing = pluginDir.listFiles();
|
||||||
@ -123,7 +132,7 @@ public class GameServer implements Runnable {
|
|||||||
return eventDispatcher;
|
return eventDispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CryptoProvider getCryptoProvider(){
|
public CryptoProvider getCryptoProvider() {
|
||||||
return cryptoProvider;
|
return cryptoProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,12 +185,10 @@ public class GameServer implements Runnable {
|
|||||||
user.getControlledUnit().getCpu().reset();
|
user.getControlledUnit().getCpu().reset();
|
||||||
int cost = user.getControlledUnit().getCpu().execute(timeout);
|
int cost = user.getControlledUnit().getCpu().execute(timeout);
|
||||||
user.getControlledUnit().spendEnergy(cost);
|
user.getControlledUnit().spendEnergy(cost);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogManager.LOGGER.severe("Error executing " + user.getUsername() + "'s code");
|
LogManager.LOGGER.severe("Error executing " + user.getUsername() + "'s code");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +250,7 @@ public class GameServer implements Runnable {
|
|||||||
public void save() {
|
public void save() {
|
||||||
|
|
||||||
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
|
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
|
||||||
try{
|
try {
|
||||||
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
|
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
|
||||||
ReplaceOptions updateOptions = new ReplaceOptions();
|
ReplaceOptions updateOptions = new ReplaceOptions();
|
||||||
updateOptions.upsert(true);
|
updateOptions.upsert(true);
|
||||||
@ -261,7 +268,7 @@ public class GameServer implements Runnable {
|
|||||||
worlds.replaceOne(new Document("_id", w.getId()), w.mongoSerialise(), updateOptions);
|
worlds.replaceOne(new Document("_id", w.getId()), w.mongoSerialise(), updateOptions);
|
||||||
|
|
||||||
//If the world should unload, it is removed from the Universe after having been saved.
|
//If the world should unload, it is removed from the Universe after having been saved.
|
||||||
if (w.shouldUnload()){
|
if (w.shouldUnload()) {
|
||||||
unloaded_worlds++;
|
unloaded_worlds++;
|
||||||
universe.removeWorld(w);
|
universe.removeWorld(w);
|
||||||
}
|
}
|
||||||
@ -312,4 +319,13 @@ public class GameServer implements Runnable {
|
|||||||
public GameRegistry getRegistry() {
|
public GameRegistry getRegistry() {
|
||||||
return gameRegistry;
|
return gameRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSecretKey() {
|
||||||
|
return secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecretKey(String secretKey) {
|
||||||
|
this.secretKey = secretKey;
|
||||||
|
config.setString("secret_key", secretKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package net.simon987.server.crypto;
|
||||||
|
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class SecretKeyGenerator {
|
||||||
|
|
||||||
|
private static final String KEY_GENERATION_ALGORITHM = "HmacSHA1";
|
||||||
|
private KeyGenerator keyGen;
|
||||||
|
|
||||||
|
public SecretKeyGenerator() {
|
||||||
|
try {
|
||||||
|
keyGen = KeyGenerator.getInstance(KEY_GENERATION_ALGORITHM);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new RuntimeException("Error creating Key generator", e);
|
||||||
|
}
|
||||||
|
keyGen.init(new SecureRandom(SecureRandom.getSeed(32)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String generate() {
|
||||||
|
SecretKey secretKey = keyGen.generateKey();
|
||||||
|
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
|
||||||
|
}
|
||||||
|
}
|
@ -83,3 +83,6 @@ electric_box_damage=5
|
|||||||
electric_box_energy_given=70
|
electric_box_energy_given=70
|
||||||
#RadioactiveObstacle
|
#RadioactiveObstacle
|
||||||
radioactive_obstacle_corruption_block_size=10
|
radioactive_obstacle_corruption_block_size=10
|
||||||
|
|
||||||
|
#SecretKey
|
||||||
|
secret_key=<your_secret_key>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user