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 3 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
8 changes: 7 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 @@ -43,8 +44,13 @@ def get_external_ip() -> Optional[str]:
if _external_ip is not None:
return _external_ip

# try to get external IP from Amazon http url
response = _make_blocking_http_get(_AWS_CHECK_IP, timeout=5)

# try to get external IP from Amazon https url
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I feel like the code here is straightforward enough that the comments aren't needed

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
16 changes: 16 additions & 0 deletions lib/tests/streamlit/net_util_test.py
Expand Up @@ -37,6 +37,22 @@ 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):
# Test that by default we do call http endpoint
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())
# Test that we do only one http call
self.assertEqual(m.call_count, 1)

def test_get_external_ip_https(self):
# Test that if http fails, we try https
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: same thing here -- we can probably drop these comments since the test names are descriptive enough + maybe edit the second test name a bit

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


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