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