Skip to content

Commit d01f803

Browse files
authored
Merge pull request #460 from will-in-wi/403-handle-ipv6-without-port
Handle IPv6 addresses without port
2 parents 2e73ecb + 1868ea5 commit d01f803

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ appear at the top.
88
* Your contribution here!
99
* [#455](https://github.com/capistrano/sshkit/pull/455): Ensure UUID of commands are stable in logging - [@lazyatom](https://github.com/lazyatom)
1010
* [#453](https://github.com/capistrano/sshkit/pull/453): `as` and `within` now properly escape their user/group/path arguments, and the command nested within an `as` block is now properly escaped before passing to `sh -c`. In the unlikely case that you were manually escaping commands passed to SSHKit as a workaround, you will no longer need to do this. See [#458](https://github.com/capistrano/sshkit/issues/458) for examples of what has been fixed. - [@grosser](https://github.com/grosser)
11+
* [#460](https://github.com/capistrano/sshkit/pull/460): Handle IPv6 addresses without port - [@will-in-wi](https://github.com/will-in-wi)
1112

1213
## [1.18.2][] (2019-02-03)
1314

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)