Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mtprotoproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@

stats = collections.Counter()
user_stats = collections.defaultdict(collections.Counter)
user_ip_conns = collections.defaultdict(lambda: collections.defaultdict(int))

config = {}

Expand Down Expand Up @@ -1687,6 +1688,7 @@ def decrypt(self, data):
task_clt_to_tg = asyncio.ensure_future(clt_to_tg)

update_user_stats(user, curr_connects=1)
user_ip_conns[user][cl_ip] += 1

tcp_limit_hit = (
user in config.USER_MAX_TCP_CONNS and
Expand All @@ -1711,6 +1713,10 @@ def decrypt(self, data):

update_user_stats(user, curr_connects=-1)

user_ip_conns[user][cl_ip] -= 1
if user_ip_conns[user][cl_ip] <= 0:
del user_ip_conns[user][cl_ip]

task_tg_to_clt.cancel()
task_clt_to_tg.cancel()

Expand Down Expand Up @@ -1830,6 +1836,11 @@ async def handle_metrics(reader, writer):
metric = {"user": user, "val": val}
metrics.append([m_name, m_type, m_desc, metric])


for user in user_stats:
metric = {"user": user, "val": len(user_ip_conns[user])}
metrics.append(["user_unique_ips", "gauge",
"number of unique IPs currently connected per user", metric])
pkt = make_metrics_pkt(metrics)
writer.write(pkt.encode())
await writer.drain()
Expand Down