state.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 attr import dataclass
  21. import attr
  22. from mautrix.types import SerializableAttrs
  23. from ..errors import IGNoCheckpointError, IGCookieNotFoundError, IGUserIDNotFoundError
  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['AndroidState']):
  31. device: AndroidDevice = attr.ib(factory=lambda: AndroidDevice())
  32. session: AndroidSession = attr.ib(factory=lambda: AndroidSession())
  33. application: AndroidApplication = attr.ib(factory=lambda: AndroidApplication())
  34. experiments: AndroidExperiments = attr.ib(factory=lambda: AndroidExperiments())
  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] = attr.ib(default=None, metadata={"json": "challenge_path"})
  39. cookies: Cookies = attr.ib(factory=lambda: Cookies())
  40. @property
  41. def client_session_id(self) -> str:
  42. return str(self._gen_temp_uuid("clientSessionId", self.client_session_id_lifetime))
  43. @property
  44. def pigeon_session_id(self) -> str:
  45. return str(self._gen_temp_uuid("pigeonSessionId", self.pigeon_session_id_lifetime))
  46. @property
  47. def user_agent(self) -> str:
  48. return (f"Instagram {self.application.APP_VERSION} Android ({self.device.descriptor}; "
  49. f"{self.device.language}; {self.application.APP_VERSION_CODE})")
  50. @property
  51. def user_id(self) -> str:
  52. try:
  53. return self.cookies.user_id
  54. except IGCookieNotFoundError:
  55. if not self.challenge or not self.challenge.user_id:
  56. raise IGUserIDNotFoundError()
  57. return str(self.challenge.user_id)
  58. @property
  59. def challenge_path(self) -> str:
  60. if not self._challenge_path:
  61. raise IGNoCheckpointError()
  62. return self._challenge_path
  63. @challenge_path.setter
  64. def challenge_path(self, val: str) -> None:
  65. self._challenge_path = val
  66. def _gen_temp_uuid(self, seed: str, lifetime: int) -> UUID:
  67. rand = random.Random(f"{seed}{self.device.id}{round(time.time() * 1000 / lifetime)}")
  68. return UUID(int=rand.getrandbits(128), version=4)