Skip to content

Commit 82246b9

Browse files
committed
fix(3x-ui): Fixed login to multiple panels
1 parent adfec19 commit 82246b9

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

backend/services/sanaei/api.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,40 @@
88

99

1010
class APIService:
11-
_last_login_time = None
12-
_last_url = None
13-
_api_instance = None
11+
_api_instances = {}
12+
_last_login_times = {}
1413

1514
def __init__(self, url: str, username: str, password: str):
16-
if APIService._api_instance is None or APIService._last_url != url:
17-
APIService._api_instance = AsyncApi(url, username, password)
18-
APIService._last_url = url
19-
self.api = APIService._api_instance
15+
self.url = url
2016
self.username = username
2117
self.password = password
22-
self.url = url
18+
19+
if url not in APIService._api_instances:
20+
APIService._api_instances[url] = AsyncApi(url, username, password)
21+
22+
self.api = APIService._api_instances[url]
2323

2424
async def ensure_login(self):
25-
if (
26-
APIService._last_login_time is None
27-
or (datetime.now() - APIService._last_login_time).total_seconds() > 3500
28-
):
29-
await self.api.login()
30-
APIService._last_login_time = datetime.now()
25+
last_login = APIService._last_login_times.get(self.url)
26+
27+
if last_login is None or (datetime.now() - last_login).total_seconds() > 3500:
28+
try:
29+
await self.api.login()
30+
APIService._last_login_times[self.url] = datetime.now()
31+
except Exception as e:
32+
APIService._api_instances[self.url] = AsyncApi(
33+
self.url, self.username, self.password
34+
)
35+
self.api = APIService._api_instances[self.url]
36+
await self.api.login()
37+
APIService._last_login_times[self.url] = datetime.now()
3138

3239
async def test_connection(self) -> Server:
3340
try:
3441
api = AsyncApi(self.url, self.username, self.password)
3542
await api.login()
3643
info = await api.server.get_status()
3744
return info
38-
3945
except Exception as e:
4046
return None
4147

0 commit comments

Comments
 (0)