Просмотр исходного кода

修改RAS公钥密码文件bug,war包上传后重起

o2wwx 5 лет назад
Родитель
Сommit
19dff992b3

+ 2 - 2
o2server/x_console/src/main/java/com/x/server/console/CommandFactory.java

@@ -17,9 +17,9 @@ public class CommandFactory {
 
 	public static final Pattern test_pattern = Pattern.compile("^ {0,}test {0,}$", Pattern.CASE_INSENSITIVE);
 
-	//public static final Pattern create_encrypt_key_pattern = Pattern.compile("^ {0,}create encrypt key {0,}$",Pattern.CASE_INSENSITIVE);
+	//public static final Pattern create_encrypt_key_pattern = Pattern.compile("^ {0,}create encrypt key{0,}$",Pattern.CASE_INSENSITIVE);
 	
-	public static final Pattern create_encrypt_key_pattern = Pattern.compile("^ {0,}create encrypt key (.+)$",
+	public static final Pattern create_encrypt_key_pattern = Pattern.compile("^ {0,}create encrypt key {0,}$",
 			Pattern.CASE_INSENSITIVE);
 	
 	public static final Pattern start_pattern = Pattern

+ 3 - 3
o2server/x_console/src/main/java/com/x/server/console/Main.java

@@ -226,7 +226,7 @@ public class Main {
 
 			matcher = CommandFactory.create_encrypt_key_pattern.matcher(cmd);
 			if (matcher.find()) {
-				createEncryptKey(matcher.group(1));
+				createEncryptKey();
 				continue;
 			}
 
@@ -264,9 +264,9 @@ public class Main {
 		return true;
 	}
 
-	private static boolean createEncryptKey(String password) {
+	private static boolean createEncryptKey() {
 		try {
-			return new ActionCreateEncryptKey().execute(password);
+			return new ActionCreateEncryptKey().execute();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}

+ 69 - 11
o2server/x_console/src/main/java/com/x/server/console/NodeAgent.java

@@ -29,6 +29,7 @@ import com.x.base.core.project.config.Config;
 import com.x.base.core.project.gson.XGsonBuilder;
 import com.x.base.core.project.logger.Logger;
 import com.x.base.core.project.logger.LoggerFactory;
+import com.x.server.console.action.ActionCreateEncryptKey;
 import com.x.server.console.server.Servers;
 
 import io.github.classgraph.ClassGraph;
@@ -44,6 +45,8 @@ public class NodeAgent extends Thread {
 
 	public static final Pattern redeploy_pattern = Pattern.compile("^redeploy:(.+)$", Pattern.CASE_INSENSITIVE);
 
+	public static final Pattern syncFile_pattern = Pattern.compile("^syncFile:(.+)$", Pattern.CASE_INSENSITIVE);
+
 	public static final Pattern upload_resource_pattern = Pattern.compile("^uploadResource:(.+)$",
 			Pattern.CASE_INSENSITIVE);
 
@@ -77,8 +80,6 @@ public class NodeAgent extends Thread {
 							DataInputStream dis = new DataInputStream(socket.getInputStream())) {
 						String json = dis.readUTF();
 
-						// logger.info("receive socket json={}",json);
-
 						CommandObject commandObject = XGsonBuilder.instance().fromJson(json, CommandObject.class);
 						if (BooleanUtils.isTrue(Config.currentNode().nodeAgentEncrypt())) {
 							String decrypt = Crypto.rsaDecrypt(commandObject.getCredential(), Config.privateKey());
@@ -88,7 +89,28 @@ public class NodeAgent extends Thread {
 								continue;
 							}
 						}
+						
+						matcher = syncFile_pattern.matcher(commandObject.getCommand());
+						if (matcher.find()) {
+							String strCommand = commandObject.getCommand();
+							strCommand = strCommand.trim();
+							strCommand = strCommand.substring(strCommand.indexOf(":") + 1, strCommand.length());
+							logger.info("收接到同步命令:" + strCommand);
+							String syncFilePath = dis.readUTF();
+							File file  = new File(Config.base(), syncFilePath);
+							fos = new FileOutputStream(file);
+							byte[] bytes = new byte[1024];
+							int length = 0;
+							while ((length = dis.read(bytes, 0, bytes.length)) != -1) {
+								fos.write(bytes, 0, length);
+								fos.flush();
+							}
+							fos.close();
+                         	logger.info("同步完成");
+							continue;
 
+						}
+						
 						matcher = redeploy_pattern.matcher(commandObject.getCommand());
 						if (matcher.find()) {
 							String strCommand = commandObject.getCommand();
@@ -125,7 +147,7 @@ public class NodeAgent extends Thread {
 							bytes = FileUtils.readFileToByteArray(file);
 							filename = filename.substring(0, filename.lastIndexOf("."));
 							// 部署
-							String result = this.redeploy(filename, bytes);
+							String result = this.redeploy(filename, bytes,true);
 							logger.info("部署:" + result);
 							continue;
 
@@ -167,7 +189,28 @@ public class NodeAgent extends Thread {
 							strCommand = strCommand.trim();
 							strCommand = strCommand.substring(strCommand.indexOf(":") + 1, strCommand.length());
 							logger.info("收接到命令:" + strCommand);
-							commandQueue.add(strCommand);
+							
+							//为了同步文件
+	                        if (strCommand.indexOf("create encrypt")>-1) {
+	                        	matcher = CommandFactory.create_encrypt_key_pattern.matcher(strCommand);
+	                			if (matcher.find()) {
+	                				try {
+	                					boolean CreateEncryptKey = new ActionCreateEncryptKey().execute();
+	                					if(CreateEncryptKey) {
+	                						dos.writeUTF("Create Encrypt Key true");
+	                						dos.flush();
+	                					}else {
+	                						dos.writeUTF("Create Encryp tKey false");
+	                						dos.flush();
+	                					}
+	                				} catch (Exception e) {
+	                					e.printStackTrace();
+	                				}
+	                			}
+							}else {
+							    commandQueue.add(strCommand);
+							}
+	                        
 							continue;
 						}
 
@@ -313,7 +356,7 @@ public class NodeAgent extends Thread {
 		return result;
 	}
 
-	private String redeploy(String name, byte[] bytes) {
+	private String redeploy(String name, byte[] bytes, boolean rebootApp) {
 		String result = "success";
 		try {
 			logger.print("redeploy:{}.", name);
@@ -325,10 +368,10 @@ public class NodeAgent extends Thread {
 					storeJar(name, bytes);
 					break;
 				case "customWar":
-					customWar(name, bytes);
+					customWar(name, bytes,rebootApp);
 					break;
 				case "customJar":
-					customJar(name, bytes);
+					customJar(name, bytes,rebootApp);
 					break;
 			}
 		} catch (Exception e) {
@@ -389,6 +432,8 @@ public class NodeAgent extends Thread {
 		for (ClassInfo info : classInfos) {
 			contextPaths.add("/" + info.getSimpleName());
 		}
+		
+		
 		if (Servers.applicationServerIsRunning()) {
 			GzipHandler gzipHandler = (GzipHandler) Servers.applicationServer.getHandler();
 			HandlerList hanlderList = (HandlerList) gzipHandler.getHandler();
@@ -415,6 +460,7 @@ public class NodeAgent extends Thread {
 				}
 			}
 		}
+		
 		if (Servers.applicationServerIsRunning()) {
 			GzipHandler gzipHandler = (GzipHandler) Servers.applicationServer.getHandler();
 			HandlerList hanlderList = (HandlerList) gzipHandler.getHandler();
@@ -443,7 +489,7 @@ public class NodeAgent extends Thread {
 		}
 	}
 
-	private void customWar(String simpleName, byte[] bytes) throws Exception {
+	private void customWar(String simpleName, byte[] bytes,boolean rebootApp) throws Exception {
 		File war = new File(Config.dir_custom(true), simpleName + ".war");
 		File dir = new File(Config.dir_servers_applicationServer_work(), simpleName);
 		FileUtils.writeByteArrayToFile(war, bytes, false);
@@ -460,10 +506,16 @@ public class NodeAgent extends Thread {
 					}
 				}
 			}
+			
+			if(rebootApp) {
+				  Servers.stopApplicationServer();
+				  Thread.sleep(3000);
+				  Servers.startApplicationServer();
+			}
 		}
 	}
 
-	private void customJar(String simpleName, byte[] bytes) throws Exception {
+	private void customJar(String simpleName, byte[] bytes,boolean rebootApp) throws Exception {
 		File jar = new File(Config.dir_custom_jars(true), simpleName + ".jar");
 		FileUtils.writeByteArrayToFile(jar, bytes, false);
 		List<String> contexts = new ArrayList<>();
@@ -476,13 +528,19 @@ public class NodeAgent extends Thread {
 			for (Handler handler : hanlderList.getHandlers()) {
 				if (QuickStartWebApp.class.isAssignableFrom(handler.getClass())) {
 					QuickStartWebApp app = (QuickStartWebApp) handler;
-					if (contexts.contains(app.getContextPath())) {
+					if (contexts.contains(app.getContextPath())  ) {
 						app.stop();
-						Thread.sleep(2000);
+						Thread.sleep(3000);
 						app.start();
 					}
 				}
 			}
+			
+			if(rebootApp) {
+				  Servers.stopApplicationServer();
+				  Thread.sleep(1000);
+				  Servers.startApplicationServer();
+			}
 		}
 	}
 

+ 15 - 15
o2server/x_console/src/main/java/com/x/server/console/action/ActionCreateEncryptKey.java

@@ -28,20 +28,16 @@ public class ActionCreateEncryptKey extends ActionBase {
 
 	private Date start;
 
+	private BufferedReader bufferedReader;
+
 	private void init() throws Exception {
 		this.start = new Date();
 	}
 
-	public boolean execute(String password) throws Exception {
-		this.init();
-		/*
-		if (!StringUtils.equals(Config.token().getPassword(), password)) {
-			logger.print("password not match.");
-			return false;
-		}*/
-		
+	public boolean execute() throws Exception {
+		this.init();		
 		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
-		SecureRandom random= new SecureRandom(password.getBytes());
+		SecureRandom random= new SecureRandom();
 		generator.initialize(1024, random);
 		KeyPair pair = generator.generateKeyPair();
 		File publicKeyFile = new File(Config.base(), "config/public.key");
@@ -62,7 +58,6 @@ public class ActionCreateEncryptKey extends ActionBase {
 	
 	public static void main(String[] args) throws Exception {
 		ActionCreateEncryptKey actionCreateEncryptKey = new ActionCreateEncryptKey();
-		actionCreateEncryptKey.writeConfigFile("ssxx");
 	}
 
 	public  boolean writeConfigFile(String publicKey) {
@@ -72,9 +67,9 @@ public class ActionCreateEncryptKey extends ActionBase {
 			dir = new File(Config.base(), "servers/webServer/x_desktop/res/config");
 			FileUtils.forceMkdir(dir);
 			File fileConfig = new File(dir, "config.json");
-			
-			BufferedReader bufferedReader = 
-					new BufferedReader(new InputStreamReader(new FileInputStream(fileConfig), "UTF-8"));
+			FileInputStream fileInputStream = new FileInputStream(fileConfig);
+			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
+			bufferedReader = new BufferedReader(inputStreamReader);
 			String line;
 			while((line=bufferedReader.readLine()) != null) {
 				stringBuffer.append(line);
@@ -84,13 +79,18 @@ public class ActionCreateEncryptKey extends ActionBase {
 			jsonObject.addProperty("publicKey", publicKey);
 
 			 FileUtils.write(fileConfig, jsonObject.toString(),DefaultCharset.charset, false);
-					
-		} catch (FileNotFoundException e) {
+			
+			 bufferedReader.close();
+			 inputStreamReader.close();
+			 fileInputStream.close();
+		}catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		} catch (Exception e) {
 			e.printStackTrace();
+		}finally {
+			
 		}
 	   return true;
   }

+ 100 - 15
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/command/ActionCommand.java

@@ -3,11 +3,16 @@ package com.x.program.center.jaxrs.command;
 import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.net.Socket;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -33,30 +38,54 @@ public class ActionCommand extends BaseAction {
 		Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
 		String ctl = wi.getCtl();
 		String nodeName = wi.getNodeName() ;
-		int nodePort =Integer.parseInt(wi.getNodePort());
 		String curServer = request.getLocalAddr();
 		Wo wo = null;
 		if(nodeName.equalsIgnoreCase("*")) {
 			Nodes nodes = Config.nodes();
-			for (String node : nodes.keySet()){
-				//先其他服务器
-				if(!node.equalsIgnoreCase(curServer)) {
-					if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
-						 wo = executeCommand(ctl, node, nodePort);
+			if (ctl.indexOf("create encrypt")>-1) {
+				//生成key文件
+				for (String node : nodes.keySet()){
+					 //当前服务器
+					if(node.equalsIgnoreCase(curServer)) {
+					    if(nodes.get(curServer).getApplication().getEnable() || nodes.get(curServer).getCenter().getEnable()){
+					    	  wo = executeCommand(ctl, node, nodes.get(node).nodeAgentPort());
+				        }
 					}
 				}
-			}
-			
-			for (String node : nodes.keySet()){
-				 //后当前服务器
-				if(node.equalsIgnoreCase(curServer)) {
-				    if(nodes.get(curServer).getApplication().getEnable() || nodes.get(curServer).getCenter().getEnable()){
-				    	  wo = executeCommand(ctl, node, nodePort);
-			        }
+				//同步key文件
+				for (String node : nodes.keySet()){
+					//其他服务器
+					if(!node.equalsIgnoreCase(curServer)) {
+						if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
+							boolean Syncflag = executeSyncFile("config/public.key" , node ,nodes.get(node).nodeAgentPort());
+							        Syncflag = executeSyncFile("config/private.key" , node ,nodes.get(node).nodeAgentPort());
+							        Syncflag = executeSyncFile("servers/webServer/x_desktop/res/config/config.json" , node ,nodes.get(node).nodeAgentPort());
+						}
+					}
+				}
+				
+			 }else {
+				
+				for (String node : nodes.keySet()){
+					//先其他服务器
+					if(!node.equalsIgnoreCase(curServer)) {
+						if(nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()){
+							 wo = executeCommand(ctl, node, nodes.get(node).nodeAgentPort());
+						}
+					}
+				}
+				
+				for (String node : nodes.keySet()){
+					 //后当前服务器
+					if(node.equalsIgnoreCase(curServer)) {
+					    if(nodes.get(curServer).getApplication().getEnable() || nodes.get(curServer).getCenter().getEnable()){
+					    	  wo = executeCommand(ctl, node, nodes.get(node).nodeAgentPort());
+				        }
+					}
 				}
 			}
 		}else {
-		   wo = executeCommand(ctl, nodeName, nodePort);
+		   wo = executeCommand(ctl, nodeName, Integer.parseInt(wi.getNodePort()));
 		}
 		
 		result.setData(wo);
@@ -77,6 +106,11 @@ public class ActionCommand extends BaseAction {
 				commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
 				dos.writeUTF(XGsonBuilder.toJson(commandObject));
 				dos.flush();
+				
+				if (ctl.indexOf("create encrypt")>-1) {
+					String createEncrypt = dis.readUTF();
+					logger.info(createEncrypt);
+				}
 			}
 		} catch (Exception ex) {
 			wo.setStatus("fail");
@@ -88,6 +122,57 @@ public class ActionCommand extends BaseAction {
 	}
    
 	
+	 private boolean executeSyncFile(String syncFilePath , String nodeName ,int nodePort){
+			  boolean syncFileFlag = false;
+			  File syncFile;
+			  InputStream fileInputStream = null;
+			 
+			try (Socket socket = new Socket(nodeName, nodePort)) {
+				
+				syncFile = new File(Config.base(), syncFilePath);
+				fileInputStream= new FileInputStream(syncFile);
+				 
+				socket.setKeepAlive(true);
+				socket.setSoTimeout(5000);
+				DataOutputStream dos = null;
+				DataInputStream dis  = null;
+				try {
+					dos = new DataOutputStream(socket.getOutputStream());
+				    dis = new DataInputStream(socket.getInputStream());
+				    
+					Map<String, Object> commandObject = new HashMap<>();
+					commandObject.put("command", "syncFile:"+ syncFilePath);
+					commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
+					dos.writeUTF(XGsonBuilder.toJson(commandObject));
+					dos.flush();
+					
+					dos.writeUTF(syncFilePath);
+					dos.flush();
+					
+			
+					logger.info("同步文件starting.......");
+					byte[] bytes = new byte[1024];
+					int length =0;
+					while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
+						dos.write(bytes, 0, length);
+						dos.flush();
+					}
+					logger.info("同步文件end.......");
+					
+				}finally {
+					dos.close();
+					dis.close();
+					socket.close();
+					fileInputStream.close();
+				}
+				
+				syncFileFlag = true;
+			} catch (Exception ex) {
+				logger.error(ex);
+				syncFileFlag = false;
+			}
+			return syncFileFlag;
+		}
 	
 	public static class Wi  extends GsonPropertyObject{
 		@FieldDescribe("命令名称")

+ 3 - 1
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/command/ActionUploadFile.java

@@ -62,6 +62,8 @@ public class ActionUploadFile  extends BaseAction {
 					}
 				}
 			}else {
+				
+				 byteArrayInputStream = new ByteArrayInputStream(byteArray);
 			     wo = executeCommand( ctl,  nodeName ,  Integer.parseInt(nodePort),  byteArrayInputStream, disposition);
 			}
 			
@@ -98,7 +100,7 @@ public class ActionUploadFile  extends BaseAction {
 					dos.write(bytes, 0, length);
 					dos.flush();
 				}
-				logger.info("发送文件end.......");
+				logger.info("发送文件end.");
 				
 			}finally {
 				dos.close();