base.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. on_response_error: Callable[[IGResponseError], Awaitable[None]] | None = None,
  75. ) -> None:
  76. self.log = log or logging.getLogger("mauigpapi.http")
  77. self.proxy_handler = proxy_handler
  78. self.on_proxy_update = on_proxy_update
  79. self.on_response_error = on_response_error
  80. self.setup_http(cookie_jar=state.cookies.jar)
  81. self.state = state
  82. self.proxy_with_retry = partial(
  83. proxy_with_retry,
  84. logger=self.log,
  85. proxy_handler=self.proxy_handler,
  86. on_proxy_change=self.on_proxy_update,
  87. # Wait 1s * errors, max 10s for fast failure
  88. max_wait_seconds=10,
  89. multiply_wait_seconds=1,
  90. )
  91. @staticmethod
  92. def sign(req: Any, filter_nulls: bool = False) -> dict[str, str]:
  93. if isinstance(req, Serializable):
  94. req = req.serialize()
  95. if isinstance(req, dict):
  96. req = json.dumps(remove_nulls(req) if filter_nulls else req)
  97. return {"signed_body": f"SIGNATURE.{req}"}
  98. @property
  99. def _headers(self) -> dict[str, str]:
  100. headers = {
  101. "x-ads-opt-out": str(int(self.state.session.ads_opt_out)),
  102. "x-device-id": self.state.device.uuid,
  103. "x-ig-app-locale": self.state.device.language,
  104. "x-ig-device-locale": self.state.device.language,
  105. "x-ig-mapped-locale": self.state.device.language,
  106. "x-pigeon-session-id": f"UFS-{self.state.pigeon_session_id}-0",
  107. "x-pigeon-rawclienttime": str(round(time.time(), 3)),
  108. "x-ig-bandwidth-speed-kbps": "-1.000",
  109. "x-ig-bandwidth-totalbytes-b": "0",
  110. "x-ig-bandwidth-totaltime-ms": "0",
  111. "x-ig-app-startup-country": self.state.device.language.split("_")[1],
  112. "x-bloks-version-id": self.state.application.BLOKS_VERSION_ID,
  113. "x-ig-www-claim": self.state.session.ig_www_claim or "0",
  114. "x-bloks-is-layout-rtl": str(self.state.device.is_layout_rtl).lower(),
  115. "x-ig-timezone-offset": self.state.device.timezone_offset,
  116. "x-ig-device-id": self.state.device.uuid,
  117. "x-ig-family-device-id": self.state.device.fdid,
  118. "x-ig-android-id": self.state.device.id,
  119. "x-ig-connection-type": self.state.device.connection_type,
  120. "x-fb-connection-type": self.state.device.connection_type,
  121. "x-ig-capabilities": self.state.application.CAPABILITIES,
  122. "x-ig-app-id": self.state.application.FACEBOOK_ANALYTICS_APPLICATION_ID,
  123. "user-agent": self.state.user_agent,
  124. "accept-language": self.state.device.language.replace("_", "-"),
  125. "authorization": self.state.session.authorization,
  126. "x-mid": self.state.cookies.get_value("mid"),
  127. "ig-u-ig-direct-region-hint": self.state.session.region_hint,
  128. "ig-u-shbid": self.state.session.shbid,
  129. "ig-u-shbts": self.state.session.shbts,
  130. "ig-u-ds-user-id": self.state.session.ds_user_id,
  131. "ig-u-rur": self.state.session.rur,
  132. "ig-intended-user-id": self.state.session.ds_user_id or "0",
  133. "ig-client-endpoint": "unknown",
  134. "x-fb-http-engine": "Liger",
  135. "x-fb-client-ip": "True",
  136. "x-fb-rmd": "cached=0;state=NO_MATCH",
  137. "x-fb-server-cluster": "True",
  138. "x-tigon-is-retry": "False",
  139. "accept-encoding": "gzip",
  140. }
  141. return {k: v for k, v in headers.items() if v is not None}
  142. def setup_http(self, cookie_jar: CookieJar) -> None:
  143. connector = None
  144. http_proxy = self.proxy_handler.get_proxy_url() if self.proxy_handler else None
  145. if http_proxy:
  146. if ProxyConnector:
  147. connector = ProxyConnector.from_url(http_proxy)
  148. else:
  149. self.log.warning("http_proxy is set, but aiohttp-socks is not installed")
  150. self.http = ClientSession(connector=connector, cookie_jar=cookie_jar)
  151. return None
  152. def raw_http_get(self, url: URL | str, **kwargs):
  153. if isinstance(url, str):
  154. url = URL(url, encoded=True)
  155. return self.http.get(
  156. url,
  157. headers={
  158. "user-agent": self.state.user_agent,
  159. "accept-language": self.state.device.language.replace("_", "-"),
  160. },
  161. **kwargs,
  162. )
  163. async def std_http_post(
  164. self,
  165. path: str,
  166. data: JSON = None,
  167. raw: bool = False,
  168. filter_nulls: bool = False,
  169. headers: dict[str, str] | None = None,
  170. query: dict[str, str] | None = None,
  171. response_type: Type[T] | None = JSON,
  172. ) -> T:
  173. headers = {**self._headers, **headers} if headers else self._headers
  174. if not raw:
  175. data = self.sign(data, filter_nulls=filter_nulls)
  176. url = self.url.with_path(path).with_query(query or {})
  177. resp = await self.proxy_with_retry(
  178. f"AndroidAPI.std_http_post: {url}",
  179. lambda: self.http.post(url=url, headers=headers, data=data),
  180. )
  181. self.log.trace(f"{path} response: {await resp.text()}")
  182. if response_type is str or response_type is None:
  183. self._handle_response_headers(resp)
  184. if response_type is str:
  185. return await resp.text()
  186. return None
  187. json_data = await self._handle_response(resp)
  188. if response_type is not JSON:
  189. return response_type.deserialize(json_data)
  190. return json_data
  191. async def std_http_get(
  192. self,
  193. path: str,
  194. query: dict[str, str] | None = None,
  195. headers: dict[str, str] | None = None,
  196. response_type: Type[T] | None = JSON,
  197. ) -> T:
  198. headers = {**self._headers, **headers} if headers else self._headers
  199. query = {k: v for k, v in (query or {}).items() if v is not None}
  200. url = self.url.with_path(path).with_query(query)
  201. resp = await self.proxy_with_retry(
  202. f"AndroidAPI.std_http_get: {url}",
  203. lambda: self.http.get(url=url, headers=headers),
  204. )
  205. self.log.trace(f"{path} response: {await resp.text()}")
  206. if response_type is None:
  207. self._handle_response_headers(resp)
  208. return None
  209. json_data = await self._handle_response(resp)
  210. if response_type is not JSON:
  211. return response_type.deserialize(json_data)
  212. return json_data
  213. async def _handle_response(self, resp: ClientResponse) -> JSON:
  214. self._handle_response_headers(resp)
  215. try:
  216. body = await resp.json()
  217. except (json.JSONDecodeError, ContentTypeError) as e:
  218. raise IGUnknownError(resp) from e
  219. if body.get("status", "fail") == "ok":
  220. return body
  221. else:
  222. err = await self._get_response_error(resp)
  223. if self.on_response_error:
  224. await self.on_response_error(err)
  225. raise err
  226. async def _get_response_error(self, resp: ClientResponse) -> IGResponseError:
  227. try:
  228. data = await resp.json()
  229. except json.JSONDecodeError:
  230. data = {}
  231. if data.get("spam", False):
  232. return IGActionSpamError(resp, data)
  233. elif data.get("two_factor_required", False):
  234. return IGLoginTwoFactorRequiredError(resp, data)
  235. elif resp.status == 404:
  236. return IGNotFoundError(resp, data)
  237. elif resp.status == 429:
  238. return IGRateLimitError(resp, data)
  239. message = data.get("message")
  240. if isinstance(message, str):
  241. if message == "challenge_required":
  242. err = IGChallengeError(resp, data)
  243. self.log.debug(f"Storing challenge URL {err.url}")
  244. self.state.challenge_path = err.url
  245. try:
  246. self.state.challenge_context = ChallengeContext.parse_json(
  247. err.body.challenge.challenge_context
  248. )
  249. except Exception:
  250. self.log.exception(
  251. "Failed to deserialize challenge_context %s",
  252. err.body.challenge.challenge_context,
  253. )
  254. return err
  255. elif message == "checkpoint_required":
  256. return IGCheckpointError(resp, data)
  257. elif message == "consent_required":
  258. return IGConsentRequiredError(resp, data)
  259. elif message == "user_has_logged_out":
  260. return IGUserHasLoggedOutError(resp, data)
  261. elif message == "login_required":
  262. return IGLoginRequiredError(resp, data)
  263. elif message.lower() == "not authorized to view user":
  264. return IGPrivateUserError(resp, data)
  265. error_type = data.get("error_type")
  266. if error_type == "sentry_block":
  267. return IGSentryBlockError(resp, data)
  268. elif error_type == "inactive_user":
  269. return IGInactiveUserError(resp, data)
  270. elif error_type == "bad_password":
  271. return IGLoginBadPasswordError(resp, data)
  272. elif error_type == "unusable_password":
  273. return IGLoginUnusablePasswordError(resp, data)
  274. elif error_type == "invalid_user":
  275. return IGLoginInvalidUserError(resp, data)
  276. elif error_type == "sms_code_validation_code_invalid":
  277. return IGBad2FACodeError(resp, data)
  278. elif error_type == "invalid_nonce":
  279. return IG2FACodeExpiredError(resp, data)
  280. elif error_type == "fb_no_contact_point_found":
  281. return IGFBNoContactPointFoundError(resp, data)
  282. elif error_type == "fb_email_taken":
  283. return IGFBEmailTaken(resp, data)
  284. elif error_type == "sso_disabled":
  285. return IGFBSSODisabled(resp, data)
  286. elif error_type == "rate_limit_error":
  287. return IGRateLimitError(resp, data)
  288. exception_name = data.get("exception_name")
  289. if exception_name == "UserInvalidCredentials":
  290. return IGLoginInvalidCredentialsError(resp, data)
  291. return IGResponseError(resp, data)
  292. def _handle_response_headers(self, resp: ClientResponse) -> None:
  293. fields = {
  294. "x-ig-set-www-claim": "ig_www_claim",
  295. "ig-set-authorization": "authorization",
  296. "ig-set-password-encryption-key-id": "password_encryption_key_id",
  297. "ig-set-password-encryption-pub-key": "password_encryption_pubkey",
  298. "ig-set-ig-u-ig-direct-region-hint": "region_hint",
  299. "ig-set-ig-u-shbid": "shbid",
  300. "ig-set-ig-u-shbts": "shbts",
  301. "ig-set-ig-u-rur": "rur",
  302. "ig-set-ig-u-ds-user-id": "ds_user_id",
  303. }
  304. for header, field in fields.items():
  305. value = resp.headers.get(header)
  306. if value and (header != "IG-Set-Authorization" or not value.endswith(":")):
  307. setattr(self.state.session, field, value)