-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon_get_webfinger.py
134 lines (129 loc) · 5.01 KB
/
daemon_get_webfinger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
__filename__ = "daemon_get_webfinger.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Core GET"
import json
from httpcodes import write2
from httpcodes import http_404
from httpheaders import redirect_headers
from httpheaders import set_headers
from webfinger import webfinger_lookup
from webfinger import webfinger_node_info
from webfinger import webfinger_meta
from webfinger import wellknown_protocol_handler
from utils import get_json_content_from_accept
from utils import convert_domains
from daemon_utils import has_accept
def get_webfinger(self, calling_domain: str, referer_domain: str,
cookie: str, path: str, debug: bool,
onion_domain: str, i2p_domain: str,
http_prefix: str, domain: str, domain_full: str,
base_dir: str, port: int) -> bool:
if not path.startswith('/.well-known'):
return False
if debug:
print('DEBUG: WEBFINGER well-known')
if debug:
print('DEBUG: WEBFINGER host-meta')
if path.startswith('/.well-known/host-meta'):
if calling_domain.endswith('.onion') and \
onion_domain:
wf_result = \
webfinger_meta('http', onion_domain)
elif (calling_domain.endswith('.i2p') and
i2p_domain):
wf_result = \
webfinger_meta('http', i2p_domain)
else:
wf_result = \
webfinger_meta(http_prefix, domain_full)
if wf_result:
msg = wf_result.encode('utf-8')
msglen = len(msg)
set_headers(self, 'application/xrd+xml', msglen,
None, calling_domain, True)
write2(self, msg)
return True
http_404(self, 6)
return True
if path.startswith('/api/statusnet') or \
path.startswith('/api/gnusocial') or \
path.startswith('/siteinfo') or \
path.startswith('/poco') or \
path.startswith('/friendi'):
http_404(self, 7)
return True
# protocol handler. See https://fedi-to.github.io/protocol-handler.html
if path.startswith('/.well-known/protocol-handler'):
if calling_domain.endswith('.onion'):
protocol_url, _ = \
wellknown_protocol_handler(path, 'http', onion_domain)
elif calling_domain.endswith('.i2p'):
protocol_url, _ = \
wellknown_protocol_handler(path, 'http', i2p_domain)
else:
protocol_url, _ = \
wellknown_protocol_handler(path, http_prefix, domain_full)
if protocol_url:
redirect_headers(self, protocol_url, cookie,
calling_domain, 308)
else:
http_404(self, 8)
return True
# nodeinfo
if path.startswith('/.well-known/nodeinfo') or \
path.startswith('/.well-known/x-nodeinfo'):
if calling_domain.endswith('.onion') and onion_domain:
wf_result = \
webfinger_node_info('http', onion_domain)
elif (calling_domain.endswith('.i2p') and i2p_domain):
wf_result = \
webfinger_node_info('http', i2p_domain)
else:
wf_result = \
webfinger_node_info(http_prefix, domain_full)
if wf_result:
msg_str = json.dumps(wf_result)
msg_str = convert_domains(calling_domain, referer_domain,
msg_str, http_prefix, domain,
onion_domain, i2p_domain)
msg = msg_str.encode('utf-8')
msglen = len(msg)
if has_accept(self, calling_domain):
accept_str = self.headers.get('Accept')
protocol_str = \
get_json_content_from_accept(accept_str)
set_headers(self, protocol_str, msglen,
None, calling_domain, True)
else:
set_headers(self, 'application/ld+json', msglen,
None, calling_domain, True)
write2(self, msg)
return True
http_404(self, 9)
return True
if debug:
print('DEBUG: WEBFINGER lookup ' + path + ' ' + str(base_dir))
wf_result = \
webfinger_lookup(path, base_dir,
domain, onion_domain,
i2p_domain, port, debug)
if wf_result:
msg_str = json.dumps(wf_result)
msg_str = convert_domains(calling_domain, referer_domain,
msg_str, http_prefix, domain,
onion_domain, i2p_domain)
msg = msg_str.encode('utf-8')
msglen = len(msg)
set_headers(self, 'application/jrd+json', msglen,
None, calling_domain, True)
write2(self, msg)
else:
if debug:
print('DEBUG: WEBFINGER lookup 404 ' + path)
http_404(self, 10)
return True