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

Allow responding to cookie requests #1453

Draft
wants to merge 2 commits into
base: dev/3.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ public String toString() {
*/
public static final class ForwardResult implements Result {

private static final ForwardResult ALLOWED = new ForwardResult(true, null);
private static final ForwardResult DENIED = new ForwardResult(false, null);
private static final ForwardResult ALLOWED = new ForwardResult(true, null, false, null);
private static final ForwardResult DENIED = new ForwardResult(false, null, false, null);

private final boolean status;
private final Key key;
private final boolean respond;
private final byte[] data;

private ForwardResult(final boolean status, final Key key) {
private ForwardResult(final boolean status, final Key key, boolean respond, byte[] data) {
this.status = status;
this.key = key;
this.respond = respond;
this.data = data;
}

@Override
Expand All @@ -88,6 +92,14 @@ public Key getKey() {
return key;
}

public boolean shouldRespond() {
return respond;
}

public byte[] getData() {
return data;
}

@Override
public String toString() {
return status ? "forward to client" : "handled by proxy";
Expand All @@ -112,6 +124,16 @@ public static ForwardResult handled() {
return DENIED;
}

/**
* Sends this response to the request to the server.
*
* @param data the data to send, null will mean missing data
* @return a result with the data
*/
public static ForwardResult respond(final byte [] data) {
return new ForwardResult(false, null, true, data);
}

/**
* Allows the cookie request to be forwarded to the client, but silently replaces the
* identifier of the cookie with another.
Expand All @@ -121,7 +143,7 @@ public static ForwardResult handled() {
*/
public static ForwardResult key(final Key key) {
Preconditions.checkNotNull(key, "key");
return new ForwardResult(true, key);
return new ForwardResult(true, key, false, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket;
import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket;
import com.velocitypowered.proxy.protocol.packet.ServerDataPacket;
import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket;
import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket;
import com.velocitypowered.proxy.protocol.packet.TransferPacket;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket;
Expand Down Expand Up @@ -435,6 +436,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) {
? event.getOriginalKey() : event.getResult().getKey();

playerConnection.write(new ClientboundCookieRequestPacket(resultedKey));
} else if (event.getResult().shouldRespond()) {
serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(packet.getKey(), event.getResult().getData()));
}
}, playerConnection.eventLoop());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.velocitypowered.proxy.protocol.packet.RemoveResourcePackPacket;
import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket;
import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket;
import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket;
import com.velocitypowered.proxy.protocol.packet.TransferPacket;
import com.velocitypowered.proxy.protocol.packet.config.ClientboundCustomReportDetailsPacket;
import com.velocitypowered.proxy.protocol.packet.config.ClientboundServerLinksPacket;
Expand Down Expand Up @@ -324,6 +325,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) {
? event.getOriginalKey() : event.getResult().getKey();

serverConn.getPlayer().getConnection().write(new ClientboundCookieRequestPacket(resultedKey));
} else if (event.getResult().shouldRespond()) {
serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData()));
}
}, serverConn.ensureConnected().eventLoop());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessagePacket;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponsePacket;
import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccessPacket;
import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket;
import com.velocitypowered.proxy.protocol.packet.SetCompressionPacket;
import com.velocitypowered.proxy.util.except.QuietRuntimeException;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -189,6 +190,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) {
? event.getOriginalKey() : event.getResult().getKey();

serverConn.getPlayer().getConnection().write(new ClientboundCookieRequestPacket(resultedKey));
} else if (event.getResult().shouldRespond()) {
serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData()));
}
}, serverConn.ensureConnected().eventLoop());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.velocitypowered.proxy.protocol.packet.KeepAlivePacket;
import com.velocitypowered.proxy.protocol.packet.PluginMessagePacket;
import com.velocitypowered.proxy.protocol.packet.RemoveResourcePackPacket;
import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket;
import com.velocitypowered.proxy.protocol.packet.TransferPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ChatQueue;
import com.velocitypowered.proxy.protocol.packet.chat.ChatType;
Expand Down Expand Up @@ -1071,6 +1072,8 @@ public void requestCookie(final Key key) {
? event.getOriginalKey() : event.getResult().getKey();

connection.write(new ClientboundCookieRequestPacket(resultedKey));
} else if (event.getResult().shouldRespond()) {
this.ensureBackendConnection().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData()));
}
}, connection.eventLoop());
}
Expand Down