浏览代码

Update pigeon session id format

Tulir Asokan 2 年之前
父节点
当前提交
20f8b37162
共有 3 个文件被更改,包括 12 次插入13 次删除
  1. 1 1
      mauigpapi/http/base.py
  2. 8 12
      mauigpapi/state/state.py
  3. 3 0
      mautrix_instagram/user.py

+ 1 - 1
mauigpapi/http/base.py

@@ -104,7 +104,7 @@ class BaseAndroidAPI:
             "x-ig-app-locale": self.state.device.language,
             "x-ig-device-locale": self.state.device.language,
             "x-ig-mapped-locale": self.state.device.language,
-            "x-pigeon-session-id": self.state.pigeon_session_id,
+            "x-pigeon-session-id": f"UFS-{self.state.pigeon_session_id}-0",
             "x-pigeon-rawclienttime": str(round(time.time(), 3)),
             "x-ig-connection-speed": f"{random.randint(1000, 3700)}kbps",
             "x-ig-bandwidth-speed-kbps": "-1.000",

+ 8 - 12
mauigpapi/state/state.py

@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 from typing import Optional
-from uuid import UUID
+from uuid import uuid4
 import random
 import time
 
@@ -37,25 +37,25 @@ class AndroidState(SerializableAttrs):
     session: AndroidSession = field(factory=lambda: AndroidSession())
     application: AndroidApplication = field(factory=lambda: AndroidApplication())
     experiments: AndroidExperiments = field(factory=lambda: AndroidExperiments(), hidden=True)
-    client_session_id_lifetime: int = 1_200_000
-    pigeon_session_id_lifetime: int = 1_200_000
     challenge: Optional[ChallengeStateResponse] = None
     challenge_context: Optional[ChallengeContext] = None
     _challenge_path: Optional[str] = field(default=None, json="challenge_path")
     cookies: Cookies = field(factory=lambda: Cookies())
     login_2fa_username: Optional[str] = field(default=None, hidden=True)
+    _pigeon_session_id: Optional[str] = field(default=None, hidden=True)
 
     def __attrs_post_init__(self) -> None:
         if self.application.APP_VERSION_CODE != AndroidApplication().APP_VERSION_CODE:
             self.application = AndroidApplication()
 
-    @property
-    def client_session_id(self) -> str:
-        return str(self._gen_temp_uuid("clientSessionId", self.client_session_id_lifetime))
-
     @property
     def pigeon_session_id(self) -> str:
-        return str(self._gen_temp_uuid("pigeonSessionId", self.pigeon_session_id_lifetime))
+        if not self._pigeon_session_id:
+            self._pigeon_session_id = str(uuid4())
+        return self._pigeon_session_id
+
+    def reset_pigeon_session_id(self) -> None:
+        self._pigeon_session_id = None
 
     @property
     def user_agent(self) -> str:
@@ -86,7 +86,3 @@ class AndroidState(SerializableAttrs):
     @staticmethod
     def gen_client_context() -> str:
         return str((int(time.time() * 1000) << 22) + random.randint(10000, 5000000))
-
-    def _gen_temp_uuid(self, seed: str, lifetime: int) -> UUID:
-        rand = random.Random(f"{seed}{self.device.id}{round(time.time() * 1000 / lifetime)}")
-        return UUID(int=rand.getrandbits(128), version=4)

+ 3 - 0
mautrix_instagram/user.py

@@ -462,6 +462,7 @@ class User(DBUser, BaseUser):
         self._is_refreshing = True
         try:
             await self.stop_listen()
+            self.state.reset_pigeon_session_id()
             if resync:
                 retry_count = 0
                 minutes = 1
@@ -639,6 +640,8 @@ class User(DBUser, BaseUser):
         await self.run_with_sync_lock(partial(self._sync, increment_total_backfilled_portals))
 
     async def _sync(self, increment_total_backfilled_portals: bool = False) -> None:
+        if not self._listen_task:
+            self.state.reset_pigeon_session_id()
         sleep_minutes = 2
         errors = 0
         while True: