function reply, get, net proxy...
This commit is contained in:
25
mswp.py
25
mswp.py
@@ -1,10 +1,15 @@
|
||||
import os
|
||||
import random
|
||||
import hashlib
|
||||
import copy
|
||||
from config import jsondata
|
||||
|
||||
|
||||
'''
|
||||
Avaliable method are
|
||||
post: used to send data, no needs to reply (deafult)
|
||||
get: used to send data, but needs to reply
|
||||
reply: used to reply "get" method
|
||||
A datapack must like:
|
||||
---------------------
|
||||
post log msw/1.0
|
||||
@@ -37,6 +42,8 @@ class Datapack:
|
||||
self.version = version
|
||||
self.body = body
|
||||
self.encode_data = b''
|
||||
if self.head.get('from'):
|
||||
self.head['from'] = process_plugins_name(self.head['from'])
|
||||
if gen_flag:
|
||||
randseed = str(random.random()).encode()
|
||||
h = hashlib.blake2b(digest_size = 4)
|
||||
@@ -80,3 +87,21 @@ class Datapack:
|
||||
return self.encode_data[index+2:]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def reply(self):
|
||||
ndp = copy.copy(self)
|
||||
ndp.app = ndp.head['from']
|
||||
ndp.method = 'reply'
|
||||
ndp.head['to'] = ndp.id
|
||||
ndp.id = self.id
|
||||
return ndp
|
||||
|
||||
|
||||
def process_plugins_name(name):
|
||||
if 'plugins/' in name and '.py' in name:
|
||||
name = name.replace('plugins/', '')
|
||||
name = name.replace('.py', '')
|
||||
return name
|
||||
else:
|
||||
return name
|
||||
|
||||
@@ -45,6 +45,22 @@ class Network_controller: # manage id and connection
|
||||
self.start_positive_connecting_thread = threading.Thread(target=self.start_positive_connecting, args=(), daemon=True)
|
||||
self.start_positive_connecting_thread.start()
|
||||
|
||||
self.start_mht_thread = threading.Thread(target=self.start_mht, args=(), daemon=True)
|
||||
self.start_mht_thread.start()
|
||||
|
||||
|
||||
def start_mht(self):
|
||||
while True:
|
||||
dp = Datapack(head={'from': __name__})
|
||||
dp.head['to'] = '*'
|
||||
dp.app = 'net'
|
||||
dp.method = 'get'
|
||||
dp.body = b'mht'
|
||||
|
||||
send_queue.put(dp)
|
||||
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
def start_positive_connecting(self):
|
||||
self.read_addrlist()
|
||||
@@ -52,15 +68,18 @@ class Network_controller: # manage id and connection
|
||||
self.try_to_connect(addr)
|
||||
|
||||
|
||||
|
||||
def try_to_connect(self, addr):
|
||||
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
conn.connect(addr)
|
||||
try:
|
||||
conn.connect(addr)
|
||||
except Exception as e:
|
||||
print('Connect to %s failed, %s: %s' % (str(addr), type(e), str(e)))
|
||||
return
|
||||
|
||||
connection = Connection(conn, addr, self, positive=True)
|
||||
connection.i_did_something()
|
||||
|
||||
|
||||
|
||||
def read_addrlist(self):
|
||||
if not os.path.exists('addrlist.txt'):
|
||||
print('addrlist.txt not exists, config that base on addrlist_sample.txt')
|
||||
@@ -83,35 +102,43 @@ class Network_controller: # manage id and connection
|
||||
def process_command(self, dp):
|
||||
if dp.body == b'status':
|
||||
print('Online %s' % str(list(self.id_dict.keys())))
|
||||
elif dp.body == b'mht':
|
||||
ndp = dp.reply()
|
||||
|
||||
|
||||
|
||||
def start_sending_dp(self):
|
||||
while True:
|
||||
dp = receive_queue.get()
|
||||
|
||||
if dp.app == 'net':
|
||||
if dp.app == 'net' and not dp.head.get('to'):
|
||||
self.process_command(dp)
|
||||
continue
|
||||
|
||||
|
||||
to_str = dp.head['to']
|
||||
to_list = to_str.split(':')
|
||||
to = to_list.pop()
|
||||
|
||||
connections = self.id_dict.get(to)
|
||||
if not connections:
|
||||
if to == ID:
|
||||
print('To id %s is yourself!' % to)
|
||||
continue
|
||||
print('To id %s has no connection now' % to)
|
||||
self.wheel_queue.put(dp)
|
||||
continue
|
||||
|
||||
to_str = ':'.join(to_list)
|
||||
to_list = to_str.split('&')
|
||||
to = to_list.pop(0)
|
||||
to_str = '&'.join(to_list)
|
||||
dp.head['to'] = to_str
|
||||
dp.encode()
|
||||
|
||||
connection = connections[0]
|
||||
connection.sendall(dp)
|
||||
|
||||
if to == '*':
|
||||
with self.lock:
|
||||
for id in self.id_dict:
|
||||
connection = self.id_dict[id][0]
|
||||
connection.sendall(dp)
|
||||
|
||||
else:
|
||||
connections = self.id_dict.get(to)
|
||||
if not connections:
|
||||
if to == ID:
|
||||
print('To id %s is yourself!' % to, dp)
|
||||
continue
|
||||
print('To id %s has no connection now' % to, dp)
|
||||
self.wheel_queue.put(dp)
|
||||
continue
|
||||
|
||||
connection = connections[0]
|
||||
connection.sendall(dp)
|
||||
|
||||
|
||||
def start_wheel(self):
|
||||
@@ -260,7 +287,9 @@ class Connection:
|
||||
1: not get "id" in head
|
||||
2: receive data failed
|
||||
3: appname is not handshake
|
||||
4: id is yourself
|
||||
'''
|
||||
data = None
|
||||
if self.positive:
|
||||
self.send_id()
|
||||
try:
|
||||
@@ -283,6 +312,10 @@ class Connection:
|
||||
|
||||
self.id = dp.head['id']
|
||||
|
||||
if self.id == jsondata.try_to_read_jsondata('id', 'unknown_id'):
|
||||
print('you connect to your self')
|
||||
return 4, dp.head.get('flag')
|
||||
|
||||
if not self.positive:
|
||||
self.send_id()
|
||||
|
||||
|
||||
@@ -44,6 +44,8 @@ def main():
|
||||
with tarfile.open(dp.head['filename'], 'r:xz') as f:
|
||||
f.extractall()
|
||||
#os.remove(dp.head['filename'])
|
||||
|
||||
# restart msw program
|
||||
msw_queue.put(0)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user