Casper 5 months ago
parent
commit
42c547c3ac
2 changed files with 32 additions and 17 deletions
  1. 3 4
      sri-server-bg03/hub.py
  2. 29 13
      sri-server-bg03/lib/Connection_e1.py

+ 3 - 4
sri-server-bg03/hub.py

@@ -9,7 +9,6 @@ methods = importlib.import_module(f"xlib")
 class Global(object):
     pb = importlib.import_module(f"protobuf.protocol_pb2")
 
-    # emqx = importlib.import_module(f"xclient.xmqtt").Client(host='10.10.61.229', port=41883)
-
-    # mdb = importlib.import_module(f"xclient.xmongo").Client(host='58.34.94.178', port=7030, database='bg',
-    #                                                         username='admin', password='admin')
+    # --- 业务数据数据库 ---
+    mdb = importlib.import_module(f"xclient.xmongo").Client(host='127.0.0.1', port=47017, database='bg',
+                                                            username='admin', password='admin')

+ 29 - 13
sri-server-bg03/lib/Connection_e1.py

@@ -1,7 +1,8 @@
 """
 """
-# from hub import methods, Global
+from hub import methods, Global
 
+from werkzeug.security import check_password_hash
 import struct
 import asyncio
 import time
@@ -51,8 +52,9 @@ class SRIConnection(asyncio.Protocol):
         建立客户端连接
         """
         peername = client.get_extra_info('peername')
-        # self.connection_id = f"{peername[0].replace('.', '')}-{peername[1]}"  # 12700130000 客户端id
-        self.connection_id = int(f"{peername[0].replace('.', '')}")
+        # self.connection_id = f"{peername[0].replace('.', '')}-{peername[1]}"  # 客户端id
+        self.connection_id = int(f"{peername[1]}")  # 客户端id(公网情况)
+        # self.connection_id = int(f"{peername[0].replace('.', '')}")  # 客户端id(固定ip情况)
         self.client = client
         # self.data = b''
         self.client_type = None
@@ -174,7 +176,7 @@ class SRIConnection(asyncio.Protocol):
                 command_id, body_length = struct.unpack(self.head_sequence, head_data)
                 if command_id not in [2008]:
                     methods.debug_log(f'{self.connection_id}|SRIConnection176',
-                                    f"command_id: {command_id}, body_length: {body_length}")
+                                      f"command_id: {command_id}, body_length: {body_length}")
 
                 # 检查命令ID是否有效
                 if not (2000 <= command_id < 5000):
@@ -281,18 +283,32 @@ class SRIConnection(asyncio.Protocol):
         methods.debug_log(f"{self.connection_id}|SRIConnection162", f"#password: {object.password}")
 
         # --- update ---
-        self.client_type = 'cockpit'
-        self.client_info = {
-            'connection_id': self.connection_id,
-            'uid': 3,  # 对应数据库里的ego的id
-            'name': 'ego',  # 对应数据库里
-            'egotype': 1,  # 舱端类型
-        }
+        ret = True
+        if object.account == "Ego" and object.password != '123456':
+            ret = False
+
+        # --- check ---
+        user = Global.mdb.get_one('UserInfo', {'username': object.account})
+        if not user:
+            ret = False
+        elif not check_password_hash(user['password'], object.password):
+            ret = False
+        else:
+            ret = True
+
+        # --- update ---
+        if ret:
+            self.client_type = 'cockpit'
+            self.client_info = {
+                'connection_id': self.connection_id,
+                'uid': 3,  # 对应数据库里的ego的id
+                'name': 'ego',  # 对应数据库里
+                'egotype': 1,  # 舱端类型
+            }
 
         # --- send 4000
         object = protobuf.SCSign()
-        object.ret = True
-        # object.uid = self.client_info.get('uid')
+        object.ret = ret
         object.uid = self.client_info.get('connection_id')
         object.name = self.client_info.get('name')
         re_command_id = protobuf.SC_Sign  # 4000