mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2024-11-26 02:30:21 -06:00
8857f0d179
1) String literals should not be duplicated. 2) Prefer using an optional chain expression instead, as it's more concise and easier to read. 3) Expected the Promise rejection reason to be an Error.
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
##########################################################################
|
|
#
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
#
|
|
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
|
# This software is released under the PostgreSQL Licence
|
|
#
|
|
##########################################################################
|
|
|
|
import secrets
|
|
import string
|
|
import urllib3
|
|
import ipaddress
|
|
from pgadmin.utils.constants import IP_ADDRESS_STRING
|
|
|
|
|
|
def get_my_ip():
|
|
""" Return the public IP of this host """
|
|
http = urllib3.PoolManager()
|
|
try:
|
|
external_ip = http.request('GET', 'https://ident.me').data
|
|
except Exception:
|
|
try:
|
|
external_ip = http.request('GET', 'https://ifconfig.me/ip').data
|
|
except Exception:
|
|
external_ip = '127.0.0.1'
|
|
|
|
if isinstance(external_ip, bytes):
|
|
external_ip = external_ip.decode('utf-8')
|
|
|
|
ip = ipaddress.ip_address(external_ip)
|
|
if isinstance(ip, ipaddress.IPv4Address):
|
|
return IP_ADDRESS_STRING.format(external_ip, 32)
|
|
elif isinstance(ip, ipaddress.IPv6Address):
|
|
return IP_ADDRESS_STRING.format(external_ip, 128)
|
|
|
|
return IP_ADDRESS_STRING.format(external_ip, 32)
|
|
|
|
|
|
def get_random_id():
|
|
""" Return a random 10 byte string """
|
|
letters = string.ascii_letters + string.digits
|
|
return ''.join(secrets.choice(letters) for _ in range(10))
|