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