1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import android_commands
13 class FakeDns(object):
14 """Wrapper class for the fake_dns tool."""
15 _FAKE_DNS_PATH
= '/data/local/tmp/fake_dns'
17 def __init__(self
, adb
, build_type
):
20 adb: the AndroidCommands to use.
21 build_type: 'Release' or 'Debug'.
24 self
._build
_type
= build_type
26 self
._original
_dns
= None
28 def _PushAndStartFakeDns(self
):
29 """Starts the fake_dns server that replies all name queries 127.0.0.1.
32 subprocess instance connected to the fake_dns process on the device.
34 self
._adb
.PushIfNeeded(
35 os
.path
.join(constants
.CHROME_DIR
, 'out', self
._build
_type
, 'fake_dns'),
36 FakeDns
._FAKE
_DNS
_PATH
)
37 return subprocess
.Popen(
38 ['adb', '-s', self
._adb
._adb
.GetSerialNumber(),
39 'shell', '%s -D' % FakeDns
._FAKE
_DNS
_PATH
])
42 """Configures the system to point to a DNS server that replies 127.0.0.1.
44 This can be used in combination with the forwarder to forward all web
45 traffic to a replay server.
47 The TearDown() method will perform all cleanup.
49 self
._adb
.RunShellCommand('ip route add 8.8.8.0/24 via 127.0.0.1 dev lo')
50 self
._fake
_dns
= self
._PushAndStartFakeDns
()
51 self
._original
_dns
= self
._adb
.RunShellCommand('getprop net.dns1')[0]
52 self
._adb
.RunShellCommand('setprop net.dns1 127.0.0.1')
53 time
.sleep(2) # Time for server to start and the setprop to take effect.
56 """Shuts down the fake_dns."""
58 if not self
._original
_dns
or self
._original
_dns
== '127.0.0.1':
59 logging
.warning('Bad original DNS, falling back to Google DNS.')
60 self
._original
_dns
= '8.8.8.8'
61 self
._adb
.RunShellCommand('setprop net.dns1 %s' % self
._original
_dns
)
63 self
._adb
.RunShellCommand('ip route del 8.8.8.0/24 via 127.0.0.1 dev lo')