Skip to content

Commit 2a773de

Browse files
committed
Handle IPv6 addresses without port
1 parent e51a7ef commit 2a773de

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/sshkit/host.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,20 @@ def port
151151
# @private
152152
# :nodoc:
153153
class IPv6HostWithPortParser < SimpleHostParser
154+
IPV6_REGEX = /\[([a-fA-F0-9:]+)\](?:\:(\d+))?/
154155

155156
def self.suitable?(host_string)
156-
host_string.match(/[a-fA-F0-9:]+:\d+/)
157+
host_string.match(IPV6_REGEX)
157158
end
158159

159160
def port
160-
@host_string.split(':').last.to_i
161+
prt = @host_string.match(IPV6_REGEX)[2]
162+
prt = prt.to_i unless prt.nil?
163+
prt
161164
end
162165

163166
def hostname
164-
@host_string.gsub!(/\[|\]/, '')
165-
@host_string.split(':')[0..-2].join(':')
167+
@host_string.match(IPV6_REGEX)[1]
166168
end
167169

168170
end

test/unit/test_host.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def test_does_not_confuse_ipv6_hosts_with_port_specification
5656
assert_equal '1fff:0:a88:85a3::ac1f', h.hostname
5757
end
5858

59+
def test_does_not_confuse_ipv6_hosts_without_port_specification
60+
h = Host.new '[2001:db8:85a3:8d3:1319:8a2e:370:7348]'
61+
assert_equal '2001:db8:85a3:8d3:1319:8a2e:370:7348', h.hostname
62+
end
63+
5964
def testing_host_casting_to_a_string
6065
assert_equal "example.com", Host.new('user@example.com:1234').to_s
6166
end

0 commit comments

Comments
 (0)