From de39613091c5440982b52adba9f2d29c2be87015 Mon Sep 17 00:00:00 2001 From: UnfamiliarLegacy <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:38:05 +0200 Subject: [PATCH] Bypass user-agent based cloudflare challenge --- .../nitro/websocket/NitroWebsocketServer.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/websocket/NitroWebsocketServer.java b/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/websocket/NitroWebsocketServer.java index 37ca63d..673badb 100644 --- a/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/websocket/NitroWebsocketServer.java +++ b/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/websocket/NitroWebsocketServer.java @@ -6,6 +6,8 @@ import gearth.protocol.connection.proxy.nitro.NitroConstants; import gearth.protocol.packethandler.PacketHandler; import gearth.protocol.packethandler.nitro.NitroPacketHandler; import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketListener; @@ -36,7 +38,11 @@ public class NitroWebsocketServer implements WebSocketListener, NitroSession { "Sec-WebSocket-Version", "Host", "Connection", - "Upgrade" + "Upgrade", + "User-Agent", // Added by default + "Accept-Encoding", // Added by default + "Cache-Control", // Added by default + "Pragma" // Added by default )); private final PacketHandler packetHandler; @@ -56,6 +62,8 @@ public class NitroWebsocketServer implements WebSocketListener, NitroSession { final ClientUpgradeRequest request = new ClientUpgradeRequest(); + request.addExtensions("permessage-deflate"); + clientHeaders.forEach((key, value) -> { if (SKIP_HEADERS.contains(key)) { return; @@ -64,6 +72,13 @@ public class NitroWebsocketServer implements WebSocketListener, NitroSession { request.setHeader(key, value); }); + if (clientHeaders.containsKey("User-Agent")) { + final String realUserAgent = clientHeaders.get(HttpHeader.USER_AGENT.toString()).get(0); + final HttpField clientUserAgent = new HttpField(HttpHeader.USER_AGENT, realUserAgent); + + client.getHttpClient().setUserAgentField(clientUserAgent); + } + logger.info("Connecting to origin websocket at {}", websocketUrl); client.start();