state.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2021 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 typing import Optional
  17. from uuid import UUID
  18. import random
  19. import time
  20. from attr import dataclass
  21. from mautrix.types import SerializableAttrs, field
  22. from ..errors import IGCookieNotFoundError, IGNoCheckpointError, IGUserIDNotFoundError
  23. from ..types import ChallengeStateResponse
  24. from .application import AndroidApplication
  25. from .cookies import Cookies
  26. from .device import AndroidDevice
  27. from .experiments import AndroidExperiments
  28. from .session import AndroidSession
  29. @dataclass
  30. class AndroidState(SerializableAttrs):
  31. device: AndroidDevice = field(factory=lambda: AndroidDevice())
  32. session: AndroidSession = field(factory=lambda: AndroidSession())
  33. application: AndroidApplication = field(factory=lambda: AndroidApplication())
  34. experiments: AndroidExperiments = field(factory=lambda: AndroidExperiments(), hidden=True)
  35. client_session_id_lifetime: int = 1_200_000
  36. pigeon_session_id_lifetime: int = 1_200_000
  37. challenge: Optional[ChallengeStateResponse] = None
  38. _challenge_path: Optional[str] = field(default=None, json="challenge_path")
  39. cookies: Cookies = field(factory=lambda: Cookies())
  40. def __attrs_post_init__(self) -> None:
  41. if self.application.APP_VERSION_CODE != AndroidApplication().APP_VERSION_CODE:
  42. self.application = AndroidApplication()
  43. @property
  44. def client_session_id(self) -> str:
  45. return str(self._gen_temp_uuid("clientSessionId", self.client_session_id_lifetime))
  46. @property
  47. def pigeon_session_id(self) -> str:
  48. return str(self._gen_temp_uuid("pigeonSessionId", self.pigeon_session_id_lifetime))
  49. @property
  50. def user_agent(self) -> str:
  51. return (
  52. f"Instagram {self.application.APP_VERSION} Android ({self.device.descriptor}; "
  53. f"{self.device.language}; {self.application.APP_VERSION_CODE})"
  54. )
  55. @property
  56. def user_id(self) -> str:
  57. try:
  58. return self.cookies.user_id
  59. except IGCookieNotFoundError:
  60. if not self.challenge or not self.challenge.user_id:
  61. raise IGUserIDNotFoundError()
  62. return str(self.challenge.user_id)
  63. @property
  64. def challenge_path(self) -> str:
  65. if not self._challenge_path:
  66. raise IGNoCheckpointError()
  67. return self._challenge_path
  68. @challenge_path.setter
  69. def challenge_path(self, val: str) -> None:
  70. self._challenge_path = val
  71. @staticmethod
  72. def gen_client_context() -> str:
  73. return str((int(time.time() * 1000) << 22) + random.randint(10000, 5000000))
  74. def _gen_temp_uuid(self, seed: str, lifetime: int) -> UUID:
  75. rand = random.Random(f"{seed}{self.device.id}{round(time.time() * 1000 / lifetime)}")
  76. return UUID(int=rand.getrandbits(128), version=4)