mirror of
https://github.com/softScheck/tplink-smartplug
synced 2026-01-11 23:38:46 +01:00
Merge pull request #67 from AMDHome/master
Refactored for native python3 support (maintain python2 support with version detection)
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import argparse
|
||||
from struct import pack
|
||||
@@ -66,23 +67,45 @@ commands = { 'info' : '{"system":{"get_sysinfo":{}}}',
|
||||
|
||||
# Encryption and Decryption of TP-Link Smart Home Protocol
|
||||
# XOR Autokey Cipher with starting key = 171
|
||||
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
|
||||
# Python 3.x Version
|
||||
if sys.version_info[0] > 2:
|
||||
def encrypt(string):
|
||||
key = 171
|
||||
result = pack('>I', len(string))
|
||||
for i in string:
|
||||
a = key ^ ord(i)
|
||||
key = a
|
||||
result += bytes([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
|
||||
def decrypt(string):
|
||||
key = 171
|
||||
result = ""
|
||||
for i in string:
|
||||
a = key ^ i
|
||||
key = i
|
||||
result += chr(a)
|
||||
return result
|
||||
|
||||
# Python 2.x Version
|
||||
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
|
||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||
@@ -123,4 +146,3 @@ try:
|
||||
|
||||
except socket.error:
|
||||
quit("Cound not connect to host " + ip + ":" + str(port))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user