Browse Source

照抄测试

boweniac 1 week ago
parent
commit
869cf57932

+ 1 - 0
pom.xml

@@ -42,6 +42,7 @@
 		<dependency>
 			<groupId>mysql</groupId>
 			<artifactId>mysql-connector-java</artifactId>
+			<version>8.0.30</version>
 		</dependency>
 	<!-- 整合mybatis -->
 	     <dependency>

+ 21 - 1
src/main/java/com/jubotech/framework/netty/async/AsyncTaskService.java

@@ -84,7 +84,27 @@ public class AsyncTaskService {
 			return;
 		}
 		try {
-			log.info(LocalDateTime.now() + " msgSend2pc 对应的线程名: " + Thread.currentThread().getName());
+//			log.info(LocalDateTime.now() + " msgSend2pc 对应的线程名: " + Thread.currentThread().getName());
+
+			log.info("{} msgSend2pc 对应的线程名: {}", LocalDateTime.now(), Thread.currentThread().getName());
+
+
+			// 转发给所有管理连接
+			log.debug("管理连接数量:{}", NettyConnectionUtil.adminId_nettyChannel.size());
+
+			for (Map.Entry<String, ChannelHandlerContext> entry : NettyConnectionUtil.adminId_nettyChannel.entrySet()) {
+				try {
+					ChannelHandlerContext chx = entry.getValue();
+					if (chx != null) {
+						log.info("转发消息给管理连接:{}", entry.getKey());
+						// 在这里执行你需要的操作,例如发送消息等
+						MessageUtil.sendJsonMsg(chx, type, nettyConnectionUtil.getNettyId(chx), null, req);
+					}
+				} catch (Exception e) {
+					log.error("处理管理连接时出错: {}", entry.getKey(), e);
+				}
+			}
+
 			// 先从缓存取,如果取不到再用sql取
 			List<WxAccountInfo> accountInfoList = Constant.accountInfoList;
 			if (null == accountInfoList || accountInfoList.size() == 0) {

+ 157 - 57
src/main/java/com/jubotech/framework/netty/handler/websocket/DeviceAuthReqWebsocketHandler.java

@@ -1,8 +1,9 @@
 package com.jubotech.framework.netty.handler.websocket;
-
+import java.net.InetSocketAddress;
 import java.util.Base64;
-
+import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
@@ -31,6 +32,8 @@ import lombok.extern.slf4j.Slf4j;
 @Service
 @Slf4j
 public class DeviceAuthReqWebsocketHandler  implements JsonMessageHandler{
+	@Value("#{'${internalcode.whitelist:}'.split(',')}")
+	private List<String> whitelist;
 	@Autowired
 	private CustomerService customerService;
 	@Autowired
@@ -55,52 +58,127 @@ public class DeviceAuthReqWebsocketHandler  implements JsonMessageHandler{
 				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
 				return;
 			}
-			// 1、校验用户信息
-			if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
+//			// 1、校验用户信息
+//			if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
+//				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
+//				return;
+//			}
+//			byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
+//			String str = new String(byteArray);
+//			if (StringUtils.isEmpty(str)) {
+//				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
+//				return;
+//			}
+//			log.debug("账号密码登陆:name=" + str);
+//			String[] strs = str.split(":");
+//			if (strs == null || strs.length < 1 || strs[0] == null) {
+//				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
+//				return;
+//			}
+//			// pc客服端
+//			AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
+//			if (null == user) {
+//				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
+//				return;
+//			}
+//
+//			if (user.getState() != 1) {// 账号状态正常的时候
+//				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
+//				return;
+//			}
+//			// 判断是否有登陆,如果有登陆直接踢下线
+//			ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
+//			if (null != chc) {
+//				MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登陆
+//				chc.close();
+//			}
+
+//			// 生成用户token信息
+//			String token = nettyConnectionUtil.getNettyId(ctx);
+//
+//			// 存储 用户id 和 通道信息
+//			nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
+//			// 存储微信全局id 与通道
+//			nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
+//
+//			CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
+			if (req.getAuthType().equals(EnumAuthType.Username)){
+				// 1、校验用户信息
+//				if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
+//					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
+//					return;
+//				}
+				byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
+				String str = new String(byteArray);
+				if (StringUtils.isEmpty(str)) {
+					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
+					return;
+				}
+				log.debug("账号密码登录:name=" + str);
+				String[] strs = str.split(":");
+				if (strs == null || strs.length < 1 || strs[0] == null) {
+					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
+					return;
+				}
+				// pc客服端
+				AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
+				if (null == user) {
+					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
+					return;
+				}
+
+				if (user.getState() != 1) {// 账号状态正常的时候
+					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
+					return;
+				}
+				// 判断是否有登录,如果有登录直接踢下线
+				ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
+				if (null != chc) {
+					MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登录
+					chc.close();
+				}
+
+				// 生成用户token信息
+				String token = nettyConnectionUtil.getNettyId(ctx);
+
+				// 存储 用户id 和 通道信息
+				nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
+				// 存储微信全局id 与通道
+				nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
+
+				CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
+
+				sendMsg(customer, user, token, ctx, vo);
+			} else if (req.getAuthType().equals(EnumAuthType.InternalCode)) {
+				// 获取客户端ip地址
+				InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
+				String clientIP = insocket.getAddress().getHostAddress();
+				Number clientPort = insocket.getPort();
+				String clientAddress = clientIP + ":" + clientPort;
+				log.debug("内部登录:address=" + clientAddress);
+				log.debug("白名单 :" + this.whitelist);
+
+				if (!this.whitelist.contains(clientIP)){
+					log.debug("非法设备");
+					MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_ILLEGALDEVICE);
+					return;
+				}
+
+				// 生成用户token信息
+				String token = nettyConnectionUtil.getNettyId(ctx);
+
+				// 存储 ip:port 和 通道信息
+				nettyConnectionUtil.saveDeviceChannel(ctx, clientAddress);
+
+				// 内部白名单,都认为是管理连接
+				nettyConnectionUtil.saveAdminChannel(ctx, clientAddress);
+
+
+				sendMsg(null, null, token, ctx, vo);
+			} else {
 				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
-				return;
-			}
-			byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
-			String str = new String(byteArray);
-			if (StringUtils.isEmpty(str)) {
-				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
-				return;
-			}
-			log.debug("账号密码登陆:name=" + str);
-			String[] strs = str.split(":");
-			if (strs == null || strs.length < 1 || strs[0] == null) {
-				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
-				return;
-			}
-			// pc客服端
-			AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
-			if (null == user) {
-				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
-				return;
 			}
-								
-			if (user.getState() != 1) {// 账号状态正常的时候
-				MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
-				return;
-			}
-			// 判断是否有登陆,如果有登陆直接踢下线
-			ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
-			if (null != chc) {
-				MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登陆
-				chc.close();
-			}
-
-			// 生成用户token信息
-			String token = nettyConnectionUtil.getNettyId(ctx);
-
-			// 存储 用户id 和 通道信息
-			nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
-			// 存储微信全局id 与通道
-			nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
-
-			CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
-
-			sendMsg(customer, user, token, ctx, vo);
+//			sendMsg(customer, user, token, ctx, vo);
 		} catch (Exception e) {
 			e.printStackTrace();
 			MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
@@ -110,19 +188,41 @@ public class DeviceAuthReqWebsocketHandler  implements JsonMessageHandler{
 	private void sendMsg(CustomerInfo customer, AccountInfo user, String token, ChannelHandlerContext ctx,
 			TransportMessage vo) {
 
-		Integer supplierid = customer.getId();
-		String suppliername = customer.getSuppliername();// 商家名称
-		long unionid = user.getId();// 个人账号id
-		String nickname = user.getNickname();// 昵称
+//		Integer supplierid = customer.getId();
+//		String suppliername = customer.getSuppliername();// 商家名称
+//		long unionid = user.getId();// 个人账号id
+//		String nickname = user.getNickname();// 昵称
 
 		ExtraMessage.Builder buider = ExtraMessage.newBuilder();
-		buider.setSupplierId(supplierid);
-		buider.setSupplierName(suppliername);
-		buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
-
-		if (0 != unionid && !StringUtils.isEmpty(nickname)) {
-			buider.setUnionId(unionid);
-			buider.setNickName(nickname);
+//		buider.setSupplierId(supplierid);
+//		buider.setSupplierName(suppliername);
+//		buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
+//
+//		if (0 != unionid && !StringUtils.isEmpty(nickname)) {
+//			buider.setUnionId(unionid);
+//			buider.setNickName(nickname);
+//		}
+
+		if (null == customer) {
+			buider.setSupplierId(1982);
+			buider.setSupplierName("微信客服系统管理连接");
+			buider.setAccountType(EnumAccountType.UnknownAccountType);//账号类型  子账号
+		}else{
+			Integer supplierid = customer.getId();
+			String suppliername = customer.getSuppliername();// 商家名称
+
+
+			buider.setSupplierId(supplierid);
+			buider.setSupplierName(suppliername+"|"+user.getLevel());
+			buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
+		}
+		if (null != user) {
+			long unionid = user.getId();// 个人账号id
+			String nickname = user.getNickname();// 昵称
+			if (0 != unionid && !StringUtils.isEmpty(nickname)) {
+				buider.setUnionId(unionid);
+				buider.setNickName(nickname);
+			}
 		}
 
 		ExtraMessage ext = buider.build();

+ 37 - 3
src/main/java/com/jubotech/framework/netty/utils/NettyConnectionUtil.java

@@ -35,7 +35,35 @@ public class NettyConnectionUtil {
 	 */
 	public static Map<String, ChannelHandlerContext> userId_nettyChannel = new ConcurrentHashMap<String, ChannelHandlerContext>();
 
-	 
+	/**
+	 * 用于管理连接,这种类型的连接会接收所有用户的消息
+	 * key 为 address 即 ip:port
+	 * value 为 channel 用于存贮通道
+	 */
+	public static Map<String, ChannelHandlerContext> adminId_nettyChannel = new ConcurrentHashMap<String, ChannelHandlerContext>();
+
+
+	public  synchronized void saveAdminChannel(ChannelHandlerContext cx, String adminId) {
+		ChannelHandlerContext cc = getNettyChannelByAdminId(adminId);
+		if (null != cc) {
+			adminId_nettyChannel.remove(adminId);
+		}
+		adminId_nettyChannel.put(adminId, cx);
+	}
+
+	public static synchronized ChannelHandlerContext getNettyChannelByAdminId(String adminId) {
+		ChannelHandlerContext ctx = adminId_nettyChannel.get(adminId);
+		if (null != ctx) {
+			return ctx;
+		} else {
+			return null;
+		}
+	}
+
+	public  synchronized String getAdminIdByNettyid(String nettyId) {
+		return getKey(adminId_nettyChannel, nettyId);
+	}
+
 	/**
 	 * 存储通道
 	 * 
@@ -93,6 +121,12 @@ public class NettyConnectionUtil {
 				if (!StringUtils.isEmpty(userid)) {
 					userId_nettyChannel.remove(userid);
 				}
+
+				// 清除管理通道
+				String adminId = getAdminIdByNettyid(nettyid);
+				if (!StringUtils.isEmpty(adminId)) {
+					adminId_nettyChannel.remove(adminId);
+				}
 				 
 			}
 		} catch (Exception e) {
@@ -107,7 +141,7 @@ public class NettyConnectionUtil {
 	 */
 	public  synchronized void exit(ChannelHandlerContext ctx) {
 		try {
-			log.info("当前netty通道连接数"+deviceid_nettyChannel.size()+"   有效通道连接数"+userId_nettyChannel.size());
+			log.info("当前netty通道连接数"+deviceid_nettyChannel.size()+"   有效通道连接数"+userId_nettyChannel.size()+ "   管理通道连接数"+adminId_nettyChannel.size());
 			removeChannel(ctx);
 			ctx.close();
 		} catch (Exception e) {
@@ -123,7 +157,7 @@ public class NettyConnectionUtil {
 	 * @param cx
 	 */
 	public  synchronized void registerUserid(String userId, ChannelHandlerContext cx) {
-		log.info("当前netty通道连接数"+deviceid_nettyChannel.size()+"   有效通道连接数"+userId_nettyChannel.size());
+		log.info("当前netty通道连接数"+deviceid_nettyChannel.size()+"   有效通道连接数"+userId_nettyChannel.size() + "   管理通道连接数"+adminId_nettyChannel.size());
 		ChannelHandlerContext chc = userId_nettyChannel.get(userId);
 		if (null != chc) {
 			userId_nettyChannel.remove(userId);

+ 2 - 2
src/main/resources/application.properties

@@ -31,9 +31,9 @@ spring.cache.ehcache.config = classpath:ehcache.xml
 
 #Êý¾ÝÔ´  
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-spring.datasource.url=jdbc:mysql://127.0.0.1:3306/workchat?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
+spring.datasource.url=jdbc:mysql://127.0.0.1:3307/workchat?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
 spring.datasource.username=root
-spring.datasource.password=123456
+spring.datasource.password=simple-admin.
 spring.datasource.connection-init-sql=SET NAMES utf8mb4