reply, proxy, create floder, bug fix...

This commit is contained in:
2020-04-01 21:52:22 +08:00
parent 70f6a177d4
commit 60c9bbc4af
6 changed files with 59 additions and 26 deletions

View File

@@ -57,6 +57,24 @@ class Jsondata:
if self.auto_save: if self.auto_save:
pass 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 = {} global_config = {}
msw_queue = queue.Queue() msw_queue = queue.Queue()
jsondata = Jsondata() jsondata = Jsondata()

View File

@@ -93,8 +93,9 @@ class Datapack:
ndp = copy.copy(self) ndp = copy.copy(self)
ndp.app = ndp.head['from'] ndp.app = ndp.head['from']
ndp.method = 'reply' ndp.method = 'reply'
ndp.head['to'] = self.head['id'] if not self.head['id'] == ID: # net package
ndp.id = ID ndp.head['to'] = self.head['id']
ndp.head['id'] = ID
return ndp return ndp

View File

@@ -4,6 +4,7 @@ import os
from mswp import Datapack from mswp import Datapack
from forwarder import receive_queues, send_queue from forwarder import receive_queues, send_queue
from config import msw_queue from config import msw_queue
from config import dprint as print
receive_queue = receive_queues[__name__] receive_queue = receive_queues[__name__]
@@ -15,11 +16,17 @@ def main():
print('Error in %s, %s: %s' % (__name__, type(e), str(e))) 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(): def _main():
file_flag = False file_flag = False
while True: while True:
file_flag = False file_flag = False
net_flag = False
raw_data = input() raw_data = input()
if raw_data == 'restart': if raw_data == 'restart':
@@ -33,12 +40,6 @@ def _main():
raw_data = raw_data[6:] raw_data = raw_data[6:]
file_flag = True 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) first_index, last_index = find_index(raw_data)
app = raw_data[:first_index] app = raw_data[:first_index]
body = raw_data[last_index:] body = raw_data[last_index:]
@@ -56,8 +57,6 @@ def _main():
app = app.replace(' ', '') app = app.replace(' ', '')
dp = Datapack(head={'from': __name__}) dp = Datapack(head={'from': __name__})
if net_flag:
dp.head.update({'to': to})
dp.head.update(ihead) dp.head.update(ihead)
@@ -72,6 +71,7 @@ def _main():
dp.body = body.encode() dp.body = body.encode()
send_queue.put(dp) send_queue.put(dp)
print('Command has been sent', dp)
def find_index(raw_data): def find_index(raw_data):
@@ -84,3 +84,5 @@ def find_index(raw_data):
thread = threading.Thread(target=main, args=(), daemon=True) thread = threading.Thread(target=main, args=(), daemon=True)
thread.start() thread.start()
thread_print_reply_func = threading.Thread(target=print_reply_func, args=(), daemon=True)
thread_print_reply_func.start()

View File

@@ -8,7 +8,7 @@ import random
import time import time
from mswp import Datapack from mswp import Datapack
from forwarder import receive_queues, send_queue from forwarder import receive_queues, send_queue
from config import jsondata from config import jsondata, create_floder
from config import dprint as print from config import dprint as print
receive_queue = receive_queues[__name__] receive_queue = receive_queues[__name__]
@@ -94,6 +94,7 @@ class Network_controller: # manage id and connection
conn.connect(addr) conn.connect(addr)
except Exception as e: except Exception as e:
#print('Connect to %s failed, %s: %s' % (str(addr), type(e), str(e))) #print('Connect to %s failed, %s: %s' % (str(addr), type(e), str(e)))
del(e)
return return
connection = Connection(conn, addr, self, positive=True, conntype=conntype) 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): def process_command(self, dp):
if dp.body == b'status': if dp.body == b'status':
print('Online %s' % str(self.id_dict)) result = ''
print('proxydict %s' % str(self.proxydict)) result += 'Online %s' % str(self.id_dict) + '\n'
print('conflist %s' % str(self.conflist)) result += 'proxydict %s' % str(self.proxydict) + '\n'
print('conflist_pass %s' % str(self.conflist_pass)) result += 'conflist %s' % str(self.conflist) + '\n'
print('mhtlist %s' % str(self.mhtlist)) result += 'conflist_pass %s' % str(self.conflist_pass) + '\n'
print('mhtlist_pass %s' % str(self.mhtlist_pass)) 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': elif dp.body == b'mht' and dp.method == 'get':
ndp = dp.reply() ndp = dp.reply()
@@ -264,7 +272,7 @@ class Network_controller: # manage id and connection
if not addr in xxxlist_pass: if not addr in xxxlist_pass:
xxxlist_pass.append(addr) xxxlist_pass.append(addr)
print('%s connected' % id) print('<%s> %s connected' % (connection.flag, id))
def del_connection(self, connection): def del_connection(self, connection):
@@ -283,7 +291,7 @@ class Network_controller: # manage id and connection
if addr in xxxlist_pass: if addr in xxxlist_pass:
xxxlist_pass.remove(addr) xxxlist_pass.remove(addr)
print('%s disconnected' % id) print('<%s> %s disconnected' % (connection.flag, id))
def getlist(self, conntype): def getlist(self, conntype):
@@ -304,6 +312,7 @@ class Connection:
self.addr = addr self.addr = addr
self.netowrk_controller = netowrk_controller self.netowrk_controller = netowrk_controller
self.id = None self.id = None
self.flag = None
self.buff = b'' self.buff = b''
self.padding_queue = queue.Queue() self.padding_queue = queue.Queue()
self.thread_send = None self.thread_send = None
@@ -324,13 +333,13 @@ class Connection:
def _init(self): # init to check connection id, threading 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: if err_code:
#print('<%s> Init connection failed, connection closed, code: %s' % (flag, err_code)) #print('<%s> Init connection failed, connection closed, code: %s' % (flag, err_code))
self.conn.close() self.conn.close()
return 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 = threading.Thread(target=self.send_func, args=(), daemon=True)
self.thread_send.start() self.thread_send.start()
@@ -358,7 +367,9 @@ class Connection:
dp.encode_data = self.buff dp.encode_data = self.buff
try: try:
self.buff = dp.decode(only_head=True) 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']): if dp.method == 'file' and os.path.exists(dp.head['filename']):
os.remove(dp.head['filename']) os.remove(dp.head['filename'])
@@ -372,7 +383,7 @@ class Connection:
if still_need > len(self.buff): if still_need > len(self.buff):
# writing tmp data # writing tmp data
if dp.method == 'file': 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) still_need -= f.write(self.buff)
else: else:
dp.body += self.buff dp.body += self.buff
@@ -381,7 +392,7 @@ class Connection:
else: # download complete setuation else: # download complete setuation
if dp.method == 'file': 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]) f.write(self.buff[:still_need])
else: else:
dp.body = self.buff[:still_need] dp.body = self.buff[:still_need]
@@ -390,6 +401,7 @@ class Connection:
# bleow code are using to process datapack # bleow code are using to process datapack
if dp.method == 'file': if dp.method == 'file':
os.rename('tmp/' + dp.head['filename'], dp.head['filename'])
print('Received file %s' % dp.head['filename'], dp) print('Received file %s' % dp.head['filename'], dp)
send_queue.put(dp) send_queue.put(dp)

View File

@@ -7,7 +7,7 @@ from config import msw_queue
receive_queue = receive_queues[__name__] 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'] remove_dir_list = ['.git', '.idea', '__pycache__', 'resources']

0
tmp/placeholder Normal file
View File