net wheel bug, onlyproxy function and more...

This commit is contained in:
2020-04-03 15:19:41 +08:00
parent b447b65c10
commit 55c6163e18
4 changed files with 79 additions and 22 deletions

View File

@@ -4,5 +4,6 @@
"listen_ip": "127.0.0.1", "listen_ip": "127.0.0.1",
"listen_num": 39, "listen_num": 39,
"buffsize": 4096, "buffsize": 4096,
"proxy": false "proxy": false,
"onlyproxy": false
} }

View File

@@ -30,7 +30,7 @@ BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
ID = jsondata.try_to_read_jsondata('id', 'unknown_id') ID = jsondata.try_to_read_jsondata('id', 'unknown_id')
class Datapack: class Datapack:
def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'', def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'',
file=None, gen_flag=True): file=None, gen_flag=True, delete=False):
self.id = ID self.id = ID
if head is None: if head is None:
head = {} head = {}
@@ -38,6 +38,7 @@ class Datapack:
self.head['id'] = self.id self.head['id'] = self.id
self.method = method self.method = method
self.file = file self.file = file
self.delete = delete
self.app = app self.app = app
self.version = version self.version = version
self.body = body self.body = body
@@ -93,6 +94,7 @@ class Datapack:
ndp = copy.deepcopy(self) ndp = copy.deepcopy(self)
ndp.app = ndp.head['from'] ndp.app = ndp.head['from']
ndp.method = 'reply' ndp.method = 'reply'
ndp.delete = False
if not self.head['id'] == ID: # net package if not self.head['id'] == ID: # net package
ndp.head['to'] = self.head['id'] ndp.head['to'] = self.head['id']
ndp.head['id'] = ID ndp.head['id'] = ID

View File

@@ -24,6 +24,7 @@ class Ffmpeg_controller:
self.server = None self.server = None
self.conver_task_queue = queue.Queue() self.conver_task_queue = queue.Queue()
self.org_filename = None self.org_filename = None
self.concat = True
self.conver_task_thread = threading.Thread(target=self.conver_task_func, args=()) self.conver_task_thread = threading.Thread(target=self.conver_task_func, args=())
self.conver_task_thread.start() self.conver_task_thread.start()
@@ -39,31 +40,47 @@ class Ffmpeg_controller:
dp = receive_queue.get() dp = receive_queue.get()
if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client
if dp.head.get('concat'):
if dp.head['concat'] == 'true':
self.concat = True
elif dp.head['concat'] == 'false':
self.concat = False
else:
print('unknown concat value')
continue
self.org_filename = dp.head['filename'] self.org_filename = dp.head['filename']
ndp = dp.reply()
ndp.body = 'Spliting file %s' % dp.head['filename']
ndp.body = ndp.body.encode()
send_queue.put(ndp)
cmd = 'ffmpeg -i ' + dp.head['filename'] + ' -c copy -f segment -segment_time 20 \ if self.concat:
-reset_timestamps 1 -y res/ffmpeg_tmp/' + '%d' + '.mp4' ndp = dp.reply()
ndp.body = 'Spliting file %s' % dp.head['filename']
ndp.body = ndp.body.encode()
send_queue.put(ndp)
os.system(cmd) cmd = 'ffmpeg -i ' + dp.head['filename'] + ' -c copy -f segment -segment_time 20 \
-reset_timestamps 1 -y res/ffmpeg_tmp/' + '%d' + '.mp4'
os.system(cmd)
self.run_as_server() self.run_as_server()
# concat all file if self.concat:
filelist = os.listdir('res/ffmpeg_finished') # concat all file
if 'filelist.txt' in filelist: filelist = os.listdir('res/ffmpeg_finished')
filelist.remove('filelist.txt') if 'filelist.txt' in filelist:
with open('res/ffmpeg_finished/filelist.txt', 'w') as f: filelist.remove('filelist.txt')
for file in filelist: with open('res/ffmpeg_finished/filelist.txt', 'w') as f:
f.write('file \'%s\'\n' % file) for file in filelist:
object_filename = self.org_filename[:-4] + '.mkv' f.write('file \'%s\'\n' % file)
subprocess.check_output('ffmpeg -f concat -i res/ffmpeg_finished/filelist.txt \ object_filename = self.org_filename[:-4] + '.mkv'
-c copy -y ' + object_filename, shell=True) subprocess.check_output('ffmpeg -f concat -i res/ffmpeg_finished/filelist.txt \
-c copy -y ' + object_filename, shell=True)
print('All process finished at ' + object_filename) for file in filelist:
os.remove('res/ffmpeg_finished/' + file)
os.remove('res/ffmpeg_finished/filelist.txt')
print('All process finished')
elif dp.method == 'post' and dp.body == b'enable': # clinet mode elif dp.method == 'post' and dp.body == b'enable': # clinet mode
self.status = 1 self.status = 1
@@ -176,10 +193,13 @@ class Ffmpeg_controller:
os.system('ffmpeg -i ' + filename + ' -c:a libopus -ab 64k \ os.system('ffmpeg -i ' + filename + ' -c:a libopus -ab 64k \
-c:v libx265 -s 1280x720 -y ' + output_filename) -c:v libx265 -s 1280x720 -y ' + output_filename)
os.remove(filename)
ndp = dp.reply() ndp = dp.reply()
ndp.head['filename'] = output_filename ndp.head['filename'] = output_filename
ndp.head['old_filename'] = filename ndp.head['old_filename'] = filename
ndp.method = 'file' ndp.method = 'file'
ndp.delete = True
send_queue.put(ndp) send_queue.put(ndp)
self.send_request() self.send_request()

View File

@@ -15,6 +15,9 @@ receive_queue = receive_queues[__name__]
BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
ID = jsondata.try_to_read_jsondata('id', 'Unknown_ID') ID = jsondata.try_to_read_jsondata('id', 'Unknown_ID')
RETRYSLEEP = 5 RETRYSLEEP = 5
MYPROXY = jsondata.try_to_read_jsondata('proxy', False)
ONLYPROXY = jsondata.try_to_read_jsondata('onlyproxy', False)
def main(): def main():
network_controller = Network_controller() network_controller = Network_controller()
@@ -23,6 +26,9 @@ def main():
class Network_controller: # manage id and connection class Network_controller: # manage id and connection
def __init__(self): def __init__(self):
if ONLYPROXY and not MYPROXY:
print('config failed because you set onlyproxy true but proxy false')
return
self.send_queue = queue.Queue() self.send_queue = queue.Queue()
self.id_dict = {} self.id_dict = {}
self.lock = threading.Lock() self.lock = threading.Lock()
@@ -153,6 +159,12 @@ 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))
for addr in self.conflist:
if not addr in connection_list:
connection_list.append(addr)
for addr in self.conflist_pass:
if not addr in connection_list:
connection_list.append(addr)
data_dict['mht'] = connection_list data_dict['mht'] = connection_list
data_dict['proxy'] = self.proxydict data_dict['proxy'] = self.proxydict
@@ -209,6 +221,13 @@ class Network_controller: # manage id and connection
elif not to: elif not to:
print('not to', dp) print('not to', dp)
elif ONLYPROXY and not to == MYPROXY:
if dp.head['to']:
dp.head['to'] = to + dp.head['to']
else:
dp.head['to'] = to
self.send_to_id(MYPROXY, dp)
else: else:
self.send_to_id(to, dp) self.send_to_id(to, dp)
@@ -349,7 +368,7 @@ class Connection:
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()
@@ -467,6 +486,13 @@ class Connection:
#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 ONLYPROXY and not self.id == MYPROXY: # refuce not proxy connection
return 5, dp.head.get('flag')
if dp.head.get('onlyuseproxy'):
if not dp.head['onlyuseproxy'] == ID:
return 6, dp.head.get('flag')
if not self.positive: if not self.positive:
self.send_id() self.send_id()
@@ -476,6 +502,8 @@ class Connection:
def send_id(self): def send_id(self):
dp = Datapack(head={'from': __name__}) dp = Datapack(head={'from': __name__})
dp.app = 'handshake' dp.app = 'handshake'
if ONLYPROXY:
dp.head['onlyuseproxy'] = MYPROXY
dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900)) dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900))
dp.encode() dp.encode()
self.conn.sendall(dp.encode_data) self.conn.sendall(dp.encode_data)
@@ -497,8 +525,14 @@ class Connection:
self.conn.sendall(data) self.conn.sendall(data)
except Exception as e: except Exception as e:
print('Failed to send file %s %s: %s' % (dp.head['filename'], type(e), str(e)), dp) print('Failed to send file %s %s: %s' % (dp.head['filename'], type(e), str(e)), dp)
if dp.head.get('to'):
dp.head['to'] = self.id + '&' + dp.head['to']
else:
dp.head['to'] = self.id
self.netowrk_controller.wheel_queue.put(dp) self.netowrk_controller.wheel_queue.put(dp)
break break
if dp.delete:
os.remove(dp.head['filename'])
print('Send file %s finished' % dp.head['filename'], dp) print('Send file %s finished' % dp.head['filename'], dp)