base.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2023 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from __future__ import annotations
  17. from typing import Any, Awaitable, Callable, Type, TypeVar
  18. from functools import partial
  19. import json
  20. import logging
  21. import time
  22. from aiohttp import ClientResponse, ClientSession, ContentTypeError, CookieJar
  23. from yarl import URL
  24. from mautrix.types import JSON, Serializable
  25. from mautrix.util.logging import TraceLogger
  26. from mautrix.util.proxy import ProxyHandler, proxy_with_retry
  27. from ..errors import (
  28. IG2FACodeExpiredError,
  29. IGActionSpamError,
  30. IGBad2FACodeError,
  31. IGChallengeError,
  32. IGCheckpointError,
  33. IGConsentRequiredError,
  34. IGFBEmailTaken,
  35. IGFBNoContactPointFoundError,
  36. IGFBSSODisabled,
  37. IGInactiveUserError,
  38. IGLoginBadPasswordError,
  39. IGLoginInvalidCredentialsError,
  40. IGLoginInvalidUserError,
  41. IGLoginRequiredError,
  42. IGLoginTwoFactorRequiredError,
  43. IGLoginUnusablePasswordError,
  44. IGNotFoundError,
  45. IGPrivateUserError,
  46. IGRateLimitError,
  47. IGResponseError,
  48. IGSentryBlockError,
  49. IGUnknownError,
  50. IGUserHasLoggedOutError,
  51. )
  52. from ..state import AndroidState
  53. from ..types import ChallengeContext
  54. try:
  55. from aiohttp_socks import ProxyConnector
  56. except ImportError:
  57. ProxyConnector = None
  58. T = TypeVar("T")
  59. def remove_nulls(d: dict) -> dict:
  60. return {
  61. k: remove_nulls(v) if isinstance(v, dict) else v for k, v in d.items() if v is not None
  62. }
  63. class BaseAndroidAPI:
  64. url = URL("https://i.instagram.com")
  65. http: ClientSession
  66. state: AndroidState
  67. log: TraceLogger
  68. def __init__(
  69. self,
  70. state: AndroidState,
  71. log: TraceLogger | None = None,
  72. proxy_handler: ProxyHandler | None = None,
  73. on_proxy_update: Callable[[], Awaitable[None]] | None = None,
  74. ) -> None:
  75. self.log = log or logging.getLogger("mauigpapi.http")
  76. self.proxy_handler = proxy_handler
  77. self.on_proxy_update = on_proxy_update
  78. self.setup_http(cookie_jar=state.cookies.jar)
  79. self.state = state
  80. self.proxy_with_retry = partial(
  81. proxy_with_retry,
  82. logger=self.log,
  83. proxy_handler=self.proxy_handler,
  84. on_proxy_change=self.on_proxy_update,
  85. # Wait 1s * errors, max 10s for fast failure
  86. max_wait_seconds=10,
  87. multiply_wait_seconds=1,
  88. )
  89. @staticmethod
  90. def sign(req: Any, filter_nulls: bool = False) -> dict[str, str]:
  91. if isinstance(req, Serializable):
  92. req = req.serialize()
  93. if isinstance(req, dict):
  94. req = json.dumps(remove_nulls(req) if filter_nulls else req)
  95. return {"signed_body": f"SIGNATURE.{req}"}
  96. @property
  97. def _headers(self) -> dict[str, str]:
  98. headers = {
  99. "x-ads-opt-out": str(int(self.state.session.ads_opt_out)),
  100. "x-device-id": self.state.device.uuid,
  101. "x-ig-app-locale": self.state.device.language,
  102. "x-ig-device-locale": self.state.device.language,
  103. "x-ig-mapped-locale": self.state.device.language,
  104. "x-pigeon-session-id": f"UFS-{self.state.pigeon_session_id}-0",
  105. "x-pigeon-rawclienttime": str(round(time.time(), 3)),
  106. "x-ig-bandwidth-speed-kbps": "-1.000",
  107. "x-ig-bandwidth-totalbytes-b": "0",
  108. "x-ig-bandwidth-totaltime-ms": "0",
  109. "x-ig-app-startup-country": self.state.device.language.split("_")[1],
  110. "x-bloks-version-id": self.state.application.BLOKS_VERSION_ID,
  111. "x-ig-www-claim": self.state.session.ig_www_claim or "0",
  112. "x-bloks-is-layout-rtl": str(self.state.device.is_layout_rtl).lower(),
  113. "x-ig-timezone-offset": self.state.device.timezone_offset,
  114. "x-ig-device-id": self.state.device.uuid,
  115. "x-ig-family-device-id": self.state.device.fdid,
  116. "x-ig-android-id": self.state.device.id,
  117. "x-ig-connection-type": self.state.device.connection_type,
  118. "x-fb-connection-type": self.state.device.connection_type,
  119. "x-ig-capabilities": self.state.application.CAPABILITIES,
  120. "x-ig-app-id": self.state.application.FACEBOOK_ANALYTICS_APPLICATION_ID,
  121. "user-agent": self.state.user_agent,
  122. "accept-language": self.state.device.language.replace("_", "-"),
  123. "authorization": self.state.session.authorization,
  124. "x-mid": self.state.cookies.get_value("mid"),
  125. "ig-u-ig-direct-region-hint": self.state.session.region_hint,
  126. "ig-u-shbid": self.state.session.shbid,
  127. "ig-u-shbts": self.state.session.shbts,
  128. "ig-u-ds-user-id": self.state.session.ds_user_id,
  129. "ig-u-rur": self.state.session.rur,
  130. "ig-intended-user-id": self.state.session.ds_user_id or "0",
  131. "ig-client-endpoint": "unknown",
  132. "x-fb-http-engine": "Liger",
  133. "x-fb-client-ip": "True",
  134. "x-fb-rmd": "cached=0;state=NO_MATCH",
  135. "x-fb-server-cluster": "True",
  136. "x-tigon-is-retry": "False",
  137. "accept-encoding": "gzip",
  138. }
  139. return {k: v for k, v in headers.items() if v is not None}
  140. def setup_http(self, cookie_jar: CookieJar) -> None:
  141. connector = None
  142. http_proxy = self.proxy_handler.get_proxy_url() if self.proxy_handler else None
  143. if http_proxy:
  144. if ProxyConnector:
  145. connector = ProxyConnector.from_url(http_proxy)
  146. else:
  147. self.log.warning("http_proxy is set, but aiohttp-socks is not installed")
  148. self.http = ClientSession(connector=connector, cookie_jar=cookie_jar)
  149. return None
  150. def raw_http_get(self, url: URL | str, **kwargs):
  151. if isinstance(url, str):
  152. url = URL(url, encoded=True)
  153. return self.http.get(
  154. url,
  155. headers={
  156. "user-agent": self.state.user_agent,
  157. "accept-language": self.state.device.language.replace("_", "-"),
  158. },
  159. **kwargs,
  160. )
  161. async def std_http_post(
  162. self,
  163. path: str,
  164. data: JSON = None,
  165. raw: bool = False,
  166. filter_nulls: bool = False,
  167. headers: dict[str, str] | None = None,
  168. query: dict[str, str] | None = None,
  169. response_type: Type[T] | None = JSON,
  170. ) -> T:
  171. headers = {**self._headers, **headers} if headers else self._headers
  172. if not raw:
  173. data = self.sign(data, filter_nulls=filter_nulls)
  174. url = self.url.with_path(path).with_query(query or {})
  175. resp = await self.proxy_with_retry(
  176. f"AndroidAPI.std_http_post: {url}",
  177. lambda: self.http.post(url=url, headers=headers, data=data),
  178. )
  179. self.log.trace(f"{path} response: {await resp.text()}")
  180. if response_type is str or response_type is None:
  181. self._handle_response_headers(resp)
  182. if response_type is str:
  183. return await resp.text()
  184. return None
  185. json_data = await self._handle_response(resp)
  186. if response_type is not JSON:
  187. return response_type.deserialize(json_data)
  188. return json_data
  189. async def std_http_get(
  190. self,
  191. path: str,
  192. query: dict[str, str] | None = None,
  193. headers: dict[str, str] | None = None,
  194. response_type: Type[T] | None = JSON,
  195. ) -> T:
  196. headers = {**self._headers, **headers} if headers else self._headers
  197. query = {k: v for k, v in (query or {}).items() if v is not None}
  198. url = self.url.with_path(path).with_query(query)
  199. resp = await self.proxy_with_retry(
  200. f"AndroidAPI.std_http_get: {url}",
  201. lambda: self.http.get(url=url, headers=headers),
  202. )
  203. self.log.trace(f"{path} response: {await resp.text()}")
  204. if response_type is None:
  205. self._handle_response_headers(resp)
  206. return None
  207. json_data = await self._handle_response(resp)
  208. if response_type is not JSON:
  209. return response_type.deserialize(json_data)
  210. return json_data
  211. async def _handle_response(self, resp: ClientResponse) -> JSON:
  212. self._handle_response_headers(resp)
  213. try:
  214. body = await resp.json()
  215. except (json.JSONDecodeError, ContentTypeError) as e:
  216. raise IGUnknownError(resp) from e
  217. if body.get("status", "fail") == "ok":
  218. return body
  219. else:
  220. await self._raise_response_error(resp)
  221. async def _raise_response_error(self, resp: ClientResponse) -> None:
  222. try:
  223. data = await resp.json()
  224. except json.JSONDecodeError:
  225. data = {}
  226. if data.get("spam", False):
  227. raise IGActionSpamError(resp, data)
  228. elif data.get("two_factor_required", False):
  229. raise IGLoginTwoFactorRequiredError(resp, data)
  230. elif resp.status == 404:
  231. raise IGNotFoundError(resp, data)
  232. elif resp.status == 429:
  233. raise IGRateLimitError(resp, data)
  234. message = data.get("message")
  235. if isinstance(message, str):
  236. if message == "challenge_required":
  237. err = IGChallengeError(resp, data)
  238. self.log.debug(f"Storing challenge URL {err.url}")
  239. self.state.challenge_path = err.url
  240. try:
  241. self.state.challenge_context = ChallengeContext.parse_json(
  242. err.body.challenge.challenge_context
  243. )
  244. except Exception:
  245. self.log.exception(
  246. "Failed to deserialize challenge_context %s",
  247. err.body.challenge.challenge_context,
  248. )
  249. raise err
  250. elif message == "checkpoint_required":
  251. raise IGCheckpointError(resp, data)
  252. elif message == "consent_required":
  253. raise IGConsentRequiredError(resp, data)
  254. elif message == "user_has_logged_out":
  255. raise IGUserHasLoggedOutError(resp, data)
  256. elif message == "login_required":
  257. raise IGLoginRequiredError(resp, data)
  258. elif message.lower() == "not authorized to view user":
  259. raise IGPrivateUserError(resp, data)
  260. error_type = data.get("error_type")
  261. if error_type == "sentry_block":
  262. raise IGSentryBlockError(resp, data)
  263. elif error_type == "inactive_user":
  264. raise IGInactiveUserError(resp, data)
  265. elif error_type == "bad_password":
  266. raise IGLoginBadPasswordError(resp, data)
  267. elif error_type == "unusable_password":
  268. raise IGLoginUnusablePasswordError(resp, data)
  269. elif error_type == "invalid_user":
  270. raise IGLoginInvalidUserError(resp, data)
  271. elif error_type == "sms_code_validation_code_invalid":
  272. raise IGBad2FACodeError(resp, data)
  273. elif error_type == "invalid_nonce":
  274. raise IG2FACodeExpiredError(resp, data)
  275. elif error_type == "fb_no_contact_point_found":
  276. raise IGFBNoContactPointFoundError(resp, data)
  277. elif error_type == "fb_email_taken":
  278. raise IGFBEmailTaken(resp, data)
  279. elif error_type == "sso_disabled":
  280. raise IGFBSSODisabled(resp, data)
  281. elif error_type == "rate_limit_error":
  282. raise IGRateLimitError(resp, data)
  283. exception_name = data.get("exception_name")
  284. if exception_name == "UserInvalidCredentials":
  285. raise IGLoginInvalidCredentialsError(resp, data)
  286. raise IGResponseError(resp, data)
  287. def _handle_response_headers(self, resp: ClientResponse) -> None:
  288. fields = {
  289. "x-ig-set-www-claim": "ig_www_claim",
  290. "ig-set-authorization": "authorization",
  291. "ig-set-password-encryption-key-id": "password_encryption_key_id",
  292. "ig-set-password-encryption-pub-key": "password_encryption_pubkey",
  293. "ig-set-ig-u-ig-direct-region-hint": "region_hint",
  294. "ig-set-ig-u-shbid": "shbid",
  295. "ig-set-ig-u-shbts": "shbts",
  296. "ig-set-ig-u-rur": "rur",
  297. "ig-set-ig-u-ds-user-id": "ds_user_id",
  298. }
  299. for header, field in fields.items():
  300. value = resp.headers.get(header)
  301. if value and (header != "IG-Set-Authorization" or not value.endswith(":")):
  302. setattr(self.state.session, field, value)