diff --git a/examples/sftp.cr b/examples/sftp.cr index b91f1b5..40e9db7 100644 --- a/examples/sftp.cr +++ b/examples/sftp.cr @@ -3,8 +3,8 @@ require "../src/ssh2" SSH2::Session.open("localhost", 2222) do |session| session.login_with_pubkey("root", "./spec/keys/id_rsa") session.sftp_session do |sftp| - sftp.open_dir(".").ll do |fn| - puts fn + sftp.open_dir(".").ll do |file_name| + puts file_name end file = sftp.open(".bashrc") puts file.read diff --git a/examples/shell.cr b/examples/shell.cr index 88792f8..ffecefc 100644 --- a/examples/shell.cr +++ b/examples/shell.cr @@ -2,24 +2,24 @@ require "../src/ssh2" SSH2::Session.open("my_server") do |session| session.login("username", "password") - session.open_session do |ch| - ch.request_pty("vt100") - ch.shell + session.open_session do |chan| + chan.request_pty("vt100") + chan.shell session.blocking = false buf_space = Bytes.new(1024) buf = buf_space.to_slice loop do - io = IO.select([STDIN, ch.socket]).first + io = IO.select([STDIN, chan.socket]).first if io == STDIN command = gets if command - ch.write(command.to_slice) + chan.write(command.to_slice) end - elsif io == ch.socket - len = ch.read(buf).to_i32 + elsif io == chan.socket + len = chan.read(buf).to_i32 print! String.new buf[0, len] - break if ch.eof? + break if chan.eof? end end end diff --git a/spec/ssh2_spec.cr b/spec/ssh2_spec.cr index 5609c5b..a7e4c2b 100644 --- a/spec/ssh2_spec.cr +++ b/spec/ssh2_spec.cr @@ -4,7 +4,7 @@ require "spec" SPEC_SSH_HOST = ENV["SPEC_SSH_HOST"]? || "localhost" SPEC_SSH_PORT = ENV["CI"]? ? 22 : 10022 -def connect_ssh +def connect_ssh(&) SSH2::Session.open(SPEC_SSH_HOST, SPEC_SSH_PORT) do |session| session.login("root", "somepassword") session.authenticated?.should be_true @@ -48,16 +48,16 @@ describe SSH2 do it "should be able to scp transfer file" do fn = "#{Time.utc.to_unix}.txt" connect_ssh do |session| - session.scp_send(fn, 0o0644, 12) do |ch| - ch.puts "hello world" + session.scp_send(fn, 0o0644, 12) do |chan| + chan.puts "hello world" end - session.open_session do |ch| - ch.command("ls -l") - ch.gets_to_end.includes?(fn).should be_true + session.open_session do |chan| + chan.command("ls -l") + chan.gets_to_end.includes?(fn).should be_true end - session.scp_recv(fn) do |ch, st| - buf = Slice(UInt8).new(st.st_size.to_i32) - ch.read(buf) + session.scp_recv(fn) do |chan, stat| + buf = Slice(UInt8).new(stat.st_size.to_i32) + chan.read(buf) String.new(buf).should eq("hello world\n") end end @@ -95,7 +95,7 @@ describe SSH2::KnownHosts do known_hosts.size.should eq(2) known_hosts.map(&.name).includes?(SPEC_SSH_HOST).should be_true known_hosts.write_file("known_hosts") - known_hosts.delete_if { |h| h.name == SPEC_SSH_HOST } + known_hosts.delete_if { |host| host.name == SPEC_SSH_HOST } known_hosts.size.should eq(1) end diff --git a/src/agent.cr b/src/agent.cr index 941fb5b..7dcfdbc 100644 --- a/src/agent.cr +++ b/src/agent.cr @@ -52,7 +52,7 @@ class SSH2::Agent raise SSH2Error.new "Failed to authenticate username #{username} with SSH agent" end - def each + def each(&) each_unsafe do |key| yield PublicKey.new( Slice.new(key.value.blob, key.value.blob_len), @@ -60,7 +60,7 @@ class SSH2::Agent end end - private def each_unsafe + private def each_unsafe(&) prev = Pointer(LibSSH2::AgentPublicKey).null until LibSSH2.agent_get_identity(self, out store, prev) == 1 yield store diff --git a/src/channel.cr b/src/channel.cr index 226461c..8d7bea5 100644 --- a/src/channel.cr +++ b/src/channel.cr @@ -13,7 +13,7 @@ class SSH2::Channel < IO end def socket - @session.socket.not_nil! + @session.socket.as(TCPSocket) end # Close an active data channel. In practice this means sending an diff --git a/src/known_hosts.cr b/src/known_hosts.cr index a842e3a..6db9b97 100644 --- a/src/known_hosts.cr +++ b/src/known_hosts.cr @@ -29,7 +29,7 @@ class SSH2::KnownHosts LibSSH2.knownhost_checkp(self, host, port, key, key.size.to_u64, typemask, out store) end - def delete_if + def delete_if(&) each_unsafe do |known_host| if yield conv_to_host(known_host) ret = LibSSH2.knownhost_del(self, known_host) @@ -53,7 +53,7 @@ class SSH2::KnownHosts check_error(ret) end - def each + def each(&) each_unsafe do |known_host| yield conv_to_host(known_host) end @@ -65,7 +65,7 @@ class SSH2::KnownHosts Host.new(name, key, known_host.value.typemask) end - private def each_unsafe + private def each_unsafe(&) prev = Pointer(LibSSH2::KnownHost).null until LibSSH2.knownhost_get(self, out store, prev) == 1 yield store diff --git a/src/session.cr b/src/session.cr index 1233846..1358852 100644 --- a/src/session.cr +++ b/src/session.cr @@ -23,7 +23,7 @@ class SSH2::Session new(socket) end - def self.open(host : String, port = 22) + def self.open(host : String, port = 22, &) TCPSocket.open(host, port) do |socket| session = new(socket) begin @@ -40,7 +40,7 @@ class SSH2::Session @socket.wait_writable if flags.outbound? end - def perform_nonblock + def perform_nonblock(&) loop do result = @request_lock.synchronize { yield } code = if result.is_a?(Tuple) @@ -57,7 +57,7 @@ class SSH2::Session end end - def nonblock_handle + def nonblock_handle(&) loop do result = @request_lock.synchronize { yield } handle = if result.is_a?(Tuple) @@ -142,10 +142,6 @@ class SSH2::Session @@callbacks_lock.synchronize { @@callbacks.delete object_id } end - private def password_cb(username : String, welcome : String) : String - @interactive_cb.not_nil!.call(username, welcome) - end - # Login with username using pub/priv key values def login_with_data(username : String, privkey : String, pubkey : String, passphrase : String? = nil) @socket.wait_writable @@ -217,10 +213,10 @@ class SSH2::Session # 64 end slice = Slice.new(handle, len) - String.build do |o| - slice.each_with_index do |b, idx| - o << b.to_s(16) - o << ":" unless idx == length - 1 + String.build do |outp| + slice.each_with_index do |byte, idx| + outp << byte.to_s(16) + outp << ":" unless idx == length - 1 end end end @@ -287,14 +283,26 @@ class SSH2::Session end # If set, libssh2 will not attempt to block SIGPIPEs but will let them trigger from the underlying socket layer. + @[Deprecated("Use `#enable_sigpipe=` instead")] + # ameba:disable Naming/AccessorMethodName def set_enable_sigpipe(value) + self.enable_sigpipe = value + end + + def enable_sigpipe=(value) perform_nonblock { LibSSH2.session_flag(self, LibSSH2::SessionFlag::SIGPIPE, value ? 1 : 0) } end # If set - before the connection negotiation is performed - libssh2 will try # to negotiate compression enabling for this connection. By default libssh2 # will not attempt to use compression. + @[Deprecated("Use `#enable_compression=` instead")] + # ameba:disable Naming/AccessorMethodName def set_enable_compression(value) + self.enable_compression = value + end + + def enable_compression=(value) perform_nonblock { LibSSH2.session_flag(self, LibSSH2::SessionFlag::COMPRESS, value ? 1 : 0) } end @@ -370,7 +378,7 @@ class SSH2::Session open_channel("session", LibSSH2::CHANNEL_WINDOW_DEFAULT, LibSSH2::CHANNEL_PACKET_DEFAULT, nil) end - def open_session + def open_session(&) channel = open_session begin yield channel @@ -388,7 +396,7 @@ class SSH2::Session Channel.new self, handle end - def direct_tcpip(host, port, source_host, source_port) + def direct_tcpip(host, port, source_host, source_port, &) channel = direct_tcpip(host, port, source_host, source_port) begin yield channel @@ -406,7 +414,7 @@ class SSH2::Session # Send a file to the remote host via SCP. # A new channel is passed to the block and closed afterwards. - def scp_send(path, mode, size, mtime = Time.utc.to_unix, atime = Time.utc.to_unix) + def scp_send(path, mode, size, mtime = Time.utc.to_unix, atime = Time.utc.to_unix, &) channel = scp_send(path, mode, size, mtime, atime) begin yield channel @@ -421,9 +429,9 @@ class SSH2::Session raise SSH2Error.new("Unable to get stat for '#{path}'") end scp_send(remote_path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64, - stat.st_mtim.tv_sec, stat.st_atim.tv_sec) do |ch| - File.open(path, "r") do |f| - IO.copy(f, ch) + stat.st_mtim.tv_sec, stat.st_atim.tv_sec) do |chan| + File.open(path, "r") do |file| + IO.copy(file, chan) end end end @@ -439,7 +447,7 @@ class SSH2::Session # Request a file from the remote host via SCP. # A new channel is passed to the block and closed afterwards. - def scp_recv(path) + def scp_recv(path, &) channel, stat = scp_recv(path) begin yield channel, stat @@ -450,11 +458,11 @@ class SSH2::Session # Download a file from the remote host via SCP to the local filesystem. def scp_recv_file(path, local_path = path) - File.open(local_path, "w") do |f| - scp_recv(path) do |ch, st| - buf = Slice(UInt8).new(st.st_size.to_i32) - ch.read(buf) - f.write(buf) + File.open(local_path, "w") do |file| + scp_recv(path) do |chan, stat| + buf = Slice(UInt8).new(stat.st_size.to_i32) + chan.read(buf) + file.write(buf) end end end @@ -466,7 +474,7 @@ class SSH2::Session SFTP::Session.new(self, handle) end - def sftp_session + def sftp_session(&) sftp = sftp_session begin yield sftp diff --git a/src/sftp/dir.cr b/src/sftp/dir.cr index 941e024..37716ab 100644 --- a/src/sftp/dir.cr +++ b/src/sftp/dir.cr @@ -5,7 +5,7 @@ module SSH2::SFTP include Node # Lists a current directory - def ls + def ls(&) loop do buf_space = uninitialized UInt8[512] buf = buf_space.to_slice @@ -18,11 +18,11 @@ module SSH2::SFTP # Lists a current directory def ls ret = [] of String - ls { |fn| ret << fn } + ls { |file_name| ret << file_name } ret end - def ll + def ll(&) loop do buf_space = uninitialized UInt8[512] buf = buf_space.to_slice @@ -42,7 +42,7 @@ module SSH2::SFTP def ll ret = [] of String - ll { |fn| ret << fn } + ll { |file_name| ret << file_name } ret end end