]>
jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/root.py
1 from twisted
.web
import resource
, server
, static
, http
2 from twisted
.internet
import error
, reactor
9 class RootResource(resource
.Resource
):
10 def getChild(self
, name
, request
):
13 return self
.primaryChild
.getChild(name
, request
)
15 # we do NOT use the built-in timeOut mixin as it's very very buggy!
16 class TimeoutHTTPChannel(http
.HTTPChannel
):
17 timeout
= config
.HTTP_REQUEST_TIMEOUT
19 def connectionMade(self
):
20 self
.customTimeout
= reactor
.callLater(self
.timeout
, self
.timeoutOccured
)
21 http
.HTTPChannel
.connectionMade(self
)
23 def timeoutOccured(self
):
24 self
.customTimeout
= None
25 self
.transport
.loseConnection()
27 def cancelTimeout(self
):
28 if self
.customTimeout
is not None:
30 self
.customTimeout
.cancel()
31 self
.customTimeout
= None
32 except error
.AlreadyCalled
:
35 def connectionLost(self
, reason
):
37 http
.HTTPChannel
.connectionLost(self
, reason
)
39 class ProxyRequest(server
.Request
):
40 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
)
41 def validIP(self
, ip
):
42 m
= self
.ip_re
.match(ip
)
47 def getClientIP(self
):
48 real_ip
= http
.Request
.getClientIP(self
)
49 if real_ip
not in config
.FORWARDED_FOR_IPS
:
52 fake_ips
= self
.getHeader(config
.FORWARDED_FOR_HEADER
)
56 fake_ip
= fake_ips
.split(",")[-1].strip()
57 if not self
.validIP(fake_ip
):
62 class RootSite(server
.Site
):
63 # we do this ourselves as the built in timeout stuff is really really buggy
64 protocol
= TimeoutHTTPChannel
66 if hasattr(config
, "FORWARDED_FOR_HEADER"):
67 requestFactory
= ProxyRequest
69 def __init__(self
, path
, *args
, **kwargs
):
71 server
.Site
.__init
__(self
, root
, *args
, **kwargs
)
74 services
["StaticEngine"] = root
.primaryChild
= engines
.StaticEngine(path
)
76 def register(service
, path
, *args
, **kwargs
):
77 sobj
= service("/" + path
, *args
, **kwargs
)
78 services
[service
.__name
__] = sobj
79 root
.putChild(path
, sobj
)
81 register(engines
.AJAXEngine
, "e")
82 register(engines
.FeedbackEngine
, "feedback")
83 register(engines
.AuthgateEngine
, "auth")
84 register(engines
.AdminEngine
, "adminengine", services
)
86 mimetypes
.types_map
[".ico"] = "image/vnd.microsoft.icon"