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