module VagrantPlugins::Share::Command::Ngrok::Connect
Public Instance Methods
Source
# File lib/vagrant-share/command/ngrok/connect.rb, line 119 def execute_connect(argv, options) opts = options.merge(retrap_int: true) connect_to_share(argv, opts) do |name, ui, ip, proxy_port, api_port| begin share = Helper.share_info("share-info", ip, api_port) rescue => e @logger.error("Failed to establish connection to share `#{name}`." \ "#{e.class}: #{e}") raise Errors::ShareNotFound, name: name end ui.output(I18n.t("vagrant_share.looking_up", name: name)) share["ports"] ||= [] if !share["ports"].empty? ui.detail(I18n.t("vagrant_share.connect_restricted_ports") + "\n ") share["ports"].each do |port| next if port.to_s == proxy_port.to_s ui.detail("Port: #{port}") end ui.detail(" ") end if share["has_private_key"] ui.detail(I18n.t("vagrant_share.connect_ssh_available")) ui.detail(" ") end # Let the user know we connected and how to connect ui.success(I18n.t("vagrant_share.started_connect")) ui.success(I18n.t( "vagrant_share.connect_socks_port", port: proxy_port.to_s)) ui.success(I18n.t("vagrant_share.connect_ip", ip: ip)) ui.success(" ") ui.success(I18n.t("vagrant_share.connect_info")) end end
Perform connection to share
@param [Array<String>] argv CLI arguments @param [Hash] options CLI options
Source
# File lib/vagrant-share/command/ngrok/connect.rb, line 160 def execute_ssh(argv, options) private_key_path = nil connect_to_share(argv, options) do |name, ui, ip, proxy_port, api_port| # Determine information about this share ui.output(I18n.t("vagrant_share.looking_up", name: name)) share = Helper.share_info("connect-ssh", ip, api_port) if !share["has_private_key"] raise Errors::SSHNotShared, name: name end if share["private_key_password"] ui.ask( I18n.t("vagrant_share.connect_password_required"), color: :yellow) end # Get the private key private_key = share["ssh_key"] if share["private_key_password"] while true password = nil while !password password = ui.ask( "#{I18n.t("vagrant_share.connect_password_prompt")} ", echo: false) end begin private_key = OpenSSL::PKey::RSA.new(private_key, password) private_key = private_key.to_pem password = nil break rescue OpenSSL::PKey::RSAError ui.error(I18n.t("vagrant_share.connect_invalid_password")) end end end private_key_file = Tempfile.new("vagrant-connect-key") private_key_path = Pathname.new(private_key_file.path) private_key_file.write(private_key) private_key_file.close # Nil out the private key so it can be GC'd private_key = nil # In 45 seconds, delete the private key file. 45 seconds # should be long enough for SSH to properly connect. key_thr = Thread.new do sleep 45 private_key_path.unlink rescue nil end # Configure SSH ssh_info = { host: ip, port: share["ssh_port"], username: share["ssh_username"], private_key_path: [private_key_path.to_s], } ui.output(I18n.t("vagrant_share.executing_ssh")) Vagrant::Util::SSH.check_key_permissions(private_key_path) Vagrant::Util::SSH.exec(ssh_info, subprocess: true) end ensure # If the private key is still around, delete it if private_key_path && private_key_path.file? private_key_path.unlink rescue nil end end
Perform SSH connection to share
@param [Array<String>] argv CLI arguments @param [Hash] options CLI options