This commit is contained in:
2020-04-01 16:20:56 +08:00
parent 1cd3e7fe08
commit 70f6a177d4
3 changed files with 76 additions and 31 deletions

View File

@@ -3,5 +3,6 @@
"listen_port": 3900, "listen_port": 3900,
"listen_ip": "127.0.0.1", "listen_ip": "127.0.0.1",
"listen_num": 39, "listen_num": 39,
"buffsize": 4096 "buffsize": 4096,
"proxy": false
} }

View File

@@ -36,10 +36,17 @@ def send_queue_function():
object_app, new_dp = process_reforware(new_dp) object_app, new_dp = process_reforware(new_dp)
receive_queues[add_plugins_string(object_app)].put(new_dp) receive_queues[add_plugins_string(object_app)].put(new_dp)
elif 'to' in dp.head: # send to net if "to" avaliable elif 'to' in dp.head: # send to net if "to" avaliable
receive_queues[add_plugins_string('net')].put(dp) put('net', dp)
else: else:
object_app, dp = process_reforware(dp) object_app, dp = process_reforware(dp)
receive_queues[add_plugins_string(object_app)].put(dp) put(object_app, dp)
def put(appname, dp):
realappname = add_plugins_string(appname)
if not receive_queues.get(realappname):
print('KeyError, Could not find queue %s' % realappname)
else:
receive_queues[realappname].put(dp)
def process_reforware(dp): def process_reforware(dp):

View File

@@ -4,6 +4,7 @@ import copy
import queue import queue
import json import json
import os import os
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
@@ -34,8 +35,10 @@ class Network_controller: # manage id and connection
self.conflist_pass = [] self.conflist_pass = []
self.mhtlist = [] # store exchanged connection self.mhtlist = [] # store exchanged connection
self.mhtlist_pass = [] self.mhtlist_pass = []
self.proxylist = [] # store connection behind proxy self.proxydict = {}
self.proxylist_pass = []
self.alllist = [self.netlist, self.netlist_pass, self.conflist, self.conflist_pass, \
self.mhtlist, self.mhtlist_pass]
self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=(), daemon=True) self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=(), daemon=True)
self.start_wheel_thread.start() self.start_wheel_thread.start()
@@ -113,6 +116,10 @@ class Network_controller: # manage id and connection
self.conflist.append((ip, port)) self.conflist.append((ip, port))
if jsondata.try_to_read_jsondata('proxy', False):
self.proxydict[ID] = jsondata.raw_jsondata['proxy']
def i_did_something(self): # go f**k your yeallow line def i_did_something(self): # go f**k your yeallow line
pass pass
@@ -120,13 +127,16 @@ 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)) print('Online %s' % str(self.id_dict))
print('proxydict %s' % str(self.proxydict))
print('conflist %s' % str(self.conflist)) print('conflist %s' % str(self.conflist))
print('conflist_pass %s' % str(self.conflist_pass)) print('conflist_pass %s' % str(self.conflist_pass))
print('mhtlist %s' % str(self.mhtlist)) print('mhtlist %s' % str(self.mhtlist))
print('mhtlist_pass %s' % str(self.mhtlist_pass)) print('mhtlist_pass %s' % str(self.mhtlist_pass))
elif dp.body == b'mht' and dp.method == 'get': elif dp.body == b'mht' and dp.method == 'get':
ndp = dp.reply() ndp = dp.reply()
data_dict = {}
connection_list = [] connection_list = []
with self.lock: with self.lock:
for id in self.id_dict: for id in self.id_dict:
@@ -135,24 +145,36 @@ class Network_controller: # manage id and connection
ip, port = connection.conn.getpeername() ip, port = connection.conn.getpeername()
port = int(connection.listen_port) port = int(connection.listen_port)
connection_list.append((ip, port)) connection_list.append((ip, port))
data_dict['mht'] = connection_list
data_dict['proxy'] = self.proxydict
ndp.body = json.dumps(connection_list).encode() ndp.body = json.dumps(data_dict).encode()
send_queue.put(ndp) send_queue.put(ndp)
elif dp.method == 'reply': elif dp.method == 'reply':
mhtstr = dp.body.decode() mhtstr = dp.body.decode()
mhtlist = json.loads(mhtstr) data_dict = json.loads(mhtstr)
mhtlist = data_dict['mht']
with self.lock: with self.lock:
for addr in mhtlist: for addr in mhtlist:
addr = (addr[0], addr[1]) addr = (addr[0], addr[1])
if not addr in self.mhtlist and not addr in self.mhtlist_pass: if not self.check_in_list(addr):
self.mhtlist.append(addr) self.mhtlist.append(addr)
self.proxydict.update(data_dict['proxy'])
else: else:
print('Received unknown command', dp) print('Received unknown command', dp)
def check_in_list(self, addr):
for l in self.alllist:
if addr in l:
return True
return False
def start_sending_dp(self): def start_sending_dp(self):
while True: while True:
dp = receive_queue.get() dp = receive_queue.get()
@@ -174,14 +196,28 @@ class Network_controller: # manage id and connection
connection.sendall(dp) connection.sendall(dp)
else: else:
self.send_to_id(to, dp)
def send_to_id(self, to, dp): # send to 1 id, process proxy at the same time
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, dp) print('To id %s is yourself!' % to, dp) # maybe proxy to yourself
continue return
if to in self.proxydict: # neat warning dangerous code
if dp.head['to']:
dp.head['to'] = self.proxydict[to] + '&' + to + '&' + dp.head['to']
else:
dp.head['to'] = self.proxydict[to] + '&' + to
send_queue.put(dp)
return
print('To id %s has no connection now' % to, dp) print('To id %s has no connection now' % to, dp)
self.wheel_queue.put(dp) self.wheel_queue.put(dp)
continue return
connection = connections[0] connection = connections[0]
connection.sendall(dp) connection.sendall(dp)
@@ -257,8 +293,6 @@ class Network_controller: # manage id and connection
return self.conflist, self.conflist_pass return self.conflist, self.conflist_pass
elif conntype == 'mht': elif conntype == 'mht':
return self.mhtlist, self.mhtlist_pass return self.mhtlist, self.mhtlist_pass
elif conntype == 'proxy':
return self.proxylist, self.proxylist_pass
else: else:
print('Could not find conntype %s' % conntype) print('Could not find conntype %s' % conntype)
return None, None return None, None
@@ -292,7 +326,7 @@ 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, 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
@@ -405,8 +439,8 @@ class Connection:
self.id = dp.head['id'] self.id = dp.head['id']
self.listen_port = int(dp.head.get('listen_port')) self.listen_port = int(dp.head.get('listen_port'))
if self.id == jsondata.try_to_read_jsondata('id', 'unknown_id'): if self.id == ID:
print('you connect to your self') #print('you connect to your self')
return 4, dp.head.get('flag') return 4, dp.head.get('flag')
if not self.positive: if not self.positive:
@@ -431,13 +465,16 @@ class Connection:
while True: while True:
dp = self.padding_queue.get() dp = self.padding_queue.get()
dp.encode() dp.encode()
if dp.method == 'file':
self.conn.sendall(dp.encode_data) self.conn.sendall(dp.encode_data)
if dp.method == 'file':
with open(dp.head['filename'], 'rb') as f: with open(dp.head['filename'], 'rb') as f:
for data in f: for data in f:
try:
self.conn.sendall(data) self.conn.sendall(data)
else: except Exception as e:
self.conn.sendall(dp.encode_data) print('Failed to send file %s %s: %s' % (dp.head['filename'], type(e), str(e)), dp)
continue
print('Send file %s finished' % dp.head['filename'], dp)
def i_did_something(self): def i_did_something(self):