]>
Commit | Line | Data |
---|---|---|
1 | #/usr/bin/python | |
2 | ||
3 | import logging | |
4 | import itertools | |
5 | import os.path | |
6 | import phonenumbers | |
7 | import phonenumbers.geocoder | |
8 | import tornado.locale | |
9 | import tornado.options | |
10 | import tornado.web | |
11 | ||
12 | import ipfire | |
13 | ||
14 | from .handlers import * | |
15 | ||
16 | from . import auth | |
17 | from . import blog | |
18 | from . import download | |
19 | from . import location | |
20 | from . import newsletter | |
21 | from . import nopaste | |
22 | from . import people | |
23 | from . import ui_modules | |
24 | ||
25 | class Application(tornado.web.Application): | |
26 | def __init__(self, config, **kwargs): | |
27 | # Initialize backend | |
28 | self.backend = ipfire.Backend(config) | |
29 | ||
30 | settings = { | |
31 | # Do not compress responses | |
32 | "gzip" : False, | |
33 | ||
34 | # Enable XSRF cookies | |
35 | "xsrf_cookies" : True, | |
36 | ||
37 | # Login | |
38 | "login_url" : "/login", | |
39 | ||
40 | # Setup directory structure | |
41 | "static_path" : self.backend.config.get("global", "static_dir"), | |
42 | "template_path" : self.backend.config.get("global", "templates_dir"), | |
43 | ||
44 | # UI Modules | |
45 | "ui_methods" : { | |
46 | "format_month_name" : self.format_month_name, | |
47 | "format_phone_number" : self.format_phone_number, | |
48 | "format_phone_number_to_e164" : self.format_phone_number_to_e164, | |
49 | "format_phone_number_location" : self.format_phone_number_location, | |
50 | "grouper" : grouper, | |
51 | }, | |
52 | "ui_modules" : { | |
53 | # Blog | |
54 | "BlogHistoryNavigation": blog.HistoryNavigationModule, | |
55 | "BlogList" : blog.ListModule, | |
56 | "BlogPost" : blog.PostModule, | |
57 | "BlogPosts" : blog.PostsModule, | |
58 | ||
59 | # Location | |
60 | "Map" : ui_modules.MapModule, | |
61 | ||
62 | # Talk | |
63 | "AccountsList" : people.AccountsListModule, | |
64 | "CDR" : people.CDRModule, | |
65 | "Channels" : people.ChannelsModule, | |
66 | "MOS" : people.MOSModule, | |
67 | "Registrations" : people.RegistrationsModule, | |
68 | "SIPStatus" : people.SIPStatusModule, | |
69 | ||
70 | # Nopaste | |
71 | "Code" : nopaste.CodeModule, | |
72 | ||
73 | # Old modules | |
74 | "LanguageName" : ui_modules.LanguageNameModule, | |
75 | ||
76 | "MirrorItem" : ui_modules.MirrorItemModule, | |
77 | "MirrorsTable" : ui_modules.MirrorsTableModule, | |
78 | "NetBootMenuConfig" : ui_modules.NetBootMenuConfigModule, | |
79 | "NetBootMenuHeader" : ui_modules.NetBootMenuHeaderModule, | |
80 | "NetBootMenuSeparator" : ui_modules.NetBootMenuSeparatorModule, | |
81 | "ProgressBar" : ui_modules.ProgressBarModule, | |
82 | "FireinfoDeviceTable" : ui_modules.FireinfoDeviceTableModule, | |
83 | "FireinfoDeviceAndGroupsTable" : ui_modules.FireinfoDeviceAndGroupsTableModule, | |
84 | "FireinfoGeoTable" : ui_modules.FireinfoGeoTableModule, | |
85 | }, | |
86 | ||
87 | # Call this when a page wasn't found | |
88 | "default_handler_class" : NotFoundHandler, | |
89 | } | |
90 | settings.update(kwargs) | |
91 | ||
92 | tornado.web.Application.__init__(self, **settings) | |
93 | ||
94 | authentication_handlers = [ | |
95 | (r"/login", auth.LoginHandler), | |
96 | (r"/logout", auth.LogoutHandler), | |
97 | ] | |
98 | ||
99 | self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [ | |
100 | # Entry site that lead the user to index | |
101 | (r"/", IndexHandler), | |
102 | ||
103 | # Download sites | |
104 | (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }), | |
105 | (r"/download", download.IndexHandler), | |
106 | (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler), | |
107 | ||
108 | # Donate | |
109 | (r"/donate", DonateHandler), | |
110 | (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }), | |
111 | ||
112 | # Newsletter | |
113 | (r"/newsletter/subscribe", newsletter.SubscribeHandler), | |
114 | ||
115 | # RSS feed | |
116 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
117 | ||
118 | # Redirect news articles to blog | |
119 | (r"/news/(.*)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }), | |
120 | ||
121 | # Static Pages | |
122 | (r"/chat", StaticHandler, { "template" : "chat.html" }), | |
123 | (r"/features", StaticHandler, { "template" : "features.html" }), | |
124 | (r"/legal", StaticHandler, { "template" : "legal.html" }), | |
125 | (r"/support", StaticHandler, { "template" : "support.html" }), | |
126 | ||
127 | # Handle old pages that have moved elsewhere | |
128 | (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }), | |
129 | (r"/(de|en)/(.*)", LangCompatHandler), | |
130 | ||
131 | # Export arbitrary error pages | |
132 | (r"/error/([45][0-9]{2})", ErrorHandler), | |
133 | ||
134 | # Block page | |
135 | (r"/blocked", BlockedHandler), | |
136 | ]) | |
137 | ||
138 | # blog.ipfire.org | |
139 | self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [ | |
140 | (r"/", blog.IndexHandler), | |
141 | (r"/authors/(\w+)", blog.AuthorHandler), | |
142 | (r"/compose", blog.ComposeHandler), | |
143 | (r"/drafts", blog.DraftsHandler), | |
144 | (r"/post/([0-9a-z\-\._]+)", blog.PostHandler), | |
145 | (r"/post/([0-9a-z\-\._]+)/edit", blog.EditHandler), | |
146 | (r"/post/([0-9a-z\-\._]+)/publish", blog.PublishHandler), | |
147 | (r"/search", blog.SearchHandler), | |
148 | (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler), | |
149 | (r"/years/([0-9]+)", blog.YearHandler), | |
150 | ||
151 | # RSS Feed | |
152 | (r"/feed.xml", blog.FeedHandler), | |
153 | ] + authentication_handlers) | |
154 | ||
155 | # downloads.ipfire.org | |
156 | self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [ | |
157 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), | |
158 | (r"/(.*)", download.FileHandler), | |
159 | ]) | |
160 | ||
161 | # mirrors.ipfire.org | |
162 | self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [ | |
163 | (r"/", MirrorIndexHandler), | |
164 | (r"/mirror/(.*)", MirrorItemHandler), | |
165 | ]) | |
166 | ||
167 | # planet.ipfire.org | |
168 | self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [ | |
169 | (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }), | |
170 | (r"/post/([A-Za-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }), | |
171 | (r"/user/([a-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}" }), | |
172 | ||
173 | # RSS | |
174 | (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
175 | (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}.rss" }), | |
176 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
177 | ]) | |
178 | ||
179 | # fireinfo.ipfire.org | |
180 | self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [ | |
181 | (r"/", FireinfoIndexHandler), | |
182 | (r"/device/driver/(.*)", FireinfoDeviceDriverDetail), | |
183 | (r"/device/vendors", FireinfoDeviceVendorsHandler), | |
184 | (r"/device/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorHandler), | |
185 | (r"/device/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelHandler), | |
186 | ||
187 | # Show profiles | |
188 | (r"/profile/random", FireinfoRandomProfileHandler), | |
189 | (r"/profile/([a-z0-9]{40})", FireinfoProfileDetailHandler), | |
190 | ||
191 | # Send profiles. | |
192 | (r"/send/([a-z0-9]+)", FireinfoProfileSendHandler), | |
193 | ||
194 | # Stats handlers | |
195 | (r"/statistics", FireinfoStatsHandler), | |
196 | (r"/statistics/processors", FireinfoStatsProcessorsHandler), | |
197 | (r"/statistics/processors/(arm|x86)", FireinfoStatsProcessorDetailHandler), | |
198 | (r"/statistics/geo-locations", FireinfoStatsGeoHandler), | |
199 | (r"/statistics/languages", FireinfoStatsLanguagesHandler), | |
200 | (r"/statistics/memory", FireinfoStatsMemoryHandler), | |
201 | (r"/statistics/networking", FireinfoStatsNetworkingHandler), | |
202 | (r"/statistics/releases", FireinfoStatsReleasesHandler), | |
203 | (r"/statistics/virtualization", FireinfoStatsVirtualHandler), | |
204 | ||
205 | # Compat handlers | |
206 | (r"/stats", tornado.web.RedirectHandler, { "url" : "/statistics" }), | |
207 | (r"/stats/cpus", tornado.web.RedirectHandler, { "url" : "/statistics/processors" }), | |
208 | (r"/stats/geo", tornado.web.RedirectHandler, { "url" : "/statistics/geo-locations" }), | |
209 | (r"/stats/network", tornado.web.RedirectHandler, { "url" : "/statistics/networking" }), | |
210 | (r"/stats/oses", tornado.web.RedirectHandler, { "url" : "/statistics/releases" }), | |
211 | (r"/stats/virtual", tornado.web.RedirectHandler, { "url" : "/statistics/virtualization" }), | |
212 | (r"/vendor/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorCompatHandler), | |
213 | (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelCompatHandler), | |
214 | ]) | |
215 | ||
216 | # i-use.ipfire.org | |
217 | self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [ | |
218 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage), | |
219 | ]) | |
220 | ||
221 | # boot.ipfire.org | |
222 | BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot") | |
223 | self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [ | |
224 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/download" }), | |
225 | ||
226 | # Configurations | |
227 | (r"/premenu.cfg", PremenuCfgHandler), | |
228 | (r"/menu.gpxe", MenuGPXEHandler), | |
229 | (r"/menu.cfg", MenuCfgHandler), | |
230 | ||
231 | # Static files | |
232 | (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)", | |
233 | tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }), | |
234 | ]) | |
235 | ||
236 | # nopaste.ipfire.org | |
237 | self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [ | |
238 | (r"/", nopaste.CreateHandler), | |
239 | (r"/raw/(.*)", nopaste.RawHandler), | |
240 | (r"/view/(.*)", nopaste.ViewHandler), | |
241 | ] + authentication_handlers) | |
242 | ||
243 | # location.ipfire.org | |
244 | self.add_handlers(r"location(\.dev)?\.ipfire\.org", [ | |
245 | (r"/", location.IndexHandler), | |
246 | (r"/lookup/(.+)", location.LookupHandler), | |
247 | ]) | |
248 | ||
249 | # geoip.ipfire.org | |
250 | self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [ | |
251 | (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }), | |
252 | ]) | |
253 | ||
254 | # talk.ipfire.org | |
255 | self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [ | |
256 | (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }), | |
257 | ]) | |
258 | ||
259 | # people.ipfire.org | |
260 | self.add_handlers(r"people(\.dev)?\.ipfire\.org", [ | |
261 | (r"/", people.IndexHandler), | |
262 | (r"/conferences", people.ConferencesHandler), | |
263 | (r"/search", people.SearchHandler), | |
264 | (r"/users", people.UsersHandler), | |
265 | (r"/users/(\w+)", people.UserHandler), | |
266 | (r"/users/(\w+)\.jpg", people.AvatarHandler), | |
267 | (r"/users/(\w+)/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler), | |
268 | (r"/users/(\w+)/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler), | |
269 | (r"/users/(\w+)/edit", people.UserEditHandler), | |
270 | (r"/users/(\w+)/passwd", people.UserPasswdHandler), | |
271 | (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler), | |
272 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)/delete", people.SSHKeysDeleteHandler), | |
273 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)", people.SSHKeysDownloadHandler), | |
274 | (r"/users/(\w+)/ssh-keys/upload", people.SSHKeysUploadHandler), | |
275 | (r"/users/(\w+)/sip", people.SIPHandler), | |
276 | ] + authentication_handlers) | |
277 | ||
278 | # ipfire.org | |
279 | self.add_handlers(r"ipfire\.org", [ | |
280 | (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" }) | |
281 | ]) | |
282 | ||
283 | logging.info("Successfully initialied application") | |
284 | ||
285 | def format_month_name(self, handler, month): | |
286 | _ = handler.locale.translate | |
287 | ||
288 | if month == 1: | |
289 | return _("January") | |
290 | elif month == 2: | |
291 | return _("February") | |
292 | elif month == 3: | |
293 | return _("March") | |
294 | elif month == 4: | |
295 | return _("April") | |
296 | elif month == 5: | |
297 | return _("May") | |
298 | elif month == 6: | |
299 | return _("June") | |
300 | elif month == 7: | |
301 | return _("July") | |
302 | elif month == 8: | |
303 | return _("August") | |
304 | elif month == 9: | |
305 | return _("September") | |
306 | elif month == 10: | |
307 | return _("October") | |
308 | elif month == 11: | |
309 | return _("November") | |
310 | elif month == 12: | |
311 | return _("December") | |
312 | ||
313 | return month | |
314 | ||
315 | def format_phone_number(self, handler, number): | |
316 | if not isinstance(number, phonenumbers.PhoneNumber): | |
317 | try: | |
318 | number = phonenumbers.parse(number, None) | |
319 | except phonenumbers.phonenumberutil.NumberParseException: | |
320 | return number | |
321 | ||
322 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL) | |
323 | ||
324 | def format_phone_number_to_e164(self, handler, number): | |
325 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164) | |
326 | ||
327 | def format_phone_number_location(self, handler, number): | |
328 | s = [ | |
329 | phonenumbers.geocoder.description_for_number(number, handler.locale.code), | |
330 | phonenumbers.region_code_for_number(number), | |
331 | ] | |
332 | ||
333 | return ", ".join((e for e in s if e)) | |
334 | ||
335 | ||
336 | def grouper(handler, iterator, n): | |
337 | """ | |
338 | Returns groups of n from the iterator | |
339 | """ | |
340 | i = iter(iterator) | |
341 | ||
342 | while True: | |
343 | ret = list(itertools.islice(i, 0, n)) | |
344 | if not ret: | |
345 | break | |
346 | ||
347 | yield ret |