From 2a15908935e8cb8823dc8e056a2d4b05ca720f85 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 29 Mar 2020 11:10:09 +0800 Subject: [PATCH] auto update --- addrlist_example.txt | 2 +- config.py | 6 ++++-- plugins/net.py | 43 +++++++++++++++++++++++++++---------------- plugins/update.py | 5 ++++- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/addrlist_example.txt b/addrlist_example.txt index 5883fea..d28e93c 100644 --- a/addrlist_example.txt +++ b/addrlist_example.txt @@ -1 +1 @@ -127.0.0.1:3900 +127.0.0.1:3900 \ No newline at end of file diff --git a/config.py b/config.py index 4b46711..6a229c6 100644 --- a/config.py +++ b/config.py @@ -8,8 +8,10 @@ import os class Print_controller: def __init__(self): self.padding_queue = queue.Queue() - self.thread = threading.Thread(target=self.start_printing, args=(), daemon=True) self.original_print = print + + self.thread = threading.Thread(target=self.start_printing, args=(), daemon=True) + self.thread.start() def start_printing(self): while True: @@ -60,4 +62,4 @@ msw_queue = queue.Queue() jsondata = Jsondata() print_controller = Print_controller() -print = print_controller.print_function +dprint = print_controller.print_function diff --git a/plugins/net.py b/plugins/net.py index 855cecd..83eecc4 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -7,6 +7,7 @@ import time from mswp import Datapack from forwarder import receive_queues, send_queue from config import jsondata +from config import dprint as print receive_queue = receive_queues[__name__] BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) @@ -217,15 +218,8 @@ class Connection: length = int(dp.head.get('length')) still_need = length - if still_need <= len(self.buff): # first download complete setuation - if dp.method == 'file': - with open(dp.head['filename'], 'ab') as f: - f.write(self.buff[:still_need]) - else: - dp.body = self.buff[:still_need] - self.buff = self.buff[still_need:] - still_need = 0 - else: # writing tmp data + if still_need > len(self.buff): + # writing tmp data if dp.method == 'file': with open(dp.head['filename'], 'ab') as f: still_need -= f.write(self.buff) @@ -233,9 +227,20 @@ class Connection: dp.body += self.buff still_need -= len(self.buff) self.buff = b'' # empty buff because all tmp data has been write + + else: # download complete setuation + if dp.method == 'file': + with open(dp.head['filename'], 'ab') as f: + f.write(self.buff[:still_need]) + else: + dp.body = self.buff[:still_need] + self.buff = self.buff[still_need:] + still_need = 0 - # bleow code are using to process datapack - send_queue.put(dp) + # bleow code are using to process datapack + if dp.method == 'file': + print('Received file %s' % dp.head['filename'], dp) + send_queue.put(dp) # below code are using to closed connection @@ -258,11 +263,7 @@ class Connection: 3: appname is not handshake ''' if self.positive: - ndp = Datapack(head={'from': __name__}) - ndp.app = 'handshake' - ndp.encode() - print(ndp.encode_data.decode()) - self.conn.sendall(ndp.encode_data) + self.send_id() data = self.conn.recv(BUFFSIZE) if not data: @@ -280,8 +281,18 @@ class Connection: self.id = dp.head['id'] + if not self.positive: + self.send_id() + return 0, dp.head.get('flag') + + def send_id(self): + dp = Datapack(head={'from': __name__}) + dp.app = 'handshake' + dp.encode() + self.conn.sendall(dp.encode_data) + def sendall(self, dp): self.padding_queue.put(dp) diff --git a/plugins/update.py b/plugins/update.py index 2b4c121..c692dc9 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -3,10 +3,11 @@ import tarfile import os from mswp import Datapack from forwarder import receive_queues, send_queue +from config import msw_queue receive_queue = receive_queues[__name__] -remove_file_list = ['__init__.py', 'netlist.txt', 'config.json', 'logger.log'] +remove_file_list = ['__init__.py', 'addrlist.txt', 'config.json', 'logger.log'] remove_dir_list = ['.git', '.idea', '__pycache__', 'resources'] @@ -42,6 +43,8 @@ def main(): print('Starting update local file') with tarfile.open(dp.head['filename'], 'r:xz') as f: f.extractall() + #os.remove(dp.head['filename']) + msw_queue.put(0)