From 994679982b74406b5d17bb46a8cbce4a24a657a8 Mon Sep 17 00:00:00 2001 From: "Foster \"Forst\" Snowhill" Date: Mon, 28 May 2018 21:30:34 +0300 Subject: [PATCH] Use external service to retrieve host's IP address. Should display a proper IP for users behind NAT (like a home router), for example. --- mtprotoproxy.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/mtprotoproxy.py b/mtprotoproxy.py index fa56ff1..0b308a7 100755 --- a/mtprotoproxy.py +++ b/mtprotoproxy.py @@ -3,6 +3,7 @@ import asyncio import socket import urllib.parse +import urllib.request import collections import time import hashlib @@ -226,17 +227,12 @@ async def stats_printer(): def print_tg_info(): - my_ip = socket.gethostbyname(socket.gethostname()) - - octets = [int(o) for o in my_ip.split(".")] - - ip_is_local = (len(octets) == 4 and ( - octets[0] in [127, 10] or - octets[0:2] == [192, 168] or - (octets[0] == 172 and 16 <= octets[1] <= 31))) - - if ip_is_local: - my_ip = "YOUR_IP" + try: + with urllib.request.urlopen('https://ifconfig.co/ip') as f: + if f.status != 200: raise Exception("Invalid status code") + my_ip = f.read().strip() + except: + my_ip = 'YOUR_IP' for user, secret in USERS.items(): params = {