Merge pull request #143 from UnfamiliarLegacy/fix/nitro-random-port

Randomize nitro websocket port
This commit is contained in:
sirjonasxx 2022-12-07 18:47:33 +01:00 committed by GitHub
commit 218468294d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 4 deletions

View File

@ -5,7 +5,6 @@ public final class NitroConstants {
public static final int HTTP_PORT = 9090; public static final int HTTP_PORT = 9090;
public static final int HTTP_BUFFER_SIZE = 1024 * 1024 * 10; public static final int HTTP_BUFFER_SIZE = 1024 * 1024 * 10;
public static final int WEBSOCKET_PORT = 2096;
public static final int WEBSOCKET_BUFFER_SIZE = 1024 * 1024 * 10; public static final int WEBSOCKET_BUFFER_SIZE = 1024 * 1024 * 10;
public static final String WEBSOCKET_REVISION = "PRODUCTION-201611291003-338511768"; public static final String WEBSOCKET_REVISION = "PRODUCTION-201611291003-338511768";
public static final String WEBSOCKET_CLIENT_IDENTIFIER = "HTML5"; public static final String WEBSOCKET_CLIENT_IDENTIFIER = "HTML5";

View File

@ -24,6 +24,7 @@ public class NitroProxyProvider implements ProxyProvider, NitroHttpProxyServerCa
private final NitroWebsocketProxy nitroWebsocketProxy; private final NitroWebsocketProxy nitroWebsocketProxy;
private final AtomicBoolean abortLock; private final AtomicBoolean abortLock;
private int websocketPort;
private String originalWebsocketUrl; private String originalWebsocketUrl;
private String originalCookies; private String originalCookies;
@ -63,6 +64,8 @@ public class NitroProxyProvider implements ProxyProvider, NitroHttpProxyServerCa
return; return;
} }
websocketPort = nitroWebsocketProxy.getPort();
stateSetter.setState(HState.WAITING_FOR_CLIENT); stateSetter.setState(HState.WAITING_FOR_CLIENT);
} }
@ -101,7 +104,7 @@ public class NitroProxyProvider implements ProxyProvider, NitroHttpProxyServerCa
public String replaceWebsocketServer(String configUrl, String websocketUrl) { public String replaceWebsocketServer(String configUrl, String websocketUrl) {
originalWebsocketUrl = websocketUrl; originalWebsocketUrl = websocketUrl;
return String.format("ws://127.0.0.1:%d", NitroConstants.WEBSOCKET_PORT); return String.format("ws://127.0.0.1:%d", websocketPort);
} }
@Override @Override

View File

@ -106,6 +106,7 @@ public class NitroHttpProxy {
.withPort(NitroConstants.HTTP_PORT) .withPort(NitroConstants.HTTP_PORT)
.withManInTheMiddle(new NitroCertificateSniffingManager(authority)) .withManInTheMiddle(new NitroCertificateSniffingManager(authority))
.withFiltersSource(new NitroHttpProxyFilterSource(serverCallback)) .withFiltersSource(new NitroHttpProxyFilterSource(serverCallback))
.withTransparent(true)
.start(); .start();
if (!initializeCertificate()) { if (!initializeCertificate()) {

View File

@ -3,10 +3,11 @@ package gearth.protocol.connection.proxy.nitro.websocket;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxySetter; import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HStateSetter; import gearth.protocol.connection.HStateSetter;
import gearth.protocol.connection.proxy.nitro.NitroConstants;
import gearth.protocol.connection.proxy.nitro.NitroProxyProvider; import gearth.protocol.connection.proxy.nitro.NitroProxyProvider;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
@ -28,7 +29,7 @@ public class NitroWebsocketProxy {
this.stateSetter = stateSetter; this.stateSetter = stateSetter;
this.connection = connection; this.connection = connection;
this.proxyProvider = proxyProvider; this.proxyProvider = proxyProvider;
this.server = new Server(NitroConstants.WEBSOCKET_PORT); this.server = new Server(0);
} }
public boolean start() { public boolean start() {
@ -56,6 +57,12 @@ public class NitroWebsocketProxy {
return false; return false;
} }
public int getPort() {
final ServerConnector serverConnector = (ServerConnector) server.getConnectors()[0];
return serverConnector.getLocalPort();
}
public void stop() { public void stop() {
try { try {
server.stop(); server.stop();