From e9d639d3b8264824f68186180d75307ea08b98ad Mon Sep 17 00:00:00 2001 From: fengjian Date: Tue, 23 Jun 2020 07:02:40 +0800 Subject: [PATCH] commit --- bootlog4j.conf | 56 ++++++++++++++ pom.xml | 4 + src/main/Resources/application.properties | 4 +- .../java/luckyclient/netty/ClientHandler.java | 7 +- .../luckyclient/utils/EncryptionUtils.java | 70 ++++++++++++++++++ .../utils/config/AppiumConfig.java | 5 +- .../utils/config/DrivenConfig.java | 5 +- .../luckyclient/utils/config/SysConfig.java | 6 +- .../utils/proxy/PropertiesProxy.java | 73 +++++++++++++++++++ src/main/java/springboot/RunService.java | 5 +- 10 files changed, 229 insertions(+), 6 deletions(-) create mode 100644 bootlog4j.conf create mode 100644 src/main/java/luckyclient/utils/EncryptionUtils.java create mode 100644 src/main/java/luckyclient/utils/proxy/PropertiesProxy.java diff --git a/bootlog4j.conf b/bootlog4j.conf new file mode 100644 index 0000000..b21125a --- /dev/null +++ b/bootlog4j.conf @@ -0,0 +1,56 @@ +log4j.rootLogger=INFO,stdout,info,warn,error + +log4j.logger.info=info +log4j.logger.warn=warn +log4j.logger.error=error + +log4j.logger.org.springframework=warn +log4j.logger.org.apache=info,error +log4j.logger.org.aspectj.weaver=warn +log4j.logger.org.hibernate=off +log4j.logger.com.mchange=off +log4j.logger.java.sql.Connection=info +log4j.logger.java.sql.Statement=info +log4j.logger.java.sql.ResultSet=info +log4j.logger.java.sql.PreparedStatement=info + +#log4j.logger.httpclient.wire.header=off +#log4j.logger.httpclient.wire.content=off +#log4j.logger.org.apache.commons.httpclient=off +#log4j.logger.org.apache.commons.httpclient.Wire=off + + +################打印到控制台 +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss\:SSS}\:%l - \n%p\: %m%n + +################记录系统日志,监控客户端运行情况 +log4j.appender.info=org.apache.log4j.DailyRollingFileAppender +log4j.appender.info.File=bootlog/INFO.log +log4j.appender.info.layout=org.apache.log4j.PatternLayout +log4j.appender.info.Threshold = INFO +log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}:%p %l - %m%n +log4j.appender.info.DatePattern = '.'yyyy-MM-dd +log4j.appender.info.encoding=UTF-8 +################ + +################记录系统告警日志 +log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender +log4j.appender.warn.File=bootlog/WARN.log +log4j.appender.warn.layout=org.apache.log4j.PatternLayout +log4j.appender.warn.Threshold = WARN +log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}:%p %l - %m%n +log4j.appender.warn.DatePattern = '.'yyyy-MM-dd +log4j.appender.warn.encoding=UTF-8 +################ + +################记录系统错误,监控程序是否出错 +log4j.appender.error=org.apache.log4j.DailyRollingFileAppender +log4j.appender.error.File=bootlog/ERROR.log +log4j.appender.error.layout=org.apache.log4j.PatternLayout +log4j.appender.error.Threshold = ERROR +log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}:%p %l - %m%n +log4j.appender.error.DatePattern = '.'yyyy-MM-dd +log4j.appender.error.encoding=UTF-8 + diff --git a/pom.xml b/pom.xml index f626d04..f1d3cdd 100644 --- a/pom.xml +++ b/pom.xml @@ -323,6 +323,10 @@ javax.xml.bind jaxb-api + + org.springframework.boot + spring-boot-starter-aop + diff --git a/src/main/Resources/application.properties b/src/main/Resources/application.properties index 6a2ad1a..880d311 100644 --- a/src/main/Resources/application.properties +++ b/src/main/Resources/application.properties @@ -1,4 +1,6 @@ spring.application.name = LuckyFrameClient server.port = 6633 spring.servlet.multipart.max-file-size=30MB -spring.servlet.multipart.max-request-size=30MB \ No newline at end of file +spring.servlet.multipart.max-request-size=30MB +client.config.slat=12331312 +client.config.pass=123 \ No newline at end of file diff --git a/src/main/java/luckyclient/netty/ClientHandler.java b/src/main/java/luckyclient/netty/ClientHandler.java index 923bcd3..48172aa 100644 --- a/src/main/java/luckyclient/netty/ClientHandler.java +++ b/src/main/java/luckyclient/netty/ClientHandler.java @@ -52,6 +52,7 @@ public class ClientHandler extends ChannelHandlerAdapter { private static final String SERVER_PORT = SysConfig.getConfiguration().getProperty("server.web.port"); + public static Integer clientId=-1; private volatile int lastLength = 0; @@ -200,7 +201,11 @@ public class ClientHandler extends ChannelHandlerAdapter { re.put("data", result); sendMessage(re.toString()); } - + } + else if("loginReturn".equals(json.get("method"))) + { + //client + clientId=Integer.valueOf(json.get("clientId").toString()); } } diff --git a/src/main/java/luckyclient/utils/EncryptionUtils.java b/src/main/java/luckyclient/utils/EncryptionUtils.java new file mode 100644 index 0000000..9b61a98 --- /dev/null +++ b/src/main/java/luckyclient/utils/EncryptionUtils.java @@ -0,0 +1,70 @@ +package luckyclient.utils; + +import org.apache.commons.codec.binary.Base64; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.crypto.Cipher; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.PBEParameterSpec; +import java.security.Key; + +/** + * @author fengjian + * @date 2020/6/22 22:17 + */ +@Component +public class EncryptionUtils { + + private static String salt; + + private static String pass; + + @Value("${client.config.slat}") + public void setSalt(String salt) { + EncryptionUtils.salt = salt; + } + + @Value("${client.config.pass}") + public void setPass(String pass) { + EncryptionUtils.pass = pass; + } + + /** + * 加密 + * @param value + * @return + */ + public static String encrypt(String value) throws Exception { + // 加密 + PBEKeySpec pbeKeySpec = new PBEKeySpec(pass.toCharArray()); + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); + Key key = factory.generateSecret(pbeKeySpec); + PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt.getBytes(),100); + Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); + cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec); + byte[] result = cipher.doFinal(value.getBytes()); + System.out.println("jdk PBE encrypt: " + Base64.encodeBase64String(result)); + return Base64.encodeBase64String(result); + } + + /** + * 解密 + * @param value + * @return + * @throws Exception + */ + public static String decrypt(String value)throws Exception + { + // 解密 + PBEKeySpec pbeKeySpec = new PBEKeySpec(pass.toCharArray()); + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); + PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt.getBytes(),100); + Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); + Key key = factory.generateSecret(pbeKeySpec); + cipher.init(Cipher.DECRYPT_MODE, key,pbeParameterSpec); + byte[] result = cipher.doFinal(Base64.decodeBase64(value)); + return new String(result); + } +} diff --git a/src/main/java/luckyclient/utils/config/AppiumConfig.java b/src/main/java/luckyclient/utils/config/AppiumConfig.java index aa62a85..75537db 100644 --- a/src/main/java/luckyclient/utils/config/AppiumConfig.java +++ b/src/main/java/luckyclient/utils/config/AppiumConfig.java @@ -8,6 +8,7 @@ import java.nio.charset.StandardCharsets; import java.util.Properties; import luckyclient.utils.LogUtil; +import luckyclient.utils.proxy.PropertiesProxy; /** * ʼAppiumò @@ -17,16 +18,18 @@ import luckyclient.utils.LogUtil; public class AppiumConfig { private static final Properties SYS_CONFIG = new Properties(); private static final String SYS_CONFIG_FILE = "/appium_config.properties"; + private static PropertiesProxy proxy=new PropertiesProxy(); static{ try { InputStream in = new BufferedInputStream(AppiumConfig.class.getResourceAsStream(SYS_CONFIG_FILE)); SYS_CONFIG.load(new InputStreamReader(in, StandardCharsets.UTF_8)); + proxy.setWapper(SYS_CONFIG); } catch (IOException e) { LogUtil.APP.error("ȡƶappium_config.propertiesļ쳣飡", e); } } private AppiumConfig(){} public static Properties getConfiguration(){ - return SYS_CONFIG; + return proxy; } } diff --git a/src/main/java/luckyclient/utils/config/DrivenConfig.java b/src/main/java/luckyclient/utils/config/DrivenConfig.java index 9882553..791d88c 100644 --- a/src/main/java/luckyclient/utils/config/DrivenConfig.java +++ b/src/main/java/luckyclient/utils/config/DrivenConfig.java @@ -8,6 +8,7 @@ import java.nio.charset.StandardCharsets; import java.util.Properties; import luckyclient.utils.LogUtil; +import luckyclient.utils.proxy.PropertiesProxy; /** * ʼݿ @@ -21,16 +22,18 @@ import luckyclient.utils.LogUtil; public class DrivenConfig { private static final Properties SYS_CONFIG = new Properties(); private static final String SYS_CONFIG_FILE = "/TestDriven/driven_config.properties"; + private static PropertiesProxy proxy=new PropertiesProxy(); static{ try { InputStream in = new BufferedInputStream(DrivenConfig.class.getResourceAsStream(SYS_CONFIG_FILE)); SYS_CONFIG.load(new InputStreamReader(in, StandardCharsets.UTF_8)); + proxy.setWapper(SYS_CONFIG); } catch (IOException e) { LogUtil.APP.error("ȡdriven_config.propertiesļ쳣飡", e); } } private DrivenConfig(){} public static Properties getConfiguration(){ - return SYS_CONFIG; + return proxy; } } diff --git a/src/main/java/luckyclient/utils/config/SysConfig.java b/src/main/java/luckyclient/utils/config/SysConfig.java index cd7d8b2..e5333f1 100644 --- a/src/main/java/luckyclient/utils/config/SysConfig.java +++ b/src/main/java/luckyclient/utils/config/SysConfig.java @@ -1,5 +1,7 @@ package luckyclient.utils.config; +import luckyclient.utils.proxy.PropertiesProxy; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -14,16 +16,18 @@ import java.util.Properties; public class SysConfig { private static final Properties SYS_CONFIG = new Properties(); private static final String SYS_CONFIG_FILE = "/sys_config.properties"; + private static PropertiesProxy proxy=new PropertiesProxy(); static{ try { InputStream in = new BufferedInputStream(SysConfig.class.getResourceAsStream(SYS_CONFIG_FILE)); SYS_CONFIG.load(new InputStreamReader(in, "GBK")); + proxy.setWapper(SYS_CONFIG); } catch (IOException e) { e.printStackTrace(); } } private SysConfig(){} public static Properties getConfiguration(){ - return SYS_CONFIG; + return proxy; } } diff --git a/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java b/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java new file mode 100644 index 0000000..fe532e0 --- /dev/null +++ b/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java @@ -0,0 +1,73 @@ +package luckyclient.utils.proxy; + +import com.alibaba.fastjson.JSONObject; +import luckyclient.netty.ClientHandler; +import luckyclient.utils.EncryptionUtils; +import luckyclient.utils.httputils.HttpRequest; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; + +/** + * @author fengjian + * @date 2020/6/22 21:26 + */ +public class PropertiesProxy extends Properties { + + private static final Logger log = LoggerFactory.getLogger(PropertiesProxy.class); + + private Properties wapper = new Properties(); + + public Properties getWapper() { + return wapper; + } + + public void setWapper(Properties wapper) { + this.wapper = wapper; + } + + @Override + public String getProperty(String key) { + log.info("get config from service"); + //ȡ + //ͨӿڻȡ + try{ + String result = HttpRequest.loadJSON("/system/clientConfig/config/"+ ClientHandler.clientId+"/"+key); + if(StringUtils.isNotEmpty(result)) + { + JSONObject res=JSONObject.parseObject(result); + if(res.get("code")!=null&&res.getInteger("code")==200) + { + String value= EncryptionUtils.decrypt(res.get("value").toString()); + log.info("get config from server:"+res.toJSONString()+";value="+value); + return value; + } + + } + }catch (Exception e) + { + log.info("û:"+key); + } + return wapper.getProperty(key); + } + + @Override + public String getProperty(String key, String defaultValue) { + log.debug("get config from service"); + //ȡ + //ͨӿڻȡ + try{ + String result = HttpRequest.loadJSON("/system/clientConfig/config/"+ ClientHandler.clientId+"/"+key); + if(StringUtils.isNotEmpty(result)) + { + return result; + } + }catch (Exception e) + { + log.debug("û:"+key); + } + return wapper.getProperty(key, defaultValue); + } +} diff --git a/src/main/java/springboot/RunService.java b/src/main/java/springboot/RunService.java index 18bcda9..1a3ed24 100644 --- a/src/main/java/springboot/RunService.java +++ b/src/main/java/springboot/RunService.java @@ -12,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import cn.hutool.core.util.BooleanUtil; import luckyclient.netty.NettyClient; import luckyclient.utils.config.SysConfig; +import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * ================================================================= @@ -32,7 +33,7 @@ public class RunService { private static final Logger log = LoggerFactory.getLogger(RunService.class); private static final Boolean NETTY_MODEL= BooleanUtil.toBoolean(SysConfig.getConfiguration().getProperty("netty.model")); public static String CLIENT_IP = ""; - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub PropertyConfigurator.configure(System.getProperty("user.dir") + File.separator +"bootlog4j.conf"); SpringApplication.run(RunService.class, args); @@ -53,6 +54,8 @@ public class RunService { }else{ HttpImpl.checkHostNet(); } + Thread.sleep(5000); + SysConfig.getConfiguration().getProperty("appid"); } }