Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 110 lines (95 sloc) 3.713 kb
ef710cc4 »
2010-12-09 initial commit
1 from google.appengine.ext import webapp
2 from google.appengine.ext import db
3 from google.appengine.ext.webapp import util, template
4 from google.appengine.api import memcache
5 from google.appengine.api import urlfetch
6 from django.utils import simplejson
7 from gdata.apps.service import AppsService
8 import logging
9 import base64
267767e9 »
2010-12-26 getting it all working again
10 import urllib
ef710cc4 »
2010-12-09 initial commit
11
12 DOMAIN = 'hackerdojo.com'
c7d8c8cf »
2010-12-26 adding bandwidth restrictions to the radius-appengine protocol
13 MEMBER_DOWNLOAD = 0
14 MEMBER_UPLOAD = 0
15 GUEST_DOWNLOAD = 1024
16 GUEST_UPLOAD = 384
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
17 #GUEST_TIMEOUT = 86400 # 24 hours
18 GUEST_TIMEOUT = 60
ef710cc4 »
2010-12-09 initial commit
19
20 # Hacker Dojo Domain API helper with caching
21 def dojo(path, force=False):
22 base_url = 'http://domain.hackerdojo.com'
23 cache_ttl = 3600
24 resp = memcache.get(path)
25 if force or not resp:
26 resp = urlfetch.fetch(base_url + path, deadline=10)
27 try:
28 resp = simplejson.loads(resp.content)
29 except Exception, e:
30 resp = []
31 cache_ttl = 10
32 memcache.set(path, resp, cache_ttl)
33 return resp
34
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
35 def is_suspended(user):
36 return dojo('/users/%s' % user)['suspended']
37
267767e9 »
2010-12-26 getting it all working again
38 class MacAddressMapping(db.Model):
39 address = db.StringProperty()
40 username = db.StringProperty()
41 created = db.DateTimeProperty(auto_now_add=True)
42
43 @classmethod
44 def get_by_mac(cls, address):
45 return cls.all().filter('address =', address).get()
46
ef710cc4 »
2010-12-09 initial commit
47 class MainHandler(webapp.RequestHandler):
267767e9 »
2010-12-26 getting it all working again
48 def get(self, data):
ef710cc4 »
2010-12-09 initial commit
49 try:
267767e9 »
2010-12-26 getting it all working again
50 data = urllib.unquote(data)
ef710cc4 »
2010-12-09 initial commit
51 mac, redirect = base64.b64decode(data).split(',')
52 self.response.out.write(template.render('templates/main.html', {
53 'redirect': redirect,
54 'mac': mac,
267767e9 »
2010-12-26 getting it all working again
55 'failure': self.request.get('fail')
ef710cc4 »
2010-12-09 initial commit
56 }))
57 except:
58 self.error(400)
267767e9 »
2010-12-26 getting it all working again
59 self.response.out.write("bad request")
ef710cc4 »
2010-12-09 initial commit
60
61 def post(self, data):
62 client = AppsService(domain=DOMAIN)
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
63 username = self.request.get('username')
64 mac = self.request.get('mac')
ef710cc4 »
2010-12-09 initial commit
65 try:
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
66 client.ClientLogin('%s@%s' % (username, DOMAIN), self.request.get('password'))
67 existing = MacAddressMapping.get_by_mac(mac)
68 if existing and not is_suspended(username):
69 self.redirect(self.request.get('redirect') or 'http://hackerdojo.com')
70 elif not is_suspended(username):
71 m = MacAddressMapping(address=mac, username=username)
267767e9 »
2010-12-26 getting it all working again
72 m.put()
73 self.redirect(self.request.get('redirect') or 'http://hackerdojo.com')
ef710cc4 »
2010-12-09 initial commit
74 else:
75 raise Exception()
76 except:
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
77 self.redirect('/%s?fail=1' % urllib.unquote(data))
ef710cc4 »
2010-12-09 initial commit
78
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
79 class GuestHandler(webapp.RequestHandler):
80 def post(self):
81 memcache.set(self.request.get('mac'), 'internet-guest', time=GUEST_TIMEOUT)
82 self.redirect(self.request.get('redirect') or 'http://hackerdojo.com')
83
ef710cc4 »
2010-12-09 initial commit
84
85 class MacHandler(webapp.RequestHandler):
86 def get(self, mac):
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
87 guest = memcache.get(mac)
88 if guest:
89 self.response.out.write("%s,%s,%s" % (guest, GUEST_DOWNLOAD, GUEST_UPLOAD))
ef710cc4 »
2010-12-09 initial commit
90 else:
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
91 mapping = MacAddressMapping.get_by_mac(mac)
92 if mapping and not is_suspended(mapping.username):
93 self.response.out.write("%s,%s,%s" % (mapping.username, MEMBER_DOWNLOAD, MEMBER_UPLOAD))
94 else:
95 self.error(404)
96 self.response.out.write("not found")
97
ef710cc4 »
2010-12-09 initial commit
98
99
100 def main():
101 application = webapp.WSGIApplication([
102 ('/api/mac/(.+)', MacHandler),
ece2b4a6 »
2010-12-26 guest internet access, cleanups and debugging info
103 ('/guest', GuestHandler),
ef710cc4 »
2010-12-09 initial commit
104 ('/(.+)', MainHandler),],
105 debug=True)
106 util.run_wsgi_app(application)
107
108
109 if __name__ == '__main__':
110 main()
Something went wrong with that request. Please try again.