state.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 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 mautrix.types import SerializableAttrs, field, dataclass
  21. from ..errors import IGNoCheckpointError, IGCookieNotFoundError
  22. from .device import AndroidDevice
  23. from .session import AndroidSession
  24. from .application import AndroidApplication
  25. from .cookies import Cookies
  26. @dataclass
  27. class AndroidState(SerializableAttrs['AndroidState']):
  28. device: AndroidDevice = field(factory=lambda: AndroidDevice())
  29. session: AndroidSession = field(factory=lambda: AndroidSession())
  30. application: AndroidApplication = field(factory=lambda: AndroidApplication())
  31. # experiments: AndroidExperiments
  32. client_session_id_lifetime: int = 1_200_000
  33. pigeon_session_id_lifetime: int = 1_200_000
  34. challenge: 'Optional[ChallengeStateResponse]' = None
  35. _challenge_path: Optional[str] = field(default=None, json="challenge_path")
  36. cookies: Cookies = field(factory=lambda: Cookies())
  37. @property
  38. def client_session_id(self) -> str:
  39. return str(self._gen_temp_uuid("clientSessionId", self.client_session_id_lifetime))
  40. @property
  41. def pigeon_session_id(self) -> str:
  42. return str(self._gen_temp_uuid("pigeonSessionId", self.pigeon_session_id_lifetime))
  43. @property
  44. def user_agent(self) -> str:
  45. return (f"Instagram {self.application.APP_VERSION} Android ({self.device.descriptor}; "
  46. f"{self.device.language}; {self.application.APP_VERSION_CODE})")
  47. @property
  48. def user_id(self) -> str:
  49. try:
  50. return self.cookies.user_id
  51. except IGCookieNotFoundError:
  52. if not self.challenge or not self.challenge.user_id:
  53. raise IGUserIDNotFoundError()
  54. return str(self.challenge.user_id)
  55. @property
  56. def challenge_path(self) -> str:
  57. if not self._challenge_path:
  58. raise IGNoCheckpointError()
  59. return self._challenge_path
  60. def _gen_temp_uuid(self, seed: str, lifetime: int) -> UUID:
  61. rand = random.Random(f"{seed}{self.device.id}{round(time.time() * 1000 / lifetime)}")
  62. return UUID(int=rand.getrandbits(128), version=4)