Permalink
Browse files

Added reconnect handlers

  • Loading branch information...
1 parent 96c03d5 commit 80be9838e7a4a51dce2bf5ec761632a2f9a6aef9 @Raphfrk committed Apr 18, 2012
@@ -5,6 +5,7 @@
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
+import com.raphfrk.craftproxylib.handler.ConnectionHandler;
import com.raphfrk.craftproxylib.handler.PacketHandler;
import com.raphfrk.craftproxylib.handler.PacketHandlerRegistry;
import com.raphfrk.craftproxylib.packet.Packet;
@@ -18,6 +19,7 @@
private final MCPassthroughThread clientToServerThread;
private final PacketHandler[] upstream;
private final PacketHandler[] downstream;
+ private final ConnectionHandler connectionHandler;
public MCBridge(Socket server, Socket client) throws IOException {
this(new MCSocket(server, true), new MCSocket(client, false));
@@ -32,24 +34,34 @@ public MCBridge(MCSocket server, MCSocket client) {
}
public MCBridge(MCSocket server, MCSocket client, PacketHandlerRegistry registry) {
- this(server, client, registry.getUpstreamHandlers(), registry.getDownstreamHandlers());
+ this(server, client, registry, null);
+ }
+
+ public MCBridge(MCSocket server, MCSocket client, PacketHandlerRegistry registry, ConnectionHandler connectionHandler) {
+ this(server, client, registry.getUpstreamHandlers(), registry.getDownstreamHandlers(), connectionHandler);
}
public MCBridge(MCSocket server, MCSocket client, MCBridge bridge) {
- this(server, client, bridge.getUpstreamHandlers(), bridge.getDownstreamHandlers());
+ this(server, client, bridge, null);
+ }
+
+ public MCBridge(MCSocket server, MCSocket client, MCBridge bridge, ConnectionHandler connectionHandler) {
+ this(server, client, bridge.getUpstreamHandlers(), bridge.getDownstreamHandlers(), connectionHandler);
}
- public MCBridge(MCSocket server, MCSocket client, PacketHandler[] upstream, PacketHandler[] downstream) {
+ public MCBridge(MCSocket server, MCSocket client, PacketHandler[] upstream, PacketHandler[] downstream, ConnectionHandler connectionHandler) {
AtomicBoolean closeSync = new AtomicBoolean(false);
this.upstream = createNewInstances(upstream);
this.downstream = createNewInstances(downstream);
- this.serverToClientThread = new MCPassthroughThread(server, client, closeSync, downstream);
- this.clientToServerThread = new MCPassthroughThread(client, server, closeSync, upstream);
+ this.serverToClientThread = new MCPassthroughThread(server, client, closeSync, this.downstream);
+ this.clientToServerThread = new MCPassthroughThread(client, server, closeSync, this.upstream);
this.serverToClientThread.setReturnThread(clientToServerThread);
this.clientToServerThread.setReturnThread(serverToClientThread);
+
+ this.connectionHandler = connectionHandler;
}
/**
@@ -113,12 +125,20 @@ public void join(long millis) throws InterruptedException {
return downstream;
}
+ public void log(String message) {
+ if (connectionHandler != null) {
+ connectionHandler.log(message);
+ } else {
+ CraftProxyLib.log(message);
+ }
+ }
+
private PacketHandler[] createNewInstances(PacketHandler[] handlers) {
PacketHandler[] newHandlers = new PacketHandler[256];
if (handlers != null) {
for (int i = 0; i < 256; i++) {
if (handlers[i] != null) {
- newHandlers[i] = handlers[i].newInstance();
+ newHandlers[i] = handlers[i].newInstance(this);
}
}
}
@@ -31,19 +31,21 @@
private final InetAddress address;
private final int port;
private final long floodTimeout;
+ private final boolean rotateIP;
private final AtomicInteger index = new AtomicInteger(1);
- public MCServerListener(int port, PacketHandlerRegistry registry, ConnectionHandler connectionHandler) throws IOException {
- this(null, port, FLOOD_DEFAULT, registry, connectionHandler);
+ public MCServerListener(int port, PacketHandlerRegistry registry, ConnectionHandler connectionHandler, boolean rotateIP) throws IOException {
+ this(null, port, FLOOD_DEFAULT, registry, connectionHandler, rotateIP);
}
- public MCServerListener(InetAddress address, int port, long floodTimeout, PacketHandlerRegistry registry, ConnectionHandler connectionHandler) throws IOException {
+ public MCServerListener(InetAddress address, int port, long floodTimeout, PacketHandlerRegistry registry, ConnectionHandler connectionHandler, boolean rotateIP) throws IOException {
this.floodTimeout = floodTimeout;
this.registry = registry;
this.connectionHandler = connectionHandler;
this.address = address;
this.port = port;
+ this.rotateIP = rotateIP;
serverSocket = new ServerSocket(port);
this.setName("Server Listener Thread-" + index.getAndIncrement());
}
@@ -144,6 +146,10 @@ public void addConnection(ConnectionHandler handler) {
connections.put(handler, Boolean.TRUE);
}
+ public int getActiveConnections() {
+ return connections.size();
+ }
+
private void safeCloseSocket(Socket s) {
if (s != null) {
try {
@@ -190,5 +196,9 @@ private boolean floodShield(Socket socket) {
}
return true;
}
+
+ public boolean getRotateIP() {
+ return rotateIP;
+ }
}
@@ -217,4 +217,22 @@ public InetAddress getInetAddress() {
return socket.getInetAddress();
}
+ public static String parseHostname(String target) {
+ String[] split = target.split(":");
+ return split[0];
+ }
+
+ public static int parsePort(String target) {
+ String[] split = target.split(":");
+ if (split.length < 2) {
+ return 25565;
+ } else {
+ try {
+ return Integer.parseInt(split[1]);
+ } catch (NumberFormatException nfe) {
+ return 25565;
+ }
+ }
+ }
+
}
@@ -226,6 +226,10 @@ private static void compressFieldMap() {
return compressedFieldMap[id];
}
+ public static Field[] getExpandedFields(int id) {
+ return expandedFieldMap[id];
+ }
+
public static int[] getEntityIdInfo(int i) {
return entityIdInfo[i];
}
@@ -4,6 +4,7 @@
import java.io.InputStream;
import com.raphfrk.craftproxylib.fields.FieldFixedLength;
+import com.raphfrk.craftproxylib.packet.Packet;
public class FieldShort extends FieldFixedLength {
@@ -4,6 +4,7 @@
import java.io.InputStream;
import com.raphfrk.craftproxylib.fields.Field;
+import com.raphfrk.craftproxylib.packet.Packet;
public class FieldString extends Field {
@@ -7,7 +7,6 @@
import java.net.Socket;
import java.util.Map;
-import com.raphfrk.craftproxylib.CraftProxyLib;
import com.raphfrk.craftproxylib.MCBridge;
import com.raphfrk.craftproxylib.MCServerListener;
import com.raphfrk.craftproxylib.login.LoginInfo;
@@ -29,16 +28,16 @@ public BasicConnectionHandler() {
this.defaultPort = 0;
}
- public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener) throws IOException {
- this(client, registry, listener, InetAddress.getLocalHost(), 25565);
+ public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, boolean rotateIP) throws IOException {
+ this(client, registry, listener, InetAddress.getLocalHost(), 25565, rotateIP);
}
- public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, InetAddress defaultAddress, int defaultPort) throws IOException {
- this(client, registry, listener, defaultAddress, defaultPort, false, null);
+ public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, InetAddress defaultAddress, int defaultPort, boolean rotateIP) throws IOException {
+ this(client, registry, listener, defaultAddress, defaultPort, false, null, rotateIP);
}
- public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, InetAddress defaultAddress, int defaultPort, boolean authenticate, Map<String, InetSocketAddress> connectMap) throws IOException {
- super(client, registry, listener);
+ public BasicConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, InetAddress defaultAddress, int defaultPort, boolean authenticate, Map<String, InetSocketAddress> connectMap, boolean rotateIP) throws IOException {
+ super(client, registry, listener, rotateIP);
this.defaultPort = defaultPort;
this.defaultAddress = defaultAddress;
this.authenticate = authenticate;
@@ -69,7 +68,7 @@ public void handleConnection() throws IOException {
* @throws IOException
*/
protected MCBridge bridgeConnections(LoginInfo info) throws IOException {
- MCBridge bridge = new MCBridge(mServer, mClient, registry);
+ MCBridge bridge = new MCBridge(mServer, mClient, registry, this);
bridge.start();
@@ -130,7 +129,7 @@ protected LoginInfo handleLogin() throws IOException {
@Override
public BasicConnectionHandler newInstance(Socket client, PacketHandlerRegistry registry, MCServerListener listener, ConnectionConfig config) throws IOException {
- return new BasicConnectionHandler(client, registry, listener);
+ return new BasicConnectionHandler(client, registry, listener, listener.getRotateIP());
}
}
@@ -31,20 +31,24 @@
protected final PacketHandlerRegistry registry;
+ protected final boolean rotateIP;
+
protected ConnectionHandler() {
this.client = null;
this.registry = PacketHandlerRegistry.nullRegistry;
this.listener = null;
+ this.rotateIP = false;
}
- public ConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener) throws IOException {
+ public ConnectionHandler(Socket client, PacketHandlerRegistry registry, MCServerListener listener, boolean rotateIP) throws IOException {
this.client = client;
if (registry == null) {
this.registry = PacketHandlerRegistry.nullRegistry;
} else {
this.registry = registry;
}
this.listener = listener;
+ this.rotateIP = rotateIP;
setName(client.getInetAddress().toString());
}
@@ -75,6 +79,12 @@ private final void endInternal() {
if (listener != null) {
listener.removeConnection(this);
}
+ int remaining = listener.getActiveConnections();
+ if (remaining == 1) {
+ log("Connection closed, " + listener.getActiveConnections() + " connection remains");
+ } else {
+ log("Connection closed, " + listener.getActiveConnections() + " connections remain");
+ }
}
}
@@ -88,6 +98,7 @@ private final void endInternal() {
* Safely ends the connection.
*/
public void end() throws IOException {
+
if (mClient != null && !mClient.isClosed()) {
mClient.close("CPLib Connection shut down");
}
@@ -108,7 +119,13 @@ public void end() throws IOException {
*/
public abstract ConnectionHandler newInstance(Socket client, PacketHandlerRegistry registry, MCServerListener listener, ConnectionConfig config) throws IOException;
- protected void log(String message) {
+
+ /**
+ * Logs a given message with a prefix for the connection.
+ *
+ * @param message
+ */
+ public void log(String message) {
CraftProxyLib.log(getName() + ": " + message);
}
@@ -2,12 +2,16 @@
import java.io.IOException;
+import com.raphfrk.craftproxylib.CraftProxyLib;
+import com.raphfrk.craftproxylib.MCBridge;
import com.raphfrk.craftproxylib.MCOutputStream;
import com.raphfrk.craftproxylib.packet.Packet;
public abstract class PacketHandler {
public final static Packet INTERRUPT = new Packet();
+
+ protected MCBridge bridge = null;
/**
* Handles a Packet.<br>
@@ -26,6 +30,31 @@ public Packet handle(Packet packet, MCOutputStream out, MCOutputStream ret) thro
return packet;
}
+ /**
+ * Creates a new instance of the packet handler for the given bridge
+ *
+ * @param bridge
+ * @return
+ */
+ public PacketHandler newInstance(MCBridge bridge) {
+ PacketHandler handler = newInstance();
+ handler.bridge = bridge;
+ return handler;
+ }
+
+ /**
+ * Creates a new instance of the packet handler
+ *
+ * @return
+ */
public abstract PacketHandler newInstance();
+ protected void log(String message) {
+ if (bridge != null) {
+ bridge.log(message);
+ } else {
+ CraftProxyLib.log(message);
+ }
+ }
+
}
Oops, something went wrong.

0 comments on commit 80be983

Please sign in to comment.