diff --git a/bug.py b/bug.py deleted file mode 100644 index c8a1b96..0000000 --- a/bug.py +++ /dev/null @@ -1,15 +0,0 @@ -class A: - def __init__(self, arg=None): - if arg is None: - self.arg = {} - - def set(self): - self.arg['something'] = 'something' - - -a1 = A() -a1.set() -a2 = A() - -print(a2.arg) -# output is {'something': 'something'} diff --git a/config.py b/config.py index d3e2706..0d80de3 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,7 @@ import threading import json import time +import queue class Jsondata: @@ -11,7 +12,7 @@ class Jsondata: self.raw_jsondata = jsondata self.auto_save = auto_save self.auto_save_time = auto_save_time - self.thread = threading.Thread(target=self.run, args=()) + self.thread = threading.Thread(target=self.run, args=(), daemon=True) self.thread.start() def try_to_read_jsondata(self, key, or_value, template=0, output=True): @@ -36,5 +37,6 @@ class Jsondata: if self.auto_save: pass -global_config = {} +global_config = {} +msw_queue = queue.Queue() jsondata = Jsondata() \ No newline at end of file diff --git a/forwarder.py b/forwarder.py index b45957a..a73d428 100644 --- a/forwarder.py +++ b/forwarder.py @@ -51,5 +51,5 @@ def process_reforware(dp): return dp.app, dp -thread = threading.Thread(target=send_queue_function, args=()) +thread = threading.Thread(target=send_queue_function, args=(), daemon=True) thread.start() diff --git a/logger.log b/logger.log index d7e22a5..c62018d 100644 --- a/logger.log +++ b/logger.log @@ -3,3 +3,9 @@ plugins.input: asdfasf plugins.input: alskdfaj plugins.input: alsdkfj plugins.input: testtest +plugins.input: hello +plugins.input: asldjfalsdjf +plugins.input: lajsdflajsdlkf +plugins.input: alsdjflasjdlkfjasldfjalsdfj +network: +network: diff --git a/main.py b/main.py new file mode 100644 index 0000000..6393e55 --- /dev/null +++ b/main.py @@ -0,0 +1,10 @@ +import os +import os.path +import sys + +python = sys.executable + +while True: + code = os.system('python msw.py') + if code: + break \ No newline at end of file diff --git a/msw.py b/msw.py index 6964ea2..d94ca7c 100644 --- a/msw.py +++ b/msw.py @@ -4,9 +4,9 @@ PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.append(PATH) os.chdir(PATH) -from mswp import Datapack import threading -from config import jsondata, global_config +from mswp import Datapack +from config import jsondata, global_config, msw_queue print('Building plugins import script...') @@ -36,3 +36,6 @@ import plugins print('Plugins import finished') +# restart +code = msw_queue.get() +sys.exit(code) diff --git a/mswp.py b/mswp.py index 6bdf27f..a74dbce 100644 --- a/mswp.py +++ b/mswp.py @@ -1,6 +1,9 @@ import os +import random +import hashlib from config import jsondata + ''' A datapack must like: --------------------- @@ -22,7 +25,7 @@ BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) class Datapack: def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'', - check_head=True, file=None): + check_head=True, file=None, gen_flag=True): self.id = jsondata.try_to_read_jsondata('id', 'unknown_id') if head is None: head = {} @@ -41,6 +44,11 @@ class Datapack: self.head = head self.body = body self.encode_data = b'' + if gen_flag: + randseed = str(random.random()).encode() + h = hashlib.blake2b(digest_size = 4) + h.update(randseed) + self.head['flag'] = h.hexdigest() def encode(self): if self.method == 'file': diff --git a/plugins/input.py b/plugins/input.py index 2dbc03b..a379784 100644 --- a/plugins/input.py +++ b/plugins/input.py @@ -3,16 +3,32 @@ import copy import os from mswp import Datapack from forwarder import receive_queues, send_queue +from config import msw_queue receive_queue = receive_queues[__name__] def main(): + while True: + try: + _main() + except Exception as e: + print('Error in %s, %s: %s' % (__name__, type(e), str(e))) + + +def _main(): file_flag = False while True: file_flag = False net_flag = False raw_data = input() + if raw_data == 'restart': + msw_queue.put(0) + break + if raw_data == 'exit': + msw_queue.put(1) + break + if raw_data[:6] == '(file)': # like "(file)log: filename.exe" raw_data = raw_data[6:] file_flag = True @@ -61,5 +77,5 @@ def find_the_last(indata): # find the last ":" index return first_index, last_index -thread = threading.Thread(target=main, args=()) +thread = threading.Thread(target=main, args=(), daemon=True) thread.start() diff --git a/plugins/log.py b/plugins/log.py index ba3ed10..b0bdbe7 100644 --- a/plugins/log.py +++ b/plugins/log.py @@ -14,6 +14,6 @@ def main(): from_app_name = 'Unknown' f.write(from_app_name + ': ' + dp.body.decode() + '\n') -thread = threading.Thread(target=main, args=()) +thread = threading.Thread(target=main, args=(), daemon=True) thread.start() diff --git a/plugins/net.py b/plugins/net.py index c4d56f9..b045694 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -26,13 +26,13 @@ class Network_controller: # manage id and connection self.all_connection_list = [] self.wheel_queue = queue.Queue() - self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=()) + self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=(), daemon=True) self.start_wheel_thread.start() - self.start_accpet_connection_thread = threading.Thread(target=self.start_accpet_connection, args=()) + self.start_accpet_connection_thread = threading.Thread(target=self.start_accpet_connection, args=(), daemon=True) self.start_accpet_connection_thread.start() - self.start_sending_dp_thread = threading.Thread(target=self.start_sending_dp, args=()) + self.start_sending_dp_thread = threading.Thread(target=self.start_sending_dp, args=(), daemon=True) self.start_sending_dp_thread.start() @@ -127,21 +127,21 @@ class Connection: self.buff = b'' self.padding_queue = queue.Queue() - self.thread_recv = threading.Thread(target=self._init, args=()) + self.thread_recv = threading.Thread(target=self._init, args=(), daemon=True) self.thread_recv.start() self.thread_send = None def _init(self): # init to check connection id, threading - err_code = self.check_id() + err_code, flag = self.check_id() if err_code: - print('Init connection failed, connection closed') + print('<%s> Init connection failed, connection closed, code: %s' % (flag, err_code)) self.conn.close() self.netowrk_controller.set_connection(self.id, self) - self.thread_send = threading.Thread(target=self.send_func, args=()) + self.thread_send = threading.Thread(target=self.send_func, args=(), daemon=True) self.thread_send.start() self.receive() @@ -213,19 +213,29 @@ class Connection: length: 0 ------------------------------- + error code list: + 1: not get "id" in head + 2: receive data failed + 3: appname is not handshake ''' data = self.conn.recv(BUFFSIZE) if not data: - return 2 + return 2, '' self.buff += data dp = Datapack() dp.encode_data = self.buff # maybe here needs to use copy.copy(self.buff) self.buff = dp.decode(only_head=True) if not dp.head.get('id'): - return 1 + return 1, dp.head.get('flag') + + if not dp.app == 'handshake': + return 3, dp.head.get('flag') + self.id = dp.head['id'] + return 0, dp.head.get('flag') + def sendall(self, dp): self.padding_queue.put(dp) @@ -246,5 +256,5 @@ class Connection: self.conn.sendall(dp.encode_data) -thread = threading.Thread(target=main, args=()) +thread = threading.Thread(target=main, args=(), daemon=True) thread.start() diff --git a/plugins/update.py b/plugins/update.py index d4f542d..d57dbec 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -70,5 +70,5 @@ class Compresser: return filelist -thread = threading.Thread(target=main, args=()) +thread = threading.Thread(target=main, args=(), daemon=True) thread.start() diff --git a/test_tool.py b/test_tool.py index 742464c..f4c3c13 100644 --- a/test_tool.py +++ b/test_tool.py @@ -6,6 +6,7 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) id = b'''post handshake msw/1.0 id: miku2 length: 0 +flag: ee20aeff '''