Sha256: 2109eb1e5451001c89d582e63dd739d4b56a4ab8f6af0104449c80ed2f24268f

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Landrush
  module Action
    class RedirectDns
      def initialize(app, env)
        @app = app
      end

      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

      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

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
landrush-0.3.1 lib/landrush/action/redirect_dns.rb
landrush-0.3.0 lib/landrush/action/redirect_dns.rb