Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WaterdogPE xuid and IP #1356

Merged
merged 6 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected Vector3 teleportPosition = null;

protected boolean connected = true;
protected final InetSocketAddress socketAddress;
protected final InetSocketAddress rawSocketAddress;
protected InetSocketAddress socketAddress;
protected boolean removeFormat = true;

protected String username;
Expand Down Expand Up @@ -679,6 +680,7 @@ public Player(SourceInterface interfaz, Long clientID, InetSocketAddress socketA
this.perm = new PermissibleBase(this);
this.server = Server.getInstance();
this.lastBreak = -1;
this.rawSocketAddress = socketAddress;
this.socketAddress = socketAddress;
this.clientID = clientID;
this.loaderId = Level.generateChunkLoaderId(this);
Expand Down Expand Up @@ -742,6 +744,18 @@ public void setSkin(Skin skin) {
}
}

public String getRawAddress() {
return this.rawSocketAddress.getAddress().getHostAddress();
}

public int getRawPort() {
return this.rawSocketAddress.getPort();
}

public InetSocketAddress getRawSocketAddress() {
return this.rawSocketAddress;
}

public String getAddress() {
return this.socketAddress.getAddress().getHostAddress();
}
Expand Down Expand Up @@ -2390,6 +2404,10 @@ public void handleDataPacket(DataPacket packet) {
break;
}

if (this.server.getConfig("settings.waterdogpe", false) && loginChainData.getWaterdogIP() != null) {
this.socketAddress = new InetSocketAddress(this.loginChainData.getWaterdogIP(), this.getRawPort());
}

this.randomClientId = loginPacket.clientId;

this.uuid = loginPacket.clientUUID;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSy
List<InetSocketAddress> targets = new ArrayList<>();
for (Player p : players) {
if (p.isConnected()) {
targets.add(p.getSocketAddress());
targets.add(p.getRawSocketAddress());
}
}

Expand Down Expand Up @@ -2052,7 +2052,7 @@ public Player[] matchPlayer(String partialName) {
}

public void removePlayer(Player player) {
Player toRemove = this.players.remove(player.getSocketAddress());
Player toRemove = this.players.remove(player.getRawSocketAddress());
if (toRemove != null) {
return;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/cn/nukkit/network/RakNetInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public boolean process() {

@Override
public int getNetworkLatency(Player player) {
RakNetServerSession session = this.raknet.getSession(player.getSocketAddress());
RakNetServerSession session = this.raknet.getSession(player.getRawSocketAddress());
return session == null ? -1 : (int) session.getPing();
}

Expand All @@ -149,7 +149,7 @@ public void close(Player player) {

@Override
public void close(Player player, String reason) {
RakNetServerSession session = this.raknet.getSession(player.getSocketAddress());
RakNetServerSession session = this.raknet.getSession(player.getRawSocketAddress());
if (session != null) {
session.close();
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public Integer putPacket(Player player, DataPacket packet, boolean needACK) {

@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK, boolean immediate) {
NukkitRakNetSession session = this.sessions.get(player.getSocketAddress());
NukkitRakNetSession session = this.sessions.get(player.getRawSocketAddress());

if (session != null) {
packet.tryEncode();
Expand Down Expand Up @@ -261,12 +261,12 @@ public void onSessionCreation(RakNetServerSession session) {
public void onUnhandledDatagram(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
this.server.handlePacket(datagramPacket.sender(), datagramPacket.content());
}

@PowerNukkitOnly
@Since("1.5.2.0-PN")
@Override
public Integer putResourcePacket(Player player, DataPacket packet) {
NukkitRakNetSession session = this.sessions.get(player.getSocketAddress());
NukkitRakNetSession session = this.sessions.get(player.getRawSocketAddress());

if (session != null) {
packet.tryEncode();
Expand All @@ -275,7 +275,7 @@ public Integer putResourcePacket(Player player, DataPacket packet) {

return null;
}

@RequiredArgsConstructor
private class NukkitRakNetSession implements RakNetSessionListener {
private final RakNetServerSession raknet;
Expand Down Expand Up @@ -368,7 +368,7 @@ private void sendPacket(byte[] payload) {
byteBuf.writeBytes(payload);
this.raknet.send(byteBuf);
}

private void sendPacketImmediately(DataPacket packet) {
BinaryStream batched = new BinaryStream();
Preconditions.checkArgument(!(packet instanceof BatchPacket), "Cannot batch BatchPacket");
Expand All @@ -386,7 +386,7 @@ private void sendPacketImmediately(DataPacket packet) {
log.error("Error occured while sending a packet immediately", e);
}
}

private void sendResourcePacket(DataPacket packet) {
BinaryStream batched = new BinaryStream();
Preconditions.checkArgument(!(packet instanceof BatchPacket), "Cannot batch BatchPacket");
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/cn/nukkit/utils/ClientChainData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.nukkit.utils;

import cn.nukkit.Server;
import cn.nukkit.network.protocol.LoginPacket;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -108,7 +109,11 @@ public String getLanguageCode() {

@Override
public String getXUID() {
return xuid;
if (this.isWaterdog()) {
return waterdogXUID;
} else {
return xuid;
}
}

private boolean xboxAuthed;
Expand Down Expand Up @@ -136,11 +141,29 @@ public int getUIProfile() {
return UIProfile;
}

@Override
public String getWaterdogXUID() {
return waterdogXUID;
}

@Override
public String getWaterdogIP() {
return waterdogIP;
}

@Override
public JsonObject getRawData() {
return rawData;
}

private boolean isWaterdog() {
if (Server.getInstance() != null) {
return Server.getInstance().getConfig("settings.waterdogpe", false) && waterdogXUID != null;
} else {
return false;
}
}

///////////////////////////////////////////////////////////////////////////
// Override
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -179,6 +202,8 @@ private static ECPublicKey generateKey(String base64) throws NoSuchAlgorithmExce
private String languageCode;
private int currentInputMode;
private int defaultInputMode;
private String waterdogIP;
private String waterdogXUID;

private int UIProfile;

Expand Down Expand Up @@ -214,6 +239,12 @@ private void decodeSkinData() {
if (skinToken.has("DefaultInputMode")) this.defaultInputMode = skinToken.get("DefaultInputMode").getAsInt();
if (skinToken.has("UIProfile")) this.UIProfile = skinToken.get("UIProfile").getAsInt();
if (skinToken.has("CapeData")) this.capeData = skinToken.get("CapeData").getAsString();
if (skinToken.has("Waterdog_IP")) this.waterdogIP = skinToken.get("Waterdog_IP").getAsString();
if (skinToken.has("Waterdog_XUID")) this.waterdogXUID = skinToken.get("Waterdog_XUID").getAsString();

if (this.isWaterdog()) {
xboxAuthed = true;
}

this.rawData = skinToken;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/nukkit/utils/LoginChainData.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public interface LoginChainData {

int getUIProfile();

String getWaterdogXUID();

String getWaterdogIP();

@Since("1.2.1.0-PN")
JsonObject getRawData();
}
1 change: 1 addition & 0 deletions src/main/resources/default-nukkit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ settings:
deprecated-verbose: true
async-workers: auto
safe-spawn: true
waterdogpe: false

network:
batch-threshold: 256
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/cn/nukkit/network/RakNetInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setUp() throws IOException {
@Test
void noSession() {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 3222);
when(player.getSocketAddress()).thenReturn(socketAddress);
when(player.getRawSocketAddress()).thenReturn(socketAddress);
DataPacket packet = new ResourcePackChunkRequestPacket();
rakNetInterface.putResourcePacket(player, packet);
assertFalse(packet.isEncoded);
Expand Down