OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import logging | 4 import logging |
5 import os | 5 import os |
6 import re | 6 import re |
7 import socket | 7 import socket |
8 import struct | 8 import struct |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if not line.startswith(' '): | 106 if not line.startswith(' '): |
107 interface_name = line.split()[1].strip(':') | 107 interface_name = line.split()[1].strip(':') |
108 elif ether_address in line: | 108 elif ether_address in line: |
109 return interface_name | 109 return interface_name |
110 | 110 |
111 def _WriteProtectedFile(self, path, contents): | 111 def _WriteProtectedFile(self, path, contents): |
112 subprocess.check_call( | 112 subprocess.check_call( |
113 ['sudo', 'bash', '-c', 'echo -e "%s" > %s' % (contents, path)]) | 113 ['sudo', 'bash', '-c', 'echo -e "%s" > %s' % (contents, path)]) |
114 | 114 |
115 def _DisableRndis(self): | 115 def _DisableRndis(self): |
116 self._adb.RunShellCommand('setprop sys.usb.config adb') | 116 self._adb.system_properties['sys.usb.config'] = 'adb' |
117 self._WaitForDevice() | 117 self._WaitForDevice() |
118 | 118 |
119 def _EnableRndis(self): | 119 def _EnableRndis(self): |
120 """Enables the RNDIS network interface.""" | 120 """Enables the RNDIS network interface.""" |
121 script_prefix = '/data/local/tmp/rndis' | 121 script_prefix = '/data/local/tmp/rndis' |
122 # This could be accomplished via "svc usb setFunction rndis" but only on | 122 # This could be accomplished via "svc usb setFunction rndis" but only on |
123 # devices which have the "USB tethering" feature. | 123 # devices which have the "USB tethering" feature. |
124 # Also, on some devices, it's necessary to go through "none" function. | 124 # Also, on some devices, it's necessary to go through "none" function. |
125 script = """ | 125 script = """ |
126 trap '' HUP | 126 trap '' HUP |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 force = True | 319 force = True |
320 raise Exception('No connectivity, giving up.') | 320 raise Exception('No connectivity, giving up.') |
321 | 321 |
322 def _GetCurrentDns(self): | 322 def _GetCurrentDns(self): |
323 """Returns current gateway, dns1, and dns2.""" | 323 """Returns current gateway, dns1, and dns2.""" |
324 routes = self._adb.RunShellCommand('cat /proc/net/route')[1:] | 324 routes = self._adb.RunShellCommand('cat /proc/net/route')[1:] |
325 routes = [route.split() for route in routes] | 325 routes = [route.split() for route in routes] |
326 default_routes = [route[0] for route in routes if route[1] == '00000000'] | 326 default_routes = [route[0] for route in routes if route[1] == '00000000'] |
327 return ( | 327 return ( |
328 default_routes[0] if default_routes else None, | 328 default_routes[0] if default_routes else None, |
329 self._adb.RunShellCommand('getprop net.dns1')[0], | 329 self._adb.system_properties['net.dns1'], |
330 self._adb.RunShellCommand('getprop net.dns2')[0], | 330 self._adb.system_properties['net.dns2'], |
331 ) | 331 ) |
332 | 332 |
333 def _OverrideDns(self, iface, dns1, dns2): | 333 def _OverrideDns(self, iface, dns1, dns2): |
334 """Overrides device's DNS configuration. | 334 """Overrides device's DNS configuration. |
335 | 335 |
336 Args: | 336 Args: |
337 iface: name of the network interface to make default | 337 iface: name of the network interface to make default |
338 dns1, dns2: nameserver IP addresses | 338 dns1, dns2: nameserver IP addresses |
339 """ | 339 """ |
340 if not iface: | 340 if not iface: |
341 return # If there is no route, then nobody cares about DNS. | 341 return # If there is no route, then nobody cares about DNS. |
342 # DNS proxy in older versions of Android is configured via properties. | 342 # DNS proxy in older versions of Android is configured via properties. |
343 # TODO(szym): run via su -c if necessary. | 343 # TODO(szym): run via su -c if necessary. |
344 self._adb.RunShellCommand('setprop net.dns1 ' + dns1) | 344 self._adb.system_properties['net.dns1'] = dns1 |
345 self._adb.RunShellCommand('setprop net.dns2 ' + dns2) | 345 self._adb.system_properties['net.dns2'] = dns2 |
346 dnschange = self._adb.RunShellCommand('getprop net.dnschange')[0] | 346 dnschange = self._adb.system_properties['net.dnschange'] |
347 if dnschange: | 347 if dnschange: |
348 self._adb.RunShellCommand('setprop net.dnschange %s' % | 348 self._adb.system_properties['net.dnschange'] = int(dnschange) + 1 |
349 (int(dnschange) + 1)) | |
350 # Since commit 8b47b3601f82f299bb8c135af0639b72b67230e6 to frameworks/base | 349 # Since commit 8b47b3601f82f299bb8c135af0639b72b67230e6 to frameworks/base |
351 # the net.dns1 properties have been replaced with explicit commands for netd | 350 # the net.dns1 properties have been replaced with explicit commands for netd |
352 self._adb.RunShellCommand('ndc netd resolver setifdns %s %s %s' % | 351 self._adb.RunShellCommand('ndc netd resolver setifdns %s %s %s' % |
353 (iface, dns1, dns2)) | 352 (iface, dns1, dns2)) |
354 # TODO(szym): if we know the package UID, we could setifaceforuidrange | 353 # TODO(szym): if we know the package UID, we could setifaceforuidrange |
355 self._adb.RunShellCommand('ndc netd resolver setdefaultif %s' % iface) | 354 self._adb.RunShellCommand('ndc netd resolver setdefaultif %s' % iface) |
356 | 355 |
357 @property | 356 @property |
358 def host_ip(self): | 357 def host_ip(self): |
359 return self._host_ip | 358 return self._host_ip |
360 | 359 |
361 @property | 360 @property |
362 def url(self): | 361 def url(self): |
363 # localhost and domains which resolve on the host's private network will not | 362 # localhost and domains which resolve on the host's private network will not |
364 # be resolved by the DNS proxy to the HTTP proxy. | 363 # be resolved by the DNS proxy to the HTTP proxy. |
365 return 'http://%s:%d' % (self._host_ip, self._host_port) | 364 return 'http://%s:%d' % (self._host_ip, self._host_port) |
366 | 365 |
367 def Close(self): | 366 def Close(self): |
368 self._OverrideDns(*self._original_dns) | 367 self._OverrideDns(*self._original_dns) |
369 self._DisableRndis() | 368 self._DisableRndis() |
OLD | NEW |