From 99483e83d0f83316f3eec3ac4e7a94e2c326519f Mon Sep 17 00:00:00 2001 From: xnbox Date: Sat, 11 Sep 2021 19:35:56 +0300 Subject: [PATCH] Add option --redirect --- src/org/tommy/common/utils/CommonUtils.java | 67 +++++++++++---------- src/org/tommy/main/Main.java | 21 +++++-- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/org/tommy/common/utils/CommonUtils.java b/src/org/tommy/common/utils/CommonUtils.java index f795328..8a8466b 100644 --- a/src/org/tommy/common/utils/CommonUtils.java +++ b/src/org/tommy/common/utils/CommonUtils.java @@ -293,48 +293,49 @@ public static void prepareTomcatConf(Path confPath, Path keystorePath, Integer p DocumentBuilder builder = builderFactory.newDocumentBuilder(); serverXmlDocument = builder.parse(is); - if (port != null) { + Node autoDeployNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Engine/Host/@autoDeploy").evaluate(serverXmlDocument, XPathConstants.NODE); + autoDeployNode.setTextContent(Boolean.toString(false)); + + Node connectorNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector").evaluate(serverXmlDocument, XPathConstants.NODE); + if (port == null) + connectorNode.getParentNode().removeChild(connectorNode); + else { Node portNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector/@port").evaluate(serverXmlDocument, XPathConstants.NODE); portNode.setTextContent(Integer.toString(port)); // update node with real TCP port number } - if (sslPort != null) { + /* Add TLS(SSL) support */ + if (port != null && sslPort != null) { Node redirectPortNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector/@redirectPort").evaluate(serverXmlDocument, XPathConstants.NODE); - redirectPortNode.setTextContent(Integer.toString(sslPort)); // update node with real TCP port number + redirectPortNode.setTextContent(Integer.toString(sslPort)); // update node with real SSL TCP port number } - Node autoDeployNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Engine/Host/@autoDeploy").evaluate(serverXmlDocument, XPathConstants.NODE); - autoDeployNode.setTextContent(Boolean.toString(false)); - - /* Add TLS(SSL) support */ - - Node serviceNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service").evaluate(serverXmlDocument, XPathConstants.NODE); - Element tlsConnectorNode = serverXmlDocument.createElement("Connector"); - - if (sslPort == null) - sslPort = 8443; - - tlsConnectorNode.setAttribute("port", Integer.toString(sslPort)); - tlsConnectorNode.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol"); - tlsConnectorNode.setAttribute("SSLEnabled", "true"); - //tlsConnectorNode.setAttribute("maxThreads", "150"); - serviceNode.appendChild(tlsConnectorNode); - - Element upgradeProtocolEl = serverXmlDocument.createElement("UpgradeProtocol"); - upgradeProtocolEl.setAttribute("className", "org.apache.coyote.http2.Http2Protocol"); - tlsConnectorNode.appendChild(upgradeProtocolEl); - - Element sslHostConfigEl = serverXmlDocument.createElement("SSLHostConfig"); - tlsConnectorNode.appendChild(sslHostConfigEl); - - Element certificateEl = serverXmlDocument.createElement("Certificate"); - certificateEl.setAttribute("certificateKeystoreFile", "conf/keystore/localhost-rsa.jks"); - certificateEl.setAttribute("certificateKeystorePassword", "changeit"); - certificateEl.setAttribute("type", "RSA"); - sslHostConfigEl.appendChild(certificateEl); + if (sslPort != null) { + Node serviceNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service").evaluate(serverXmlDocument, XPathConstants.NODE); + Element tlsConnectorNode = serverXmlDocument.createElement("Connector"); + + tlsConnectorNode.setAttribute("port", Integer.toString(sslPort)); + tlsConnectorNode.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol"); + tlsConnectorNode.setAttribute("SSLEnabled", "true"); + serviceNode.appendChild(tlsConnectorNode); + + Element upgradeProtocolEl = serverXmlDocument.createElement("UpgradeProtocol"); + upgradeProtocolEl.setAttribute("className", "org.apache.coyote.http2.Http2Protocol"); + tlsConnectorNode.appendChild(upgradeProtocolEl); + + Element sslHostConfigEl = serverXmlDocument.createElement("SSLHostConfig"); + tlsConnectorNode.appendChild(sslHostConfigEl); + + Element certificateEl = serverXmlDocument.createElement("Certificate"); + certificateEl.setAttribute("certificateKeystoreFile", "conf/keystore/localhost-rsa.jks"); + certificateEl.setAttribute("certificateKeystorePassword", "changeit"); + certificateEl.setAttribute("type", "RSA"); + sslHostConfigEl.appendChild(certificateEl); + } } } + /* update web.xml document */ Document webXmlDocument = null; try (InputStream is = cl.getResourceAsStream("META-INF/tomcat/conf/web.xml")) { if (is != null) { @@ -372,7 +373,7 @@ public static void prepareTomcatConf(Path confPath, Path keystorePath, Integer p copyConfDocumentXml(confPath, "server.xml", serverXmlDocument); copyConfDocumentXml(confPath, "web.xml", webXmlDocument); - //copyConfResource(confPath, "web.xml"); + copyConfResource(confPath, "tomcat-users.xsd"); copyConfResource(confPath, "tomcat-users.xml"); copyConfResource(confPath, "logging.properties"); diff --git a/src/org/tommy/main/Main.java b/src/org/tommy/main/Main.java index 118108c..4df18f6 100644 --- a/src/org/tommy/main/Main.java +++ b/src/org/tommy/main/Main.java @@ -55,6 +55,9 @@ The manifest file can have any name, but is commonly named manifest.json and ser */ public class Main { + private static final int DEFAULT_PORT_8080 = 8080; + private static final int DEFAULT_SSL_PORT_8433 = 8433; + private static Class clazz = Main.class; private static ClassLoader cl = clazz.getClassLoader(); private static Logger logger = LoggerUtils.createLogger(clazz); @@ -67,7 +70,7 @@ public class Main { private static final String ARGS_HELP_OPTION = "--help"; private static final String ARGS_PORT_OPTION = "--port"; private static final String ARGS_PORT_SSL_OPTION = "--port-ssl"; - private static final String ARGS_NO_REDIRECT_OPTION = "--no-redirect"; + private static final String ARGS_REDIRECT_OPTION = "--redirect"; private static final String ARGS_CONTEXT_PATH_OPTION = "--context-path"; public static void main(String[] args) throws Throwable { @@ -85,7 +88,7 @@ public static void main(String[] args) throws Throwable { Integer sslPort = null; String contextPath = "/"; boolean help = false; - boolean noRedirect = false; + boolean redirect = false; for (int i = 1; i < args.length; i++) { if (args[i].equals(ARGS_APP_OPTION)) { @@ -117,12 +120,18 @@ public static void main(String[] args) throws Throwable { } catch (Throwable e) { // ignore exception } - } else if (args[i].equals(ARGS_NO_REDIRECT_OPTION)) - noRedirect = true; + } else if (args[i].equals(ARGS_REDIRECT_OPTION)) + redirect = true; else if (args[i].equals(ARGS_HELP_OPTION)) help = true; } + if (sslPort == null) { + if (port == null) + port = DEFAULT_PORT_8080; + if (redirect) + sslPort = DEFAULT_SSL_PORT_8433; + } /** * Custom command line args */ @@ -150,7 +159,7 @@ else if (args[i].equals(ARGS_HELP_OPTION)) sb.append(" --app run app from ZIP or WAR archive, directory or URL\n"); sb.append(" --port HTTP TCP port number, default: 8080 \n"); sb.append(" --port-ssl HTTPS TCP port number, default: 8443 \n"); - sb.append(" --no-redirect disable redirect HTTP to HTTPS \n"); + sb.append(" --redirect redirect HTTP to HTTPS \n"); sb.append(" --context-path context path, default: / \n"); sb.append(" --password provide password for encrypted ZIP or WAR archive\n"); System.out.println(sb); @@ -213,7 +222,7 @@ else if (args[i].equals(ARGS_HELP_OPTION)) */ contextPath = CommonUtils.getContextPath(contextPath); - CommonUtils.prepareTomcatConf(confPath, keystorePath, port, sslPort, !noRedirect); + CommonUtils.prepareTomcatConf(confPath, keystorePath, port, sslPort, redirect); Tomcat tomcat = CommonUtils.prepareTomcat(logger, catalinaHome, app, argz); org.apache.catalina.Context ctx = tomcat.addWebapp(contextPath, warPath.toString());