From 60c9bbc4afbeb67ada00711b585d9b5634c29da0 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Wed, 1 Apr 2020 21:52:22 +0800 Subject: [PATCH] reply, proxy, create floder, bug fix... --- config.py | 18 ++++++++++++++++++ mswp.py | 5 +++-- plugins/input.py | 20 +++++++++++--------- plugins/net.py | 40 ++++++++++++++++++++++++++-------------- plugins/update.py | 2 +- tmp/placeholder | 0 6 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 tmp/placeholder diff --git a/config.py b/config.py index 6a229c6..ef06da9 100644 --- a/config.py +++ b/config.py @@ -57,6 +57,24 @@ class Jsondata: if self.auto_save: pass + +def create_floder(path): + pathlist = list(os.path.split(path)) + pathlist.pop() + flordpath = os.path.join(pathlist) + + if not os.path.exists(flordpath): + _create_floder(flordpath) + +def _create_floder(path): + pathlist = list(os.path.split(path)) + pathlist.pop() + flordpath = os.path.join(pathlist) + + if not os.path.exists(flordpath): + _create_floder(flordpath) + os.mkdir(flordpath) + global_config = {} msw_queue = queue.Queue() jsondata = Jsondata() diff --git a/mswp.py b/mswp.py index 7c1898a..572447a 100644 --- a/mswp.py +++ b/mswp.py @@ -93,8 +93,9 @@ class Datapack: ndp = copy.copy(self) ndp.app = ndp.head['from'] ndp.method = 'reply' - ndp.head['to'] = self.head['id'] - ndp.id = ID + if not self.head['id'] == ID: # net package + ndp.head['to'] = self.head['id'] + ndp.head['id'] = ID return ndp diff --git a/plugins/input.py b/plugins/input.py index 41beb8a..6f4d7b3 100644 --- a/plugins/input.py +++ b/plugins/input.py @@ -4,6 +4,7 @@ import os from mswp import Datapack from forwarder import receive_queues, send_queue from config import msw_queue +from config import dprint as print receive_queue = receive_queues[__name__] @@ -15,11 +16,17 @@ def main(): print('Error in %s, %s: %s' % (__name__, type(e), str(e))) +def print_reply_func(): + while True: + dp = receive_queue.get() + dp.encode() + print(dp.encode_data.decode()) + + def _main(): file_flag = False while True: file_flag = False - net_flag = False raw_data = input() if raw_data == 'restart': @@ -33,12 +40,6 @@ def _main(): raw_data = raw_data[6:] file_flag = True - if raw_data[:5] == '(net ': # like "(net miku)log: hello" or "(file)(net miku)log: filename.exe" - index = raw_data.index(')') - to = raw_data[5:index] - raw_data = raw_data[index+1:] - net_flag = True - first_index, last_index = find_index(raw_data) app = raw_data[:first_index] body = raw_data[last_index:] @@ -56,8 +57,6 @@ def _main(): app = app.replace(' ', '') dp = Datapack(head={'from': __name__}) - if net_flag: - dp.head.update({'to': to}) dp.head.update(ihead) @@ -72,6 +71,7 @@ def _main(): dp.body = body.encode() send_queue.put(dp) + print('Command has been sent', dp) def find_index(raw_data): @@ -84,3 +84,5 @@ def find_index(raw_data): thread = threading.Thread(target=main, args=(), daemon=True) thread.start() +thread_print_reply_func = threading.Thread(target=print_reply_func, args=(), daemon=True) +thread_print_reply_func.start() diff --git a/plugins/net.py b/plugins/net.py index 46d2352..b5ed293 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -8,7 +8,7 @@ import random import time from mswp import Datapack from forwarder import receive_queues, send_queue -from config import jsondata +from config import jsondata, create_floder from config import dprint as print receive_queue = receive_queues[__name__] @@ -94,6 +94,7 @@ class Network_controller: # manage id and connection conn.connect(addr) except Exception as e: #print('Connect to %s failed, %s: %s' % (str(addr), type(e), str(e))) + del(e) return connection = Connection(conn, addr, self, positive=True, conntype=conntype) @@ -126,12 +127,19 @@ class Network_controller: # manage id and connection def process_command(self, dp): if dp.body == b'status': - print('Online %s' % str(self.id_dict)) - print('proxydict %s' % str(self.proxydict)) - print('conflist %s' % str(self.conflist)) - print('conflist_pass %s' % str(self.conflist_pass)) - print('mhtlist %s' % str(self.mhtlist)) - print('mhtlist_pass %s' % str(self.mhtlist_pass)) + result = '' + result += 'Online %s' % str(self.id_dict) + '\n' + result += 'proxydict %s' % str(self.proxydict) + '\n' + result += 'conflist %s' % str(self.conflist) + '\n' + result += 'conflist_pass %s' % str(self.conflist_pass) + '\n' + result += 'netlist %s' % str(self.netlist) + '\n' + result += 'netlist_pass %s' % str(self.netlist_pass) + '\n' + result += 'mhtlist %s' % str(self.mhtlist) + '\n' + result += 'mhtlist_pass %s' % str(self.mhtlist_pass) + + ndp = dp.reply() + ndp.body = result.encode() + send_queue.put(ndp) elif dp.body == b'mht' and dp.method == 'get': ndp = dp.reply() @@ -264,7 +272,7 @@ class Network_controller: # manage id and connection if not addr in xxxlist_pass: xxxlist_pass.append(addr) - print('%s connected' % id) + print('<%s> %s connected' % (connection.flag, id)) def del_connection(self, connection): @@ -283,7 +291,7 @@ class Network_controller: # manage id and connection if addr in xxxlist_pass: xxxlist_pass.remove(addr) - print('%s disconnected' % id) + print('<%s> %s disconnected' % (connection.flag, id)) def getlist(self, conntype): @@ -304,6 +312,7 @@ class Connection: self.addr = addr self.netowrk_controller = netowrk_controller self.id = None + self.flag = None self.buff = b'' self.padding_queue = queue.Queue() self.thread_send = None @@ -324,13 +333,13 @@ class Connection: def _init(self): # init to check connection id, threading - err_code, flag = self.check_id() + err_code, self.flag = self.check_id() if err_code: #print('<%s> Init connection failed, connection closed, code: %s' % (flag, err_code)) self.conn.close() return - self.netowrk_controller.set_connection(self) + self.netowrk_controller.set_connection(self,) self.thread_send = threading.Thread(target=self.send_func, args=(), daemon=True) self.thread_send.start() @@ -358,7 +367,9 @@ class Connection: dp.encode_data = self.buff try: self.buff = dp.decode(only_head=True) - + + if dp.method == 'file': + create_floder(dp.head['filename']) if dp.method == 'file' and os.path.exists(dp.head['filename']): os.remove(dp.head['filename']) @@ -372,7 +383,7 @@ class Connection: if still_need > len(self.buff): # writing tmp data if dp.method == 'file': - with open(dp.head['filename'], 'ab') as f: + with open('tmp/' + dp.head['filename'], 'ab') as f: still_need -= f.write(self.buff) else: dp.body += self.buff @@ -381,7 +392,7 @@ class Connection: else: # download complete setuation if dp.method == 'file': - with open(dp.head['filename'], 'ab') as f: + with open('tmp/' + dp.head['filename'], 'ab') as f: f.write(self.buff[:still_need]) else: dp.body = self.buff[:still_need] @@ -390,6 +401,7 @@ class Connection: # bleow code are using to process datapack if dp.method == 'file': + os.rename('tmp/' + dp.head['filename'], dp.head['filename']) print('Received file %s' % dp.head['filename'], dp) send_queue.put(dp) diff --git a/plugins/update.py b/plugins/update.py index 0f2a62f..ee74c5d 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -7,7 +7,7 @@ from config import msw_queue receive_queue = receive_queues[__name__] -remove_file_list = ['__init__.py', 'addrlist.txt', 'config.json', 'logger.log'] +remove_file_list = ['__init__.py', 'addrlist.txt', 'config.json', 'logger.log', 'update.tar.xz'] remove_dir_list = ['.git', '.idea', '__pycache__', 'resources'] diff --git a/tmp/placeholder b/tmp/placeholder new file mode 100644 index 0000000..e69de29