From d68d94ae29987d123c5c4e207ee65c713052348c Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期四, 23 九月 2021 14:40:49 +0800
Subject: [PATCH] 2021-09-23 1.增加本地加密通信

---
 HDL_ON/DAL/DriverLayer/Control_TcpServer.cs   |    2 
 HDL_ON/DAL/Server/HttpUtil.cs                 |    4 
 .vs/HDL_APP_Project/xs/UserPrefs.xml          |   34 +++++++++--
 HDL_ON/DAL/DriverLayer/Control.cs             |  108 +++++++++++++++++++++++++++++++----
 HDL_ON/DAL/DriverLayer/Control_Udp.cs         |    2 
 HDL_ON/Entity/Device/DeviceModule.cs          |    9 +++
 HDL_ON/Entity/ResponseEntity/RegionInfoRes.cs |    4 +
 7 files changed, 140 insertions(+), 23 deletions(-)

diff --git a/.vs/HDL_APP_Project/xs/UserPrefs.xml b/.vs/HDL_APP_Project/xs/UserPrefs.xml
index 52caede..83ce664 100644
--- a/.vs/HDL_APP_Project/xs/UserPrefs.xml
+++ b/.vs/HDL_APP_Project/xs/UserPrefs.xml
@@ -1,17 +1,39 @@
 锘�<Properties StartupConfiguration="{D998E133-F0DD-4760-BE3C-461632F54DA4}|Default">
   <MonoDevelop.Ide.ItemProperties.HDL-ON__iOS PreferredExecutionTarget="MonoDevelop.IPhone.IPhoneDeviceTarget.00008030-00014C392121802E" />
-  <MonoDevelop.Ide.Workbench ActiveDocument="HDL-ON_iOS/AppDelegate.cs">
+  <MonoDevelop.Ide.Workbench ActiveDocument="HDL_ON/Entity/Device/DeviceModule.cs">
     <Files>
-      <File FileName="HDL_ON/UI/UI2/1-HomePage/MessageCenterPage.cs" Line="346" Column="5" />
-      <File FileName="HDL-ON_iOS/Info.plist" />
-      <File FileName="HDL-ON_iOS/AppDelegate.cs" Line="303" Column="1" />
+      <File FileName="HDL_ON/DAL/DriverLayer/Control.cs" Line="856" Column="58" />
+      <File FileName="HDL_ON/DAL/DriverLayer/Control_TcpServer.cs" Line="102" Column="56" />
+      <File FileName="HDL_ON/Entity/Device/DeviceModule.cs" Line="49" Column="21" />
     </Files>
     <Pads>
       <Pad Id="ProjectPad">
         <State name="__root__">
           <Node name="HDL_APP_Project" expanded="True">
-            <Node name="HDL-ON_iOS" expanded="True">
-              <Node name="AppDelegate.cs" selected="True" />
+            <Node name="HDL_ON" expanded="True">
+              <Node name="DAL" expanded="True">
+                <Node name="DriverLayer" expanded="True" />
+                <Node name="Mqtt" expanded="True" />
+                <Node name="Server" expanded="True" />
+              </Node>
+              <Node name="Entity" expanded="True">
+                <Node name="Device" expanded="True">
+                  <Node name="DeviceModule.cs" selected="True" />
+                </Node>
+                <Node name="ResponseEntity" expanded="True" />
+              </Node>
+              <Node name="UI" expanded="True">
+                <Node name="BindingResidence" expanded="True" />
+                <Node name="UI0-Public" expanded="True" />
+                <Node name="UI2" expanded="True">
+                  <Node name="3-Intelligence" expanded="True">
+                    <Node name="Automation" expanded="True" />
+                  </Node>
+                  <Node name="4-PersonalCenter" expanded="True">
+                    <Node name="PirDevice" expanded="True" />
+                  </Node>
+                </Node>
+              </Node>
             </Node>
           </Node>
         </State>
diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index d493641..09db486 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -63,7 +63,19 @@
         /// 鏄惁鎼滅储鏈湴缃戝叧鎴愬姛
         /// </summary>
         public bool IsSearchLocalGatewaySuccessful = false;
-      
+        /// <summary>
+        /// 鏄惁鏈湴鍔犲瘑锛岀洰鍓嶅彧瀵笰缃戝叧鏈夌敤
+        /// </summary>
+        public bool IsLocalEncrypt;
+        /// <summary>
+        /// 鍒ゆ柇鏄惁鏈湴鍔犲瘑骞朵笖鍔犲瘑key涓嶄负绌�
+        /// </summary>
+        public bool IsLocalEncryptAndGetAesKey {
+            get {
+                return IsLocalEncrypt && (!string.IsNullOrEmpty(DB_ResidenceData.Instance.CurrentRegion.localSecret));
+            }
+        }
+
         bool _GatewayOnline_Local = false;
         /// <summary>
         /// 缃戝叧鍦ㄧ嚎-灞�鍩熺綉
@@ -689,15 +701,45 @@
         /// <summary>
         /// 杞崲鍙戦�佹暟鎹�
         /// </summary>
-        public byte[] ConvertSendBodyData(string topic, string bodyDataString)
+        /// <param name="topic">涓婚</param>
+        /// <param name="bodyDataString">body鍐呭鏁版嵁</param>
+        /// <param name="isEncryption">鏄惁瑕佸body鍔犲瘑</param>
+        /// <returns></returns>
+        public byte[] ConvertSendBodyData(string topic, string bodyDataString, bool isEncryption = true)
         {
-            string topicString = "Topic:" + topic + "\r\n";
-            byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
-            string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
+            //string topicString = "Topic:" + topic + "\r\n";
+            //byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
+            //string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
 
-            string sendDataString = topicString + lengthString + bodyDataString;
-            byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString);
-            MainPage.Log($"杞崲HDL-Link鏁版嵁\r\n{sendDataString}\r\n");
+            //string sendDataString = topicString + lengthString + bodyDataString;
+            //byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString);
+            //MainPage.Log($"杞崲HDL-Link鏁版嵁\r\n{sendDataString}\r\n");
+
+            //***************************************************************
+            //2021-09-23 澧炲姞鏈湴閫氫俊鍔犲瘑澶勭悊
+            //1.鎷兼帴澶�
+            string topicString = "Topic:" + topic + "\r\n";
+            //2.Body瀛楃涓茶浆涓篵yte鏁扮粍
+            byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
+            //鍒ゆ柇鏄惁闇�鍔犲瘑Body鏁版嵁
+            if (isEncryption && IsLocalEncryptAndGetAesKey)
+            {
+                bodyBytes = Securitys.EncryptionService.AesEncryptPayload(bodyBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret);
+                //bodyDataString = Encoding.UTF8.GetString(bodyBytes);
+                //MainPage.Log($"杞崲HDL-Link鏁版嵁 鍔犲瘑key锛�" + DB_ResidenceData.Instance.CurrentRegion.localSecret);
+            }
+            //3.鎷兼帴body鐨凩ength闀垮害鏁版嵁
+            string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
+            string topicAndLengthString = topicString + lengthString;
+            byte[] topicAndLengthBytes = Encoding.ASCII.GetBytes(topicAndLengthString);
+            //4.鎷兼帴鍚堝苟 Topic 鍜� body鐨刡yte鏁扮粍鏁版嵁
+            byte[] sendDataBytes = new byte[topicAndLengthBytes.Length + bodyBytes.Length];
+            topicAndLengthBytes.CopyTo(sendDataBytes, 0);
+            bodyBytes.CopyTo(sendDataBytes, topicAndLengthBytes.Length);
+
+            //var sendDataString = Encoding.UTF8.GetString(sendDataBytes);
+            //MainPage.Log($"杞崲HDL-Link鏁版嵁\r\n{sendDataString}\r\n");
+            //***************************************************************
 
             return sendDataBytes;
         }
@@ -708,15 +750,19 @@
         public void ConvertReceiveData(byte[] receiveBytes)
         {
             var reString = Encoding.UTF8.GetString(receiveBytes);
-            AnalysisReceiveData(reString);
+            AnalysisReceiveData(reString, receiveBytes);
         }
         /// <summary>
         /// 杞崲鎺ユ敹鍒扮殑鏁版嵁
         /// </summary>
+        /// <param name="receiveString">杞琒tring鍚庣殑鏁版嵁</param>
+        /// <param name="originalReceiveBytes"鍘熷Bytes鏁版嵁</param>
         /// <returns></returns>
-        public LocalCommunicationData AnalysisReceiveData(string receiveString)
+        public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes)
         {
             LocalCommunicationData receiveObj = new LocalCommunicationData();
+
+            MainPage.Log($"灞�鍩熺綉淇℃伅: \r\n{receiveString}");
 
             var res = receiveString.Split("\r\n\r\n");
 
@@ -738,8 +784,7 @@
                     }
                 }
 
-                MainPage.Log($"灞�鍩熺綉淇℃伅: {receiveObj.Topic}  : 鍐呭: {res[1]}");
-
+                //MainPage.Log($"灞�鍩熺綉淇℃伅: {receiveObj.Topic}  : 鍐呭: {res[1]}");
 
                 //楠岃瘉鏈夋晥鏁版嵁闀垮害
                 //if (res[1].Length != receiveObj.Length)
@@ -748,6 +793,40 @@
                 //    return receiveObj;
                 //}
                 receiveObj.BodyDataString = res[1];
+
+                //2021-09-23 杩囨护涓嶉渶瑕佽В瀵嗙殑涓婚 鐩墠鎼滅储缃戝叧涓婚涓嶅姞瀵�
+                if (receiveObj.Topic != CommunicationTopic.SearchLoaclGatewayReply)
+                {
+                    //鍒ゆ柇褰撳墠缃戝叧鏄惁寮�鍚簡鏈湴鍔犲瘑
+                    if (IsLocalEncryptAndGetAesKey)
+                    {
+                        MainPage.Log($"灞�鍩熺綉淇℃伅 寮�濮嬭В瀵�");
+                        if (originalReceiveBytes != null)
+                        {
+                            //鎷垮埌鍘熷Bytes鏁版嵁鍘昏В瀵�
+                            byte[] topicBytes = Encoding.UTF8.GetBytes(res[0]);
+                            byte[] bodyBytes = new byte[receiveObj.Length];
+                            Array.Copy(originalReceiveBytes, topicBytes.Length + 4, bodyBytes, 0, receiveObj.Length);
+                            byte[] receiveBytes = Securitys.EncryptionService.AesDecryptPayload(bodyBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret);
+                            var revString = Encoding.UTF8.GetString(receiveBytes);
+                            receiveObj.BodyDataString = revString;
+                            MainPage.Log($"灞�鍩熺綉淇℃伅: 瑙e瘑鍚庯細" + receiveObj.BodyDataString);
+                        }
+                        else
+                        {
+                            //鐩墠涓嶆嬁鍘熷Bytes鏁版嵁 瑙e瘑涓嶄簡
+                            //byte[] receiveBytes = Encoding.UTF8.GetBytes(res[1]);
+                            //MainPage.Log($"灞�鍩熺綉淇℃伅 receiveBytes {receiveBytes.Length}");
+                            //receiveBytes = Securitys.EncryptionService.AesDecryptPayload(receiveBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret);
+                            //MainPage.Log($"灞�鍩熺綉淇℃伅 瑙e瘑鍚庯細receiveBytes {receiveBytes.Length}");
+                            //var revString = Encoding.UTF8.GetString(receiveBytes);
+                            //receiveObj.BodyDataString = revString;
+                            //MainPage.Log($"灞�鍩熺綉淇℃伅: 瑙e瘑鍚庯細" + receiveObj.BodyDataString);
+                        }
+
+                    }
+                }
+
 
                 if (receiveObj.Topic == CommunicationTopic.SearchLoaclGatewayReply || receiveObj.Topic == CommunicationTopic.GatewayBroadcast)
                 {
@@ -772,7 +851,10 @@
                         {
                             Ins.GatewayId = device.device_mac;
                         }
-                        reportIp = device.ip_address;//涓绘挱鍦板潃涔熻兘鎺у埗璁惧//"239.0.168.188";// 
+                        reportIp = device.ip_address;//涓绘挱鍦板潃涔熻兘鎺у埗璁惧//"239.0.168.188";//
+                        //2021-09-23 鏂板鑾峰彇褰撳墠缃戝叧鏄惁鏈湴鍔犲瘑
+                        Ins.IsLocalEncrypt = device.isLocalEncrypt;
+                        //MainPage.Log("缃戝叧鏈湴鍔犲瘑鐘舵�侊細" + device.local_encrypt.ToString());
                     }
                 }
                 else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
diff --git a/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs b/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs
index 82a2caa..295098b 100644
--- a/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs
+++ b/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs
@@ -182,7 +182,7 @@
         void AnalysisTcpData(Socket socket, string tcpBodyDataString)
         {
             MainPage.Log($"0001 tcpDataString:\r\n {tcpBodyDataString}");
-            var tcpDataObj = Control.Ins.AnalysisReceiveData(tcpBodyDataString);
+            var tcpDataObj = Control.Ins.AnalysisReceiveData(tcpBodyDataString, null);
             if (tcpDataObj.BodyDataString == null)
             {
                 return;
diff --git a/HDL_ON/DAL/DriverLayer/Control_Udp.cs b/HDL_ON/DAL/DriverLayer/Control_Udp.cs
index 37ee1e3..5707532 100644
--- a/HDL_ON/DAL/DriverLayer/Control_Udp.cs
+++ b/HDL_ON/DAL/DriverLayer/Control_Udp.cs
@@ -1128,7 +1128,7 @@
                 var sendJob = new JObject { { "id", Control.Ins.msg_id.ToString() }, { "time_stamp", Utlis.GetTimestamp ()} };
                 var bodyString = JsonConvert.SerializeObject(sendJob);
                 
-                var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString);
+                var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString, false);
                 //缁勬挱鍙戦��
                 packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
                 packet.HaveSendCount = 4;
diff --git a/HDL_ON/DAL/Server/HttpUtil.cs b/HDL_ON/DAL/Server/HttpUtil.cs
index cf4a06a..d2292ee 100644
--- a/HDL_ON/DAL/Server/HttpUtil.cs
+++ b/HDL_ON/DAL/Server/HttpUtil.cs
@@ -17,8 +17,8 @@
         /// 鍥哄畾鍩熷悕,姝e紡鐜
         /// 鍏叡鍩熷悕灏辫繎瑙f瀽
         /// </summary>
-        //public const string GlobalRequestHttpsHost = "https://nearest.hdlcontrol.com";
-        public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";//mmmm
+        public const string GlobalRequestHttpsHost = "https://nearest.hdlcontrol.com";
+        //public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";//mmmm
         /// <summary>
         /// RegionMark
         /// </summary>
diff --git a/HDL_ON/Entity/Device/DeviceModule.cs b/HDL_ON/Entity/Device/DeviceModule.cs
index ed330ea..bbb8cd6 100644
--- a/HDL_ON/Entity/Device/DeviceModule.cs
+++ b/HDL_ON/Entity/Device/DeviceModule.cs
@@ -40,5 +40,14 @@
         /// 鍖哄垎鏄痓us璁惧锛岃繕鏄痥nx璁惧
         /// </summary>
         public string driver_code = "";
+        /// <summary>
+        /// 鏄惁涓荤綉鍏�
+        /// </summary>
+        public bool master;
+        /// <summary>
+        /// 鏄惁寮�鍚簡鏈湴鍔犲瘑
+        /// 2021-09-23 娉ㄦ剰锛氫负浜嗗吋瀹规帶鍒朵互鍓嶄笉鍔犲瘑缃戝叧瑕佺敤杩欎釜鏂扮殑瀛楁锛屼笉鑳界敤local_encrypt锛屽洜涓哄彂鐜颁互鍓嶉儴鍒嗗浐浠跺凡缁忔湁杩欎釜鏍囪骞朵笖鍥炲true锛屼絾瀹為檯涓婇�氫俊杩樻病鍔犲瘑鐨�
+        /// </summary>
+        public bool isLocalEncrypt;
     }
 }
diff --git a/HDL_ON/Entity/ResponseEntity/RegionInfoRes.cs b/HDL_ON/Entity/ResponseEntity/RegionInfoRes.cs
index 7028c70..5708458 100644
--- a/HDL_ON/Entity/ResponseEntity/RegionInfoRes.cs
+++ b/HDL_ON/Entity/ResponseEntity/RegionInfoRes.cs
@@ -122,6 +122,10 @@
         /// 璋冭瘯鏉冮檺鏄惁鎵撳紑
         /// </summary>
         public bool debugPerm;
+        /// <summary>
+        /// 缃戝叧鏈湴閫氫俊鍔犲瘑Key锛屽悓涓�涓綇瀹� 瀵嗛挜鍒涘缓鍚庡氨涓嶅彉
+        /// </summary>
+        public string localSecret;
     }
 
 }

--
Gitblit v1.8.0