Skip to content

Commit

Permalink
Merge branch 'stable' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
zachriggle committed Jan 5, 2017
2 parents 7ed1187 + 54e7228 commit a2c67e5
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions pwnlib/adb/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ def __flat__(self):
class Connection(remote):
"""Connection to the ADB server"""
def __init__(self, host, port, level=None, *a, **kw):
# Try to make sure ADB is running if it's on the default host and port.
if host == context.defaults['adb_host'] \
and port == context.defaults['adb_port']:
with context.quiet:
process(context.adb + ['start-server']).recvall()

with context.quiet:
super(Connection, self).__init__(host, port, level=level, *a, **kw)
super(Connection, self).__init__(host, port, level=level, *a, **kw)

self._executable = None
self._argv = None
Expand Down Expand Up @@ -91,6 +84,20 @@ def __init__(self, level=None):
@property
def c(self):
"""Client's connection to the ADB server"""
if not self._c:
try:
self._c = Connection(self.host, self.port, level=self.level)
except Exception:
# If the connection fails, try starting a server on that port
# as long as it's the *default* port.
if self.host == context.defaults['adb_host'] \
and self.port == context.defaults['adb_port']:
log.warn("Could not connect to ADB server, trying to start it")
process(context.adb + ['start-server']).recvall()
else:
log.exception('Could not connect to ADB server')

# Final attempt...
if not self._c:
self._c = Connection(self.host, self.port, level=self.level)
return self._c
Expand Down Expand Up @@ -135,12 +142,23 @@ def recvl(self):

@_autoclose
def kill(self):
"""Kills the remote ADB server"""
"""Kills the remote ADB server"
>>> c=adb.protocol.Client()
>>> c.kill()
The server is automatically re-started on the next request,
if the default host/port are used.
>>> c.version() > (4,0)
True
"""
try:
self.send('host:kill')
except EOFError:
pass

@_autoclose
def version(self):
"""
Returns:
Expand Down

0 comments on commit a2c67e5

Please sign in to comment.