| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  | __filename__ = "daemon_get_webfinger.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2024-12-25 14:31:14 +00:00
										 |  |  | __module_group__ = "Daemon GET" | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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, | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                   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'): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if debug: | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         print('DEBUG: WEBFINGER well-known') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if debug: | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         print('DEBUG: WEBFINGER host-meta') | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if path.startswith('/.well-known/host-meta'): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         if calling_domain.endswith('.onion') and \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |            onion_domain: | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_meta('http', onion_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         elif (calling_domain.endswith('.i2p') and | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |               i2p_domain): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_meta('http', i2p_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_meta(http_prefix, domain_full) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if path.startswith('/api/statusnet') or \ | 
					
						
							|  |  |  |        path.startswith('/api/gnusocial') or \ | 
					
						
							|  |  |  |        path.startswith('/siteinfo') or \ | 
					
						
							|  |  |  |        path.startswith('/poco') or \ | 
					
						
							|  |  |  |        path.startswith('/friendi'): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         http_404(self, 7) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     # protocol handler. See https://fedi-to.github.io/protocol-handler.html | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if path.startswith('/.well-known/protocol-handler'): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         if calling_domain.endswith('.onion'): | 
					
						
							|  |  |  |             protocol_url, _ = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 wellknown_protocol_handler(path, 'http', onion_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         elif calling_domain.endswith('.i2p'): | 
					
						
							|  |  |  |             protocol_url, _ = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 wellknown_protocol_handler(path, 'http', i2p_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             protocol_url, _ = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 wellknown_protocol_handler(path, http_prefix, domain_full) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         if protocol_url: | 
					
						
							|  |  |  |             redirect_headers(self, protocol_url, cookie, | 
					
						
							|  |  |  |                              calling_domain, 308) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             http_404(self, 8) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     # nodeinfo | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if path.startswith('/.well-known/nodeinfo') or \ | 
					
						
							|  |  |  |        path.startswith('/.well-known/x-nodeinfo'): | 
					
						
							|  |  |  |         if calling_domain.endswith('.onion') and onion_domain: | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_node_info('http', onion_domain) | 
					
						
							|  |  |  |         elif (calling_domain.endswith('.i2p') and i2p_domain): | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_node_info('http', i2p_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |                 webfinger_node_info(http_prefix, domain_full) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         if wf_result: | 
					
						
							|  |  |  |             msg_str = json.dumps(wf_result) | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |             msg_str = convert_domains(calling_domain, referer_domain, | 
					
						
							|  |  |  |                                       msg_str, http_prefix, domain, | 
					
						
							|  |  |  |                                       onion_domain, i2p_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |             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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |     if debug: | 
					
						
							|  |  |  |         print('DEBUG: WEBFINGER lookup ' + path + ' ' + str(base_dir)) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |     wf_result = \ | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |         webfinger_lookup(path, base_dir, | 
					
						
							|  |  |  |                          domain, onion_domain, | 
					
						
							|  |  |  |                          i2p_domain, port, debug) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |     if wf_result: | 
					
						
							|  |  |  |         msg_str = json.dumps(wf_result) | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |         msg_str = convert_domains(calling_domain, referer_domain, | 
					
						
							|  |  |  |                                   msg_str, http_prefix, domain, | 
					
						
							|  |  |  |                                   onion_domain, i2p_domain) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         msg = msg_str.encode('utf-8') | 
					
						
							|  |  |  |         msglen = len(msg) | 
					
						
							|  |  |  |         set_headers(self, 'application/jrd+json', msglen, | 
					
						
							|  |  |  |                     None, calling_domain, True) | 
					
						
							|  |  |  |         write2(self, msg) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-04-12 10:19:56 +00:00
										 |  |  |         if debug: | 
					
						
							|  |  |  |             print('DEBUG: WEBFINGER lookup 404 ' + path) | 
					
						
							| 
									
										
										
										
											2024-03-03 12:07:39 +00:00
										 |  |  |         http_404(self, 10) | 
					
						
							|  |  |  |     return True |