2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class ProxyError(Exception):
|
|
|
|
def __init__(self, code, msg, headers=None):
|
|
|
|
self.code, self.msg, self.headers = code, msg, headers
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "ProxyError(%s, %s)" % (self.code, self.msg)
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionTypeChange(Exception):
|
|
|
|
"""
|
|
|
|
Gets raised if the connection type has been changed (e.g. after HTTP/1.1 101 Switching Protocols).
|
|
|
|
It's up to the raising ProtocolHandler to specify the new conntype before raising the exception.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
dst = self.resolver.original_addr(conn)
|
|
|
|
if not dst:
|
|
|
|
raise ProxyError(502, "Transparent mode failure: could not resolve original destination.")
|
|
|
|
|
|
|
|
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 AddressPriority(object):
|
|
|
|
"""
|
|
|
|
Enum that signifies the priority of the given address when choosing the destination host.
|
|
|
|
Higher is better (None < i)
|
|
|
|
"""
|
2014-03-10 04:11:51 +00:00
|
|
|
MANUALLY_CHANGED = 3
|
2014-03-09 20:51:24 +00:00
|
|
|
"""user changed the target address in the ui"""
|
2014-03-10 04:11:51 +00:00
|
|
|
FROM_SETTINGS = 2
|
2014-03-13 23:02:00 +00:00
|
|
|
"""upstream server from arguments (reverse proxy, upstream proxy or from transparent resolver)"""
|
2014-03-09 20:51:24 +00:00
|
|
|
FROM_PROTOCOL = 1
|
|
|
|
"""derived from protocol (e.g. absolute-form http requests)"""
|
|
|
|
|
|
|
|
|
|
|
|
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
|