2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2014-06-25 21:25:49 +00:00
|
|
|
class ProxyError(Exception):
|
|
|
|
def __init__(self, code, message, headers=None):
|
2014-08-31 14:52:26 +00:00
|
|
|
super(ProxyError, self).__init__(message)
|
2014-06-25 21:25:49 +00:00
|
|
|
self.code, self.headers = code, headers
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyServerError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-10 04:11:51 +00:00
|
|
|
class UpstreamServerResolver(object):
|
|
|
|
def __call__(self, conn):
|
|
|
|
"""
|
|
|
|
Returns the address of the server to connect to.
|
|
|
|
"""
|
2014-03-11 01:16:22 +00:00
|
|
|
raise NotImplementedError # pragma: nocover
|
2014-03-10 04:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConstUpstreamServerResolver(UpstreamServerResolver):
|
|
|
|
def __init__(self, dst):
|
|
|
|
self.dst = dst
|
|
|
|
|
|
|
|
def __call__(self, conn):
|
|
|
|
return self.dst
|
|
|
|
|
|
|
|
|
|
|
|
class TransparentUpstreamServerResolver(UpstreamServerResolver):
|
|
|
|
def __init__(self, resolver, sslports):
|
|
|
|
self.resolver = resolver
|
|
|
|
self.sslports = sslports
|
|
|
|
|
|
|
|
def __call__(self, conn):
|
2014-08-10 14:10:49 +00:00
|
|
|
try:
|
|
|
|
dst = self.resolver.original_addr(conn)
|
|
|
|
except Exception, e:
|
|
|
|
raise ProxyError(502, "Transparent mode failure: %s" % str(e))
|
2014-03-10 04:11:51 +00:00
|
|
|
|
|
|
|
if dst[1] in self.sslports:
|
|
|
|
ssl = True
|
|
|
|
else:
|
|
|
|
ssl = False
|
|
|
|
return [ssl, ssl] + list(dst)
|
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class Log:
|
2014-03-13 00:22:12 +00:00
|
|
|
def __init__(self, msg, level="info"):
|
2014-03-13 00:04:45 +00:00
|
|
|
self.msg = msg
|
|
|
|
self.level = level
|