hrr_rb_ssh 0.4.0.pre1 → 0.4.0.pre2

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -8
  3. data/demo/client.rb +1 -3
  4. data/demo/echo_server.rb +1 -3
  5. data/demo/more_flexible_auth.rb +1 -3
  6. data/demo/multi_step_auth.rb +1 -3
  7. data/demo/server.rb +1 -3
  8. data/demo/subsystem_echo_server.rb +1 -3
  9. data/hrr_rb_ssh.gemspec +1 -1
  10. data/lib/hrr_rb_ssh/algorithm/publickey/ecdsa_sha2.rb +12 -9
  11. data/lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb +10 -7
  12. data/lib/hrr_rb_ssh/algorithm/publickey/ssh_rsa.rb +9 -6
  13. data/lib/hrr_rb_ssh/authentication/method/keyboard_interactive/context.rb +9 -8
  14. data/lib/hrr_rb_ssh/authentication/method/keyboard_interactive/info_request.rb +6 -5
  15. data/lib/hrr_rb_ssh/authentication/method/keyboard_interactive/info_response.rb +5 -2
  16. data/lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb +12 -10
  17. data/lib/hrr_rb_ssh/authentication/method/none/context.rb +7 -6
  18. data/lib/hrr_rb_ssh/authentication/method/none.rb +10 -8
  19. data/lib/hrr_rb_ssh/authentication/method/password/context.rb +7 -6
  20. data/lib/hrr_rb_ssh/authentication/method/password.rb +10 -8
  21. data/lib/hrr_rb_ssh/authentication/method/publickey/algorithm/functionable.rb +13 -11
  22. data/lib/hrr_rb_ssh/authentication/method/publickey/context.rb +5 -2
  23. data/lib/hrr_rb_ssh/authentication/method/publickey.rb +16 -14
  24. data/lib/hrr_rb_ssh/authentication.rb +28 -27
  25. data/lib/hrr_rb_ssh/client.rb +58 -56
  26. data/lib/hrr_rb_ssh/codable.rb +20 -10
  27. data/lib/hrr_rb_ssh/connection/channel/channel_type/direct_tcpip.rb +20 -18
  28. data/lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb +20 -18
  29. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/proc_chain/chain_context.rb +0 -3
  30. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/proc_chain.rb +0 -2
  31. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/env/context.rb +5 -4
  32. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/env.rb +2 -5
  33. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/exec/context.rb +5 -4
  34. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/exec.rb +2 -5
  35. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/pty_req/context.rb +5 -4
  36. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/pty_req.rb +2 -5
  37. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/shell/context.rb +5 -4
  38. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/shell.rb +2 -5
  39. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/subsystem/context.rb +5 -4
  40. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/subsystem.rb +2 -5
  41. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/window_change/context.rb +5 -4
  42. data/lib/hrr_rb_ssh/connection/channel/channel_type/session/request_type/window_change.rb +2 -5
  43. data/lib/hrr_rb_ssh/connection/channel/channel_type/session.rb +14 -12
  44. data/lib/hrr_rb_ssh/connection/channel.rb +73 -71
  45. data/lib/hrr_rb_ssh/connection/global_request_handler.rb +14 -12
  46. data/lib/hrr_rb_ssh/connection/request_handler/reference_env_request_handler.rb +0 -2
  47. data/lib/hrr_rb_ssh/connection/request_handler/reference_exec_request_handler.rb +4 -6
  48. data/lib/hrr_rb_ssh/connection/request_handler/reference_pty_req_request_handler.rb +10 -12
  49. data/lib/hrr_rb_ssh/connection/request_handler/reference_shell_request_handler.rb +4 -6
  50. data/lib/hrr_rb_ssh/connection/request_handler/reference_window_change_request_handler.rb +0 -2
  51. data/lib/hrr_rb_ssh/connection/request_handler.rb +1 -3
  52. data/lib/hrr_rb_ssh/connection.rb +53 -53
  53. data/lib/hrr_rb_ssh/loggable.rb +42 -0
  54. data/lib/hrr_rb_ssh/server.rb +11 -9
  55. data/lib/hrr_rb_ssh/transport/compression_algorithm/functionable.rb +5 -3
  56. data/lib/hrr_rb_ssh/transport/compression_algorithm/unfunctionable.rb +5 -3
  57. data/lib/hrr_rb_ssh/transport/encryption_algorithm/functionable.rb +5 -3
  58. data/lib/hrr_rb_ssh/transport/encryption_algorithm/unfunctionable.rb +5 -3
  59. data/lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb +9 -8
  60. data/lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group_exchange.rb +13 -12
  61. data/lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb +9 -8
  62. data/lib/hrr_rb_ssh/transport/mac_algorithm/functionable.rb +5 -3
  63. data/lib/hrr_rb_ssh/transport/mac_algorithm/unfunctionable.rb +5 -3
  64. data/lib/hrr_rb_ssh/transport/receiver.rb +8 -7
  65. data/lib/hrr_rb_ssh/transport/sender.rb +5 -3
  66. data/lib/hrr_rb_ssh/transport/sequence_number.rb +0 -4
  67. data/lib/hrr_rb_ssh/transport/server_host_key_algorithm/functionable.rb +5 -3
  68. data/lib/hrr_rb_ssh/transport.rb +41 -40
  69. data/lib/hrr_rb_ssh/version.rb +1 -1
  70. data/lib/hrr_rb_ssh.rb +0 -1
  71. metadata +6 -6
  72. data/lib/hrr_rb_ssh/logger.rb +0 -56
@@ -2,7 +2,6 @@
2
2
  # vim: et ts=2 sw=2
3
3
 
4
4
  require 'etc'
5
- require 'hrr_rb_ssh/logger'
6
5
  require 'hrr_rb_ssh/connection/request_handler'
7
6
 
8
7
  module HrrRbSsh
@@ -10,7 +9,6 @@ module HrrRbSsh
10
9
  class RequestHandler
11
10
  class ReferenceExecRequestHandler < RequestHandler
12
11
  def initialize
13
- @logger = Logger.new self.class.name
14
12
  @proc = Proc.new { |context|
15
13
  ptm = context.vars[:ptm]
16
14
  pts = context.vars[:pts]
@@ -58,22 +56,22 @@ module HrrRbSsh
58
56
 
59
57
  begin
60
58
  pid, status = Process.waitpid2 pid
61
- context.logger.info { "program exited with status #{status.inspect}" }
59
+ context.log_info { "program exited with status #{status.inspect}" }
62
60
  status.exitstatus
63
61
  ensure
64
62
  unless status
65
- context.logger.info { "exiting program" }
63
+ context.log_info { "exiting program" }
66
64
  Process.kill :TERM, pid
67
65
  begin
68
66
  Timeout.timeout(1) do
69
67
  pid, status = Process.waitpid2 pid
70
68
  end
71
69
  rescue Timeout::Error
72
- context.logger.warn { "force exiting program" }
70
+ context.log_warn { "force exiting program" }
73
71
  Process.kill :KILL, pid
74
72
  pid, status = Process.waitpid2 pid
75
73
  end
76
- context.logger.info { "program exited with status #{status.inspect}" }
74
+ context.log_info { "program exited with status #{status.inspect}" }
77
75
  end
78
76
  end
79
77
  end
@@ -5,7 +5,6 @@ require 'etc'
5
5
  require 'fileutils'
6
6
  require 'pty'
7
7
  require 'io/console'
8
- require 'hrr_rb_ssh/logger'
9
8
  require 'hrr_rb_ssh/connection/request_handler'
10
9
 
11
10
  module HrrRbSsh
@@ -13,7 +12,6 @@ module HrrRbSsh
13
12
  class RequestHandler
14
13
  class ReferencePtyReqRequestHandler < RequestHandler
15
14
  def initialize
16
- @logger = Logger.new self.class.name
17
15
  @proc = Proc.new { |context|
18
16
  begin
19
17
  ptm, pts = PTY.open
@@ -32,16 +30,16 @@ module HrrRbSsh
32
30
  begin
33
31
  context.io[1].write ptm.readpartial(10240)
34
32
  rescue EOFError => e
35
- context.logger.info { "ptm is EOF in ptm_read_thread" }
33
+ context.log_info { "ptm is EOF in ptm_read_thread" }
36
34
  break
37
35
  rescue IOError => e
38
- context.logger.warn { "IO Error in ptm_read_thread" }
36
+ context.log_warn { "IO Error in ptm_read_thread" }
39
37
  break
40
38
  rescue Errno::EIO => e
41
- context.logger.info { "EIO Error in ptm_read_thread" }
39
+ context.log_info { "EIO Error in ptm_read_thread" }
42
40
  break
43
41
  rescue => e
44
- context.logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
42
+ context.log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
45
43
  break
46
44
  end
47
45
  end
@@ -51,29 +49,29 @@ module HrrRbSsh
51
49
  begin
52
50
  ptm.write context.io[0].readpartial(10240)
53
51
  rescue EOFError => e
54
- context.logger.info { "IO is EOF in ptm_write_thread" }
52
+ context.log_info { "IO is EOF in ptm_write_thread" }
55
53
  break
56
54
  rescue IOError => e
57
- context.logger.warn { "IO Error in ptm_write_thread" }
55
+ context.log_warn { "IO Error in ptm_write_thread" }
58
56
  break
59
57
  rescue Errno::EIO => e
60
- context.logger.info { "EIO Error in ptm_read_thread" }
58
+ context.log_info { "EIO Error in ptm_read_thread" }
61
59
  break
62
60
  rescue => e
63
- context.logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
61
+ context.log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
64
62
  break
65
63
  end
66
64
  end
67
65
  }
68
66
  chain.call_next
69
67
  ensure
70
- context.logger.info { "closing pty-req request handler chain_proc" }
68
+ context.log_info { "closing pty-req request handler chain_proc" }
71
69
  context.vars[:ptm].close rescue nil
72
70
  context.vars[:pts].close rescue nil
73
71
  ptm_read_thread.join
74
72
  ptm_write_thread.exit
75
73
  ptm_write_thread.join
76
- context.logger.info { "pty-req request handler chain_proc closed" }
74
+ context.log_info { "pty-req request handler chain_proc closed" }
77
75
  end
78
76
  }
79
77
  rescue => e
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'etc'
5
5
  require 'timeout'
6
- require 'hrr_rb_ssh/logger'
7
6
  require 'hrr_rb_ssh/connection/request_handler'
8
7
 
9
8
  module HrrRbSsh
@@ -11,7 +10,6 @@ module HrrRbSsh
11
10
  class RequestHandler
12
11
  class ReferenceShellRequestHandler < RequestHandler
13
12
  def initialize
14
- @logger = Logger.new self.class.name
15
13
  @proc = Proc.new { |context|
16
14
  ptm = context.vars[:ptm]
17
15
  pts = context.vars[:pts]
@@ -49,22 +47,22 @@ module HrrRbSsh
49
47
 
50
48
  begin
51
49
  pid, status = Process.waitpid2 pid
52
- context.logger.info { "shell exited with status #{status.inspect}" }
50
+ context.log_info { "shell exited with status #{status.inspect}" }
53
51
  status.exitstatus
54
52
  ensure
55
53
  unless status
56
- context.logger.info { "exiting shell" }
54
+ context.log_info { "exiting shell" }
57
55
  Process.kill :TERM, pid
58
56
  begin
59
57
  Timeout.timeout(1) do
60
58
  pid, status = Process.waitpid2 pid
61
59
  end
62
60
  rescue Timeout::Error
63
- context.logger.warn { "force exiting shell" }
61
+ context.log_warn { "force exiting shell" }
64
62
  Process.kill :KILL, pid
65
63
  pid, status = Process.waitpid2 pid
66
64
  end
67
- context.logger.info { "shell exited with status #{status.inspect}" }
65
+ context.log_info { "shell exited with status #{status.inspect}" }
68
66
  end
69
67
  end
70
68
  }
@@ -2,7 +2,6 @@
2
2
  # vim: et ts=2 sw=2
3
3
 
4
4
  require 'io/console'
5
- require 'hrr_rb_ssh/logger'
6
5
  require 'hrr_rb_ssh/connection/request_handler'
7
6
 
8
7
  module HrrRbSsh
@@ -10,7 +9,6 @@ module HrrRbSsh
10
9
  class RequestHandler
11
10
  class ReferenceWindowChangeRequestHandler < RequestHandler
12
11
  def initialize
13
- @logger = Logger.new self.class.name
14
12
  @proc = Proc.new { |context|
15
13
  context.vars[:ptm].winsize = [context.terminal_height_rows, context.terminal_width_columns, context.terminal_width_pixels, context.terminal_height_pixels]
16
14
  }
@@ -1,15 +1,13 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
5
-
6
4
  module HrrRbSsh
7
5
  class Connection
8
6
  class RequestHandler
9
7
  def initialize &block
10
- @logger = Logger.new self.class.name
11
8
  @proc = block
12
9
  end
10
+
13
11
  def run context
14
12
  @proc.call context
15
13
  end
@@ -1,27 +1,27 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
4
+ require 'hrr_rb_ssh/loggable'
5
5
  require 'hrr_rb_ssh/error/closed_connection'
6
6
  require 'hrr_rb_ssh/connection/global_request_handler'
7
7
  require 'hrr_rb_ssh/connection/channel'
8
8
 
9
9
  module HrrRbSsh
10
10
  class Connection
11
+ include Loggable
12
+
11
13
  attr_reader \
12
14
  :username,
13
15
  :variables,
14
16
  :options,
15
17
  :mode
16
18
 
17
- def initialize authentication, mode, options={}
18
- @logger = Logger.new self.class.name
19
-
19
+ def initialize authentication, mode, options={}, logger: nil
20
+ self.logger = logger
20
21
  @authentication = authentication
21
22
  @mode = mode
22
23
  @options = options
23
-
24
- @global_request_handler = GlobalRequestHandler.new self
24
+ @global_request_handler = GlobalRequestHandler.new self, logger: logger
25
25
  @channels = Hash.new
26
26
  @username = nil
27
27
  @variables = nil
@@ -48,7 +48,7 @@ module HrrRbSsh
48
48
  end
49
49
 
50
50
  def start foreground: true
51
- @logger.info { "start connection" }
51
+ log_info { "start connection" }
52
52
  @authentication.start
53
53
  @closed = false
54
54
  @connection_loop_thread = connection_loop_thread
@@ -62,20 +62,20 @@ module HrrRbSsh
62
62
  end
63
63
 
64
64
  def close
65
- @logger.info { "closing connection" }
65
+ log_info { "closing connection" }
66
66
  @closed = true
67
67
  @authentication.close
68
68
  @channels.values.each do |channel|
69
69
  begin
70
70
  channel.close
71
71
  rescue => e
72
- @logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
72
+ log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
73
73
  end
74
74
  end
75
75
  @channels.clear
76
76
  @global_request_handler.close
77
77
  @connection_loop_thread.join unless @connection_loop_thread == Thread.current
78
- @logger.info { "connection closed" }
78
+ log_info { "connection closed" }
79
79
  end
80
80
 
81
81
  def closed?
@@ -83,14 +83,14 @@ module HrrRbSsh
83
83
  end
84
84
 
85
85
  def connection_loop_thread
86
- @logger.info { "start connection loop" }
86
+ log_info { "start connection loop" }
87
87
  Thread.new do
88
88
  begin
89
89
  while true
90
90
  begin
91
91
  payload = @authentication.receive
92
92
  rescue Error::ClosedAuthentication => e
93
- @logger.info { "authentication closed" }
93
+ log_info { "authentication closed" }
94
94
  break
95
95
  end
96
96
  @username ||= @authentication.username
@@ -115,22 +115,22 @@ module HrrRbSsh
115
115
  when Message::SSH_MSG_CHANNEL_CLOSE::VALUE
116
116
  channel_close payload
117
117
  else
118
- @logger.warn { "received unsupported message: id: #{payload[0,1].unpack("C")[0]}" }
118
+ log_warn { "received unsupported message: id: #{payload[0,1].unpack("C")[0]}" }
119
119
  end
120
120
  end
121
121
  rescue => e
122
- @logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
122
+ log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
123
123
  ensure
124
- @logger.info { "closing connection loop" }
124
+ log_info { "closing connection loop" }
125
125
  close
126
- @logger.info { "connection loop closed" }
126
+ log_info { "connection loop closed" }
127
127
  end
128
128
  end
129
129
  end
130
130
 
131
131
  def global_request payload
132
- @logger.info { 'received ' + Message::SSH_MSG_GLOBAL_REQUEST::ID }
133
- message = Message::SSH_MSG_GLOBAL_REQUEST.decode payload
132
+ log_info { 'received ' + Message::SSH_MSG_GLOBAL_REQUEST::ID }
133
+ message = Message::SSH_MSG_GLOBAL_REQUEST.decode payload, logger: logger
134
134
  begin
135
135
  @global_request_handler.request message
136
136
  rescue
@@ -145,10 +145,10 @@ module HrrRbSsh
145
145
  end
146
146
 
147
147
  def channel_open_start address, port, socket
148
- @logger.info { 'channel open start' }
149
- channel = Channel.new self, {:'channel type' => "forwarded-tcpip"}, socket
148
+ log_info { 'channel open start' }
149
+ channel = Channel.new self, {:'channel type' => "forwarded-tcpip"}, socket, logger: logger
150
150
  @channels[channel.local_channel] = channel
151
- @logger.info { 'channel opened' }
151
+ log_info { 'channel opened' }
152
152
  message = {
153
153
  :'message number' => Message::SSH_MSG_CHANNEL_OPEN::VALUE,
154
154
  :'channel type' => "forwarded-tcpip",
@@ -164,25 +164,25 @@ module HrrRbSsh
164
164
  end
165
165
 
166
166
  def channel_open payload
167
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_OPEN::ID }
168
- message = Message::SSH_MSG_CHANNEL_OPEN.decode payload
167
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_OPEN::ID }
168
+ message = Message::SSH_MSG_CHANNEL_OPEN.decode payload, logger: logger
169
169
  begin
170
- channel = Channel.new self, message
170
+ channel = Channel.new self, message, logger: logger
171
171
  @channels[channel.local_channel] = channel
172
172
  channel.start
173
173
  send_channel_open_confirmation channel
174
174
  rescue => e
175
- @logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
175
+ log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
176
176
  recipient_channel = message[:'sender channel']
177
177
  send_channel_open_failure recipient_channel, Message::SSH_MSG_CHANNEL_OPEN_FAILURE::ReasonCode::SSH_OPEN_CONNECT_FAILED, e.message
178
178
  end
179
179
  end
180
180
 
181
181
  def request_channel_open channel_type, channel_specific_message={}, wait_response=true
182
- @logger.info { 'request channel open' }
182
+ log_info { 'request channel open' }
183
183
  case channel_type
184
184
  when "session"
185
- channel = Channel.new self, {:'channel type' => channel_type}
185
+ channel = Channel.new self, {:'channel type' => channel_type}, logger: logger
186
186
  @channels[channel.local_channel] = channel
187
187
  end
188
188
  message = {
@@ -193,13 +193,13 @@ module HrrRbSsh
193
193
  :'maximum packet size' => channel.local_maximum_packet_size,
194
194
  }
195
195
  send_channel_open message.merge(channel_specific_message)
196
- @logger.info { 'sent channel open' }
196
+ log_info { 'sent channel open' }
197
197
  if wait_response
198
- @logger.info { 'wait response' }
198
+ log_info { 'wait response' }
199
199
  channel.wait_until_started
200
200
  end
201
201
  unless channel.closed?
202
- @logger.info { 'channel opened' }
202
+ log_info { 'channel opened' }
203
203
  channel
204
204
  else
205
205
  raise "Faild opening channel"
@@ -207,67 +207,67 @@ module HrrRbSsh
207
207
  end
208
208
 
209
209
  def channel_open_confirmation payload
210
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION::ID }
211
- message = Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION.decode payload
210
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION::ID }
211
+ message = Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION.decode payload, logger: logger
212
212
  channel = @channels[message[:'recipient channel']]
213
213
  channel.set_remote_parameters message
214
214
  channel.start
215
215
  end
216
216
 
217
217
  def channel_request payload
218
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_REQUEST::ID }
219
- message = Message::SSH_MSG_CHANNEL_REQUEST.decode payload
218
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_REQUEST::ID }
219
+ message = Message::SSH_MSG_CHANNEL_REQUEST.decode payload, logger: logger
220
220
  local_channel = message[:'recipient channel']
221
221
  @channels[local_channel].receive_message_queue.enq message
222
222
  end
223
223
 
224
224
  def channel_window_adjust payload
225
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_WINDOW_ADJUST::ID }
226
- message = Message::SSH_MSG_CHANNEL_WINDOW_ADJUST.decode payload
225
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_WINDOW_ADJUST::ID }
226
+ message = Message::SSH_MSG_CHANNEL_WINDOW_ADJUST.decode payload, logger: logger
227
227
  local_channel = message[:'recipient channel']
228
228
  @channels[local_channel].receive_message_queue.enq message
229
229
  end
230
230
 
231
231
  def channel_data payload
232
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_DATA::ID }
233
- message = Message::SSH_MSG_CHANNEL_DATA.decode payload
232
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_DATA::ID }
233
+ message = Message::SSH_MSG_CHANNEL_DATA.decode payload, logger: logger
234
234
  local_channel = message[:'recipient channel']
235
235
  @channels[local_channel].receive_message_queue.enq message
236
236
  end
237
237
 
238
238
  def channel_extended_data payload
239
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_EXTENDED_DATA::ID }
240
- message = Message::SSH_MSG_CHANNEL_EXTENDED_DATA.decode payload
239
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_EXTENDED_DATA::ID }
240
+ message = Message::SSH_MSG_CHANNEL_EXTENDED_DATA.decode payload, logger: logger
241
241
  local_channel = message[:'recipient channel']
242
242
  @channels[local_channel].receive_message_queue.enq message
243
243
  end
244
244
 
245
245
  def channel_eof payload
246
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_EOF::ID }
247
- message = Message::SSH_MSG_CHANNEL_EOF.decode payload
246
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_EOF::ID }
247
+ message = Message::SSH_MSG_CHANNEL_EOF.decode payload, logger: logger
248
248
  local_channel = message[:'recipient channel']
249
249
  @channels[local_channel].receive_message_queue.enq message
250
250
  end
251
251
 
252
252
  def channel_close payload
253
- @logger.info { 'received ' + Message::SSH_MSG_CHANNEL_CLOSE::ID }
254
- message = Message::SSH_MSG_CHANNEL_CLOSE.decode payload
253
+ log_info { 'received ' + Message::SSH_MSG_CHANNEL_CLOSE::ID }
254
+ message = Message::SSH_MSG_CHANNEL_CLOSE.decode payload, logger: logger
255
255
  local_channel = message[:'recipient channel']
256
256
  channel = @channels[local_channel]
257
257
  channel.close
258
- @logger.info { "wait until threads closed in channel" }
258
+ log_info { "wait until threads closed in channel" }
259
259
  channel.wait_until_closed
260
- @logger.info { "channel closed" }
261
- @logger.info { "deleting channel" }
260
+ log_info { "channel closed" }
261
+ log_info { "deleting channel" }
262
262
  @channels.delete local_channel
263
- @logger.info { "channel deleted" }
263
+ log_info { "channel deleted" }
264
264
  end
265
265
 
266
266
  def send_request_success
267
267
  message = {
268
268
  :'message number' => Message::SSH_MSG_REQUEST_SUCCESS::VALUE,
269
269
  }
270
- payload = Message::SSH_MSG_REQUEST_SUCCESS.encode message
270
+ payload = Message::SSH_MSG_REQUEST_SUCCESS.encode message, logger: logger
271
271
  @authentication.send payload
272
272
  end
273
273
 
@@ -275,12 +275,12 @@ module HrrRbSsh
275
275
  message = {
276
276
  :'message number' => Message::SSH_MSG_REQUEST_FAILURE::VALUE,
277
277
  }
278
- payload = Message::SSH_MSG_REQUEST_FAILURE.encode message
278
+ payload = Message::SSH_MSG_REQUEST_FAILURE.encode message, logger: logger
279
279
  @authentication.send payload
280
280
  end
281
281
 
282
282
  def send_channel_open message
283
- payload = Message::SSH_MSG_CHANNEL_OPEN.encode message
283
+ payload = Message::SSH_MSG_CHANNEL_OPEN.encode message, logger: logger
284
284
  @authentication.send payload
285
285
  end
286
286
 
@@ -293,7 +293,7 @@ module HrrRbSsh
293
293
  :'initial window size' => channel.local_window_size,
294
294
  :'maximum packet size' => channel.local_maximum_packet_size,
295
295
  }
296
- payload = Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION.encode message
296
+ payload = Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION.encode message, logger: logger
297
297
  @authentication.send payload
298
298
  end
299
299
 
@@ -305,7 +305,7 @@ module HrrRbSsh
305
305
  :'description' => description,
306
306
  :'language tag' => "",
307
307
  }
308
- payload = Message::SSH_MSG_CHANNEL_OPEN_FAILURE.encode message
308
+ payload = Message::SSH_MSG_CHANNEL_OPEN_FAILURE.encode message, logger: logger
309
309
  @authentication.send payload
310
310
  end
311
311
  end
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ # vim: et ts=2 sw=2
3
+
4
+ module HrrRbSsh
5
+ module Loggable
6
+ attr_accessor :logger, :log_key
7
+
8
+ def log_fatal
9
+ if logger
10
+ logger.fatal(log_key){ yield }
11
+ end
12
+ end
13
+
14
+ def log_error
15
+ if logger
16
+ logger.error(log_key){ yield }
17
+ end
18
+ end
19
+
20
+ def log_warn
21
+ if logger
22
+ logger.warn(log_key){ yield }
23
+ end
24
+ end
25
+
26
+ def log_info
27
+ if logger
28
+ logger.info(log_key){ yield }
29
+ end
30
+ end
31
+
32
+ def log_debug
33
+ if logger
34
+ logger.debug(log_key){ yield }
35
+ end
36
+ end
37
+
38
+ def log_key
39
+ @log_key ||= self.class.to_s + "[%x]" % object_id
40
+ end
41
+ end
42
+ end
@@ -1,28 +1,30 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
4
+ require 'hrr_rb_ssh/loggable'
5
5
  require 'hrr_rb_ssh/transport'
6
6
  require 'hrr_rb_ssh/authentication'
7
7
  require 'hrr_rb_ssh/connection'
8
8
 
9
9
  module HrrRbSsh
10
10
  class Server
11
- def self.start io, options={}
12
- server = self.new options
11
+ include Loggable
12
+
13
+ def self.start io, options={}, logger: nil
14
+ server = self.new options, logger: logger
13
15
  server.start io
14
16
  end
15
17
 
16
- def initialize options={}
17
- @logger = Logger.new self.class.name
18
+ def initialize options={}, logger: nil
19
+ self.logger = logger
18
20
  @options = options
19
21
  end
20
22
 
21
23
  def start io
22
- @logger.info { "start server service" }
23
- transport = Transport.new io, Mode::SERVER, @options
24
- authentication = Authentication.new transport, Mode::SERVER, @options
25
- connection = Connection.new authentication, Mode::SERVER, @options
24
+ log_info { "start server service" }
25
+ transport = Transport.new io, Mode::SERVER, @options, logger: logger
26
+ authentication = Authentication.new transport, Mode::SERVER, @options, logger: logger
27
+ connection = Connection.new authentication, Mode::SERVER, @options, logger: logger
26
28
  connection.start
27
29
  end
28
30
  end
@@ -2,14 +2,16 @@
2
2
  # vim: et ts=2 sw=2
3
3
 
4
4
  require 'zlib'
5
- require 'hrr_rb_ssh/logger'
5
+ require 'hrr_rb_ssh/loggable'
6
6
 
7
7
  module HrrRbSsh
8
8
  class Transport
9
9
  class CompressionAlgorithm
10
10
  module Functionable
11
- def initialize direction
12
- @logger = Logger.new(self.class.name)
11
+ include Loggable
12
+
13
+ def initialize direction, logger: nil
14
+ self.logger = logger
13
15
  case direction
14
16
  when Direction::OUTGOING
15
17
  @deflator = ::Zlib::Deflate.new
@@ -1,14 +1,16 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
4
+ require 'hrr_rb_ssh/loggable'
5
5
 
6
6
  module HrrRbSsh
7
7
  class Transport
8
8
  class CompressionAlgorithm
9
9
  module Unfunctionable
10
- def initialize direction=nil
11
- @logger = Logger.new(self.class.name)
10
+ include Loggable
11
+
12
+ def initialize direction=nil, logger: nil
13
+ self.logger = logger
12
14
  end
13
15
 
14
16
  def deflate data
@@ -1,20 +1,22 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
4
+ require 'hrr_rb_ssh/loggable'
5
5
 
6
6
  module HrrRbSsh
7
7
  class Transport
8
8
  class EncryptionAlgorithm
9
9
  module Functionable
10
+ include Loggable
11
+
10
12
  def self.included klass
11
13
  cipher = OpenSSL::Cipher.new(klass::CIPHER_NAME)
12
14
  klass.const_set(:IV_LENGTH, cipher.iv_len)
13
15
  klass.const_set(:KEY_LENGTH, cipher.key_len)
14
16
  end
15
17
 
16
- def initialize direction, iv, key
17
- @logger = Logger.new(self.class.name)
18
+ def initialize direction, iv, key, logger: nil
19
+ self.logger = logger
18
20
  @cipher = OpenSSL::Cipher.new(self.class::CIPHER_NAME)
19
21
  case direction
20
22
  when Direction::OUTGOING
@@ -1,19 +1,21 @@
1
1
  # coding: utf-8
2
2
  # vim: et ts=2 sw=2
3
3
 
4
- require 'hrr_rb_ssh/logger'
4
+ require 'hrr_rb_ssh/loggable'
5
5
 
6
6
  module HrrRbSsh
7
7
  class Transport
8
8
  class EncryptionAlgorithm
9
9
  module Unfunctionable
10
+ include Loggable
11
+
10
12
  def self.included klass
11
13
  klass.const_set(:IV_LENGTH, 0)
12
14
  klass.const_set(:KEY_LENGTH, 0)
13
15
  end
14
16
 
15
- def initialize direction=nil, iv=nil, key=nil
16
- @logger = Logger.new(self.class.name)
17
+ def initialize direction=nil, iv=nil, key=nil, logger: nil
18
+ self.logger = logger
17
19
  end
18
20
 
19
21
  def block_size