mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-30 03:14:22 +00:00
Simplify netlib and improve API.
This commit is contained in:
parent
4e53f1ee90
commit
18a03c063e
@ -1,4 +1,4 @@
|
|||||||
import select, socket, threading
|
import select, socket, threading, traceback
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
|
|
||||||
|
|
||||||
@ -124,9 +124,6 @@ class TCPServer:
|
|||||||
self.server_address = self.socket.getsockname()
|
self.server_address = self.socket.getsockname()
|
||||||
self.socket.listen(self.request_queue_size)
|
self.socket.listen(self.request_queue_size)
|
||||||
|
|
||||||
def fileno(self):
|
|
||||||
return self.socket.fileno()
|
|
||||||
|
|
||||||
def request_thread(self, request, client_address):
|
def request_thread(self, request, client_address):
|
||||||
try:
|
try:
|
||||||
self.handle_connection(request, client_address)
|
self.handle_connection(request, client_address)
|
||||||
@ -139,8 +136,8 @@ class TCPServer:
|
|||||||
self.__is_shut_down.clear()
|
self.__is_shut_down.clear()
|
||||||
try:
|
try:
|
||||||
while not self.__shutdown_request:
|
while not self.__shutdown_request:
|
||||||
r, w, e = select.select([self], [], [], poll_interval)
|
r, w, e = select.select([self.socket], [], [], poll_interval)
|
||||||
if self in r:
|
if self.socket in r:
|
||||||
try:
|
try:
|
||||||
request, client_address = self.socket.accept()
|
request, client_address = self.socket.accept()
|
||||||
except socket.error:
|
except socket.error:
|
||||||
@ -160,14 +157,25 @@ class TCPServer:
|
|||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
self.__shutdown_request = True
|
self.__shutdown_request = True
|
||||||
self.__is_shut_down.wait()
|
self.__is_shut_down.wait()
|
||||||
|
self.handle_shutdown()
|
||||||
|
|
||||||
def handle_error(self, request, client_address):
|
def handle_error(self, request, client_address):
|
||||||
print '-'*40
|
"""
|
||||||
print 'Exception happened during processing of request from',
|
Called when handle_connection raises an exception.
|
||||||
print client_address
|
"""
|
||||||
import traceback
|
print >> sys.stderr, '-'*40
|
||||||
traceback.print_exc() # XXX But this goes to stderr!
|
print >> sys.stderr, "Error processing of request from %s"%client_address
|
||||||
print '-'*40
|
traceback.print_exc()
|
||||||
|
print >> sys.stderr, '-'*40
|
||||||
|
|
||||||
def handle_connection(self, request, client_address):
|
def handle_connection(self, request, client_address):
|
||||||
|
"""
|
||||||
|
Called after client connection.
|
||||||
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def handle_shutdown(self):
|
||||||
|
"""
|
||||||
|
Called after server shutdown.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
@ -12,12 +12,6 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
|
||||||
A simple proxy server implementation, which always reads all of a server
|
|
||||||
response into memory, performs some transformation, and then writes it back
|
|
||||||
to the client.
|
|
||||||
"""
|
|
||||||
import sys, os, string, socket, time
|
import sys, os, string, socket, time
|
||||||
import shutil, tempfile, threading
|
import shutil, tempfile, threading
|
||||||
import optparse, SocketServer
|
import optparse, SocketServer
|
||||||
@ -535,8 +529,7 @@ class ProxyServer(netlib.TCPServer):
|
|||||||
def handle_connection(self, request, client_address):
|
def handle_connection(self, request, client_address):
|
||||||
ProxyHandler(self.config, request, client_address, self, self.masterq)
|
ProxyHandler(self.config, request, client_address, self, self.masterq)
|
||||||
|
|
||||||
def shutdown(self):
|
def handle_shutdown(self):
|
||||||
netlib.TCPServer.shutdown(self)
|
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(self.certdir)
|
shutil.rmtree(self.certdir)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
Loading…
Reference in New Issue
Block a user