Mercurial > libervia-web
comparison src/server/server.py @ 810:3905bc24eb17
server: proper options handling
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 18 Dec 2015 21:29:24 +0100 |
parents | 68eadda8a59a |
children | fd6965c16e7e |
comparison
equal
deleted
inserted
replaced
809:c500bdb0c216 | 810:3905bc24eb17 |
---|---|
788 return server.NOT_DONE_YET | 788 return server.NOT_DONE_YET |
789 return Exception('Unknown submit type') | 789 return Exception('Unknown submit type') |
790 | 790 |
791 def _loginAccount(self, request, new_account_domain): | 791 def _loginAccount(self, request, new_account_domain): |
792 """Try to authenticate the user with the request information. | 792 """Try to authenticate the user with the request information. |
793 | 793 |
794 @param request: request of the register form | 794 @param request: request of the register form |
795 @param new_account_domain (unicode): host corresponding to the local domain | 795 @param new_account_domain (unicode): host corresponding to the local domain |
796 @return: a constant indicating the state: | 796 @return: a constant indicating the state: |
797 - C.BAD_REQUEST: something is wrong in the request (bad arguments) | 797 - C.BAD_REQUEST: something is wrong in the request (bad arguments) |
798 - C.PROFILE_AUTH_ERROR: either the profile (login) or the profile password is wrong | 798 - C.PROFILE_AUTH_ERROR: either the profile (login) or the profile password is wrong |
989 # XXX: we put this method in Register because we get menus before being logged | 989 # XXX: we put this method in Register because we get menus before being logged |
990 return self.sat_host.bridge.getMenus('', C.SECURITY_LIMIT) | 990 return self.sat_host.bridge.getMenus('', C.SECURITY_LIMIT) |
991 | 991 |
992 def __getSecurityWarning(self): | 992 def __getSecurityWarning(self): |
993 """@return: a security warning message, or None if the connection is secure""" | 993 """@return: a security warning message, or None if the connection is secure""" |
994 if self.request.URLPath().scheme == 'https' or not self.sat_host.security_warning: | 994 if self.request.URLPath().scheme == 'https' or not self.sat_host.options['security_warning']: |
995 return None | 995 return None |
996 text = "<p>" + D_("You are about to connect to an unsecure service.") + "</p><p> </p><p>" | 996 text = "<p>" + D_("You are about to connect to an unsecure service.") + "</p><p> </p><p>" |
997 | 997 |
998 if self.sat_host.connection_type == 'both': | 998 if self.sat_host.options['connection_type'] == 'both': |
999 new_port = (':%s' % self.sat_host.port_https_ext) if self.sat_host.port_https_ext != HTTPS_PORT else '' | 999 new_port = (':%s' % self.sat_host.options['port_https_ext']) if self.sat_host.options['port_https_ext'] != HTTPS_PORT else '' |
1000 url = "https://%s" % self.request.URLPath().netloc.replace(':%s' % self.sat_host.port, new_port) | 1000 url = "https://%s" % self.request.URLPath().netloc.replace(':%s' % self.sat_host.options['port'], new_port) |
1001 text += D_('Please read our %(faq_prefix)ssecurity notice%(faq_suffix)s regarding HTTPS') % {'faq_prefix': '<a href="http://salut-a-toi.org/faq.html#https" target="#">', 'faq_suffix': '</a>'} | 1001 text += D_('Please read our %(faq_prefix)ssecurity notice%(faq_suffix)s regarding HTTPS') % {'faq_prefix': '<a href="http://salut-a-toi.org/faq.html#https" target="#">', 'faq_suffix': '</a>'} |
1002 text += "</p><p>" + D_('and use the secure version of this website:') | 1002 text += "</p><p>" + D_('and use the secure version of this website:') |
1003 text += '</p><p> </p><p align="center"><a href="%(url)s">%(url)s</a>' % {'url': url} | 1003 text += '</p><p> </p><p align="center"><a href="%(url)s">%(url)s</a>' % {'url': url} |
1004 else: | 1004 else: |
1005 text += D_('You should ask the administrator of %(url)s to turn on HTTPS.') | 1005 text += D_('You should ask the administrator of %(url)s to turn on HTTPS.') |
1223 | 1223 |
1224 | 1224 |
1225 class Libervia(service.Service): | 1225 class Libervia(service.Service): |
1226 | 1226 |
1227 | 1227 |
1228 def __init__(self, *args, **kwargs): | 1228 def __init__(self, options): |
1229 self.options = options | |
1229 self.initialised = defer.Deferred() | 1230 self.initialised = defer.Deferred() |
1230 | 1231 |
1231 # options managing | 1232 if not self.options['port_https_ext']: |
1232 for opt in OPT_PARAMETERS_BOTH + OPT_PARAMETERS_CFG: | 1233 self.options['port_https_ext'] = self.options['port_https'] |
1233 opt_name = opt[0] | 1234 if self.options['data_dir'] == DATA_DIR_DEFAULT: |
1234 setattr(self, opt_name, kwargs.get(opt_name, opt[2])) | 1235 coerceDataDir(self.options['data_dir']) # this is not done when using the default value |
1235 if not self.port_https_ext: | 1236 |
1236 self.port_https_ext = self.port_https | 1237 self.html_dir = os.path.join(self.options['data_dir'], C.HTML_DIR) |
1237 if self.data_dir == DATA_DIR_DEFAULT: | 1238 self.themes_dir = os.path.join(self.options['data_dir'], C.THEMES_DIR) |
1238 coerceDataDir(self.data_dir) # this is not done when using the default value | |
1239 | |
1240 self.html_dir = os.path.join(self.data_dir, C.HTML_DIR) | |
1241 self.themes_dir = os.path.join(self.data_dir, C.THEMES_DIR) | |
1242 | 1239 |
1243 self._cleanup = [] | 1240 self._cleanup = [] |
1244 | 1241 |
1245 root = ProtectedFile(self.html_dir) | 1242 root = ProtectedFile(self.html_dir) |
1246 | 1243 |
1337 log.error(_(u"Connection failed: %s") % e) | 1334 log.error(_(u"Connection failed: %s") % e) |
1338 self.stop() | 1335 self.stop() |
1339 | 1336 |
1340 def initOk(dummy): | 1337 def initOk(dummy): |
1341 if not self.bridge.isConnected(C.SERVICE_PROFILE): | 1338 if not self.bridge.isConnected(C.SERVICE_PROFILE): |
1342 self.bridge.asyncConnect(C.SERVICE_PROFILE, self.passphrase, | 1339 self.bridge.asyncConnect(C.SERVICE_PROFILE, self.options['passphrase'], |
1343 callback=self._startService, errback=eb) | 1340 callback=self._startService, errback=eb) |
1344 else: | 1341 else: |
1345 self._startService() | 1342 self._startService() |
1346 | 1343 |
1347 self.initialised.addCallback(initOk) | 1344 self.initialised.addCallback(initOk) |
1349 def _startService(self, dummy=None): | 1346 def _startService(self, dummy=None): |
1350 """Actually start the HTTP(S) server(s) after the profile for Libervia is connected. | 1347 """Actually start the HTTP(S) server(s) after the profile for Libervia is connected. |
1351 @raise IOError: the certificate file doesn't exist | 1348 @raise IOError: the certificate file doesn't exist |
1352 @raise OpenSSL.crypto.Error: the certificate file is invalid | 1349 @raise OpenSSL.crypto.Error: the certificate file is invalid |
1353 """ | 1350 """ |
1354 if self.connection_type in ('https', 'both'): | 1351 if self.options['connection_type'] in ('https', 'both'): |
1355 if not ssl_available: | 1352 if ssl is None: |
1356 raise(ImportError(_("Python module pyOpenSSL is not installed!"))) | 1353 raise ImportError(u"Python module pyOpenSSL is not installed!") |
1357 try: | 1354 try: |
1358 with open(os.path.expanduser(self.ssl_certificate)) as keyAndCert: | 1355 with open(os.path.expanduser(self.options['ssl_certificate'])) as keyAndCert: |
1359 try: | 1356 try: |
1360 cert = ssl.PrivateCertificate.loadPEM(keyAndCert.read()) | 1357 cert = ssl.PrivateCertificate.loadPEM(keyAndCert.read()) |
1361 except OpenSSL.crypto.Error as e: | 1358 except OpenSSL.crypto.Error as e: |
1362 log.error(_(u"The file '%s' must contain both private and public parts of the certificate") % self.ssl_certificate) | 1359 log.error(_(u"The file '%s' must contain both private and public parts of the certificate") % self.options['ssl_certificate']) |
1363 raise e | 1360 raise e |
1364 except IOError as e: | 1361 except IOError as e: |
1365 log.error(_(u"The file '%s' doesn't exist") % self.ssl_certificate) | 1362 log.error(_(u"The file '%s' doesn't exist") % self.options['ssl_certificate']) |
1366 raise e | 1363 raise e |
1367 reactor.listenSSL(self.port_https, self.site, cert.options()) | 1364 reactor.listenSSL(self.options['port_https'], self.site, cert.options()) |
1368 if self.connection_type in ('http', 'both'): | 1365 if self.options['connection_type'] in ('http', 'both'): |
1369 if self.connection_type == 'both' and self.redirect_to_https: | 1366 if self.options['connection_type'] == 'both' and self.options['redirect_to_https']: |
1370 reactor.listenTCP(self.port, server.Site(RedirectToHTTPS(self.port, self.port_https_ext))) | 1367 reactor.listenTCP(self.options['port'], server.Site(RedirectToHTTPS(self.options['port'], self.options['port_https_ext']))) |
1371 else: | 1368 else: |
1372 reactor.listenTCP(self.port, self.site) | 1369 reactor.listenTCP(self.options['port'], self.site) |
1373 | 1370 |
1374 def stopService(self): | 1371 def stopService(self): |
1375 log.info(_("launching cleaning methods")) | 1372 log.info(_("launching cleaning methods")) |
1376 for callback, args, kwargs in self._cleanup: | 1373 for callback, args, kwargs in self._cleanup: |
1377 callback(*args, **kwargs) | 1374 callback(*args, **kwargs) |