|
36 | 36 | from hub.services.broadcast.broadcast import Broadcast |
37 | 37 | from hub.services.shadow.shadow import Shadow |
38 | 38 | from hub.services.ota.ota import Ota |
| 39 | +import os |
39 | 40 |
|
40 | 41 | class SingletonType(type): |
41 | 42 | _instance_lock = threading.Lock() |
@@ -901,17 +902,49 @@ def dynregDevice(self, timeout=10, dynregDomain=None): |
901 | 902 | if 'Len' in resp and resp['Len'] > 0: |
902 | 903 | reply_obj_data = reply_obj['Response']["Payload"] |
903 | 904 | if reply_obj_data is not None: |
904 | | - psk = self.__codec._AESUtil.decrypt(reply_obj_data.encode('UTF-8') , product_secret[:self.__codec._AESUtil.BLOCK_SIZE_16].encode('UTF-8'), |
| 905 | + payload = self.__codec._AESUtil.decrypt(reply_obj_data.encode('UTF-8') , product_secret[:self.__codec._AESUtil.BLOCK_SIZE_16].encode('UTF-8'), |
905 | 906 | '0000000000000000'.encode('UTF-8')) |
906 | | - psk = psk.decode('UTF-8', 'ignore').strip().strip(b'\x00'.decode()) |
907 | | - user_dict = json.loads(psk) |
| 907 | + payload = payload.decode('UTF-8', 'ignore').strip().strip(b'\x00'.decode()) |
| 908 | + user_dict = json.loads(payload) |
908 | 909 | self._logger.info('encrypt type: {}'.format( |
909 | 910 | user_dict['encryptionType'])) |
910 | 911 |
|
911 | | - self.__device_info.update_config_file(user_dict['psk']) |
912 | | - self.__protocol_reinit() |
913 | | - |
914 | | - return 0, user_dict['psk'] |
| 912 | + encryptionType = user_dict['encryptionType'] |
| 913 | + |
| 914 | + if encryptionType is not None: |
| 915 | + if encryptionType == 2: #2表示密钥认证 |
| 916 | + self.__device_info.update_config_file(user_dict['psk']) |
| 917 | + self.__protocol_reinit() |
| 918 | + return 0, user_dict['psk'] |
| 919 | + |
| 920 | + elif encryptionType == 1: #1表示证书认证 |
| 921 | + #获取证书和秘钥 |
| 922 | + cert = user_dict['clientCert'] |
| 923 | + privateKey = user_dict['clientKey'] |
| 924 | + #cert文件创建路径 |
| 925 | + current_path = os.getcwd() |
| 926 | + cert_file_path = os.path.join(current_path,'cert.txt') |
| 927 | + cert_file = open(cert_file_path,'w') |
| 928 | + cert_file.write(cert) |
| 929 | + cert_file.close() |
| 930 | + #privateKey文件创建路径 |
| 931 | + privateKey_file_path = os.path.join(current_path,'privateKey.txt') |
| 932 | + privateKey_file = open(privateKey_file_path,'w') |
| 933 | + privateKey_file.write(privateKey) |
| 934 | + privateKey_file.close() |
| 935 | + |
| 936 | + #替换json文件对应路径 |
| 937 | + self.__device_info.update_cert_config_file(cert_file_path) |
| 938 | + self.__device_info.update_privateKey_config_file(privateKey_file_path) |
| 939 | + self.__protocol_reinit() |
| 940 | + |
| 941 | + return 0, 'success' |
| 942 | + else: |
| 943 | + self._logger.warring('encryptionType is other type') |
| 944 | + return -1, 'encryptionType is other type' |
| 945 | + else: |
| 946 | + self._logger.warring('encryptionType is null') |
| 947 | + return -1, 'encryptionType is null' |
915 | 948 | else: |
916 | 949 | self._logger.warring('payload is null') |
917 | 950 | return -1, 'payload is null' |
|
0 commit comments