Skip to content

Commit fe20f4e

Browse files
Patrick MeenanPatrick Meenan
Patrick Meenan
authored and
Patrick Meenan
committed
Use the hosts file on Android for DNS overrides
1 parent 7e84375 commit fe20f4e

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

internal/adb.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ def start(self):
131131
if out is not None:
132132
ret = True
133133
# Set the CPU affinity for adb which helps avoid hangs
134-
for proc in psutil.process_iter():
135-
if proc.name() == "adb.exe" or proc.name() == "adb" or proc.name() == "adb-arm":
136-
proc.cpu_affinity([0])
134+
if platform.system() != "Darwin":
135+
for proc in psutil.process_iter():
136+
if proc.name() == "adb.exe" or proc.name() == "adb" or proc.name() == "adb-arm":
137+
proc.cpu_affinity([0])
137138
# install the tun0 device if necessary
138139
if (self.options.vpntether or self.options.vpntether2) and platform.system() == "Linux":
139140
self.sudo(['ip', 'tuntap', 'add', 'dev', 'tun0', 'mode', 'tun'])

internal/android_browser.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(self, adb, options, job, config):
5656
self.video_enabled = bool(job['video'])
5757
self.tcpdump_enabled = bool('tcpdump' in job and job['tcpdump'])
5858
self.tcpdump_file = None
59+
self.hosts_backup = '/data/local/tmp/hosts.bak'
5960
if self.config['type'] == 'blackbox':
6061
self.tcpdump_enabled = True
6162
self.video_enabled = True
@@ -119,6 +120,47 @@ def prepare(self, job, task):
119120
pass
120121
# kill any running instances
121122
self.adb.shell(['am', 'force-stop', self.config['package']])
123+
# Modify the hosts file for non-Chrome browsers
124+
self.restore_hosts()
125+
if 'dns_override' in task:
126+
self.modify_hosts(task, task['dns_override'])
127+
self.profile_end('desktop.prepare')
128+
129+
def modify_hosts(self, task, hosts):
130+
"""Add entries to the system's hosts file"""
131+
hosts_tmp = os.path.join(task['dir'], "hosts.wpt")
132+
hosts_remote_tmp = '/data/local/tmp/hosts.wpt'
133+
hosts_file = '/etc/hosts'
134+
if len(hosts):
135+
logging.debug('Modifying hosts file:')
136+
# Make sure the system files are writable
137+
self.adb.su('mount -o rw,remount /system')
138+
try:
139+
hosts_text = self.adb.su('cat {}'.format(hosts_file))
140+
if hosts_text is not None:
141+
hosts_text += "\n"
142+
for pair in hosts:
143+
hosts_text += "{0} {1}\n".format(pair[1], pair[0])
144+
with open(hosts_tmp, 'w') as f_out:
145+
f_out.write(hosts_text)
146+
self.adb.adb(['push', hosts_tmp, hosts_remote_tmp])
147+
self.adb.su('cp {} {}'.format(hosts_remote_tmp, hosts_file))
148+
self.adb.su('rm {}'.format(hosts_remote_tmp))
149+
self.adb.su('chown root:root {}'.format(hosts_file))
150+
self.adb.su('chmod 644 {}'.format(hosts_file))
151+
os.unlink(hosts_tmp)
152+
logging.debug(hosts_text)
153+
except Exception as err:
154+
logging.exception("Exception modifying hosts file: %s", err.__str__())
155+
156+
def restore_hosts(self):
157+
"""See if we have a backup hosts file to restore"""
158+
self.adb.su('cp {} {}'.format(self.hosts_backup, '/etc/hosts'))
159+
self.adb.su('rm {}'.format(self.hosts_backup))
160+
161+
def stop(self, job, task):
162+
""" Post-test cleanup """
163+
self.restore_hosts()
122164

123165
def stop_all_browsers(self):
124166
"""Kill all instances of known browsers"""

internal/blackbox_android.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def stop(self, job, task):
155155
self.adb.shell(['am', 'force-stop', self.config['package']])
156156
self.adb.shell(['rm', '/data/local/tmp/chrome-command-line'])
157157
self.adb.su('rm /data/local/chrome-command-line')
158+
AndroidBrowser.stop(self, job, task)
158159

159160
def on_stop_capture(self, task):
160161
"""Do any quick work to stop things that are capturing data"""

internal/chrome_android.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def stop(self, job, task):
292292
self.adb.shell(['am', 'force-stop', self.config['package']])
293293
self.adb.shell(['rm', '/data/local/tmp/' + self.config['command_line_file']])
294294
self.adb.su('rm /data/local/' + self.config['command_line_file'])
295+
AndroidBrowser.stop(self, job, task)
295296
# grab the netlog if there was one
296297
if 'netlog' in job and job['netlog']:
297298
netlog_file = os.path.join(task['dir'], task['prefix']) + '_netlog.txt'

0 commit comments

Comments
 (0)