tmatilai / vagrant-proxyconf

Vagrant plugin that configures the virtual machine to use proxies
MIT License
531 stars 74 forks source link

Auto-detection capibility #112

Closed estahn closed 9 years ago

estahn commented 9 years ago

Hi @tmatilai,

Would it be possible to have some sort of proxy auto-detection added?

Example:

  if Vagrant.has_plugin?("vagrant-proxyconf") && proxy_running?
    vagrant.proxy.autodetect = true
    vagrant.proxy.autodetect_port = 8123
  end

I'm using @fnicols snippet in my Vagrantfile: https://gist.github.com/fnichol/7551540

require 'socket'

# @return [String] public IP address of workstation used for egress traffic
def local_ip
  @local_ip ||= begin
                  # turn off reverse DNS resolution temporarily
    orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true

    UDPSocket.open do |s|
      s.connect '64.233.187.99', 1 # a google IP, does not hit network
      s.addr.last
    end
  ensure
    Socket.do_not_reverse_lookup = orig
  end
end

# @return [Integer] default polipo listening port
def local_port ; 8123 ; end

# @return [String] the polipo proxy URL
def http_proxy_url ; "http://#{local_ip}:#{local_port}" ; end

# @return [TrueClass,FalseClass] whether or not the polipo port is listening
def proxy_running?
  socket = TCPSocket.new(local_ip, local_port)
  true
rescue SocketError, Errno::ECONNREFUSED,
    Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
  false
rescue Errno::EPERM, Errno::ETIMEDOUT
  false
ensure
  socket && socket.close
end

Further down:

  if Vagrant.has_plugin?("vagrant-proxyconf") && proxy_running?
    vagrant.proxy.http     = http_proxy_url
    vagrant.proxy.https    = http_proxy_url
    vagrant.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
  end

Would be nice to have this in the plugin already and a cleaner Vagrantfile.

Enrico

estahn commented 9 years ago

Duplicate of #38