module VirtualboxWSL2::WSL2CompileForwardedPorts

Public Instance Methods

compile_forwarded_ports(config) click to toggle source

This method compiles the forwarded ports into {ForwardedPort} models.

# File lib/virtualbox_WSL2.rb, line 14
def compile_forwarded_ports(config)
  mappings = {}

  config.vm.networks.each do |type, options|
    if type == :forwarded_port
      guest_port = options[:guest]
      host_port  = options[:host]
      host_ip    = options[:host_ip]
      protocol   = options[:protocol] || "tcp"
      options    = scoped_hash_override(options, :virtualbox)
      id         = options[:id]

      # If the forwarded port was marked as disabled, ignore.
      next if options[:disabled]

      key = "#{host_ip}#{protocol}#{host_port}"
      mappings[key] =
        VagrantPlugins::ProviderVirtualBox::Model::ForwardedPort.new(id, host_port, guest_port, options)
    end
  end

  # Creating the second port forwarding entry for connections from WSL2 via Windows IP
  mappings.dup.each do |k, v|
    if Vagrant::Util::Platform.wsl? and k.start_with?("127.0.0.1tcp") and (v.id == "ssh")
      host_ip       = "0.0.0.0"
      host_port     = v.host_port
      guest_port    = v.guest_port
      protocol      = v.protocol
      id            = "ssh_wsl2"
      auto_correct  = v.auto_correct
      adapter       = v.adapter
      guest_ip      = v.guest_ip

      key = "#{host_ip}#{protocol}#{host_port}"
      mappings[key] =
        VagrantPlugins::ProviderVirtualBox::Model::ForwardedPort.new(id, host_port, guest_port, {:host_ip => host_ip, :protocol => protocol, :auto_correct => auto_correct, :adapter => adapter, :guest_ip => guest_ip})
    end
  end

  mappings.values
end