Class: VagrantRubydns::Action::RedirectDns

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-rubydns/action/redirect_dns.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RedirectDns

Returns a new instance of RedirectDns.



4
5
6
# File 'lib/vagrant-rubydns/action/redirect_dns.rb', line 4

def initialize(app, env)
  @app = app
end

Instance Method Details

#_run_command(command) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/vagrant-rubydns/action/redirect_dns.rb', line 24

def _run_command(command)
  @machine.communicate.sudo(command) do |data, type|
    if [:stderr, :stdout].include?(type)
      color = (type == :stdout) ? :green : :red
      @machine.env.ui.info(data.chomp, :color => color, :prefix => false)
    end
  end
end

#call(env) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/vagrant-rubydns/action/redirect_dns.rb', line 8

def call(env)
  @machine = env[:machine]

  @machine.ui.info "setting up machine's DNS to point to our server"

  redirect_dns('10.0.2.3', 53, '10.0.2.2', 10053)
end

#redirect_dns(original_server, original_port, target_server, target_port) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/vagrant-rubydns/action/redirect_dns.rb', line 16

def redirect_dns(original_server, original_port, target_server, target_port)
  %w[tcp udp].each do |protocol|
    rule = "OUTPUT -t nat -d #{original_server} -p #{protocol} --dport #{original_port} -j DNAT --to-destination #{target_server}:#{target_port}"
    command = %Q(iptables -C #{rule} 2> /dev/null || iptables -A #{rule})
    _run_command(command)
  end
end