2
2
Only exports find_hostname_in_userinput function
3
3
"""
4
4
5
+ from typing import List
6
+
5
7
from aikido_zen .helpers .get_port_from_url import get_port_from_url
6
8
from aikido_zen .helpers .try_parse_url import try_parse_url
7
9
@@ -13,14 +15,12 @@ def find_hostname_in_userinput(user_input, hostname, port=None):
13
15
if len (user_input ) <= 1 :
14
16
return False
15
17
16
- hostname_url = try_parse_url (f"http://{ hostname } " )
17
- if not hostname_url :
18
- return False
18
+ hostname_options = get_hostname_options (hostname )
19
19
20
20
variants = [user_input , f"http://{ user_input } " , f"https://{ user_input } " ]
21
21
for variant in variants :
22
22
user_input_url = try_parse_url (variant )
23
- if user_input_url and user_input_url .hostname == hostname_url . hostname :
23
+ if user_input_url and user_input_url .hostname in hostname_options :
24
24
user_port = get_port_from_url (user_input_url .geturl ())
25
25
26
26
# We were unable to retrieve the port from the URL, likely because it contains an invalid port.
@@ -35,3 +35,17 @@ def find_hostname_in_userinput(user_input, hostname, port=None):
35
35
return True
36
36
37
37
return False
38
+
39
+
40
+ def get_hostname_options (raw_hostname : str ) -> List [str ]:
41
+ options = []
42
+ hostname_url = try_parse_url (f"http://{ raw_hostname } " )
43
+ if hostname_url and hostname_url .hostname :
44
+ options .append (hostname_url .hostname )
45
+
46
+ # Add a case for hostnames like ::1 or ::ffff:127.0.0.1, who need brackets to be parsed
47
+ hostname_url_ipv6 = try_parse_url (f"http://[{ raw_hostname } ]" )
48
+ if hostname_url_ipv6 and hostname_url_ipv6 .hostname :
49
+ options .append (hostname_url_ipv6 .hostname )
50
+
51
+ return options
0 commit comments