2022-02-14 00:43:48 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2024-12-31 23:56:42 -06:00
|
|
|
# Copyright (C) 2013 - 2025, The pgAdmin Development Team
|
2022-02-14 00:43:48 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2022-08-12 06:40:26 -05:00
|
|
|
import secrets
|
2022-02-14 00:43:48 -06:00
|
|
|
import string
|
|
|
|
import urllib3
|
2022-03-11 03:03:30 -06:00
|
|
|
import ipaddress
|
2022-02-14 00:43:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
def get_my_ip():
|
|
|
|
""" Return the public IP of this host """
|
|
|
|
http = urllib3.PoolManager()
|
2024-07-24 05:04:30 -05:00
|
|
|
IP_ADDRESS_STRING = '{}/{}'
|
2022-02-14 00:43:48 -06:00
|
|
|
try:
|
2022-03-02 07:32:35 -06:00
|
|
|
external_ip = http.request('GET', 'https://ident.me').data
|
2022-02-14 00:43:48 -06:00
|
|
|
except Exception:
|
|
|
|
try:
|
2022-03-02 07:32:35 -06:00
|
|
|
external_ip = http.request('GET', 'https://ifconfig.me/ip').data
|
2022-02-14 00:43:48 -06:00
|
|
|
except Exception:
|
|
|
|
external_ip = '127.0.0.1'
|
|
|
|
|
2023-07-31 07:44:39 -05:00
|
|
|
if isinstance(external_ip, bytes):
|
2022-03-11 03:03:30 -06:00
|
|
|
external_ip = external_ip.decode('utf-8')
|
|
|
|
|
|
|
|
ip = ipaddress.ip_address(external_ip)
|
|
|
|
if isinstance(ip, ipaddress.IPv4Address):
|
2024-06-10 07:34:32 -05:00
|
|
|
return IP_ADDRESS_STRING.format(external_ip, 32)
|
2022-03-11 03:03:30 -06:00
|
|
|
elif isinstance(ip, ipaddress.IPv6Address):
|
2024-06-10 07:34:32 -05:00
|
|
|
return IP_ADDRESS_STRING.format(external_ip, 128)
|
2022-03-11 03:03:30 -06:00
|
|
|
|
2024-06-10 07:34:32 -05:00
|
|
|
return IP_ADDRESS_STRING.format(external_ip, 32)
|
2022-02-14 00:43:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
def get_random_id():
|
|
|
|
""" Return a random 10 byte string """
|
|
|
|
letters = string.ascii_letters + string.digits
|
2022-08-12 06:40:26 -05:00
|
|
|
return ''.join(secrets.choice(letters) for _ in range(10))
|