Skip to content

Commit

Permalink
Allow to access external server IP via https. (streamlit#7712)
Browse files Browse the repository at this point in the history
Fallback to HTTPS endpoint call to check external IP if HTTP call failed.

---------

Co-authored-by: Lars Hillebrand <lars.patrick.hillebrand@iais.fraunhofer.de>
Co-authored-by: Karen Javadyan <kajarenc@gmail.com>
  • Loading branch information
3 people authored and Your Name committed Mar 22, 2024
1 parent 4249498 commit cf90e50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/streamlit/net_util.py
Expand Up @@ -22,8 +22,9 @@

LOGGER = get_logger(__name__)

# URL for checking the current machine's external IP address.
# URLs for checking the current machine's external IP address.
_AWS_CHECK_IP: Final = "http://checkip.amazonaws.com"
_AWS_CHECK_IP_HTTPS: Final = "https://checkip.amazonaws.com"

_external_ip: Optional[str] = None
_internal_ip: Optional[str] = None
Expand All @@ -45,6 +46,9 @@ def get_external_ip() -> Optional[str]:

response = _make_blocking_http_get(_AWS_CHECK_IP, timeout=5)

if response is None:
response = _make_blocking_http_get(_AWS_CHECK_IP_HTTPS, timeout=5)

if _looks_like_an_ip_adress(response):
_external_ip = response
else:
Expand Down
14 changes: 14 additions & 0 deletions lib/tests/streamlit/net_util_test.py
Expand Up @@ -37,6 +37,20 @@ def test_get_external_ip(self):
m.get(net_util._AWS_CHECK_IP, exc=requests.exceptions.ConnectTimeout)
self.assertEqual(None, net_util.get_external_ip())

def test_get_external_ip_use_http_by_default(self):
with requests_mock.mock() as m:
m.get(net_util._AWS_CHECK_IP, text="1.2.3.4")
m.get(net_util._AWS_CHECK_IP_HTTPS, text="5.6.7.8")
self.assertEqual("1.2.3.4", net_util.get_external_ip())
self.assertEqual(m.call_count, 1)

def test_get_external_ip_https_if_http_fails(self):
with requests_mock.mock() as m:
m.get(net_util._AWS_CHECK_IP, exc=requests.exceptions.ConnectTimeout)
m.get(net_util._AWS_CHECK_IP_HTTPS, text="5.6.7.8")
self.assertEqual("5.6.7.8", net_util.get_external_ip())
self.assertEqual(m.call_count, 2)

def test_get_external_ip_html(self):
# This tests the case where the external URL returns a web page.
# https://github.com/streamlit/streamlit/issues/554#issuecomment-604847244
Expand Down

0 comments on commit cf90e50

Please sign in to comment.