annotate libervia/server/proxy.py @ 1360:389a83eefe62

server: SàT applications integration: - a SàT Application can be added to the menu (if necessary values are exposed), by using the `sat-app:[application_name]` in `menu_json` or `menu_extra_json`. The application will then be started with Libervia, and embedded, i.e. Libervia menu will appear and application will be integrated under it. - the same `sat-app:[application_name]` thing can be used in redirection, in this case the redirection will reverse proxy directly the application, without embedding it (no Libervia menu will appear) - the ReverseProxy will replace headers if necessary to allow embedding in a iframe from the same domain - new `embed_app` page to embed a SàT Application
author Goffi <goffi@goffi.org>
date Mon, 28 Sep 2020 21:12:21 +0200
parents
children 822bd0139769
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: a Salut à Toi frontend
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org>
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 from twisted.web import proxy
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from twisted.python.compat import urlquote
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.log import getLogger
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = getLogger(__name__)
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class SatProxyClient(proxy.ProxyClient):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def handleHeader(self, key, value):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if key.lower() == b"x-frame-options":
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 value = b"sameorigin"
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 elif key.lower() == b"content-security-policy":
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 value = value.replace(b"frame-ancestors 'none'", b"frame-ancestors 'self'")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super().handleHeader(key, value)
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 class SatProxyClientFactory(proxy.ProxyClientFactory):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 protocol = SatProxyClient
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 class SatReverseProxyResource(proxy.ReverseProxyResource):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 """Resource Proxy rewritting headers to allow embedding in iframe on same domain"""
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 proxyClientFactoryClass = SatProxyClientFactory
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def getChild(self, path, request):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 return SatReverseProxyResource(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.host, self.port,
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.path + b'/' + urlquote(path, safe=b"").encode('utf-8'),
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.reactor
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 )