Launcher/libLauncher/src/main/java/ru/gravit/launcher/LauncherConfig.java

103 lines
4 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launcher;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
2018-09-17 10:20:34 +03:00
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper;
2018-09-17 10:07:32 +03:00
import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.stream.StreamObject;
public final class LauncherConfig extends StreamObject {
@LauncherAPI
public static final String ADDRESS_OVERRIDE_PROPERTY = "launcher.addressOverride";
@LauncherAPI
public static final String ADDRESS_OVERRIDE = System.getProperty(ADDRESS_OVERRIDE_PROPERTY, null);
private static final AutogenConfig config = new AutogenConfig();
2018-09-22 17:33:00 +03:00
2018-10-13 11:01:10 +03:00
2018-09-22 17:33:00 +03:00
public static AutogenConfig getAutogenConfig() {
2018-09-17 10:07:32 +03:00
return config;
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
// Instance
@LauncherAPI
public final InetSocketAddress address;
@LauncherAPI
public final String projectname;
public final int clientPort;
2018-10-08 16:57:29 +03:00
public String secretKeyClient;
@LauncherAPI
2018-09-17 10:07:32 +03:00
public final RSAPublicKey publicKey;
@LauncherAPI
public final Map<String, byte[]> runtime;
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
@LauncherAPI
public LauncherConfig(HInput input) throws IOException, InvalidKeySpecException {
String localAddress = config.address;
address = InetSocketAddress.createUnresolved(
ADDRESS_OVERRIDE == null ? localAddress : ADDRESS_OVERRIDE, config.port);
publicKey = SecurityHelper.toPublicRSAKey(input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH));
projectname = config.projectname;
clientPort = config.clientPort;
2018-10-08 16:57:29 +03:00
secretKeyClient = config.secretKeyClient;
2018-09-17 10:07:32 +03:00
// Read signed runtime
int count = input.readLength(0);
Map<String, byte[]> localResources = new HashMap<>(count);
for (int i = 0; i < count; i++) {
String name = input.readString(255);
VerifyHelper.putIfAbsent(localResources, name,
input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH),
String.format("Duplicate runtime resource: '%s'", name));
}
runtime = Collections.unmodifiableMap(localResources);
// Print warning if address override is enabled
if (ADDRESS_OVERRIDE != null)
2018-09-22 17:33:00 +03:00
LogHelper.warning("Address override is enabled: '%s'", ADDRESS_OVERRIDE);
2018-09-17 10:07:32 +03:00
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
@LauncherAPI
@SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
2018-09-22 17:33:00 +03:00
public LauncherConfig(String address, int port, RSAPublicKey publicKey, Map<String, byte[]> runtime, String projectname) {
this.address = InetSocketAddress.createUnresolved(address, port);
this.publicKey = Objects.requireNonNull(publicKey, "publicKey");
this.runtime = Collections.unmodifiableMap(new HashMap<>(runtime));
this.projectname = projectname;
this.clientPort = 32148;
}
2018-09-22 17:33:00 +03:00
@LauncherAPI
@SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
2018-09-17 10:07:32 +03:00
public LauncherConfig(String address, int port, RSAPublicKey publicKey, Map<String, byte[]> runtime) {
this.address = InetSocketAddress.createUnresolved(address, port);
this.publicKey = Objects.requireNonNull(publicKey, "publicKey");
this.runtime = Collections.unmodifiableMap(new HashMap<>(runtime));
this.projectname = "Minecraft";
this.clientPort = 32148;
2018-09-17 10:07:32 +03:00
}
@Override
public void write(HOutput output) throws IOException {
output.writeByteArray(publicKey.getEncoded(), SecurityHelper.CRYPTO_MAX_LENGTH);
// Write signed runtime
Set<Map.Entry<String, byte[]>> entrySet = runtime.entrySet();
output.writeLength(entrySet.size(), 0);
for (Map.Entry<String, byte[]> entry : runtime.entrySet()) {
output.writeString(entry.getKey(), 255);
output.writeByteArray(entry.getValue(), SecurityHelper.CRYPTO_MAX_LENGTH);
}
}
}