99import io .netty .handler .codec .http .websocketx .WebSocketVersion ;
1010import io .netty .handler .codec .http .websocketx .extensions .compression .WebSocketClientCompressionHandler ;
1111import io .netty .handler .ssl .SslHandler ;
12+ import io .netty .util .AsciiString ;
1213import io .netty .util .AttributeKey ;
1314import net .lenni0451 .lambdaevents .EventHandler ;
1415import net .raphimc .netminecraft .constants .MCPipeline ;
2829
2930import javax .net .ssl .*;
3031import java .io .File ;
32+ import java .lang .reflect .Field ;
33+ import java .lang .reflect .Modifier ;
3134import java .net .URI ;
3235import java .net .URISyntaxException ;
3336import java .security .KeyManagementException ;
@@ -125,6 +128,20 @@ public void flush(ChannelHandlerContext ctx) throws Exception {
125128 }
126129 }
127130
131+ static {
132+ try {
133+ Field field = HttpHeaderValues .class .getDeclaredField ("UPGRADE" );
134+ field .setAccessible (true );
135+
136+ Field modifiersField = Field .class .getDeclaredField ("modifiers" );
137+ modifiersField .setAccessible (true );
138+ modifiersField .setInt (field , field .getModifiers () & ~Modifier .FINAL );
139+
140+ field .set (null , AsciiString .cached ("Upgrade" )); // some servers need it to be uppercase :/
141+ } catch (NoSuchFieldException | IllegalAccessException ignored ) {
142+ }
143+ }
144+
128145 private static void doWsServerStuff (Channel ch , NetClient proxyConnection , Channel c2p , ServerAddress addr ) throws URISyntaxException {
129146 ch .attr (MCPipeline .COMPRESSION_THRESHOLD_ATTRIBUTE_KEY ).set (-2 );
130147 if (proxyConnection instanceof ProxyConnection && ((ProxyConnection ) proxyConnection ).getServerVersion ().isNewerThan (VersionEnum .r1_6_4 )) {
@@ -152,7 +169,7 @@ private static void doWsServerStuff(Channel ch, NetClient proxyConnection, Chann
152169 sslEngine .setNeedClientAuth (false );
153170 ch .pipeline ().addFirst ("eag-server-ssl" , new SslHandler (sslEngine ) {
154171 @ Override
155- public void channelInactive (ChannelHandlerContext ctx ) throws Exception {
172+ public void channelInactive (ChannelHandlerContext ctx ) {
156173 if (this .handshakeFuture ().cause () != null ) {
157174 ExceptionUtil .handleNettyException (ctx , this .handshakeFuture ().cause (), null );
158175 }
@@ -165,17 +182,16 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
165182 ch .pipeline ().addFirst ("eag-server-http-codec" , new HttpClientCodec ());
166183 }
167184 url .append ("://" ).append (addr .getAddress ());
168- boolean addPort = (secure && addr .getPort () != 443 ) || (!secure && addr .getPort () != 80 );
169- if (addPort ) {
185+ if ((secure && addr .getPort () != 443 ) || (!secure && addr .getPort () != 80 )) {
170186 url .append (":" ).append (addr .getPort ());
171187 }
188+ url .append ("/" );
172189 String path = c2p .attr (wsPath ).get ();
173190 if (path != null ) {
174- url .append ("/" ). append ( path );
191+ url .append (path );
175192 }
176193 URI uri = new URI (url .toString ());
177- HttpHeaders headers = c2p .hasAttr (EaglercraftHandler .httpHeadersKey ) ? c2p .attr (EaglercraftHandler .httpHeadersKey ).get () : new DefaultHttpHeaders ().set (HttpHeaderNames .ORIGIN , "via.shhnowisnottheti.me" );
178- headers .set (HttpHeaderNames .HOST , uri .getHost () + (addPort ? ":" + uri .getPort () : "" ));
194+ HttpHeaders headers = c2p .hasAttr (EaglercraftHandler .httpHeadersKey ) ? c2p .attr (EaglercraftHandler .httpHeadersKey ).get () : new DefaultHttpHeaders ().clear ().set (HttpHeaderNames .ORIGIN , "https://via.shhnowisnottheti.me" );
179195 ch .pipeline ().addAfter ("eag-server-http-codec" , "eag-server-http-aggregator" , new HttpObjectAggregator (2097152 , true ));
180196 ch .pipeline ().addAfter ("eag-server-http-aggregator" , "eag-server-ws-compression" , WebSocketClientCompressionHandler .INSTANCE );
181197 ch .pipeline ().addAfter ("eag-server-ws-compression" , "eag-server-ws-handshaker" , new WebSocketClientProtocolHandler (WebSocketClientHandshakerFactory .newHandshaker (uri , WebSocketVersion .V13 , null , true , headers , 2097152 )));
0 commit comments