state.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 IGNoCheckpointError, IGCookieNotFoundError, IGUserIDNotFoundError
  23. from ..types import ChallengeStateResponse
  24. from .device import AndroidDevice
  25. from .session import AndroidSession
  26. from .application import AndroidApplication
  27. from .experiments import AndroidExperiments
  28. from .cookies import Cookies
  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 (f"Instagram {self.application.APP_VERSION} Android ({self.device.descriptor}; "
  52. f"{self.device.language}; {self.application.APP_VERSION_CODE})")
  53. @property
  54. def user_id(self) -> str:
  55. try:
  56. return self.cookies.user_id
  57. except IGCookieNotFoundError:
  58. if not self.challenge or not self.challenge.user_id:
  59. raise IGUserIDNotFoundError()
  60. return str(self.challenge.user_id)
  61. @property
  62. def challenge_path(self) -> str:
  63. if not self._challenge_path:
  64. raise IGNoCheckpointError()
  65. return self._challenge_path
  66. @challenge_path.setter
  67. def challenge_path(self, val: str) -> None:
  68. self._challenge_path = val
  69. def _gen_temp_uuid(self, seed: str, lifetime: int) -> UUID:
  70. rand = random.Random(f"{seed}{self.device.id}{round(time.time() * 1000 / lifetime)}")
  71. return UUID(int=rand.getrandbits(128), version=4)