Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to access external server IP via https. #7712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to also verify that we make two endpoint calls

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