device.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 typing import Optional, Union
  17. from uuid import UUID
  18. import random
  19. import string
  20. import time
  21. from attr import dataclass
  22. import attr
  23. from mautrix.types import SerializableAttrs
  24. @dataclass
  25. class AndroidDevice(SerializableAttrs):
  26. id: Optional[str] = None
  27. descriptor: Optional[str] = None
  28. uuid: Optional[str] = None
  29. fdid: Optional[str] = None
  30. phone_id: Optional[str] = attr.ib(default=None, metadata={"json": "phoneId"})
  31. # Google Play advertising ID
  32. adid: Optional[str] = None
  33. language: str = "en_US"
  34. radio_type: str = "wifi-none"
  35. connection_type: str = "WIFI"
  36. timezone_offset: str = str(-time.timezone)
  37. is_layout_rtl: bool = False
  38. @property
  39. def battery_level(self) -> int:
  40. rand = random.Random(self.id)
  41. percent_time = rand.randint(200, 600)
  42. return 100 - round(time.time() / percent_time) % 100
  43. @property
  44. def is_charging(self) -> bool:
  45. rand = random.Random(f"{self.id}{round(time.time() / 10800)}")
  46. return rand.choice([True, False])
  47. @property
  48. def payload(self) -> dict:
  49. device_parts = self.descriptor.split(";")
  50. android_version, android_release, *_ = device_parts[0].split("/")
  51. manufacturer, *_ = device_parts[3].split("/")
  52. model = device_parts[4]
  53. return {
  54. "android_version": android_version,
  55. "android_release": android_release,
  56. "manufacturer": manufacturer,
  57. "model": model,
  58. }
  59. def generate(self, seed: Union[str, bytes]) -> None:
  60. rand = random.Random(seed)
  61. self.phone_id = str(UUID(int=rand.getrandbits(128), version=4))
  62. self.adid = str(UUID(int=rand.getrandbits(128), version=4))
  63. self.id = f"android-{''.join(rand.choices(string.hexdigits, k=16))}".lower()
  64. self.descriptor = "33/13; 420dpi; 1080x2219; Google/google; Pixel 6; oriole; oriole"
  65. # "33/13; 560dpi; 1440x2934; Google/google; Pixel 6 Pro; raven; raven",
  66. self.uuid = str(UUID(int=rand.getrandbits(128), version=4))
  67. self.fdid = str(UUID(int=rand.getrandbits(128), version=4))