Skip to content

Commit

Permalink
Handle unbracketed ipv6 addresses (#538)
Browse files Browse the repository at this point in the history
* Add failing test
* Parse unbracketed ipv6 hosts with no ports
* Tweak style
  • Loading branch information
jeromedalbert authored Jun 22, 2024
1 parent c8401f8 commit 02198fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/sshkit/host.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ostruct'
require 'resolv'

module SSHKit

Expand Down Expand Up @@ -122,6 +123,22 @@ def attributes

end

# @private
# :nodoc:
class IPv6HostParser < SimpleHostParser
def self.suitable?(host_string)
host_string.match(Resolv::IPv6::Regex)
end

def port

end

def hostname
@host_string.match(Resolv::IPv6::Regex)[0]
end
end

class HostWithPortParser < SimpleHostParser

def self.suitable?(host_string)
Expand Down Expand Up @@ -192,6 +209,7 @@ def hostname

PARSERS = [
SimpleHostParser,
IPv6HostParser,
HostWithPortParser,
HostWithUsernameAndPortParser,
IPv6HostWithPortParser,
Expand Down
12 changes: 12 additions & 0 deletions test/unit/test_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def test_host_with_port
assert_equal 'example.com', h.hostname
end

def test_custom_host_with_port
h = Host.new 'db:22'
assert_equal 22, h.port
assert_equal 'db', h.hostname
end

def test_host_with_username
h = Host.new 'root@example.com'
assert_equal 'root', h.username
Expand All @@ -50,6 +56,12 @@ def test_host_local
assert_equal 'localhost', h.hostname
end

def test_ipv6_without_brackets
h = Host.new '1fff:0:a88:85a3::ac1f'
assert_nil h.port
assert_equal '1fff:0:a88:85a3::ac1f', h.hostname
end

def test_does_not_confuse_ipv6_hosts_with_port_specification
h = Host.new '[1fff:0:a88:85a3::ac1f]:8001'
assert_equal 8001, h.port
Expand Down

0 comments on commit 02198fe

Please sign in to comment.