poolparty 1.3.4 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. data/Rakefile +2 -2
  2. data/VERSION.yml +1 -1
  3. data/bin/cloud-bootstrap +1 -0
  4. data/bin/cloud-configure +1 -0
  5. data/bin/cloud-contract +1 -0
  6. data/bin/cloud-misc +34 -0
  7. data/bin/cloud-setup +36 -0
  8. data/bin/cloud-ssh +4 -1
  9. data/config/jeweler.rb +4 -3
  10. data/examples/monitored_cloud.rb +1 -1
  11. data/examples/thrift/thrift_example.rb +5 -3
  12. data/examples/vmware.rb +28 -0
  13. data/lib/cloud_providers/cloud_provider_instance.rb +14 -5
  14. data/lib/cloud_providers/connections.rb +1 -1
  15. data/lib/core/file.rb +12 -0
  16. data/lib/core/object.rb +2 -2
  17. data/lib/dependency_resolvers/base.rb +1 -1
  18. data/lib/dependency_resolvers/chef.rb +9 -7
  19. data/lib/dependency_resolvers/proxy_object.rb +11 -3
  20. data/lib/mixins/askable.rb +16 -7
  21. data/lib/poolparty/base.rb +8 -7
  22. data/lib/poolparty/cloud.rb +77 -7
  23. data/lib/poolparty/default.rb +1 -0
  24. data/lib/poolparty/installer.rb +8 -4
  25. data/lib/poolparty/installers/ec2.rb +75 -5
  26. data/lib/poolparty/installers/vmware.rb +17 -5
  27. data/lib/poolparty/plugin.rb +1 -5
  28. data/lib/poolparty/plugins/apache.rb +10 -7
  29. data/lib/poolparty/plugins/apache2/base.conf.erb +2 -2
  30. data/lib/poolparty/plugins/apache2/browser_fixes.conf.erb +1 -1
  31. data/lib/poolparty/plugins/apache2/passenger_site.rb +2 -2
  32. data/lib/poolparty/plugins/collectd/templates/collectd.conf.erb +369 -0
  33. data/lib/poolparty/plugins/collectd.rb +24 -0
  34. data/lib/poolparty/plugins/hermes.rb +89 -0
  35. data/lib/poolparty/pool.rb +33 -3
  36. data/lib/poolparty/resource.rb +32 -18
  37. data/lib/poolparty/resources/directory.rb +5 -1
  38. data/lib/poolparty/resources/exec.rb +2 -2
  39. data/lib/poolparty/resources/file.rb +8 -2
  40. data/lib/poolparty/resources/gem_package.rb +2 -2
  41. data/lib/poolparty/resources/line.rb +23 -6
  42. data/lib/poolparty/resources/mount.rb +2 -2
  43. data/lib/poolparty/resources/package.rb +2 -2
  44. data/lib/poolparty/resources/service.rb +2 -2
  45. data/lib/poolparty/resources/user.rb +2 -2
  46. data/lib/poolparty/resources/variable.rb +4 -3
  47. data/lib/poolparty.rb +5 -3
  48. data/lib/proto/command_interface_handler.rb +17 -1
  49. data/lib/proto/gen-py/cloudthrift/CommandInterface.pyc +0 -0
  50. data/lib/proto/gen-py/cloudthrift/__init__.pyc +0 -0
  51. data/lib/proto/gen-py/cloudthrift/constants.pyc +0 -0
  52. data/lib/proto/gen-py/cloudthrift/ttypes.pyc +0 -0
  53. data/lib/proto/gen-py/thrift/Thrift.pyc +0 -0
  54. data/lib/proto/gen-py/thrift/__init__.pyc +0 -0
  55. data/lib/proto/gen-py/thrift/protocol/TBinaryProtocol.pyc +0 -0
  56. data/lib/proto/gen-py/thrift/protocol/TProtocol.pyc +0 -0
  57. data/lib/proto/gen-py/thrift/protocol/__init__.pyc +0 -0
  58. data/lib/proto/gen-py/thrift/transport/TSocket.pyc +0 -0
  59. data/lib/proto/gen-py/thrift/transport/TTransport.pyc +0 -0
  60. data/lib/proto/gen-py/thrift/transport/__init__.pyc +0 -0
  61. data/test/lib/dependency_resolvers/chef_test.rb +92 -100
  62. data/test/lib/poolparty/base_test.rb +13 -0
  63. data/test/lib/poolparty/cloud_test.rb +50 -2
  64. data/test/lib/poolparty/monitor_test.rb +2 -2
  65. data/test/lib/poolparty/resource_test.rb +5 -0
  66. data/test/lib/poolparty/resources/line_test.rb +3 -3
  67. data/test/lib/poolparty/resources/service_test.rb +1 -1
  68. data/test/lib/poolparty/resources/variable_test.rb +33 -10
  69. data/vendor/gems/net-ssh/CHANGELOG.rdoc +127 -0
  70. data/vendor/gems/net-ssh/Manifest +104 -0
  71. data/vendor/gems/net-ssh/README.rdoc +110 -0
  72. data/vendor/gems/net-ssh/Rakefile +26 -0
  73. data/vendor/gems/net-ssh/THANKS.rdoc +16 -0
  74. data/vendor/gems/net-ssh/lib/net/ssh/authentication/agent.rb +176 -0
  75. data/vendor/gems/net-ssh/lib/net/ssh/authentication/constants.rb +18 -0
  76. data/vendor/gems/net-ssh/lib/net/ssh/authentication/key_manager.rb +193 -0
  77. data/vendor/gems/net-ssh/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  78. data/vendor/gems/net-ssh/lib/net/ssh/authentication/methods/hostbased.rb +71 -0
  79. data/vendor/gems/net-ssh/lib/net/ssh/authentication/methods/keyboard_interactive.rb +66 -0
  80. data/vendor/gems/net-ssh/lib/net/ssh/authentication/methods/password.rb +39 -0
  81. data/vendor/gems/net-ssh/lib/net/ssh/authentication/methods/publickey.rb +92 -0
  82. data/vendor/gems/net-ssh/lib/net/ssh/authentication/pageant.rb +183 -0
  83. data/vendor/gems/net-ssh/lib/net/ssh/authentication/session.rb +134 -0
  84. data/vendor/gems/net-ssh/lib/net/ssh/buffer.rb +340 -0
  85. data/vendor/gems/net-ssh/lib/net/ssh/buffered_io.rb +149 -0
  86. data/vendor/gems/net-ssh/lib/net/ssh/config.rb +181 -0
  87. data/vendor/gems/net-ssh/lib/net/ssh/connection/channel.rb +625 -0
  88. data/vendor/gems/net-ssh/lib/net/ssh/connection/constants.rb +33 -0
  89. data/vendor/gems/net-ssh/lib/net/ssh/connection/session.rb +596 -0
  90. data/vendor/gems/net-ssh/lib/net/ssh/connection/term.rb +178 -0
  91. data/vendor/gems/net-ssh/lib/net/ssh/errors.rb +85 -0
  92. data/vendor/gems/net-ssh/lib/net/ssh/key_factory.rb +102 -0
  93. data/vendor/gems/net-ssh/lib/net/ssh/known_hosts.rb +129 -0
  94. data/vendor/gems/net-ssh/lib/net/ssh/loggable.rb +61 -0
  95. data/vendor/gems/net-ssh/lib/net/ssh/packet.rb +102 -0
  96. data/vendor/gems/net-ssh/lib/net/ssh/prompt.rb +93 -0
  97. data/vendor/gems/net-ssh/lib/net/ssh/proxy/errors.rb +14 -0
  98. data/vendor/gems/net-ssh/lib/net/ssh/proxy/http.rb +94 -0
  99. data/vendor/gems/net-ssh/lib/net/ssh/proxy/socks4.rb +70 -0
  100. data/vendor/gems/net-ssh/lib/net/ssh/proxy/socks5.rb +129 -0
  101. data/vendor/gems/net-ssh/lib/net/ssh/ruby_compat.rb +7 -0
  102. data/vendor/gems/net-ssh/lib/net/ssh/service/forward.rb +267 -0
  103. data/vendor/gems/net-ssh/lib/net/ssh/test/channel.rb +129 -0
  104. data/vendor/gems/net-ssh/lib/net/ssh/test/extensions.rb +152 -0
  105. data/vendor/gems/net-ssh/lib/net/ssh/test/kex.rb +44 -0
  106. data/vendor/gems/net-ssh/lib/net/ssh/test/local_packet.rb +51 -0
  107. data/vendor/gems/net-ssh/lib/net/ssh/test/packet.rb +81 -0
  108. data/vendor/gems/net-ssh/lib/net/ssh/test/remote_packet.rb +38 -0
  109. data/vendor/gems/net-ssh/lib/net/ssh/test/script.rb +157 -0
  110. data/vendor/gems/net-ssh/lib/net/ssh/test/socket.rb +59 -0
  111. data/vendor/gems/net-ssh/lib/net/ssh/test.rb +89 -0
  112. data/vendor/gems/net-ssh/lib/net/ssh/transport/algorithms.rb +384 -0
  113. data/vendor/gems/net-ssh/lib/net/ssh/transport/cipher_factory.rb +80 -0
  114. data/vendor/gems/net-ssh/lib/net/ssh/transport/constants.rb +30 -0
  115. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/abstract.rb +78 -0
  116. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/md5.rb +12 -0
  117. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  118. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/none.rb +15 -0
  119. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  120. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  121. data/vendor/gems/net-ssh/lib/net/ssh/transport/hmac.rb +31 -0
  122. data/vendor/gems/net-ssh/lib/net/ssh/transport/identity_cipher.rb +55 -0
  123. data/vendor/gems/net-ssh/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
  124. data/vendor/gems/net-ssh/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +77 -0
  125. data/vendor/gems/net-ssh/lib/net/ssh/transport/kex.rb +13 -0
  126. data/vendor/gems/net-ssh/lib/net/ssh/transport/openssl.rb +128 -0
  127. data/vendor/gems/net-ssh/lib/net/ssh/transport/packet_stream.rb +230 -0
  128. data/vendor/gems/net-ssh/lib/net/ssh/transport/server_version.rb +60 -0
  129. data/vendor/gems/net-ssh/lib/net/ssh/transport/session.rb +276 -0
  130. data/vendor/gems/net-ssh/lib/net/ssh/transport/state.rb +201 -0
  131. data/vendor/gems/net-ssh/lib/net/ssh/verifiers/lenient.rb +30 -0
  132. data/vendor/gems/net-ssh/lib/net/ssh/verifiers/null.rb +12 -0
  133. data/vendor/gems/net-ssh/lib/net/ssh/verifiers/strict.rb +53 -0
  134. data/vendor/gems/net-ssh/lib/net/ssh/version.rb +62 -0
  135. data/vendor/gems/net-ssh/lib/net/ssh.rb +215 -0
  136. data/vendor/gems/net-ssh/net-ssh.gemspec +33 -0
  137. data/vendor/gems/net-ssh/setup.rb +1585 -0
  138. data/vendor/gems/net-ssh/test/authentication/methods/common.rb +28 -0
  139. data/vendor/gems/net-ssh/test/authentication/methods/test_abstract.rb +51 -0
  140. data/vendor/gems/net-ssh/test/authentication/methods/test_hostbased.rb +114 -0
  141. data/vendor/gems/net-ssh/test/authentication/methods/test_keyboard_interactive.rb +98 -0
  142. data/vendor/gems/net-ssh/test/authentication/methods/test_password.rb +50 -0
  143. data/vendor/gems/net-ssh/test/authentication/methods/test_publickey.rb +127 -0
  144. data/vendor/gems/net-ssh/test/authentication/test_agent.rb +205 -0
  145. data/vendor/gems/net-ssh/test/authentication/test_key_manager.rb +105 -0
  146. data/vendor/gems/net-ssh/test/authentication/test_session.rb +93 -0
  147. data/vendor/gems/net-ssh/test/common.rb +106 -0
  148. data/vendor/gems/net-ssh/test/configs/eqsign +3 -0
  149. data/vendor/gems/net-ssh/test/configs/exact_match +8 -0
  150. data/vendor/gems/net-ssh/test/configs/wild_cards +14 -0
  151. data/vendor/gems/net-ssh/test/connection/test_channel.rb +452 -0
  152. data/vendor/gems/net-ssh/test/connection/test_session.rb +488 -0
  153. data/vendor/gems/net-ssh/test/test_all.rb +6 -0
  154. data/vendor/gems/net-ssh/test/test_buffer.rb +336 -0
  155. data/vendor/gems/net-ssh/test/test_buffered_io.rb +63 -0
  156. data/vendor/gems/net-ssh/test/test_config.rb +84 -0
  157. data/vendor/gems/net-ssh/test/test_key_factory.rb +67 -0
  158. data/vendor/gems/net-ssh/test/transport/hmac/test_md5.rb +39 -0
  159. data/vendor/gems/net-ssh/test/transport/hmac/test_md5_96.rb +25 -0
  160. data/vendor/gems/net-ssh/test/transport/hmac/test_none.rb +34 -0
  161. data/vendor/gems/net-ssh/test/transport/hmac/test_sha1.rb +34 -0
  162. data/vendor/gems/net-ssh/test/transport/hmac/test_sha1_96.rb +25 -0
  163. data/vendor/gems/net-ssh/test/transport/kex/test_diffie_hellman_group1_sha1.rb +146 -0
  164. data/vendor/gems/net-ssh/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +92 -0
  165. data/vendor/gems/net-ssh/test/transport/test_algorithms.rb +302 -0
  166. data/vendor/gems/net-ssh/test/transport/test_cipher_factory.rb +171 -0
  167. data/vendor/gems/net-ssh/test/transport/test_hmac.rb +34 -0
  168. data/vendor/gems/net-ssh/test/transport/test_identity_cipher.rb +40 -0
  169. data/vendor/gems/net-ssh/test/transport/test_packet_stream.rb +435 -0
  170. data/vendor/gems/net-ssh/test/transport/test_server_version.rb +57 -0
  171. data/vendor/gems/net-ssh/test/transport/test_session.rb +315 -0
  172. data/vendor/gems/net-ssh/test/transport/test_state.rb +173 -0
  173. metadata +116 -4
  174. data/bin/install-poolparty +0 -20
@@ -0,0 +1,93 @@
1
+ module Net; module SSH
2
+
3
+ # A basic prompt module that can be mixed into other objects. If HighLine is
4
+ # installed, it will be used to display prompts and read input from the
5
+ # user. Otherwise, the termios library will be used. If neither HighLine
6
+ # nor termios is installed, a simple prompt that echos text in the clear
7
+ # will be used.
8
+
9
+ module PromptMethods
10
+
11
+ # Defines the prompt method to use if the Highline library is installed.
12
+ module Highline
13
+ # Uses Highline#ask to present a prompt and accept input. If +echo+ is
14
+ # +false+, the characters entered by the user will not be echoed to the
15
+ # screen.
16
+ def prompt(prompt, echo=true)
17
+ @highline ||= ::HighLine.new
18
+ @highline.ask(prompt + " ") { |q| q.echo = echo }
19
+ end
20
+ end
21
+
22
+ # Defines the prompt method to use if the Termios library is installed.
23
+ module Termios
24
+ # Displays the prompt to $stdout. If +echo+ is false, the Termios
25
+ # library will be used to disable keystroke echoing for the duration of
26
+ # this method.
27
+ def prompt(prompt, echo=true)
28
+ $stdout.print(prompt)
29
+ $stdout.flush
30
+
31
+ set_echo(false) unless echo
32
+ $stdin.gets.chomp
33
+ ensure
34
+ if !echo
35
+ set_echo(true)
36
+ $stdout.puts
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # Enables or disables keystroke echoing using the Termios library.
43
+ def set_echo(enable)
44
+ term = ::Termios.getattr($stdin)
45
+
46
+ if enable
47
+ term.c_lflag |= (::Termios::ECHO | ::Termios::ICANON)
48
+ else
49
+ term.c_lflag &= ~::Termios::ECHO
50
+ end
51
+
52
+ ::Termios.setattr($stdin, ::Termios::TCSANOW, term)
53
+ end
54
+ end
55
+
56
+ # Defines the prompt method to use when neither Highline nor Termios are
57
+ # installed.
58
+ module Clear
59
+ # Displays the prompt to $stdout and pulls the response from $stdin.
60
+ # Text is always echoed in the clear, regardless of the +echo+ setting.
61
+ # The first time a prompt is given and +echo+ is false, a warning will
62
+ # be written to $stderr recommending that either Highline or Termios
63
+ # be installed.
64
+ def prompt(prompt, echo=true)
65
+ @seen_warning ||= false
66
+ if !echo && !@seen_warning
67
+ $stderr.puts "Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text."
68
+ @seen_warning = true
69
+ end
70
+
71
+ $stdout.print(prompt)
72
+ $stdout.flush
73
+ $stdin.gets.chomp
74
+ end
75
+ end
76
+ end
77
+
78
+ # Try to load Highline and Termios in turn, selecting the corresponding
79
+ # PromptMethods module to use. If neither are available, choose PromptMethods::Clear.
80
+ Prompt = begin
81
+ require 'highline'
82
+ HighLine.track_eof = false
83
+ PromptMethods::Highline
84
+ rescue LoadError
85
+ begin
86
+ require 'termios'
87
+ PromptMethods::Termios
88
+ rescue LoadError
89
+ PromptMethods::Clear
90
+ end
91
+ end
92
+
93
+ end; end
@@ -0,0 +1,14 @@
1
+ require 'net/ssh/errors'
2
+
3
+ module Net; module SSH; module Proxy
4
+
5
+ # A general exception class for all Proxy errors.
6
+ class Error < Net::SSH::Exception; end
7
+
8
+ # Used for reporting proxy connection errors.
9
+ class ConnectError < Error; end
10
+
11
+ # Used when the server doesn't recognize the user's credentials.
12
+ class UnauthorizedError < Error; end
13
+
14
+ end; end; end
@@ -0,0 +1,94 @@
1
+ require 'socket'
2
+ require 'net/ssh/proxy/errors'
3
+
4
+ module Net; module SSH; module Proxy
5
+
6
+ # An implementation of an HTTP proxy. To use it, instantiate it, then
7
+ # pass the instantiated object via the :proxy key to Net::SSH.start:
8
+ #
9
+ # require 'net/ssh/proxy/http'
10
+ #
11
+ # proxy = Net::SSH::Proxy::HTTP.new('proxy.host', proxy_port)
12
+ # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
13
+ # ...
14
+ # end
15
+ #
16
+ # If the proxy requires authentication, you can pass :user and :password
17
+ # to the proxy's constructor:
18
+ #
19
+ # proxy = Net::SSH::Proxy::HTTP.new('proxy.host', proxy_port,
20
+ # :user => "user", :password => "password")
21
+ #
22
+ # Note that HTTP digest authentication is not supported; Basic only at
23
+ # this point.
24
+ class HTTP
25
+
26
+ # The hostname or IP address of the HTTP proxy.
27
+ attr_reader :proxy_host
28
+
29
+ # The port number of the proxy.
30
+ attr_reader :proxy_port
31
+
32
+ # The map of additional options that were given to the object at
33
+ # initialization.
34
+ attr_reader :options
35
+
36
+ # Create a new socket factory that tunnels via the given host and
37
+ # port. The +options+ parameter is a hash of additional settings that
38
+ # can be used to tweak this proxy connection. Specifically, the following
39
+ # options are supported:
40
+ #
41
+ # * :user => the user name to use when authenticating to the proxy
42
+ # * :password => the password to use when authenticating
43
+ def initialize(proxy_host, proxy_port=80, options={})
44
+ @proxy_host = proxy_host
45
+ @proxy_port = proxy_port
46
+ @options = options
47
+ end
48
+
49
+ # Return a new socket connected to the given host and port via the
50
+ # proxy that was requested when the socket factory was instantiated.
51
+ def open(host, port)
52
+ socket = TCPSocket.new(proxy_host, proxy_port)
53
+ socket.write "CONNECT #{host}:#{port} HTTP/1.0\r\n"
54
+
55
+ if options[:user]
56
+ credentials = ["#{options[:user]}:#{options[:password]}"].pack("m*").gsub(/\s/, "")
57
+ socket.write "Proxy-Authorization: Basic #{credentials}\r\n"
58
+ end
59
+
60
+ socket.write "\r\n"
61
+
62
+ resp = parse_response(socket)
63
+
64
+ return socket if resp[:code] == 200
65
+
66
+ socket.close
67
+ raise ConnectError, resp.inspect
68
+ end
69
+
70
+ private
71
+
72
+ def parse_response(socket)
73
+ version, code, reason = socket.gets.chomp.split(/ /, 3)
74
+ headers = {}
75
+
76
+ while (line = socket.gets.chomp) != ""
77
+ name, value = line.split(/:/, 2)
78
+ headers[name.strip] = value.strip
79
+ end
80
+
81
+ if headers["Content-Length"]
82
+ body = socket.read(headers["Content-Length"].to_i)
83
+ end
84
+
85
+ return { :version => version,
86
+ :code => code.to_i,
87
+ :reason => reason,
88
+ :headers => headers,
89
+ :body => body }
90
+ end
91
+
92
+ end
93
+
94
+ end; end; end
@@ -0,0 +1,70 @@
1
+ require 'socket'
2
+ require 'resolv'
3
+ require 'ipaddr'
4
+ require 'net/ssh/proxy/errors'
5
+
6
+ module Net
7
+ module SSH
8
+ module Proxy
9
+
10
+ # An implementation of a SOCKS4 proxy. To use it, instantiate it, then
11
+ # pass the instantiated object via the :proxy key to Net::SSH.start:
12
+ #
13
+ # require 'net/ssh/proxy/socks4'
14
+ #
15
+ # proxy = Net::SSH::Proxy::SOCKS4.new('proxy.host', proxy_port, :user => 'user')
16
+ # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
17
+ # ...
18
+ # end
19
+ class SOCKS4
20
+
21
+ # The SOCKS protocol version used by this class
22
+ VERSION = 4
23
+
24
+ # The packet type for connection requests
25
+ CONNECT = 1
26
+
27
+ # The status code for a successful connection
28
+ GRANTED = 90
29
+
30
+ # The proxy's host name or IP address, as given to the constructor.
31
+ attr_reader :proxy_host
32
+
33
+ # The proxy's port number.
34
+ attr_reader :proxy_port
35
+
36
+ # The additional options that were given to the proxy's constructor.
37
+ attr_reader :options
38
+
39
+ # Create a new proxy connection to the given proxy host and port.
40
+ # Optionally, a :user key may be given to identify the username
41
+ # with which to authenticate.
42
+ def initialize(proxy_host, proxy_port=1080, options={})
43
+ @proxy_host = proxy_host
44
+ @proxy_port = proxy_port
45
+ @options = options
46
+ end
47
+
48
+ # Return a new socket connected to the given host and port via the
49
+ # proxy that was requested when the socket factory was instantiated.
50
+ def open(host, port)
51
+ socket = TCPSocket.new(proxy_host, proxy_port)
52
+ ip_addr = IPAddr.new(Resolv.getaddress(host))
53
+
54
+ packet = [VERSION, CONNECT, port.to_i, ip_addr.to_i, options[:user]].pack("CCnNZ*")
55
+ socket.send packet, 0
56
+
57
+ version, status, port, ip = socket.recv(8).unpack("CCnN")
58
+ if status != GRANTED
59
+ socket.close
60
+ raise ConnectError, "error connecting to proxy (#{status})"
61
+ end
62
+
63
+ return socket
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,129 @@
1
+ require 'socket'
2
+ require 'net/ssh/ruby_compat'
3
+ require 'net/ssh/proxy/errors'
4
+
5
+ module Net
6
+ module SSH
7
+ module Proxy
8
+
9
+ # An implementation of a SOCKS5 proxy. To use it, instantiate it, then
10
+ # pass the instantiated object via the :proxy key to Net::SSH.start:
11
+ #
12
+ # require 'net/ssh/proxy/socks5'
13
+ #
14
+ # proxy = Net::SSH::Proxy::SOCKS5.new('proxy.host', proxy_port,
15
+ # :user => 'user', :password => "password")
16
+ # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
17
+ # ...
18
+ # end
19
+ class SOCKS5
20
+ # The SOCKS protocol version used by this class
21
+ VERSION = 5
22
+
23
+ # The SOCKS authentication type for requests without authentication
24
+ METHOD_NO_AUTH = 0
25
+
26
+ # The SOCKS authentication type for requests via username/password
27
+ METHOD_PASSWD = 2
28
+
29
+ # The SOCKS authentication type for when there are no supported
30
+ # authentication methods.
31
+ METHOD_NONE = 0xFF
32
+
33
+ # The SOCKS packet type for requesting a proxy connection.
34
+ CMD_CONNECT = 1
35
+
36
+ # The SOCKS address type for connections via IP address.
37
+ ATYP_IPV4 = 1
38
+
39
+ # The SOCKS address type for connections via domain name.
40
+ ATYP_DOMAIN = 3
41
+
42
+ # The SOCKS response code for a successful operation.
43
+ SUCCESS = 0
44
+
45
+ # The proxy's host name or IP address
46
+ attr_reader :proxy_host
47
+
48
+ # The proxy's port number
49
+ attr_reader :proxy_port
50
+
51
+ # The map of options given at initialization
52
+ attr_reader :options
53
+
54
+ # Create a new proxy connection to the given proxy host and port.
55
+ # Optionally, :user and :password options may be given to
56
+ # identify the username and password with which to authenticate.
57
+ def initialize(proxy_host, proxy_port=1080, options={})
58
+ @proxy_host = proxy_host
59
+ @proxy_port = proxy_port
60
+ @options = options
61
+ end
62
+
63
+ # Return a new socket connected to the given host and port via the
64
+ # proxy that was requested when the socket factory was instantiated.
65
+ def open(host, port)
66
+ socket = TCPSocket.new(proxy_host, proxy_port)
67
+
68
+ methods = [METHOD_NO_AUTH]
69
+ methods << METHOD_PASSWD if options[:user]
70
+
71
+ packet = [VERSION, methods.size, *methods].pack("C*")
72
+ socket.send packet, 0
73
+
74
+ version, method = socket.recv(2).unpack("CC")
75
+ if version != VERSION
76
+ socket.close
77
+ raise Net::SSH::Proxy::Error, "invalid SOCKS version (#{version})"
78
+ end
79
+
80
+ if method == METHOD_NONE
81
+ socket.close
82
+ raise Net::SSH::Proxy::Error, "no supported authorization methods"
83
+ end
84
+
85
+ negotiate_password(socket) if method == METHOD_PASSWD
86
+
87
+ packet = [VERSION, CMD_CONNECT, 0].pack("C*")
88
+
89
+ if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
90
+ packet << [ATYP_IPV4, $1.to_i, $2.to_i, $3.to_i, $4.to_i].pack("C*")
91
+ else
92
+ packet << [ATYP_DOMAIN, host.length, host].pack("CCA*")
93
+ end
94
+
95
+ packet << [port].pack("n")
96
+ socket.send packet, 0
97
+
98
+ version, reply, = socket.recv(4).unpack("C*")
99
+ len = socket.recv(1).getbyte(0)
100
+ socket.recv(len + 2)
101
+
102
+ unless reply == SUCCESS
103
+ socket.close
104
+ raise ConnectError, "#{reply}"
105
+ end
106
+
107
+ return socket
108
+ end
109
+
110
+ private
111
+
112
+ # Simple username/password negotiation with the SOCKS5 server.
113
+ def negotiate_password(socket)
114
+ packet = [0x01, options[:user].length, options[:user],
115
+ options[:password].length, options[:password]].pack("CCA*CA*")
116
+ socket.send packet, 0
117
+
118
+ version, status = socket.recv(2).unpack("CC")
119
+
120
+ if status != SUCCESS
121
+ socket.close
122
+ raise UnauthorizedError, "could not authorize user"
123
+ end
124
+ end
125
+ end
126
+
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+ if RUBY_VERSION < "1.9"
3
+ def getbyte(index)
4
+ self[index]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,267 @@
1
+ require 'net/ssh/loggable'
2
+
3
+ module Net; module SSH; module Service
4
+
5
+ # This class implements various port forwarding services for use by
6
+ # Net::SSH clients. The Forward class should never need to be instantiated
7
+ # directly; instead, it should be accessed via the singleton instance
8
+ # returned by Connection::Session#forward:
9
+ #
10
+ # ssh.forward.local(1234, "www.capify.org", 80)
11
+ class Forward
12
+ include Loggable
13
+
14
+ # The underlying connection service instance that the port-forwarding
15
+ # services employ.
16
+ attr_reader :session
17
+
18
+ # A simple class for representing a requested remote forwarded port.
19
+ Remote = Struct.new(:host, :port) #:nodoc:
20
+
21
+ # Instantiates a new Forward service instance atop the given connection
22
+ # service session. This will register new channel open handlers to handle
23
+ # the specialized channels that the SSH port forwarding protocols employ.
24
+ def initialize(session)
25
+ @session = session
26
+ self.logger = session.logger
27
+ @remote_forwarded_ports = {}
28
+ @local_forwarded_ports = {}
29
+ @agent_forwarded = false
30
+
31
+ session.on_open_channel('forwarded-tcpip', &method(:forwarded_tcpip))
32
+ session.on_open_channel('auth-agent', &method(:auth_agent_channel))
33
+ session.on_open_channel('auth-agent@openssh.com', &method(:auth_agent_channel))
34
+ end
35
+
36
+ # Starts listening for connections on the local host, and forwards them
37
+ # to the specified remote host/port via the SSH connection. This method
38
+ # accepts either three or four arguments. When four arguments are given,
39
+ # they are:
40
+ #
41
+ # * the local address to bind to
42
+ # * the local port to listen on
43
+ # * the remote host to forward connections to
44
+ # * the port on the remote host to connect to
45
+ #
46
+ # If three arguments are given, it is as if the local bind address is
47
+ # "127.0.0.1", and the rest are applied as above.
48
+ #
49
+ # ssh.forward.local(1234, "www.capify.org", 80)
50
+ # ssh.forward.local("0.0.0.0", 1234, "www.capify.org", 80)
51
+ def local(*args)
52
+ if args.length < 3 || args.length > 4
53
+ raise ArgumentError, "expected 3 or 4 parameters, got #{args.length}"
54
+ end
55
+
56
+ bind_address = "127.0.0.1"
57
+ bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
58
+
59
+ local_port = args.shift.to_i
60
+ remote_host = args.shift
61
+ remote_port = args.shift.to_i
62
+
63
+ socket = TCPServer.new(bind_address, local_port)
64
+
65
+ @local_forwarded_ports[[local_port, bind_address]] = socket
66
+
67
+ session.listen_to(socket) do |server|
68
+ client = server.accept
69
+ debug { "received connection on #{bind_address}:#{local_port}" }
70
+
71
+ channel = session.open_channel("direct-tcpip", :string, remote_host, :long, remote_port, :string, bind_address, :long, local_port) do |achannel|
72
+ achannel.info { "direct channel established" }
73
+ end
74
+
75
+ prepare_client(client, channel, :local)
76
+
77
+ channel.on_open_failed do |ch, code, description|
78
+ channel.error { "could not establish direct channel: #{description} (#{code})" }
79
+ channel[:socket].close
80
+ end
81
+ end
82
+ end
83
+
84
+ # Terminates an active local forwarded port. If no such forwarded port
85
+ # exists, this will raise an exception. Otherwise, the forwarded connection
86
+ # is terminated.
87
+ #
88
+ # ssh.forward.cancel_local(1234)
89
+ # ssh.forward.cancel_local(1234, "0.0.0.0")
90
+ def cancel_local(port, bind_address="127.0.0.1")
91
+ socket = @local_forwarded_ports.delete([port, bind_address])
92
+ socket.shutdown rescue nil
93
+ socket.close rescue nil
94
+ session.stop_listening_to(socket)
95
+ end
96
+
97
+ # Returns a list of all active locally forwarded ports. The returned value
98
+ # is an array of arrays, where each element is a two-element tuple
99
+ # consisting of the local port and bind address corresponding to the
100
+ # forwarding port.
101
+ def active_locals
102
+ @local_forwarded_ports.keys
103
+ end
104
+
105
+ # Requests that all connections on the given remote-port be forwarded via
106
+ # the local host to the given port/host. The last argument describes the
107
+ # bind address on the remote host, and defaults to 127.0.0.1.
108
+ #
109
+ # This method will return immediately, but the port will not actually be
110
+ # forwarded immediately. If the remote server is not able to begin the
111
+ # listener for this request, an exception will be raised asynchronously.
112
+ #
113
+ # If you want to know when the connection is active, it will show up in the
114
+ # #active_remotes list. If you want to block until the port is active, you
115
+ # could do something like this:
116
+ #
117
+ # ssh.forward.remote(80, "www.google.com", 1234, "0.0.0.0")
118
+ # ssh.loop { !ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
119
+ def remote(port, host, remote_port, remote_host="127.0.0.1")
120
+ session.send_global_request("tcpip-forward", :string, remote_host, :long, remote_port) do |success, response|
121
+ if success
122
+ debug { "remote forward from remote #{remote_host}:#{remote_port} to #{host}:#{port} established" }
123
+ @remote_forwarded_ports[[remote_port, remote_host]] = Remote.new(host, port)
124
+ else
125
+ error { "remote forwarding request failed" }
126
+ raise Net::SSH::Exception, "remote forwarding request failed"
127
+ end
128
+ end
129
+ end
130
+
131
+ # an alias, for token backwards compatibility with the 1.x API
132
+ alias :remote_to :remote
133
+
134
+ # Requests that a remote forwarded port be cancelled. The remote forwarded
135
+ # port on the remote host, bound to the given address on the remote host,
136
+ # will be terminated, but not immediately. This method returns immediately
137
+ # after queueing the request to be sent to the server. If for some reason
138
+ # the port cannot be cancelled, an exception will be raised (asynchronously).
139
+ #
140
+ # If you want to know when the connection has been cancelled, it will no
141
+ # longer be present in the #active_remotes list. If you want to block until
142
+ # the port is no longer active, you could do something like this:
143
+ #
144
+ # ssh.forward.cancel_remote(1234, "0.0.0.0")
145
+ # ssh.loop { ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
146
+ def cancel_remote(port, host="127.0.0.1")
147
+ session.send_global_request("cancel-tcpip-forward", :string, host, :long, port) do |success, response|
148
+ if success
149
+ @remote_forwarded_ports.delete([port, host])
150
+ else
151
+ raise Net::SSH::Exception, "could not cancel remote forward request on #{host}:#{port}"
152
+ end
153
+ end
154
+ end
155
+
156
+ # Returns all active forwarded remote ports. The returned value is an
157
+ # array of two-element tuples, where the first element is the port on the
158
+ # remote host and the second is the bind address.
159
+ def active_remotes
160
+ @remote_forwarded_ports.keys
161
+ end
162
+
163
+ # Enables SSH agent forwarding on the given channel. The forwarded agent
164
+ # will remain active even after the channel closes--the channel is only
165
+ # used as the transport for enabling the forwarded connection. You should
166
+ # never need to call this directly--it is called automatically the first
167
+ # time a session channel is opened, when the connection was created with
168
+ # :forward_agent set to true:
169
+ #
170
+ # Net::SSH.start("remote.host", "me", :forwrd_agent => true) do |ssh|
171
+ # ssh.open_channel do |ch|
172
+ # # agent will be automatically forwarded by this point
173
+ # end
174
+ # ssh.loop
175
+ # end
176
+ def agent(channel)
177
+ return if @agent_forwarded
178
+ @agent_forwarded = true
179
+
180
+ channel.send_channel_request("auth-agent-req@openssh.com") do |achannel, success|
181
+ if success
182
+ debug { "authentication agent forwarding is active" }
183
+ else
184
+ achannel.send_channel_request("auth-agent-req") do |a2channel, success2|
185
+ if success2
186
+ debug { "authentication agent forwarding is active" }
187
+ else
188
+ error { "could not establish forwarding of authentication agent" }
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ private
196
+
197
+ # Perform setup operations that are common to all forwarded channels.
198
+ # +client+ is a socket, +channel+ is the channel that was just created,
199
+ # and +type+ is an arbitrary string describing the type of the channel.
200
+ def prepare_client(client, channel, type)
201
+ client.extend(Net::SSH::BufferedIo)
202
+ client.logger = logger
203
+
204
+ session.listen_to(client)
205
+ channel[:socket] = client
206
+
207
+ channel.on_data do |ch, data|
208
+ ch[:socket].enqueue(data)
209
+ end
210
+
211
+ channel.on_close do |ch|
212
+ debug { "closing #{type} forwarded channel" }
213
+ ch[:socket].close if !client.closed?
214
+ session.stop_listening_to(ch[:socket])
215
+ end
216
+
217
+ channel.on_process do |ch|
218
+ if ch[:socket].closed?
219
+ ch.info { "#{type} forwarded connection closed" }
220
+ ch.close
221
+ elsif ch[:socket].available > 0
222
+ data = ch[:socket].read_available(8192)
223
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
224
+ ch.send_data(data)
225
+ end
226
+ end
227
+ end
228
+
229
+ # The callback used when a new "forwarded-tcpip" channel is requested
230
+ # by the server. This will open a new socket to the host/port specified
231
+ # when the forwarded connection was first requested.
232
+ def forwarded_tcpip(session, channel, packet)
233
+ connected_address = packet.read_string
234
+ connected_port = packet.read_long
235
+ originator_address = packet.read_string
236
+ originator_port = packet.read_long
237
+
238
+ remote = @remote_forwarded_ports[[connected_port, connected_address]]
239
+
240
+ if remote.nil?
241
+ raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
242
+ end
243
+
244
+ client = TCPSocket.new(remote.host, remote.port)
245
+ info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
246
+
247
+ prepare_client(client, channel, :remote)
248
+ rescue SocketError => err
249
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to remote host (#{remote.host}:#{remote.port}): #{err.message}")
250
+ end
251
+
252
+ # The callback used when an auth-agent channel is requested by the server.
253
+ def auth_agent_channel(session, channel, packet)
254
+ info { "opening auth-agent channel" }
255
+ channel[:invisible] = true
256
+
257
+ begin
258
+ agent = Authentication::Agent.connect(logger)
259
+ prepare_client(agent.socket, channel, :agent)
260
+ rescue Exception => e
261
+ error { "attempted to connect to agent but failed: #{e.class.name} (#{e.message})" }
262
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to authentication agent")
263
+ end
264
+ end
265
+ end
266
+
267
+ end; end; end