1360
|
1 #!/usr/bin/env python3 |
|
2 |
|
3 # Libervia: a Salut à Toi frontend |
1396
|
4 # Copyright (C) 2011-2021 Jérôme Poisson <goffi@goffi.org> |
1360
|
5 |
|
6 # This program is free software: you can redistribute it and/or modify |
|
7 # it under the terms of the GNU Affero General Public License as published by |
|
8 # the Free Software Foundation, either version 3 of the License, or |
|
9 # (at your option) any later version. |
|
10 |
|
11 # This program is distributed in the hope that it will be useful, |
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 # GNU Affero General Public License for more details. |
|
15 |
|
16 # You should have received a copy of the GNU Affero General Public License |
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18 from twisted.web import proxy |
|
19 from twisted.python.compat import urlquote |
|
20 from sat.core.log import getLogger |
|
21 |
|
22 log = getLogger(__name__) |
|
23 |
|
24 |
|
25 |
|
26 class SatProxyClient(proxy.ProxyClient): |
|
27 |
|
28 def handleHeader(self, key, value): |
|
29 if key.lower() == b"x-frame-options": |
|
30 value = b"sameorigin" |
|
31 elif key.lower() == b"content-security-policy": |
|
32 value = value.replace(b"frame-ancestors 'none'", b"frame-ancestors 'self'") |
|
33 |
|
34 super().handleHeader(key, value) |
|
35 |
|
36 |
|
37 class SatProxyClientFactory(proxy.ProxyClientFactory): |
|
38 protocol = SatProxyClient |
|
39 |
|
40 |
|
41 class SatReverseProxyResource(proxy.ReverseProxyResource): |
|
42 """Resource Proxy rewritting headers to allow embedding in iframe on same domain""" |
|
43 proxyClientFactoryClass = SatProxyClientFactory |
|
44 |
|
45 def getChild(self, path, request): |
|
46 return SatReverseProxyResource( |
|
47 self.host, self.port, |
|
48 self.path + b'/' + urlquote(path, safe=b"").encode('utf-8'), |
|
49 self.reactor |
|
50 ) |