detect external and public ip

Signed-off-by: Vladimir Mandic <mandic00@live.com>
This commit is contained in:
Vladimir Mandic
2025-01-27 10:25:01 -05:00
parent 79416af994
commit 1afa4be02c
+29
View File
@@ -209,6 +209,27 @@ def async_policy():
asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
def get_external_ip():
import socket
try:
ip_address = socket.gethostbyname(socket.gethostname())
if ip_address.startswith('127.'):
return None
return ip_address
except Exception:
return None
def get_remote_ip():
import requests
try:
response = requests.get('https://api.ipify.org?format=json', timeout=2)
ip_address = response.json()['ip']
return ip_address
except Exception:
return None
def start_common():
log.debug('Entering start sequence')
if shared.cmd_opts.data_dir is not None and len(shared.cmd_opts.data_dir) > 0:
@@ -284,6 +305,14 @@ def start_ui():
if shared.cmd_opts.data_dir is not None:
gr_tempdir.register_tmp_file(shared.demo, os.path.join(shared.cmd_opts.data_dir, 'x'))
shared.log.info(f'Local URL: {local_url}')
if shared.cmd_opts.listen:
proto = 'https' if shared.cmd_opts.tls_keyfile is not None else 'http'
external_ip = get_external_ip()
if external_ip is not None:
shared.log.info(f'External URL: {proto}://{external_ip}:{shared.cmd_opts.port}')
public_ip = get_remote_ip()
if public_ip is not None:
shared.log.info(f'Public URL: {proto}://{public_ip}:{shared.cmd_opts.port}')
if shared.cmd_opts.docs:
shared.log.info(f'API Docs: {local_url[:-1]}/docs') # pylint: disable=unsubscriptable-object
shared.log.info(f'API ReDocs: {local_url[:-1]}/redocs') # pylint: disable=unsubscriptable-object