2
0
mirror of https://github.com/softScheck/tplink-smartplug synced 2026-01-11 23:38:46 +01:00

Refactored code for py3 while keeping py2 support (not using future)

This commit is contained in:
Samuel Tseng
2020-02-19 23:56:55 -08:00
parent 395c352100
commit 2ab78544ae

View File

@@ -19,6 +19,7 @@
# limitations under the License. # limitations under the License.
# #
import sys
import socket import socket
import argparse import argparse
from struct import pack from struct import pack
@@ -51,23 +52,43 @@ commands = {'info' : '{"system":{"get_sysinfo":{}}}',
# Encryption and Decryption of TP-Link Smart Home Protocol # Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171 # XOR Autokey Cipher with starting key = 171
def encrypt(string): if sys.version_info[0] > 2:
key = 171 def encrypt(string):
result = pack('>I', len(string)) key = 171
for i in string: result = pack('>I', len(string))
a = key ^ ord(i) for i in string:
key = a a = key ^ ord(i)
result += chr(a) key = a
return result result += bytes([a])
return result
def decrypt(string): def decrypt(string):
key = 171 key = 171
result = "" result = ""
for i in string: for i in string:
a = key ^ ord(i) a = key ^ i
key = ord(i) key = i
result += chr(a) result += chr(a)
return result return result
else:
def encrypt(string):
key = 171
result = pack('>I', len(string))
for i in string:
a = key ^ ord(i)
key = a
result += chr(a)
return result
def decrypt(string):
key = 171
result = ""
for i in string:
a = key ^ ord(i)
key = ord(i)
result += chr(a)
return result
# Parse commandline arguments # Parse commandline arguments
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version)) parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
@@ -96,8 +117,7 @@ try:
data = sock_tcp.recv(2048) data = sock_tcp.recv(2048)
sock_tcp.close() sock_tcp.close()
print "Sent: ", cmd print("Sent: ", cmd)
print "Received: ", decrypt(data[4:]) print("Received: ", decrypt(data[4:]))
except socket.error: except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port)) quit("Cound not connect to host " + ip + ":" + str(port))