fix for android #171

This commit is contained in:
n1nj4sec 2016-09-17 13:08:57 +02:00
parent e2911f7b05
commit a0532281a3
2 changed files with 35 additions and 13 deletions

27
pupy/network/lib/servers.py Normal file → Executable file
View File

@ -8,7 +8,18 @@ from rpyc.utils.authenticators import AuthenticationError
from rpyc.utils.registry import UDPRegistryClient from rpyc.utils.registry import UDPRegistryClient
from rpyc.core.stream import Stream from rpyc.core.stream import Stream
from buffer import Buffer from buffer import Buffer
import multiprocessing, socket, time import socket, time
try:
import multiprocessing
Process=multiprocessing.Process
Lock=multiprocessing.Lock
Event=multiprocessing.Event
except ImportError: #multiprocessing not available on android ?
import threading
Process=threading.Thread
Lock=threading.Lock
Event=threading.Event
from streams.PupySocketStream import addGetPeer from streams.PupySocketStream import addGetPeer
class PseudoStreamDecoder(Stream): class PseudoStreamDecoder(Stream):
@ -18,8 +29,8 @@ class PseudoStreamDecoder(Stream):
self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443))) self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443))) self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.transport=transport_class(self, **transport_kwargs) self.transport=transport_class(self, **transport_kwargs)
self.lockin=multiprocessing.Lock() self.lockin=Lock()
self.lockout=multiprocessing.Lock() self.lockout=Lock()
def decode_data(self, data): def decode_data(self, data):
with self.lockin: with self.lockin:
@ -82,7 +93,7 @@ class PupyAsyncServer(object):
self.clients[cookie].buf_in.cookie=cookie self.clients[cookie].buf_in.cookie=cookie
self.clients[cookie].buf_out.cookie=cookie self.clients[cookie].buf_out.cookie=cookie
conn=Connection(self.service, Channel(self.clients[cookie]), config=config, _lazy=True) conn=Connection(self.service, Channel(self.clients[cookie]), config=config, _lazy=True)
p = multiprocessing.Process(target=self.handle_new_conn, args=(conn,)) p = Process(target=self.handle_new_conn, args=(conn,))
p.daemon=True p.daemon=True
p.start() p.start()
resp=None resp=None
@ -152,7 +163,7 @@ class PupyAsyncTCPServer(PupyAsyncServer):
def accept(self): def accept(self):
try: try:
s, addr = self.sock.accept() s, addr = self.sock.accept()
t=multiprocessing.Process(target=self.serve_request, args=(s, addr,)) t=Process(target=self.serve_request, args=(s, addr,))
t.daemon=True t.daemon=True
t.start() t.start()
#TODO : make a pool of threads #TODO : make a pool of threads
@ -243,8 +254,8 @@ class PupyTCPServer(ThreadPoolServer):
stream=self.stream_class(sock, self.transport_class, self.transport_kwargs) stream=self.stream_class(sock, self.transport_class, self.transport_kwargs)
event=multiprocessing.Event() event=Event()
t=multiprocessing.Process(target=check_timeout, args=(event, stream.close)) t=Process(target=check_timeout, args=(event, stream.close))
t.daemon=True t.daemon=True
t.start() t.start()
try: try:
@ -327,7 +338,7 @@ class PupyUDPServer(object):
raise raise
self.clients[addr]=self.stream_class((self.sock, addr), self.transport_class, self.transport_kwargs, client_side=False) self.clients[addr]=self.stream_class((self.sock, addr), self.transport_class, self.transport_kwargs, client_side=False)
conn=Connection(self.service, Channel(self.clients[addr]), config=config, _lazy=True) conn=Connection(self.service, Channel(self.clients[addr]), config=config, _lazy=True)
t = multiprocessing.Process(target = self.handle_new_conn, args=(conn,)) t = Process(target = self.handle_new_conn, args=(conn,))
t.daemon=True t.daemon=True
t.start() t.start()
with self.clients[addr].downstream_lock: with self.clients[addr].downstream_lock:

21
pupy/network/lib/streams/PupyAsyncStream.py Normal file → Executable file
View File

@ -7,9 +7,20 @@ __all__=["PupyAsyncTCPStream", "PupyAsyncUDPStream"]
from rpyc.core.stream import Stream from rpyc.core.stream import Stream
from ..buffer import Buffer from ..buffer import Buffer
import sys, socket, time, errno, logging, traceback, string, random, multiprocessing import sys, socket, time, errno, logging, traceback, string, random
from rpyc.lib.compat import select, select_error, BYTES_LITERAL, get_exc_errno, maxint from rpyc.lib.compat import select, select_error, BYTES_LITERAL, get_exc_errno, maxint
from PupySocketStream import addGetPeer from PupySocketStream import addGetPeer
try:
import multiprocessing
Process=multiprocessing.Process
Lock=multiprocessing.Lock
Event=multiprocessing.Event
except ImportError: #multiprocessing not available on android ?
import threading
Process=threading.Thread
Lock=threading.Lock
Event=threading.Event
class addGetPeer(object): class addGetPeer(object):
""" add some functions needed by some obfsproxy transports""" """ add some functions needed by some obfsproxy transports"""
@ -43,18 +54,18 @@ class PupyAsyncStream(Stream):
#buffers for transport #buffers for transport
self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443))) self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443))) self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.upstream_lock=multiprocessing.Lock() self.upstream_lock=Lock()
self.downstream_lock=multiprocessing.Lock() self.downstream_lock=Lock()
self.transport=transport_class(self, **transport_kwargs) self.transport=transport_class(self, **transport_kwargs)
self.max_pull_interval=2 self.max_pull_interval=2
self.pull_interval=0 self.pull_interval=0
self.pull_event=multiprocessing.Event() self.pull_event=Event()
self.MAX_IO_CHUNK=32000*100 #3Mo because it is a async transport self.MAX_IO_CHUNK=32000*100 #3Mo because it is a async transport
self.client_side=self.transport.client self.client_side=self.transport.client
if self.client_side: if self.client_side:
self.poller_thread=multiprocessing.Process(target=self.poller_loop) self.poller_thread=Process(target=self.poller_loop)
self.poller_thread.daemon=True self.poller_thread.daemon=True
self.poller_thread.start() self.poller_thread.start()
self.on_connect() self.on_connect()