]>
jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/root.py
1 from twisted
.protocols
.policies
import TimeoutMixin
2 from twisted
.web
import resource
, server
, static
, http
3 from twisted
.internet
import error
, reactor
10 class RootResource(resource
.Resource
):
11 def getChild(self
, name
, request
):
14 return self
.primaryChild
.getChild(name
, request
)
16 class ProxyRequest(server
.Request
):
17 ip_re
= re
.compile(r
"^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})|(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2})))$", re
.IGNORECASE
)
18 def validIP(self
, ip
):
19 m
= self
.ip_re
.match(ip
)
24 def getClientIP(self
):
25 real_ip
= http
.Request
.getClientIP(self
)
26 if real_ip
not in config
.FORWARDED_FOR_IPS
:
29 fake_ips
= self
.getHeader(config
.FORWARDED_FOR_HEADER
)
33 fake_ip
= fake_ips
.split(",")[-1].strip()
34 if not self
.validIP(fake_ip
):
39 class HTTPChannel(http
.HTTPChannel
):
40 def timeoutConnection(self
):
41 self
.transport
.abortConnection()
43 class RootSite(server
.Site
):
44 protocol
= HTTPChannel
46 if hasattr(config
, "FORWARDED_FOR_HEADER"):
47 requestFactory
= ProxyRequest
49 def __init__(self
, path
, *args
, **kwargs
):
51 kwargs
["timeout"] = config
.HTTP_REQUEST_TIMEOUT
52 server
.Site
.__init
__(self
, root
, *args
, **kwargs
)
54 services
["StaticEngine"] = root
.primaryChild
= engines
.StaticEngine(path
)
56 def register(service
, path
, *args
, **kwargs
):
57 sobj
= service("/" + path
, *args
, **kwargs
)
58 services
[service
.__name
__] = sobj
59 root
.putChild(path
, sobj
)
61 register(engines
.AJAXEngine
, "e")
63 register(engines
.WebSocketEngine
, "w")
64 except AttributeError:
66 register(engines
.FeedbackEngine
, "feedback")
67 register(engines
.AuthgateEngine
, "auth")
68 register(engines
.AdminEngine
, "adminengine", services
)
70 mimetypes
.types_map
[".ico"] = "image/vnd.microsoft.icon"