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