From 68e1445b1e222825fccde1acd77b6ce264ecc86d Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 5 Oct 2012 15:24:58 +1000 Subject: [PATCH] Everything seems to work except switching bridges. --- .../java/net/md_5/bungee/Configuration.java | 23 ++++++------- .../java/net/md_5/bungee/EncryptionUtil.java | 6 ++-- .../java/net/md_5/bungee/InitialHandler.java | 3 +- .../net/md_5/bungee/ServerConnection.java | 10 +++--- .../java/net/md_5/bungee/UserConnection.java | 32 +++++++++---------- .../md_5/bungee/command/CommandServer.java | 8 +++-- .../bungee/packet/PacketCDClientStatus.java | 2 +- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/md_5/bungee/Configuration.java b/src/main/java/net/md_5/bungee/Configuration.java index 7ab5316f..9ef17ab9 100644 --- a/src/main/java/net/md_5/bungee/Configuration.java +++ b/src/main/java/net/md_5/bungee/Configuration.java @@ -172,24 +172,21 @@ public class Configuration { } } - public InetSocketAddress getHostFor(String user, String requestedHost) { - String entry = user + ";" + requestedHost; - - String hostLine; + public String getServerFor(String user, String requestedHost) { + String server; if (forcedServers.containsKey(requestedHost)) { - hostLine = servers.get(forcedServers.get(requestedHost)); + server = servers.get(forcedServers.get(requestedHost)); } else { - hostLine = reconnectLocations.get(entry); + server = reconnectLocations.get(user); } - if (hostLine == null) { - hostLine = servers.get(defaultServerName); + if (server == null) { + server = servers.get(defaultServerName); } - return Util.getAddr(hostLine); + return server; } - public void setHostFor(UserConnection user, String host) { - String entry = user.username + ";" + Util.getAddr(user.handshake.host); - reconnectLocations.put(entry, host); + public void setHostFor(UserConnection user, String server) { + reconnectLocations.put(user.username, server); } public InetSocketAddress getServer(String name) { @@ -198,7 +195,7 @@ public class Configuration { if (server != null) { return Util.getAddr(server); } else { - return Util.getAddr(servers.get(defaultServerName)); + return getServer(null); } } diff --git a/src/main/java/net/md_5/bungee/EncryptionUtil.java b/src/main/java/net/md_5/bungee/EncryptionUtil.java index 1a031a8f..32cc48b6 100644 --- a/src/main/java/net/md_5/bungee/EncryptionUtil.java +++ b/src/main/java/net/md_5/bungee/EncryptionUtil.java @@ -57,7 +57,9 @@ public class EncryptionUtil { return new PacketFDEncryptionRequest(hash, pubKey, verify); } - public static SecretKey getSecret(PacketFCEncryptionResponse resp, PacketFDEncryptionRequest request) throws Exception { + public static SecretKey getSecret(PacketFCEncryptionResponse resp, PacketFDEncryptionRequest request) throws BadPaddingException, IllegalBlockSizeException, + IllegalStateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { + Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, keys.getPrivate()); byte[] decrypted = cipher.doFinal(resp.verifyToken); @@ -113,7 +115,7 @@ public class EncryptionUtil { return hasher.doFinal(b); } - public static byte[] getShared(SecretKey key, PublicKey pubkey) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { + public static byte[] getShared(SecretKey key, PublicKey pubkey) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubkey); return cipher.doFinal(key.getEncoded()); diff --git a/src/main/java/net/md_5/bungee/InitialHandler.java b/src/main/java/net/md_5/bungee/InitialHandler.java index 16856334..968b1f35 100644 --- a/src/main/java/net/md_5/bungee/InitialHandler.java +++ b/src/main/java/net/md_5/bungee/InitialHandler.java @@ -1,6 +1,5 @@ package net.md_5.bungee; -import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; @@ -53,7 +52,7 @@ public class InitialHandler implements Runnable { UserConnection userCon = new UserConnection(socket, in, out, handshake); userCon.register(); - userCon.connect(BungeeCord.instance.config.getHostFor(handshake.username, handshake.host)); + userCon.connect(BungeeCord.instance.config.getServerFor(handshake.username, handshake.host)); break; case 0xFE: throw new KickException(BungeeCord.instance.config.motd + ChatColor.COLOR_CHAR + BungeeCord.instance.connections.size() + ChatColor.COLOR_CHAR + BungeeCord.instance.config.maxPlayers); diff --git a/src/main/java/net/md_5/bungee/ServerConnection.java b/src/main/java/net/md_5/bungee/ServerConnection.java index c9cefe62..c5158b22 100644 --- a/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/src/main/java/net/md_5/bungee/ServerConnection.java @@ -17,14 +17,16 @@ import org.bouncycastle.crypto.io.CipherOutputStream; public class ServerConnection extends GenericConnection { + public final String name; public final Packet1Login loginPacket; - public ServerConnection(Socket socket, PacketInputStream in, OutputStream out, Packet1Login loginPacket) { + public ServerConnection(String name, Socket socket, PacketInputStream in, OutputStream out, Packet1Login loginPacket) { super(socket, in, out); + this.name = name; this.loginPacket = loginPacket; } - public static ServerConnection connect(InetSocketAddress address, Packet2Handshake handshake, boolean retry) { + public static ServerConnection connect(String name, InetSocketAddress address, Packet2Handshake handshake, boolean retry) { try { Socket socket = new Socket(); socket.connect(address, BungeeCord.instance.config.timeout); @@ -57,13 +59,13 @@ public class ServerConnection extends GenericConnection { } Packet1Login login = new Packet1Login(loginResponse); - return new ServerConnection(socket, in, out, login); + return new ServerConnection(name, socket, in, out, login); } catch (KickException ex) { throw ex; } catch (Exception ex) { InetSocketAddress def = BungeeCord.instance.config.getServer(null); if (retry && address != def) { - return connect(def, handshake, false); + return connect(name, def, handshake, false); } else { throw new RuntimeException("Could not connect to target server"); } diff --git a/src/main/java/net/md_5/bungee/UserConnection.java b/src/main/java/net/md_5/bungee/UserConnection.java index 27545722..4c7fed0c 100644 --- a/src/main/java/net/md_5/bungee/UserConnection.java +++ b/src/main/java/net/md_5/bungee/UserConnection.java @@ -37,19 +37,16 @@ public class UserConnection extends GenericConnection implements CommandSender { public void connect(String server) { InetSocketAddress addr = BungeeCord.instance.config.getServer(server); - if (addr == null) { - sendMessage(ChatColor.RED + "Specified server does not exist"); + if (addr.equals(curServer())) { + sendMessage(ChatColor.RED + "You are already on this server"); return; } - connect(addr); + connect(server, addr); } - public void connect(InetSocketAddress serverAddr) { + private void connect(String name, InetSocketAddress serverAddr) { try { - ServerConnection newServer = ServerConnection.connect(serverAddr, handshake, true); - if (downBridge != null) { - downBridge.interrupt(); - } + ServerConnection newServer = ServerConnection.connect(name, serverAddr, handshake, server == null); if (server == null) { clientEntityId = newServer.loginPacket.entityId; out.write(newServer.loginPacket.getPacket()); @@ -59,10 +56,10 @@ public class UserConnection extends GenericConnection implements CommandSender { server.disconnect("Quitting"); out.write(new Packet9Respawn((byte) 1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); out.write(new Packet9Respawn((byte) -1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); - out.write(new Packet46GameState((byte) 2, (byte) 0).getPacket()); Packet1Login login = newServer.loginPacket; serverEntityId = login.entityId; out.write(new Packet9Respawn(login.dimension, login.difficulty, login.gameMode, (short) 256, login.levelType).getPacket()); + out.write(new Packet46GameState((byte) 2, (byte) 0).getPacket()); if (heldItem != null) { newServer.out.write(heldItem.getPacket()); } @@ -85,20 +82,21 @@ public class UserConnection extends GenericConnection implements CommandSender { BungeeCord.instance.connections.put(username, this); } + private InetSocketAddress curServer() { + return (server == null) ? null : new InetSocketAddress(server.socket.getInetAddress(), server.socket.getPort()); + } + private void destory(String reason) { if (BungeeCord.instance.isRunning) { BungeeCord.instance.connections.remove(username); } - if (upBridge != null) { - upBridge.interrupt(); - } - if (downBridge != null) { - downBridge.interrupt(); - } disconnect(reason); if (server != null) { server.disconnect("Quitting"); } + if (server != null) { + BungeeCord.instance.config.setHostFor(this, server.name); + } } @Override @@ -114,7 +112,7 @@ public class UserConnection extends GenericConnection implements CommandSender { @Override public void run() { - while (!interrupted()) { + while (!socket.isClosed()) { try { byte[] packet = in.readPacket(); boolean sendPacket = true; @@ -151,7 +149,7 @@ public class UserConnection extends GenericConnection implements CommandSender { @Override public void run() { - while (!interrupted()) { + while (!server.socket.isClosed()) { try { byte[] packet = server.in.readPacket(); boolean sendPacket = true; diff --git a/src/main/java/net/md_5/bungee/command/CommandServer.java b/src/main/java/net/md_5/bungee/command/CommandServer.java index 87748d95..f22ff386 100644 --- a/src/main/java/net/md_5/bungee/command/CommandServer.java +++ b/src/main/java/net/md_5/bungee/command/CommandServer.java @@ -13,8 +13,8 @@ public class CommandServer extends Command { return; } UserConnection con = (UserConnection) sender; + Collection servers = BungeeCord.instance.config.servers.keySet(); if (args.length <= 0) { - Collection servers = BungeeCord.instance.config.servers.keySet(); StringBuilder serverList = new StringBuilder(); for (String server : servers) { serverList.append(server); @@ -24,7 +24,11 @@ public class CommandServer extends Command { con.sendMessage(ChatColor.GOLD + "You may connect to the following servers at this time: " + serverList.toString()); } else { String server = args[0]; - con.connect(server); + if (!servers.contains(server)) { + con.sendMessage(ChatColor.RED + "The specified server does not exist"); + } else { + con.connect(server); + } } } } diff --git a/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java b/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java index 0d64b998..8e846691 100644 --- a/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java +++ b/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java @@ -14,6 +14,6 @@ public class PacketCDClientStatus extends DefinedPacket { */ public PacketCDClientStatus(byte payload) { super(0xCD); - super.writeByte(payload); + writeByte(payload); } }