device.py 3.0 KB

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