summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian May <brian@linuxpenguins.xyz>2020-08-13 07:53:38 +1000
committerBrian May <brian@linuxpenguins.xyz>2020-08-13 07:53:38 +1000
commit9c5f1f5bbf2ed2b7a7009044b61318b4e4752f14 (patch)
tree664fd4c26fa85074b339487421cf3c3c10f0cc8e
parentc5dcc918db666dfd1b30afc72cc198abfb3b3aa9 (diff)
Fix parse_hostport to always return string for hosttest_parse_hostport
This fixes #488 and provides an alternative solution for #489.
-rw-r--r--sshuttle/ssh.py4
-rw-r--r--tests/ssh/test_parse_hostport.py20
2 files changed, 22 insertions, 2 deletions
diff --git a/sshuttle/ssh.py b/sshuttle/ssh.py
index c93aa8c..9313215 100644
--- a/sshuttle/ssh.py
+++ b/sshuttle/ssh.py
@@ -65,12 +65,12 @@ def parse_hostport(rhostport):
try:
# try to parse host as an IP adress,
# if that works it is an IPv6 address
- host = ipaddress.ip_address(host)
+ host = str(ipaddress.ip_address(host))
except ValueError:
# if that fails parse as URL to get the port
parsed = urlparse('//{}'.format(host))
try:
- host = ipaddress.ip_address(parsed.hostname)
+ host = str(ipaddress.ip_address(parsed.hostname))
except ValueError:
# else if both fails, we have a hostname with port
host = parsed.hostname
diff --git a/tests/ssh/test_parse_hostport.py b/tests/ssh/test_parse_hostport.py
new file mode 100644
index 0000000..759bb07
--- /dev/null
+++ b/tests/ssh/test_parse_hostport.py
@@ -0,0 +1,20 @@
+from sshuttle.ssh import parse_hostport
+
+
+def test_host_only():
+ assert parse_hostport("host") == (None, None, None, "host")
+ assert parse_hostport("1.2.3.4") == (None, None, None, "1.2.3.4")
+ assert parse_hostport("2001::1") == (None, None, None, "2001::1")
+ assert parse_hostport("[2001::1]") == (None, None, None, "2001::1")
+
+
+def test_host_and_port():
+ assert parse_hostport("host:22") == (None, None, 22, "host")
+ assert parse_hostport("1.2.3.4:22") == (None, None, 22, "1.2.3.4")
+ assert parse_hostport("[2001::1]:22") == (None, None, 22, "2001::1")
+
+
+def test_username_and_host():
+ assert parse_hostport("user@host") == ("user", None, None, "host")
+ assert parse_hostport("user:@host") == ("user", None, None, "host")
+ assert parse_hostport("user:pass@host") == ("user", "pass", None, "host")