Adding TLS mode to websocket BrowserDebugTool.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-05-13 11:13:59 -05:00
parent 2c14d93885
commit 45a5b9c8b9
1 changed files with 44 additions and 5 deletions

View File

@ -21,14 +21,19 @@ package org.eclipse.jetty.websocket.server.browser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig; import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
import org.eclipse.jetty.websocket.common.extensions.FrameDebugExtension; import org.eclipse.jetty.websocket.common.extensions.FrameDebugExtension;
import org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension;
import org.eclipse.jetty.websocket.server.WebSocketHandler; import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest; import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
@ -44,10 +49,12 @@ import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
public class BrowserDebugTool implements WebSocketCreator public class BrowserDebugTool implements WebSocketCreator
{ {
private static final Logger LOG = Log.getLogger(BrowserDebugTool.class); private static final Logger LOG = Log.getLogger(BrowserDebugTool.class);
private ServerConnector secureConnector;
public static void main(String[] args) public static void main(String[] args)
{ {
int port = 8080; int port = 8080;
int securePort = 8443;
for (int i = 0; i < args.length; i++) for (int i = 0; i < args.length; i++)
{ {
@ -56,12 +63,17 @@ public class BrowserDebugTool implements WebSocketCreator
{ {
port = Integer.parseInt(args[++i]); port = Integer.parseInt(args[++i]);
} }
if ("-sP".equals(a) || "--securePort".equals(a))
{
securePort = Integer.parseInt(args[++i]);
}
} }
try try
{ {
BrowserDebugTool tool = new BrowserDebugTool(); BrowserDebugTool tool = new BrowserDebugTool();
tool.prepare(port); tool.prepare(port, securePort);
tool.start(); tool.start();
} }
catch (Throwable t) catch (Throwable t)
@ -119,13 +131,39 @@ public class BrowserDebugTool implements WebSocketCreator
return connector.getLocalPort(); return connector.getLocalPort();
} }
public void prepare(int port) public int getSecurePort()
{
return secureConnector.getLocalPort();
}
public void prepare(int port, int securePort)
{ {
server = new Server(); server = new Server();
connector = new ServerConnector(server);
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSecureScheme("https");
httpConfiguration.setSecurePort(securePort);
connector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration));
connector.setPort(port); connector.setPort(port);
server.addConnector(connector); server.addConnector(connector);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.setKeyManagerPassword("keypwd");
// SSL HTTP Configuration
HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
httpsConfiguration.addCustomizer(new SecureRequestCustomizer());
// SSL Connector
secureConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory,"http/1.1"),
new HttpConnectionFactory(httpsConfiguration));
secureConnector.setPort(securePort);
server.addConnector(secureConnector);
WebSocketHandler wsHandler = new WebSocketHandler() WebSocketHandler wsHandler = new WebSocketHandler()
{ {
@Override @Override
@ -162,7 +200,8 @@ public class BrowserDebugTool implements WebSocketCreator
public void start() throws Exception public void start() throws Exception
{ {
server.start(); server.start();
LOG.info("Server available on port {}",getPort()); LOG.info("Server available on port {}", getPort());
LOG.info("Server available on secure (TLS) port {}", getSecurePort());
} }
public void stop() throws Exception public void stop() throws Exception