Merge a6ca781ad8
into a8f9e9d65c
This commit is contained in:
commit
d5b901a0cd
1 changed files with 74 additions and 57 deletions
|
@ -19,13 +19,15 @@
|
|||
# limitations under the License.
|
||||
#
|
||||
#
|
||||
from __future__ import print_function
|
||||
import socket
|
||||
import argparse
|
||||
from builtins import bytes
|
||||
version = 0.2
|
||||
|
||||
version = 0.1
|
||||
|
||||
# Check if IP is valid
|
||||
def validIP(ip):
|
||||
"""Check if IP is valid"""
|
||||
try:
|
||||
socket.inet_pton(socket.AF_INET, ip)
|
||||
except socket.error:
|
||||
|
@ -49,30 +51,46 @@ 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 = "\0\0\0\0"
|
||||
# for i in string:
|
||||
# a = key ^ ord(i)
|
||||
# key = a
|
||||
# result += chr(a)
|
||||
# return result
|
||||
|
||||
|
||||
def encrypt(string):
|
||||
key = 171
|
||||
result = "\0\0\0\0"
|
||||
for i in string:
|
||||
a = key ^ ord(i)
|
||||
result = b"\0\0\0" + bytes([len(string)])
|
||||
for i in bytes(string.encode('latin-1')):
|
||||
a = key ^ i
|
||||
key = a
|
||||
result += chr(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
|
||||
result = b""
|
||||
for i in bytes(string):
|
||||
a = key ^ i
|
||||
key = i
|
||||
result += bytes([a])
|
||||
return result.decode('latin-1')
|
||||
|
||||
# Parse commandline arguments
|
||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP)
|
||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug " +
|
||||
"Client v" + str(version))
|
||||
parser.add_argument("-t", "--target", metavar="<ip>", required=True,
|
||||
help="Target IP Address", type=validIP)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
|
||||
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
|
||||
group.add_argument("-c", "--command", metavar="<command>",
|
||||
help="Preset command to send. Choices are: " +
|
||||
", ".join(commands), choices=commands)
|
||||
group.add_argument("-j", "--json", metavar="<JSON string>",
|
||||
help="Full JSON string of command to send")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Set target IP, port and command to send
|
||||
|
@ -83,9 +101,8 @@ if args.command is None:
|
|||
else:
|
||||
cmd = commands[args.command]
|
||||
|
||||
|
||||
|
||||
# Send command and receive reply
|
||||
|
||||
try:
|
||||
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock_tcp.connect((ip, port))
|
||||
|
@ -93,7 +110,7 @@ try:
|
|||
data = sock_tcp.recv(2048)
|
||||
sock_tcp.close()
|
||||
|
||||
print "Sent: ", cmd
|
||||
print "Received: ", decrypt(data[4:])
|
||||
print("Sent: ", cmd)
|
||||
print("Received: ", decrypt(data[4:]))
|
||||
except socket.error:
|
||||
quit("Cound not connect to host " + ip + ":" + str(port))
|
||||
|
|
Loading…
Add table
Reference in a new issue