module VagrantPlugins::Share::Command::Ngrok::Share
Ngrok
share implementation
Constants
- GUEST_PROXY_PORT
-
Guest port within proxy
Public Instance Methods
Source
# File lib/vagrant-share/command/ngrok/share.rb, line 409 def configure_ssh_connect(machine, configuration, options) ssh_password = nil ssh_privkey = nil machine.ui.output(I18n.t("vagrant_share.generating_ssh_key")) if !options[:ssh_no_password] while !ssh_password ssh_password = machine.ui.ask( "#{I18n.t("vagrant_share.ssh_password_prompt")} ", echo: false) end while ssh_password.length < 4 machine.ui.warn( "#{I18n.t("vagrant_share.password_not_long_enough")}") ssh_password = machine.ui.ask( "#{I18n.t("vagrant_share.ssh_password_prompt")} ", echo: false) end confirm_password = nil while confirm_password != ssh_password confirm_password = machine.ui.ask( "#{I18n.t("vagrant_share.ssh_password_confirm_prompt")} ", echo: false) end else configuration["tunnels"]["ssh"] = { "proto" => "tcp", "addr" => ssh_port } end _, ssh_privkey, openssh_key = Helper.generate_keypair(ssh_password) machine.ui.detail(I18n.t("vagrant_share.inserting_ssh_key")) machine.guest.capability(:insert_public_key, openssh_key) [ssh_password, ssh_privkey] end
Configure settings for SSH connect
@param [Vagrant::Machine] machine Machine to share @param [Hash] configuration ngrok configuration hash @param [Hash] options CLI options @param [Array<String>] ssh password, ssh privkey
Source
# File lib/vagrant-share/command/ngrok/share.rb, line 279 def start_connect_info_watcher(share_info_output, ui, options) Thread.new do until((info = share_info_output.pop).nil?) begin case info when String ui.error(info) when Hash if info[:name] i_uri = URI.parse(info[:tcp]) if i_uri.host != DEFAULT_NGROK_TCP_ENDPOINT driver_name = i_uri.host else driver_name = "ngrok" end ui.success("") ui.success(I18n.t("vagrant_share.started", name: info[:name])) if options[:full_share] ui.success("") ui.success(I18n.t("vagrant_share.ngrok.started_full", name: info[:name], driver: driver_name)) end if options[:ssh] ui.success("") ui.success(I18n.t("vagrant_share.ngrok.started_ssh", name: info[:name], driver: driver_name)) end ui.success("") end if info[:http] ui.success("HTTP URL: #{info[:http]}") ui.success("") end if info[:https] ui.success("HTTPS URL: #{info[:https]}") ui.success("") end else @logger.warn("Unknown data type receied for output: #{e.class} - #{e}") end rescue => e @logger.error("Unexpected error processing connect information: #{e.class} - #{e}") end end end end
Start the share information watcher to print connect instructions
@param [Queue] share_info_output Queue to receive share information @param [Vagrant::UI] ui UI for output @param [Hash] options CLI options
Source
# File lib/vagrant-share/command/ngrok/share.rb, line 184 def start_ngrok_proxy(ui, configuration, share_info_output, options) ngrok_process = nil base_config = File.expand_path("~/.ngrok2/ngrok.yml") share_config = Tempfile.new("vagrant-share") share_config.write(configuration.to_yaml) share_config.close if !File.exists?(base_config) base_config = share_config.path end @logger.debug("Generated configuration for ngrok:\n#{configuration.to_yaml}") @logger.debug("Starting ngrok proxy process.") ngrok_process = Vagrant::Util::Subprocess.new( *["ngrok", "start", "--config", base_config, "--config", share_config.path, "--all", "--log", "stdout", "--log-format", "json", "--log-level", "debug"], notify: [:stdout] ) Thread.new do begin share_info = {} share_info_keys = [] share_info_keys.push(:http) if options[:http_port] share_info_keys.push(:https) if options[:https_port] share_info_keys.push(:name) if options[:ssh] || options[:full_share] ngrok_process.execute do |type, data| if type == :stdout data.split("\n").each do |line| begin info = JSON.parse(line) if info["msg"].to_s == "decoded response" begin r_info = info["resp"] if !r_info["Error"].to_s.empty? @logger.error("Error encountered with ngrok connection: #{r_info["Error"]}") share_info_output.push(r_info["Error"]) Process.kill("INT", Process.pid) end if r_info["URL"] && r_info["Proto"] share_info[:uri] = URI.parse(r_info["URL"]) case share_info[:uri].scheme when "http" share_info[:http] = share_info[:uri].to_s when "https" share_info[:https] = share_info[:uri].to_s when "tcp" connect_name = [share_info[:uri].port, options[:vagrant_api_port]].map do |item| Helper.wordify(item).join('_') end share_info[:tcp] = share_info[:uri].to_s share_info[:name] = connect_name.join(":") if share_info[:uri].host != DEFAULT_NGROK_TCP_ENDPOINT host_num = share_info[:uri].host.split(".").first host_num_word = Helper.wordify(host_num.to_i).join("_") share_info[:name] += "@#{host_num_word}" end else @logger.warn("Unhandled URI scheme detected: #{share_info[:uri].scheme} - `#{share_info[:uri]}`") share_info.delete(:uri) end end rescue => err @logger.warn("Failed to parse line: #{err}") end end if info["err"] && info["msg"] == "start tunnel listen" && info["err"] != "<nil>" @logger.error("Error encountered with ngrok connection: #{info["err"]}") share_info_output.push(info["err"]) # Force shutdown Process.kill("INT", Process.pid) end if share_info_keys.all?{|key| share_info.keys.include?(key)} share_info_output.push(share_info.dup) share_info = {} end rescue => e @logger.warn("Failure handling ngrok process output line: #{e.class} - #{e} (`#{line}`)") end end end end ensure share_config.unlink end end ngrok_process end
Start the ngrok proxy process
@param [Vagrant::UI] ui UI instance for output @param [Hash] configurations ngrok process configuration @param [Queue] share_info_output location to push share information @param [Hash] options CLI options
Source
# File lib/vagrant-share/command/ngrok/share.rb, line 487 def validate_ngrok_installation! begin Vagrant::Util::Subprocess.new("ngrok") rescue Vagrant::Errors::CommandUnavailable raise Errors::NgrokUnavailable end end
Check that ngrok is available on user’s PATH
Source
# File lib/vagrant-share/command/ngrok/share.rb, line 329 def validate_target_machine(machine, options) if !machine.ssh_info # We use this as a flag of whether or not the machine is # running. We can't share a machine that is not running. raise Errors::MachineNotReady end if options[:ssh] # Do some quick checks to make sure we can setup this # machine for SSH access from other users. begin if !machine.guest.capability?(:insert_public_key) raise Errors::SSHCantInsertKey, guest: machine.guest.name.to_s end rescue Vagrant::Errors::MachineGuestNotReady raise Errors::SSHNotReady end end target = nil if !machine.provider.capability?(:public_address) machine.ui.warn(I18n.t( "vagrant_share.provider_unsupported", provider: machine.provider_name.to_s, )) else target = machine.provider.capability(:public_address) end target end
Validate the target machine for the share
@param [Vagrant::Machine] machine @param [Hash] options @return [String, NilClass] public address