zhourui 5 лет назад
Родитель
Сommit
fe018e7784

+ 9 - 0
o2server/pom.xml

@@ -144,6 +144,10 @@
 			<groupId>org.eclipse.jetty</groupId>
 			<artifactId>jetty-quickstart</artifactId>
 		</dependency>
+		<dependency>
+			<groupId>org.eclipse.jetty</groupId>
+			<artifactId>jetty-proxy</artifactId>
+		</dependency>
 		<dependency>
 			<groupId>com.google.zxing</groupId>
 			<artifactId>core</artifactId>
@@ -545,6 +549,11 @@
 				<artifactId>jetty-quickstart</artifactId>
 				<version>9.4.20.v20190813</version>
 			</dependency>
+			<dependency>
+				<groupId>org.eclipse.jetty</groupId>
+				<artifactId>jetty-proxy</artifactId>
+				<version>9.4.31.v20200723</version>
+			</dependency>
 			<dependency>
 				<groupId>com.google.zxing</groupId>
 				<artifactId>core</artifactId>

+ 52 - 0
o2server/x_console/src/main/java/com/x/server/console/server/web/Proxy.java

@@ -0,0 +1,52 @@
+package com.x.server.console.server.web;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.eclipse.jetty.proxy.ProxyServlet;
+
+public class Proxy extends ProxyServlet {
+
+	private static final long serialVersionUID = 2737360000716631564L;
+
+	@Override
+	protected String rewriteTarget(HttpServletRequest request) {
+		String url = request.getRequestURL().toString();
+		return target(url, this.getServletConfig().getInitParameter("port"));
+	}
+
+	private String target(String url, String port) {
+		int x = StringUtils.indexOf(url, ":", 8);
+		int y = StringUtils.indexOf(url, "/", 8);
+		if ((x > 0) && (y > 0)) {
+			return url.substring(0, x) + port(url, port) + url.substring(y);
+		} else if (y > 0) {
+			return url.substring(0, y) + port(url, port) + url.substring(y);
+		} else {
+			return null;
+		}
+	}
+
+	private String port(String url, String port) {
+		if (StringUtils.startsWithIgnoreCase(url, "https://")) {
+			if (StringUtils.equals(port, "443")) {
+				return "";
+			}
+		} else if (StringUtils.startsWithIgnoreCase(url, "http://")) {
+			if (StringUtils.equals(port, "80")) {
+				return "";
+			}
+		}
+		return ":" + port;
+	}
+
+//	@Test
+//	public void test1() {
+//		System.out.println(target("http://www.o2oa.net:20030/111/22?1=1", "80"));
+//		System.out.println(target("http://www.o2oa.net:20030/111/22?1=1", "81"));
+//		System.out.println(target("https://www.o2oa.net:20030/111/22?1=1", "80"));
+//		System.out.println(target("https://www.o2oa.net:20030/111/22?1=1", "443"));
+//		System.out.println(target("https://www.o2oa.net:20030/111/22?1=1", "4430"));
+//	}
+
+}

+ 38 - 7
o2server/x_console/src/main/java/com/x/server/console/server/web/WebServerTools.java

@@ -104,6 +104,16 @@ public class WebServerTools extends JettySeverTools {
 		}
 		/* stat end */
 		server.setHandler(context);
+
+		if (BooleanUtils.isTrue(webServer.getProxyCenterEnable())) {
+			proxyCenter(context);
+		}
+
+		if (BooleanUtils.isTrue(webServer.getProxyApplicationEnable())) {
+			proxyApplication(context, Config.dir_store().toPath());
+			proxyApplication(context, Config.dir_custom().toPath());
+		}
+
 		server.setDumpAfterStart(false);
 		server.setDumpBeforeStop(false);
 		server.setStopAtShutdown(true);
@@ -117,6 +127,27 @@ public class WebServerTools extends JettySeverTools {
 		return server;
 	}
 
+	private static void proxyCenter(WebAppContext context) throws Exception {
+		ServletHolder proxyHolder = new ServletHolder(Proxy.class);
+		proxyHolder.setInitParameter("port", Config.currentNode().getCenter().getPort() + "");
+		context.addServlet(proxyHolder, "/" + x_program_center.class.getSimpleName() + "/*");
+	}
+
+	private static void proxyApplication(WebAppContext context, Path path) throws Exception {
+		try (Stream<Path> stream = Files.list(path)) {
+			stream.filter(o -> StringUtils.endsWithIgnoreCase(o.getFileName().toString(), ".war"))
+					.map(Path::getFileName).map(Path::toString).map(FilenameUtils::getBaseName).forEach(o -> {
+						try {
+							ServletHolder proxyHolder = new ServletHolder(Proxy.class);
+							proxyHolder.setInitParameter("port", Config.currentNode().getApplication().getPort() + "");
+							context.addServlet(proxyHolder, "/" + x_program_center.class.getSimpleName() + "/*");
+						} catch (Exception e) {
+							logger.error(e);
+						}
+					});
+		}
+	}
+
 	private static void copyDefaultHtml() throws Exception {
 		File file = new File(Config.dir_config(), "default.html");
 		if (file.exists() && file.isFile()) {
@@ -205,16 +236,16 @@ public class WebServerTools extends JettySeverTools {
 			/* 密码规则 */
 			map.put("passwordRegex", Config.person().getPasswordRegex());
 			map.put("passwordRegexHint", Config.person().getPasswordRegexHint());
-			
-		    /*RSA*/
+
+			/* RSA */
 			File publicKeyFile = new File(Config.base(), "config/public.key");
 			if (publicKeyFile.exists() && publicKeyFile.isFile()) {
-					 String publicKey = FileUtils.readFileToString(publicKeyFile, "utf-8");
-					 byte[] publicKeyB = Base64.decodeBase64(publicKey);
-					 publicKey = new String(Base64.encodeBase64(publicKeyB));
-					 map.put("publicKey", publicKey);
+				String publicKey = FileUtils.readFileToString(publicKeyFile, "utf-8");
+				byte[] publicKeyB = Base64.decodeBase64(publicKey);
+				publicKey = new String(Base64.encodeBase64(publicKeyB));
+				map.put("publicKey", publicKey);
 			}
-			
+
 			FileUtils.writeStringToFile(file, gson.toJson(map), DefaultCharset.charset);
 		}
 	}