mirror of
https://github.com/System-End/Discord-MC-Chat.git
synced 2026-04-19 22:05:11 +00:00
生成shared_secret
This commit is contained in:
parent
b150f4db11
commit
1394aba988
1 changed files with 20 additions and 0 deletions
|
|
@ -11,6 +11,8 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.xujiayao.discord_mc_chat.Constants.LOGGER;
|
||||
|
|
@ -58,6 +60,12 @@ public class ConfigManager {
|
|||
// This ensures the generated config file uses the user's system language if supported.
|
||||
template = template.replace("language: \"to_be_auto_replaced\"", StringUtils.format("language: \"{}\"", I18nManager.getLanguage()));
|
||||
|
||||
// If in standalone mode, generate a secure random shared secret
|
||||
if ("standalone".equals(expectedMode)) {
|
||||
String randomSecret = generateRandomSecret();
|
||||
template = template.replace("shared_secret: \"to_be_auto_replaced\"", StringUtils.format("shared_secret: \"{}\"", randomSecret));
|
||||
}
|
||||
|
||||
// Write the config file with the replaced language setting
|
||||
Files.writeString(CONFIG_FILE_PATH, template, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
|
@ -100,6 +108,18 @@ public class ConfigManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a cryptographically strong random string for the shared secret.
|
||||
*
|
||||
* @return A random 32-character string.
|
||||
*/
|
||||
private static String generateRandomSecret() {
|
||||
SecureRandom random = new SecureRandom();
|
||||
byte[] bytes = new byte[24]; // 24 bytes becomes 32 chars in Base64
|
||||
random.nextBytes(bytes);
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a specific configuration value as a JsonNode.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue