Debian Codegraph @

proton/session/transports/alternativerouting.py

raw
1 """
2 Copyright (c) 2023 Proton AG
3
4 This file is part of Proton.
5
6 Proton is free software: you can redistribute it and/or modify
7 it under the terms of the GNU 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 Proton 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>.
18 """
19 from dataclasses import dataclass
20 from typing import Awaitable, List
21 import aiohttp
22 from ..exceptions import *
23 from .aiohttp import AiohttpTransport
24
25 import json, base64, struct, time, asyncio, random, itertools
26
27 from urllib.parse import urlparse
28
29 from ..api import sync_wrapper
30 from .utils.dns import DNSParser, DNSResponseError
31
32
33 @dataclass
34 class AlternativeRoutingDNSQueryAnswer:
35 """Contains the result of a successful DNS query to retrieve the
36 alternative routing server domain."""
37 expiration_time: float
38 domain: str
39
40
41 class AlternativeRoutingTransport(AiohttpTransport):
42 DNS_PROVIDERS = [
43 #dns.google
44 (("8.8.4.4", "8.8.8.8"), ("2001:4860:4860::8844", "2001:4860:4860::8888"), '/dns-query'),
45 #dns11.quad9.net
46 (("149.112.112.11", "9.9.9.11"), ("2620:fe::fe:11", "2620:fe::11"), '/dns-query'),
47 ]
48
49 STRUCT_REPLY_COUNTS = struct.Struct('>HHHH')
50 STRUCT_REC_FORMAT = struct.Struct('>HHIH')
51
52 #Delay between DNS requests
53 DELAY_DNS_REQUEST = 2
54 TIMEOUT_DNS_REQUEST = 10
55
56 @classmethod
57 def _get_priority(cls):
58 return 5
59
60
61 def __init__(self, session):
62 super().__init__(session)
63 self._alternative_routes = []
64
65 @classmethod
66 def _compute_ar_domain(cls, host):
67 return b'd' + base64.b32encode(host.encode('ascii')).strip(b'=') + b".protonpro.xyz"
68
69 async def _async_dns_query(
70 self, domain, dns_server_ip, dns_server_path, delay=0
71 ) -> List[AlternativeRoutingDNSQueryAnswer]:
72 import aiohttp
73
74 if delay > 0:
75 await asyncio.sleep(delay)
76
77 ardomain = self._compute_ar_domain(domain)
78 dns_request = DNSParser.build_query(ardomain, qtype=16, qclass=1) # TXT IN
79 dot_url = f'https://{dns_server_ip}{dns_server_path}'
80
81 async with aiohttp.ClientSession() as session:
82 async with session.post(dot_url, headers=[("Content-Type","application/dns-message")], data=dns_request) as r:
83 reply_data = await r.content.read()
84
85 try:
86 dns_answers = DNSParser.parse(reply_data)
87 except DNSResponseError as e:
88 raise ProtonAPINotReachable(str(e))
89
90 now = time.time()
91 # Tuples (TTL, data)
92 answers = []
93 for rec_ttl, rec_val in dns_answers:
94 answers.append(AlternativeRoutingDNSQueryAnswer(
95 expiration_time=now + rec_ttl,
96 domain=rec_val)
97 )
98
99 return answers
100
101 @property
102 def _http_domain(self):
103 return urlparse(super().http_base_url).netloc
104
105 async def _get_alternative_routes(self):
106 # We generate a random list of dns servers,
107 # we query them following that order, simultaneoulsy on IPv4/IPv6
108 choices_ipv4 = []
109 choices_ipv6 = []
110 for dns_server_ipv4s, dns_server_ipv6s, dns_server_path in self.DNS_PROVIDERS:
111 for ip in dns_server_ipv4s:
112 choices_ipv4.append((ip, dns_server_path))
113 for ip in dns_server_ipv6s:
114 choices_ipv6.append((ip, dns_server_path))
115
116 random.shuffle(choices_ipv4)
117 random.shuffle(choices_ipv6)
118
119 pending = []
120 i = 0
121 for ipv4, ipv6 in itertools.zip_longest(choices_ipv4, choices_ipv6, fillvalue=None):
122 if i * self.DELAY_DNS_REQUEST > self.TIMEOUT_DNS_REQUEST:
123 break
124
125 if ipv4 is not None:
126 pending.append(asyncio.create_task(self._async_dns_query(self._http_domain, ipv4[0], ipv4[1], delay=i * self.DELAY_DNS_REQUEST)))
127 if ipv6 is not None:
128 pending.append(asyncio.create_task(self._async_dns_query(self._http_domain, f'[{ipv6[0]}]', ipv6[1], delay=i * self.DELAY_DNS_REQUEST)))
129
130 i += 1
131
132 results_ok = []
133 results_fail = []
134 proton_api_not_available_errors = []
135 final_timestamp = time.time() + self.TIMEOUT_DNS_REQUEST
136 while len(pending) > 0 and len(results_ok) == 0:
137 done, pending = await asyncio.wait(pending, timeout=max(0.1, final_timestamp - time.time()), return_when=asyncio.FIRST_COMPLETED)
138 for task in done:
139 try:
140 results_ok += task.result()
141 except ProtonAPINotAvailable as e:
142 # That means that we were able to do a resolution, but it explicitly failed
143 proton_api_not_available_errors.append(e)
144 except Exception as e:
145 results_fail.append(e)
146
147 for task in pending:
148 task.cancel()
149
150 if proton_api_not_available_errors:
151 raise proton_api_not_available_errors[0]
152
153 if len(results_ok) == 0:
154 if len(self._alternative_routes) > 0:
155 # We have routes, but we were not able to resolve new ones. Just keep the old ones
156 return
157 else:
158 # No routes, and failed to get new ones
159 raise ProtonAPINotReachable("Couldn't resolve any alternative routing names")
160
161 domains = [x.domain for x in results_ok]
162 # Filter names that are in our results (we don't want duplicates)
163 self._alternative_routes = [
164 x for x in self._alternative_routes
165 if x.domain not in domains and x.expiration_time >= time.time()
166 ]
167 # Add the results
168 self._alternative_routes += results_ok
169 # Sort them so we have the most recent on top
170 self._alternative_routes.sort(key=lambda x: x.expiration_time, reverse=True)
171
172 @property
173 def http_base_url(self):
174 if len(self._alternative_routes) == 0:
175 raise ProtonAPINotReachable("AlternativeRouting transport doesn't have any route")
176
177 path = urlparse(super().http_base_url).path
178
179 return f'https://{self._alternative_routes[0].domain}{path}'
180
181 @property
182 def tls_pinning_hashes(self):
183 return self._environment.tls_pinning_hashes_ar
184
185 async def async_api_request(
186 self, endpoint,
187 jsondata=None, data=None, additional_headers=None,
188 method=None, params=None, return_raw=False
189 ):
190 if len(self._alternative_routes) == 0 or self._alternative_routes[0].expiration_time < time.time():
191 await self._get_alternative_routes()
192
193 return await super().async_api_request(endpoint, jsondata, data, additional_headers, method,
194 params, return_raw=return_raw)