implemented KraSocketClient
parent
9b9c96b748
commit
e9683bc9a7
15
pom.xml
15
pom.xml
|
@ -68,6 +68,11 @@
|
|||
<groupId>org.mongodb</groupId>
|
||||
<version>3.12.10</version> <!-- 3.0.4 -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>earth.krakatoa</groupId>
|
||||
<artifactId>KraSocketClient</artifactId>
|
||||
<version>1.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -86,5 +91,15 @@
|
|||
</snapshots>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>krakatoa-earth</id>
|
||||
<url>https://repo.krakatoa.umbach.dev/repository/krakatoa-earth/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
|
@ -1,8 +1,11 @@
|
|||
package net.krakatoa.proxy;
|
||||
|
||||
import com.mongodb.async.client.MongoCollection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import earth.krakatao.KraSocketClient;
|
||||
import earth.krakatao.KraSocketClientConfig;
|
||||
import earth.krakatao.events.KraSocketClientEventInitiater;
|
||||
import earth.krakatao.events.KraSocketClientEventInterface;
|
||||
import earth.krakatao.protocol.KraSocketClientProtocol;
|
||||
import java.util.Arrays;
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
|
@ -10,9 +13,8 @@ import net.krakatoa.proxy.command.CodeCommand;
|
|||
import net.krakatoa.proxy.config.ConfigHandler;
|
||||
import net.krakatoa.proxy.listener.PlayerDisconnectListener;
|
||||
import net.krakatoa.proxy.listener.PostLoginListener;
|
||||
import net.krakatoa.proxy.listener.SocketClientMessageListener;
|
||||
import net.krakatoa.proxy.mongo.MongoManager;
|
||||
import net.krakatoa.proxy.protocol.KraProtocol;
|
||||
import net.krakatoa.proxy.socket.SocketClient;
|
||||
import net.krakatoa.proxy.util.Formatter;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.api.plugin.PluginManager;
|
||||
|
@ -26,9 +28,11 @@ public class ProxySystem extends Plugin {
|
|||
|
||||
private ConfigHandler configHandler;
|
||||
private MongoManager mongoManager;
|
||||
private SocketClient socketClient;
|
||||
private KraProtocol kraProtocol;
|
||||
private Formatter formatter;
|
||||
private KraSocketClientProtocol kraSocketClientProtocol;
|
||||
private KraSocketClient kraSocketClient;
|
||||
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||
private KraSocketClientEventInterface kraSocketClientEventInterface;
|
||||
|
||||
private MongoCollection<Document> players;
|
||||
|
||||
|
@ -37,11 +41,25 @@ public class ProxySystem extends Plugin {
|
|||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
configHandler = new ConfigHandler();
|
||||
configHandler.load();
|
||||
this.configHandler = new ConfigHandler();
|
||||
this.configHandler.load();
|
||||
|
||||
kraProtocol = new KraProtocol();
|
||||
formatter = new Formatter();
|
||||
this.formatter = new Formatter();
|
||||
|
||||
this.kraSocketClientProtocol = new KraSocketClientProtocol();
|
||||
|
||||
KraSocketClientConfig kraSocketClientConfig = new KraSocketClientConfig(
|
||||
this.configHandler.getWebSocketProtocol(), this.configHandler.getWebSocketHost(),
|
||||
this.configHandler.getWebSocketPort(), this.configHandler.getWebSocketAccessKey(),
|
||||
"proxy-1");
|
||||
|
||||
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
||||
this.kraSocketClientEventInterface = new SocketClientMessageListener();
|
||||
|
||||
this.kraSocketClient = new KraSocketClient(kraSocketClientConfig,
|
||||
this.kraSocketClientEventInterface, this.kraSocketClientProtocol);
|
||||
|
||||
this.kraSocketClient.getSocketClient().connect();
|
||||
|
||||
System.out.println(
|
||||
"mongo cfg " + configHandler.getMongodbHost() + " " + configHandler.getMongodbUsername());
|
||||
|
@ -51,17 +69,6 @@ public class ProxySystem extends Plugin {
|
|||
configHandler.getMongodbPassword());
|
||||
mongoManager.connect(configHandler.getMongodbDatabase());
|
||||
|
||||
try {
|
||||
socketClient = new SocketClient(
|
||||
new URI(configHandler.getWebSocketProtocol() + "://" + configHandler.getWebSocketHost()
|
||||
+ ":" + configHandler.getWebSocketPort() + "/ws?ak="
|
||||
+ configHandler.getWebSocketAccessKey() + "&s=proxy-1"));
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
socketClient.connect();
|
||||
|
||||
players = mongoManager.getMongoDatabase().getCollection("players");
|
||||
|
||||
loadListeners();
|
||||
|
@ -70,8 +77,8 @@ public class ProxySystem extends Plugin {
|
|||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
socketClient.getConnection().close();
|
||||
socketClient.getConnection().closeConnection(1, "");
|
||||
this.kraSocketClient.getSocketClient().getConnection().close();
|
||||
this.kraSocketClient.getSocketClient().getConnection().closeConnection(1, "");
|
||||
|
||||
instance = null;
|
||||
}
|
||||
|
|
|
@ -1,62 +1,35 @@
|
|||
package net.krakatoa.proxy.socket;
|
||||
package net.krakatoa.proxy.listener;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import earth.krakatao.events.KraSocketClientEventInterface;
|
||||
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||
import net.krakatoa.proxy.ProxySystem;
|
||||
import net.krakatoa.proxy.protocol.KraProtocolMessage;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
public class SocketClient extends WebSocketClient {
|
||||
|
||||
public SocketClient(URI uri) {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
public void SendMessage(byte[] message) {
|
||||
if (isOpen()) {
|
||||
send(message);
|
||||
}
|
||||
}
|
||||
public class SocketClientMessageListener implements KraSocketClientEventInterface {
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake serverHandshake) {
|
||||
System.out.println("Socket connection opened!");
|
||||
|
||||
//ProxySystem.getInstance().getKraProtocol()
|
||||
// .SendMessage(1, 126375, 248, "myuui123asdrsdasadsdasdadsadsdsa", 65017, "player joined");
|
||||
|
||||
for (ProxiedPlayer proxiedPlayer : ProxyServer.getInstance().getPlayers()) {
|
||||
proxiedPlayer.sendMessage(new TextComponent("§cSocket §8» §aConnected"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
System.out.println("recv s: " + s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(ByteBuffer byteBuffer) {
|
||||
System.out.println("recv b: " + Arrays.toString(byteBuffer.array()));
|
||||
|
||||
KraProtocolMessage kraProtocolMessage = ProxySystem.getInstance().getKraProtocol()
|
||||
.DecodeMessage(byteBuffer.array());
|
||||
public void onMessage(KraSocketClientProtocolMessage kraProtocolMessage) {
|
||||
|
||||
System.out.println(
|
||||
"kraProtocolMessage " + kraProtocolMessage.getCmdID() + " " + kraProtocolMessage.getArgs());
|
||||
|
||||
// remove cmdID from cmdID list
|
||||
ProxySystem.getInstance().getKraProtocol().getCmdIDs()
|
||||
ProxySystem.getInstance().getKraSocketClientProtocol().getCmdIDs()
|
||||
.removeIf(cmdID -> cmdID == kraProtocolMessage.getCmdID());
|
||||
|
||||
System.out.println(
|
||||
"consumer ids " + ProxySystem.getInstance().getKraProtocol().getConsumerHashMap().keySet());
|
||||
"consumer ids " + ProxySystem.getInstance().getKraSocketClientProtocol()
|
||||
.getConsumerHashMap().keySet());
|
||||
|
||||
// test - mobile or voice socket connected
|
||||
if (kraProtocolMessage.getCmdNumber() == 15) {
|
||||
|
@ -79,7 +52,7 @@ public class SocketClient extends WebSocketClient {
|
|||
});
|
||||
}
|
||||
|
||||
ProxySystem.getInstance().getKraProtocol().getConsumerHashMap()
|
||||
ProxySystem.getInstance().getKraSocketClientProtocol().getConsumerHashMap()
|
||||
.forEach((cmdID, consumer) -> {
|
||||
if (cmdID == kraProtocolMessage.getCmdID()) {
|
||||
System.out.println("accept " + cmdID + " " + kraProtocolMessage.getCmdID());
|
||||
|
@ -90,32 +63,10 @@ public class SocketClient extends WebSocketClient {
|
|||
|
||||
@Override
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
System.out.println(
|
||||
"Connection! closed by " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: "
|
||||
+ reason);
|
||||
|
||||
for (ProxiedPlayer proxiedPlayer : ProxyServer.getInstance().getPlayers()) {
|
||||
proxiedPlayer.sendMessage(
|
||||
new TextComponent(
|
||||
"§cSocket §8» §c" + (remote ? "Connection! Closed by remote peer" : reason)));
|
||||
}
|
||||
|
||||
/* 1 called on plugin disable */
|
||||
if (code != 1) {
|
||||
ProxyServer.getInstance().getScheduler().schedule(ProxySystem.getInstance(), () -> {
|
||||
for (ProxiedPlayer proxiedPlayer : ProxyServer.getInstance().getPlayers()) {
|
||||
proxiedPlayer.sendMessage(new TextComponent("§cSocket §8» §7Reconnecting..."));
|
||||
}
|
||||
|
||||
System.out.println("try reconnect");
|
||||
reconnect();
|
||||
}, 3L, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
System.out.println("Error: " + e);
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
package net.krakatoa.proxy.protocol;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.Getter;
|
||||
import net.krakatoa.proxy.ProxySystem;
|
||||
|
||||
public class KraProtocol {
|
||||
|
||||
@Getter
|
||||
ArrayList<Integer> cmdIDs = Lists.newArrayList();
|
||||
|
||||
@Getter
|
||||
HashMap<Integer, Consumer<KraProtocolMessage>> consumerHashMap = new HashMap<>();
|
||||
|
||||
public int getCmdID() {
|
||||
if (cmdIDs.isEmpty()) {
|
||||
cmdIDs.add(10);
|
||||
return 10;
|
||||
}
|
||||
|
||||
int cmdID = cmdIDs.get(cmdIDs.size() - 1) + 1;
|
||||
|
||||
cmdIDs.add(cmdID);
|
||||
|
||||
// TODO: check max int value 2^32
|
||||
|
||||
return cmdID;
|
||||
}
|
||||
|
||||
public void SendMessage(KraProtocolMessage kraProtocolMessage) {
|
||||
SendMessage(kraProtocolMessage.getStatus(), kraProtocolMessage.getCmdID(),
|
||||
kraProtocolMessage.getDest(), kraProtocolMessage.getUuid(),
|
||||
kraProtocolMessage.getCmdNumber(), kraProtocolMessage.getArgs());
|
||||
}
|
||||
|
||||
public void SendMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
||||
String args) {
|
||||
System.out.println("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
||||
|
||||
int argLen = args.length();
|
||||
|
||||
byte[] raw = new byte[40 + argLen];
|
||||
|
||||
raw[0] = (byte) status;
|
||||
|
||||
raw[1] = (byte) cmdID;
|
||||
raw[2] = (byte) (cmdID >> 8);
|
||||
raw[3] = (byte) (cmdID >> 16);
|
||||
raw[4] = (byte) (cmdID >> 24);
|
||||
|
||||
raw[5] = (byte) dest;
|
||||
|
||||
byte[] uuidBytes = uuid.getBytes();
|
||||
|
||||
/* for (int i = 0; i < 32; i++) {
|
||||
raw[6 + i] = uuidBytes[i];
|
||||
}*/
|
||||
|
||||
System.arraycopy(uuidBytes, 0, raw, 6, 32);
|
||||
|
||||
raw[38] = (byte) cmdNumber;
|
||||
raw[39] = (byte) (cmdNumber >> 8);
|
||||
|
||||
if (argLen > 0) {
|
||||
for (int i = 0; i < argLen; i++) {
|
||||
raw[40 + i] = (byte) args.charAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
ProxySystem.getInstance().getSocketClient().SendMessage(raw);
|
||||
}
|
||||
|
||||
public KraProtocolMessage DecodeMessage(byte[] data) {
|
||||
System.out.println("start decode");
|
||||
|
||||
byte status = data[0];
|
||||
|
||||
int cmdID = Byte.toUnsignedInt(data[1]);
|
||||
|
||||
cmdID += Byte.toUnsignedInt(data[2]) * 256;
|
||||
cmdID += Byte.toUnsignedInt(data[3]) * 256 * 256;
|
||||
cmdID += Byte.toUnsignedInt(data[4]) * 256 * 256 * 256;
|
||||
|
||||
int dest = Byte.toUnsignedInt(data[5]);
|
||||
|
||||
byte[] playerUuidBytes = new byte[32];
|
||||
|
||||
System.arraycopy(data, 6, playerUuidBytes, 0, 32);
|
||||
|
||||
/* for (int i = 0; i < 32; i++) {
|
||||
playerUuidBytes[i] = data[6 + i];
|
||||
} */
|
||||
|
||||
String playerUuid = new String(playerUuidBytes, StandardCharsets.UTF_8);
|
||||
|
||||
short cmdNumber = (short) (Byte.toUnsignedInt(data[38]) + (Byte.toUnsignedInt(data[39]) * 256));
|
||||
|
||||
int argLen = data.length - 40;
|
||||
byte[] argsBytes = new byte[argLen];
|
||||
|
||||
/* for (int i = 0; i < argLen; i++) {
|
||||
argsBytes[i] = data[40 + i];
|
||||
} */
|
||||
|
||||
System.arraycopy(data, 40, argsBytes, 0, argLen);
|
||||
|
||||
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
||||
|
||||
System.out.println(
|
||||
"decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
||||
+ cmdNumber + " " + args + " ");
|
||||
|
||||
return new KraProtocolMessage(status, cmdID, dest, playerUuid, cmdNumber, args);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package net.krakatoa.proxy.protocol;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class KraProtocolMessage {
|
||||
|
||||
private final byte status;
|
||||
private final int cmdID; // long
|
||||
private final int dest;
|
||||
private final String uuid;
|
||||
private final short cmdNumber;
|
||||
private final String args;
|
||||
|
||||
public int getStatus() {
|
||||
return Byte.toUnsignedInt(status);
|
||||
}
|
||||
|
||||
public int getCmdNumber() {
|
||||
return Short.toUnsignedInt(cmdNumber);
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package net.krakatoa.proxy.protocol;
|
||||
|
||||
public class KraProtocolMessageHandler {
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package net.krakatoa.proxy.protocol;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public enum KraProtocolStatus {
|
||||
|
||||
SEND(10),
|
||||
GET(11),
|
||||
MESSAGE_ALREADY_IN_QUEUE(12),
|
||||
ERROR_NO_PERMS(13),
|
||||
ERROR_TRY_AGAIN(14),
|
||||
ERROR_ARG_LEN_TOO_BIG(15),
|
||||
REPLY(16);
|
||||
|
||||
@Getter
|
||||
private final int status;
|
||||
|
||||
KraProtocolStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue