[ create a new paste ] login | about

Link: http://codepad.org/2gTJ21Wg    [ raw code | fork ]

Python, pasted on Mar 18:
from ConfigParser import SafeConfigParser as ConfigParser
from os import system
from time import time

from circuits.web import Server, Controller, Static
from mako.lookup import TemplateLookup

class Root(Controller):

	def _render(self, page, **kwargs):
		template = self.lookup.get_template(page)
		return template.render(**kwargs)
		
	def _virtualhost(self, session, ip=None):
		if ip:
			allow = "Allow from %s" % ip
		else:
			allow = ""
		host = self._render("%s.host" % session, allow=allow)
		f = open("/etc/apache2/sites-available/Session-%s" % session, "w")
		f.write(host)
		f.close()
		system("service apache2 reload")
		
	def _expirekeys(self):
		t = time()
		for i in self.inuse:
			if self.inuse[i] and self.inuse[i] < t:
				self.inuse[i] = False
				del self.revinuseby[self.inuseby[i]]
				self.inuseby[i] = None
				self._virtualhost(i)
		
	def _identifier(self):
		return self.request.headers["X-Forwarded-For"]
	
	def __init__(self):
		super(Root, self).__init__()
		self.config = ConfigParser()
		self.config.read("m2oproxy.ini")
		self.max = self.config.getint("sessions", "max")
		self.servers = {}
		self.inuse = {}
		self.inuseby = {}
		self.revinuseby = {}
		for i in range(1, self.max+1):
			i = str(i)
			self.servers[i] = self.config.get("sessions", str(i))
			self.inuse[i] = False
			self.inuseby[i] = None
		self.lookup = TemplateLookup(directories=['templates'])
	
	def index(self):
		self._expirekeys()
		if self._identifier() in self.revinuseby:
			return self.redirect("/getsession?key=%s&open=False"
				% self.revinuseby[self._identifier()])
		return self._render("index.html")
		
	def getkey(self):
		self._expirekeys()
		key = None
		for i in self.inuse:
			if not self.inuse[i]:
				key = i
				break
		if not key:
			page = self._render("nokeys.html")
		else:
			self.inuse[key] = time() + 900
			self.inuseby[key] = self._identifier()
			self.revinuseby[self._identifier()] = key
			self._virtualhost(key, self.request.remote.ip)
			page = self.redirect("/getsession?key=%s" % key)
		return page
		
	def getsession(self, key, open=True):
		self._expirekeys()
		if not self.inuse[key] or self._identifier() != self.inuseby[key]:
			page = self._render("index.html", error="key expired")
		else:
			page = self._render("server.html", server=self.servers[key],
				key=key, open=open, seconds=self.inuse[key] - time())
		return page
		
	def done(self, key):
		self.inuse[key] = False
		return self._render("thankyou.html")
		
	def broken(self, key):
		self.inuse[key] = time() + 1000000
		return "So sorry. Life sucks. We've disabled that server."
		
	def extend(self, key):
		self._expirekeys()
		if not self.inuse[key] or self._identifier() != self.inuseby[key]:
			return self._render("index.html", error="key expired")
		self.inuse[key] = time() + 900
		return self._render("server.html", server=self.servers[key], key=key,
			open=False, seconds=self.inuse[key] - time())
		
if __name__ == "__main__":
	s = (Server(("0.0.0.0", 8080)) + Root() + Static(docroot="static"))
	s.run()


Create a new paste based on this one


Comments: