Move encryption keys to a variable

This commit is contained in:
ME1312 2019-04-19 11:46:39 -04:00
parent 6debb5b605
commit 2c41c40213
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
11 changed files with 48 additions and 36 deletions

View File

@ -86,7 +86,7 @@ public class Client {
private void recievePacket(Value input) {
try {
YAMLSection data = subdata.getCipher().decrypt(subdata.plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
YAMLSection data = subdata.getCipher().decrypt(subdata.password, input);
for (PacketIn packet : SubDataServer.decodePacket(this, data)) {
boolean auth = authorized == null;
if (auth || packet instanceof PacketAuthorization) {
@ -134,7 +134,7 @@ public class Client {
public void sendPacket(PacketOut packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (!isClosed()) try {
out.packValue(subdata.getCipher().encrypt(subdata.plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), SubDataServer.encodePacket(this, packet)));
out.packValue(subdata.getCipher().encrypt(subdata.password, SubDataServer.encodePacket(this, packet)));
out.flush();
} catch (Throwable e) {
e.printStackTrace();

View File

@ -6,6 +6,7 @@ import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Network.Client;
import net.ME1312.SubServers.Bungee.Network.PacketIn;
import net.ME1312.SubServers.Bungee.Network.PacketOut;
import net.ME1312.SubServers.Bungee.Network.SubDataServer;
import net.ME1312.SubServers.Bungee.SubPlugin;
/**
@ -49,7 +50,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
@Override
public void execute(Client client, YAMLSection data) {
try {
if (data.getRawString("password").equals(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"))) {
if (data.getRawString("password").equals(Util.reflect(SubDataServer.class.getDeclaredField("password"), plugin.subdata))) {
client.authorize();
client.sendPacket(new PacketAuthorization(0, "Successfully Logged in"));
} else {

View File

@ -34,6 +34,7 @@ public final class SubDataServer {
private ServerSocket server;
private Cipher cipher;
protected SubPlugin plugin;
String password;
/**
* SubData Server Instance
@ -55,6 +56,7 @@ public final class SubDataServer {
}
if (UPnP.isUPnPAvailable() && plugin.config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-SubData", false)) UPnP.openPortTCP(port);
this.plugin = plugin;
this.password = plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password");
this.cipher = (cipher != null)?cipher:new Cipher() {
@Override
public String getName() {

View File

@ -15,16 +15,18 @@ import java.lang.reflect.Method;
public final class PacketAuthorization implements PacketIn, PacketOut {
private SubPlugin plugin;
private String password;
public PacketAuthorization(SubPlugin plugin) {
public PacketAuthorization(SubPlugin plugin, String password) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
this.password = password;
}
@Override
public YAMLSection generate() {
YAMLSection json = new YAMLSection();
json.set("password", plugin.config.get().getSection("Settings").getSection("SubData").getString("Password"));
json.set("password", password);
return json;
}

View File

@ -38,6 +38,7 @@ public final class SubDataClient {
private NamedContainer<Boolean, Socket> socket;
private String name;
private Cipher cipher;
private String password;
private SubPlugin plugin;
private LinkedList<NamedContainer<String, PacketOut>> queue;
@ -58,6 +59,7 @@ public final class SubDataClient {
this.name = (name == null || name.length() > 0)?name:null;
this.out = MessagePack.newDefaultPacker(socket.get().getOutputStream());
this.queue = new LinkedList<NamedContainer<String, PacketOut>>();
this.password = plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password");
this.cipher = (cipher != null)?cipher:new Cipher() {
@Override
public String getName() {
@ -77,7 +79,7 @@ public final class SubDataClient {
if (!defaults) loadDefaults();
loop();
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin)));
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin, password)));
}
private void init() {
@ -98,7 +100,7 @@ public final class SubDataClient {
} private void loadDefaults() {
defaults = true;
registerPacket(new PacketAuthorization(plugin), "SubData", "Authorization");
registerPacket(new PacketAuthorization(plugin, null), "SubData", "Authorization");
registerPacket(new PacketCommandServer(), "SubServers", "CommandServer");
registerPacket(new PacketCreateServer(), "SubServers", "CreateServer");
registerPacket(new PacketDownloadGroupInfo(), "SubServers", "DownloadGroupInfo");
@ -160,7 +162,7 @@ public final class SubDataClient {
private void recieve(Value input) {
try {
YAMLSection data = cipher.decrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
YAMLSection data = cipher.decrypt(password, input);
for (PacketIn packet : decodePacket(data)) {
if (plugin.isEnabled()) Bukkit.getScheduler().runTask(plugin, () -> {
try {
@ -332,7 +334,7 @@ public final class SubDataClient {
try {
YAMLSection data = encodePacket(packet.get());
if (packet.name() != null) data.set("f", packet.name());
out.packValue(getCipher().encrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), data));
out.packValue(getCipher().encrypt(password, data));
out.flush();
} catch (Throwable e) {
e.printStackTrace();

View File

@ -17,17 +17,18 @@ import java.lang.reflect.Method;
public final class PacketAuthorization implements PacketIn, PacketOut {
private SubPlugin plugin;
private Logger log = null;
private String password;
public PacketAuthorization(SubPlugin plugin) {
public PacketAuthorization(SubPlugin plugin, String password) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
this.password = password;
}
@Override
public YAMLSection generate() {
YAMLSection json = new YAMLSection();
json.set("password", plugin.config.get().getSection("Settings").getSection("SubData").getString("Password"));
json.set("password", password);
return json;
}

View File

@ -41,6 +41,7 @@ public final class SubDataClient {
private NamedContainer<Boolean, Socket> socket;
private String name;
private Cipher cipher;
private String password;
private SubPlugin plugin;
private LinkedList<NamedContainer<String, PacketOut>> queue;
@ -61,6 +62,7 @@ public final class SubDataClient {
this.name = (name == null || name.length() > 0)?name:null;
this.out = MessagePack.newDefaultPacker(socket.get().getOutputStream());
this.queue = new LinkedList<NamedContainer<String, PacketOut>>();
this.password = plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password");
this.cipher = (cipher != null)?cipher:new Cipher() {
@Override
public String getName() {
@ -80,7 +82,7 @@ public final class SubDataClient {
if (!defaults) loadDefaults();
loop();
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin)));
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin, password)));
}
private void init() {
@ -102,7 +104,7 @@ public final class SubDataClient {
defaults = true;
log = LoggerFactory.getLogger("SubData");
registerPacket(new PacketAuthorization(plugin), "SubData", "Authorization");
registerPacket(new PacketAuthorization(plugin, null), "SubData", "Authorization");
registerPacket(new PacketCommandServer(), "SubServers", "CommandServer");
registerPacket(new PacketCreateServer(), "SubServers", "CreateServer");
registerPacket(new PacketDownloadGroupInfo(), "SubServers", "DownloadGroupInfo");
@ -164,7 +166,7 @@ public final class SubDataClient {
private void recieve(Value input) {
try {
YAMLSection data = getCipher().decrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
YAMLSection data = getCipher().decrypt(password, input);
for (PacketIn packet : decodePacket(data)) {
Sponge.getScheduler().createTaskBuilder().execute(() -> {
try {
@ -336,7 +338,7 @@ public final class SubDataClient {
try {
YAMLSection data = encodePacket(packet.get());
if (packet.name() != null) data.set("f", packet.name());
out.packValue(getCipher().encrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), data));
out.packValue(getCipher().encrypt(password, data));
out.flush();
} catch (Throwable e) {
e.printStackTrace();

View File

@ -20,22 +20,18 @@ import java.lang.reflect.Method;
public final class PacketAuthorization implements PacketIn, PacketOut {
private ExHost host;
private Logger log = null;
private String password;
/**
* New PacketAuthorization
*
* @param host SubServers.Host
*/
public PacketAuthorization(ExHost host) {
public PacketAuthorization(ExHost host, String password) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
this.password = password;
}
@Override
public YAMLSection generate() {
YAMLSection json = new YAMLSection();
json.set("password", host.config.get().getSection("Settings").getSection("SubData").getString("Password"));
json.set("password", password);
return json;
}

View File

@ -44,6 +44,7 @@ public final class SubDataClient {
private NamedContainer<Boolean, Socket> socket;
private String name;
private Cipher cipher;
private String password;
private ExHost host;
private LinkedList<NamedContainer<String, PacketOut>> queue;
@ -64,6 +65,7 @@ public final class SubDataClient {
this.name = name;
this.out = MessagePack.newDefaultPacker(socket.get().getOutputStream());
this.queue = new LinkedList<NamedContainer<String, PacketOut>>();
this.password = host.config.get().getSection("Settings").getSection("SubData").getRawString("Password");
this.cipher = (cipher != null)?cipher:new Cipher() {
@Override
public String getName() {
@ -83,7 +85,7 @@ public final class SubDataClient {
if (!defaults) loadDefaults();
loop();
sendPacket(new NamedContainer<>(null, new PacketAuthorization(host)));
sendPacket(new NamedContainer<>(null, new PacketAuthorization(host, password)));
}
private void init() {
@ -107,7 +109,7 @@ public final class SubDataClient {
defaults = true;
log = new Logger("SubData");
registerPacket(new PacketAuthorization(host), "SubData", "Authorization");
registerPacket(new PacketAuthorization(host, null), "SubData", "Authorization");
registerPacket(new PacketCommandServer(), "SubServers", "CommandServer");
registerPacket(new PacketCreateServer(), "SubServers", "CreateServer");
registerPacket(new PacketDownloadGroupInfo(), "SubServers", "DownloadGroupInfo");
@ -184,7 +186,7 @@ public final class SubDataClient {
private void recieve(Value input) {
try {
YAMLSection data = cipher.decrypt(host.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
YAMLSection data = cipher.decrypt(password, input);
for (PacketIn packet : decodePacket(data)) {
try {
packet.execute((data.contains("c"))?data.getSection("c"):null);
@ -354,7 +356,7 @@ public final class SubDataClient {
try {
YAMLSection data = encodePacket(packet.get());
if (packet.name() != null) data.set("f", packet.name());
out.packValue(getCipher().encrypt(host.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), data));
out.packValue(getCipher().encrypt(password, data));
out.flush();
} catch (Throwable e) {
log.error.println(e);

View File

@ -13,17 +13,19 @@ import java.lang.reflect.Method;
public final class PacketAuthorization implements PacketIn, PacketOut {
private SubPlugin plugin;
private String password;
public PacketAuthorization(SubPlugin plugin) {
public PacketAuthorization(SubPlugin plugin, String password) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
this.password = password;
}
@Override
public YAMLSection generate() {
YAMLSection data = new YAMLSection();
data.set("password", plugin.config.get().getSection("Settings").getSection("SubData").getString("Password"));
return data;
YAMLSection json = new YAMLSection();
json.set("password", password);
return json;
}
@Override

View File

@ -47,6 +47,7 @@ public final class SubDataClient {
private NamedContainer<Boolean, Socket> socket;
private String name = null;
private Cipher cipher;
private String password;
private SubPlugin plugin;
private LinkedList<NamedContainer<String, PacketOut>> queue;
@ -65,6 +66,7 @@ public final class SubDataClient {
this.plugin = plugin;
this.name = (name == null || name.length() > 0)?name:null;
this.out = MessagePack.newDefaultPacker(socket.get().getOutputStream());
this.password = plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password");
this.cipher = (cipher != null)?cipher:new Cipher() {
@Override
public String getName() {
@ -85,7 +87,7 @@ public final class SubDataClient {
if (!defaults) loadDefaults();
loop();
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin)));
sendPacket(new NamedContainer<>(null, new PacketAuthorization(plugin, password)));
}
private void init() {
@ -131,7 +133,7 @@ public final class SubDataClient {
} private void loadDefaults() {
defaults = true;
registerPacket(new PacketAuthorization(plugin), "SubData", "Authorization");
registerPacket(new PacketAuthorization(plugin, null), "SubData", "Authorization");
registerPacket(new PacketCommandServer(), "SubServers", "CommandServer");
registerPacket(new PacketCreateServer(), "SubServers", "CreateServer");
registerPacket(new PacketDownloadGroupInfo(), "SubServers", "DownloadGroupInfo");
@ -193,7 +195,7 @@ public final class SubDataClient {
private void recieve(Value input) {
try {
YAMLSection data = cipher.decrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
YAMLSection data = cipher.decrypt(password, input);
for (PacketIn packet : decodePacket(data)) {
try {
packet.execute((data.contains("c")) ? data.getSection("c") : null);
@ -374,7 +376,7 @@ public final class SubDataClient {
try {
YAMLSection data = encodePacket(packet.get());
if (packet.name() != null) data.set("f", packet.name());
out.packValue(getCipher().encrypt(plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), data));
out.packValue(getCipher().encrypt(password, data));
out.flush();
} catch (Throwable e) {
e.printStackTrace();