From 06ea34dee9c691ce8861acbc00fc9062fd99bfee Mon Sep 17 00:00:00 2001 From: PixeLInc Date: Wed, 9 May 2018 23:44:00 -0500 Subject: [PATCH 1/3] Refactor --- .../aws-based-rc/client/.gitignore | 1 + .../aws-based-rc/client/PythonClient.py | 96 ------------------- .../client/PythonClient/PythonClient.py | 96 ------------------- .../aws-based-rc/client/__init__.py | 0 .../aws-based-rc/client/client.py | 62 ++++++++++++ .../aws-based-rc/client/run.py | 26 +++++ .../aws-based-rc/client/winapi.py | 30 ++++++ 7 files changed, 119 insertions(+), 192 deletions(-) create mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/.gitignore delete mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient.py delete mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient/PythonClient.py create mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/__init__.py create mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/client.py create mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/run.py create mode 100644 Unofficial/Python/remote-control-client/aws-based-rc/client/winapi.py diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/.gitignore b/Unofficial/Python/remote-control-client/aws-based-rc/client/.gitignore new file mode 100644 index 0000000..96403d3 --- /dev/null +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/.gitignore @@ -0,0 +1 @@ +__pycache__/* diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient.py deleted file mode 100644 index 0267019..0000000 --- a/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient.py +++ /dev/null @@ -1,96 +0,0 @@ -#Written for Python 3.6.x - -import urllib.request -import json -import ctypes -import random -import string -import time -import os -from subprocess import call -dir_path = os.path.dirname(os.path.realpath(__file__)) - -API_BASE = 'your_api_url/' -API_KEY = 'your_api_key' - - -#Different Types Of Boxes -#Ok Only -MB_OK = 0x0 -#Ok and Cancel -MB_OKCXL = 0x01 -#Yes, No and Cancel -MB_YESNOCXL = 0x03 -#Yes and No -MB_YESNO = 0x04 -#Help Modifier - Largely useless -MB_HELP = 0x4000 -#Icons -#Exclamation Point -ICON_EXCLAIM = 0x30 -#Lowercase i in circle -ICON_INFO = 0x40 -#Question Mark -ICON_QUESTION = 0x20 -#"Stop" Cross -ICON_STOP = 0x10 - -btypes_map = {"okonly": MB_OK, "okcancel": MB_OKCXL, "yesnocancel": MB_YESNOCXL, "yesno": MB_YESNO} -bicons_map = {"exclaim": ICON_EXCLAIM, "info": ICON_INFO, "question": ICON_QUESTION, "stop": ICON_STOP} - -def Mbox(title, content, type=MB_OK, icon=None): - if icon == None: - result = ctypes.windll.user32.MessageBoxW(0, content, title, type) - else: - result = ctypes.windll.user32.MessageBoxW(0, content, title, type | icon) - return result - -def generateClientID(): - cID = "" - choices = [list(string.ascii_letters), list(string.digits)] - for i in range(0, random.randint(12,14)): - cID = cID + random.choice(random.choice(choices)) - Mbox("Client ID", cID, MB_OK, ICON_INFO) - return cID - -cID = generateClientID() - -def runner(cID): - with urllib.request.urlopen(API_BASE+cID+"?key="+API_KEY) as response: - jsonR = response.read() - jsonR = json.loads(jsonR) - return jsonR - -#Mbox("Wirus Alert", "I am wirus push ok to accept wirus to you network. you need fixation network security", MB_OK, ICON_EXCLAIM) - -while True: - result = runner(cID) - for act in result: - if act['clientID'] == cID: - #Good the clientID Matches - if act['action'] == "messagebox": - rData = act['data'] - rData = json.loads(rData) - try: - Mbox(rData['title'], rData['message'], btypes_map[rData['type']], bicons_map[rData['icon_type']]) - except KeyError: - Mbox(rData['title'], rData['message'], btypes_map["okonly"]) - elif act['action'] == "showimage": - rData = act['data'] - rData = json.loads(rData) - try: - opener = urllib.request.build_opener() - opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] - urllib.request.install_opener(opener) - urllib.request.urlretrieve(rData['url'], 'tmp.image') - #Lazy - Run the windows photo viewer of the just dl-d image. - call('rundll32 "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen {}\\tmp.image'.format(dir_path)) - except: - pass - else: - pass - #NO IDEA WHAT THIS ACTION IS! - else: - pass - #Ignore the message, it's for someone else, shouldn't be encountered but edge case. - time.sleep(20) diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient/PythonClient.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient/PythonClient.py deleted file mode 100644 index 0267019..0000000 --- a/Unofficial/Python/remote-control-client/aws-based-rc/client/PythonClient/PythonClient.py +++ /dev/null @@ -1,96 +0,0 @@ -#Written for Python 3.6.x - -import urllib.request -import json -import ctypes -import random -import string -import time -import os -from subprocess import call -dir_path = os.path.dirname(os.path.realpath(__file__)) - -API_BASE = 'your_api_url/' -API_KEY = 'your_api_key' - - -#Different Types Of Boxes -#Ok Only -MB_OK = 0x0 -#Ok and Cancel -MB_OKCXL = 0x01 -#Yes, No and Cancel -MB_YESNOCXL = 0x03 -#Yes and No -MB_YESNO = 0x04 -#Help Modifier - Largely useless -MB_HELP = 0x4000 -#Icons -#Exclamation Point -ICON_EXCLAIM = 0x30 -#Lowercase i in circle -ICON_INFO = 0x40 -#Question Mark -ICON_QUESTION = 0x20 -#"Stop" Cross -ICON_STOP = 0x10 - -btypes_map = {"okonly": MB_OK, "okcancel": MB_OKCXL, "yesnocancel": MB_YESNOCXL, "yesno": MB_YESNO} -bicons_map = {"exclaim": ICON_EXCLAIM, "info": ICON_INFO, "question": ICON_QUESTION, "stop": ICON_STOP} - -def Mbox(title, content, type=MB_OK, icon=None): - if icon == None: - result = ctypes.windll.user32.MessageBoxW(0, content, title, type) - else: - result = ctypes.windll.user32.MessageBoxW(0, content, title, type | icon) - return result - -def generateClientID(): - cID = "" - choices = [list(string.ascii_letters), list(string.digits)] - for i in range(0, random.randint(12,14)): - cID = cID + random.choice(random.choice(choices)) - Mbox("Client ID", cID, MB_OK, ICON_INFO) - return cID - -cID = generateClientID() - -def runner(cID): - with urllib.request.urlopen(API_BASE+cID+"?key="+API_KEY) as response: - jsonR = response.read() - jsonR = json.loads(jsonR) - return jsonR - -#Mbox("Wirus Alert", "I am wirus push ok to accept wirus to you network. you need fixation network security", MB_OK, ICON_EXCLAIM) - -while True: - result = runner(cID) - for act in result: - if act['clientID'] == cID: - #Good the clientID Matches - if act['action'] == "messagebox": - rData = act['data'] - rData = json.loads(rData) - try: - Mbox(rData['title'], rData['message'], btypes_map[rData['type']], bicons_map[rData['icon_type']]) - except KeyError: - Mbox(rData['title'], rData['message'], btypes_map["okonly"]) - elif act['action'] == "showimage": - rData = act['data'] - rData = json.loads(rData) - try: - opener = urllib.request.build_opener() - opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] - urllib.request.install_opener(opener) - urllib.request.urlretrieve(rData['url'], 'tmp.image') - #Lazy - Run the windows photo viewer of the just dl-d image. - call('rundll32 "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen {}\\tmp.image'.format(dir_path)) - except: - pass - else: - pass - #NO IDEA WHAT THIS ACTION IS! - else: - pass - #Ignore the message, it's for someone else, shouldn't be encountered but edge case. - time.sleep(20) diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/__init__.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py new file mode 100644 index 0000000..0c26b4a --- /dev/null +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py @@ -0,0 +1,62 @@ +# Written for Python 3.6.x + +import json +import os +import random +import string +import subprocess +import time +from urllib import request + +from winapi import WinApi + +class Client: + + def __init__(self, api_base, api_key): + self.api_base = api_base + self.api_key = api_key + + self.client_id = self.generate_id() + self.current_path = os.path.dirname(os.path.realpath(__file__)) + + def generate_id(self): + client_id = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(12, 14))) + + WinApi.show_message('Client ID', client_id, WinApi.BoxType.MB_OK, WinApi.IconType.INFO) + + return client_id + + def get_response(self, client_id): + with request.urlopen(f'{self.api_base}{client_id}?key={self.api_key}') as response: + return json.loads(response.read()) + + def loop(self): + while True: + response = self.get_response(self.client_id) + + for item in response: + # Check if the server echos back the client id + if item['clientID'] == self.client_id: + action = item['action'] + + if item['data']: + data = item['data'] + + if action == 'messagebox': + WinApi.show_message( + data['title'] or '', + data['message'] or '', + WinApi.box_type(data['type'] or ''), + data['icon'] and WinApi.IconType[data['icon']] or None + ) + + elif action == 'showimage': + opener = request.build_opener() + opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] + request.install_opener(opener) + + request.urlretrieve(data['url'], 'image.tmp') + + subprocess.call(f'rundll32 C:\Program Files\Windows Photo Viewer\PhotoViewer.dll, ImageView_FullScreen {self.current_path}\\image.tmp') + + time.sleep(20) diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/run.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/run.py new file mode 100644 index 0000000..b03c977 --- /dev/null +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/run.py @@ -0,0 +1,26 @@ +# A file for you to run the client with ease. +from optparse import OptionParser +import os +import sys + +from client import Client + +parser = OptionParser() +parser.add_option('-k', '--key', dest='api_key', help='The Api Key', metavar='api_key', default=None) +parser.add_option('-b', '--base', dest='api_base', help='Base url of the api', metavar='base_url', default=None) + +(options, args) = parser.parse_args() + +API_KEY = os.environ.get('AWS_CONTROL_API_KEY') or options.api_key +API_BASE = os.environ.get('AWS_CONTROL_API_BASE') or options.api_base + +if not API_KEY or not API_BASE: + print('Invalid API_KEY/API_BASE set, please use `file.py -k -b ` or set the `AWS_CONTROL_API_KEY/AWS_CONTROL_API_BASE` env vars!') + sys.exit(0) + +client = Client(API_BASE, API_KEY) + +# loop blocks until it's exited +client.loop() + +print('Exiting program...') diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/winapi.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/winapi.py new file mode 100644 index 0000000..ac3bc20 --- /dev/null +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/winapi.py @@ -0,0 +1,30 @@ +from enum import Enum +import ctypes + + +class WinApi: + class BoxType(Enum): + MB_OK = 0x0 + MB_OKCXL = 0x01 + MB_YESNOCXL = 0x03 + MB_YESNO = 0x04 + MB_HELP = 0x4000 + + class IconType(Enum): + EXCLAIM = 0x30 + INFO = 0x40 + QUESTION = 0x20 + STOP = 0x10 + + READABLE_MAP = { + 'okonly': BoxType.MB_OK, + 'okcancel': BoxType.MB_OKCXL, + 'yesnocancel': BoxType.MB_YESNOCXL, + 'yesno': BoxType.MB_YESNO + } + + def box_type(string): + return WinApi.READABLE_MAP.get(string) or WinApi.BoxType.MB_OK + + def show_message(title, content, box_type=BoxType.MB_OK, icon=None): + return icon and ctypes.windll.user32.MessageBoxW(0, content, box_type.value | icon.value) or ctypes.windll.user32.MessageBoxW(0, content, title, box_type.value) From 9f91623f1f5f8cc71c5a1c321a187efc3fd0fc80 Mon Sep 17 00:00:00 2001 From: PixeLInc Date: Thu, 10 May 2018 02:25:24 -0500 Subject: [PATCH 2/3] Fixes --- .../aws-based-rc/client/client.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py index 0c26b4a..653feec 100644 --- a/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py @@ -36,18 +36,18 @@ def loop(self): for item in response: # Check if the server echos back the client id - if item['clientID'] == self.client_id: - action = item['action'] + if item.get('clientID') == self.client_id: + action = item.get('action') - if item['data']: - data = item['data'] + if action and item.get('data'): + data = json.loads(item['data']) if action == 'messagebox': WinApi.show_message( - data['title'] or '', - data['message'] or '', - WinApi.box_type(data['type'] or ''), - data['icon'] and WinApi.IconType[data['icon']] or None + data.get('title') or '', + data.get('message') or '', + WinApi.box_type(data.get('type') or ''), + data.get('icon') and WinApi.IconType[data['icon'].upper()] or None ) elif action == 'showimage': @@ -55,8 +55,8 @@ def loop(self): opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] request.install_opener(opener) - request.urlretrieve(data['url'], 'image.tmp') + request.urlretrieve(data.get('url') or '', 'image.tmp') - subprocess.call(f'rundll32 C:\Program Files\Windows Photo Viewer\PhotoViewer.dll, ImageView_FullScreen {self.current_path}\\image.tmp') + subprocess.call(f'rundll32 "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen {self.current_path}\\image.tmp') time.sleep(20) From 2b61c5a5823b8935aacdfd50be980c6b67716385 Mon Sep 17 00:00:00 2001 From: PixeLInc Date: Mon, 18 Jun 2018 03:54:00 -0500 Subject: [PATCH 3/3] Try/Except & Global Opener --- .../aws-based-rc/client/client.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py index 653feec..526a566 100644 --- a/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py +++ b/Unofficial/Python/remote-control-client/aws-based-rc/client/client.py @@ -6,10 +6,13 @@ import string import subprocess import time + from urllib import request +from urllib.error import URLError from winapi import WinApi + class Client: def __init__(self, api_base, api_key): @@ -19,6 +22,10 @@ def __init__(self, api_base, api_key): self.client_id = self.generate_id() self.current_path = os.path.dirname(os.path.realpath(__file__)) + opener = request.build_opener() + opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] + request.install_opener(opener) + def generate_id(self): client_id = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(12, 14))) @@ -27,13 +34,20 @@ def generate_id(self): return client_id def get_response(self, client_id): - with request.urlopen(f'{self.api_base}{client_id}?key={self.api_key}') as response: - return json.loads(response.read()) + try: + with request.urlopen(f'{self.api_base}{client_id}?key={self.api_key}') as response: + return json.loads(response.read()) + except URLError as e: + print(f'Failed to open url: {e}') + return None def loop(self): while True: response = self.get_response(self.client_id) + if response is None: + continue + for item in response: # Check if the server echos back the client id if item.get('clientID') == self.client_id: @@ -51,11 +65,11 @@ def loop(self): ) elif action == 'showimage': - opener = request.build_opener() - opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; x64; rv:60.0) Gecko/20100101 Firefox/60.0')] - request.install_opener(opener) - - request.urlretrieve(data.get('url') or '', 'image.tmp') + try: + request.urlretrieve(data.get('url') or '', 'image.tmp') + except URLError as e: + print('Failed to retrieve image: {e}') + continue subprocess.call(f'rundll32 "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen {self.current_path}\\image.tmp')