Coder Social home page Coder Social logo

Random adb ports in Android 11+ about adbutils HOT 5 OPEN

openatx avatar openatx commented on May 12, 2024
Random adb ports in Android 11+

from adbutils.

Comments (5)

codeskyblue avatar codeskyblue commented on May 12, 2024

from ENV? for example env:ANDROID_ADB_SERVER_PORT

from adbutils.

ReenigneArcher avatar ReenigneArcher commented on May 12, 2024

Hi, I'm not looking for the port of the adb server, but the adb port of the client device. Android 10 and below I believe always used 5555, but now it is random.

My adb server is Windows 10 but don't think that matters.

I believe the random port can be between 30000 and 60000, but not 100% sure.

Maybe a multi threaded port scan function could be added?

from adbutils.

ReenigneArcher avatar ReenigneArcher commented on May 12, 2024

I solved this by making a multi threaded port scanner and attempting to connect. This may not work right out of the box (I tried to copy the relevant parts out of my script)

import socket
from adbutils import adb

def myfunction(clientIP):
    global scannerIP
    scannerIP = clientIP
    
    adbRanges = [ [5555, 5585], [30000 , 50000] ] #https://www.reddit.com/r/tasker/comments/jbzeg5/adb_wifi_and_android_11_wireless_debugging/
    for adbRange in adbRanges:
        adbThreadedScan(adbRange)
        adbAddress = adbConnect(clientIP, adbPortsFound)
        if adbAddress != None:
            device = adb.device(serial=adbAddress)
            break


def adbConnect(clientIP, adbPortsFound):
    for adbPort in adbPortsFound:
        adbAddress = clientIP + ':' + str(adbPort)
        adbConnectOutput = adb.connect(adbAddress)
        message = adbConnectOutput.split(clientIP + ':' + str(adbPort), 1)[0].strip()
        if message == 'connected to' or message == 'already connected to':
            logging.info('adb connected on port: %s' % (adbPort))
            return adbAddress


def adbThreadedScan(adbRange):
    from threading import Thread
    from queue import Queue
    
    # number of threads, imported from dictionary of preferences
    rangeThreads = adbRange[-1] - adbRange[0]
    prefThreads = Prefs['int_PortScanThreads']
    if rangeThreads < prefThreads:
        N_THREADS = rangeThreads
    else:
        N_THREADS = prefThreads
    # thread queue
    global q
    q = Queue()
    
    global adbPortsFound
    adbPortsFound = []
    for t in range(N_THREADS):
        try:
            #for each thread, start it
            t = Thread(target=port_scan_thread)
            #when we set daemon to true, that thread will end when the main thread ends
            t.daemon = True
            #start the daemon thread
            t.start()
        except RuntimeError as e:
            break

    for port in range(adbRange[0], adbRange[-1]):
        if (port % 2) != 0: #if port is an odd number
            #for each port, put that port into the queue
            #to start scanning
            q.put(port)
    q.join() #wait for all ports to finish being scanned


def port_scan(host, port): #determine whether `host` has the `port` open
    try:
        # creates a new socket
        s = socket.socket()
        # tries to connect to host using that port
        s.connect((host, port))
        # make timeout if you want it a little faster ( less accuracy )
        # s.settimeout(0.2)
    except:
        # cannot connect, port is closed
        pass
    else:
        # the connection was established, port is open!
        adbPortsFound.append(port)
    finally:
        s.close()


def port_scan_thread():
    while True:
        # get the port number from the queue
        port = q.get()
        # scan that port number
        port_scan(scannerIP, port)
        # tells the queue that the scanning for that port 
        # is done
        q.task_done()

I will keep this open, but if the dev would like to close it that's fine. It would be nice to see the functionality added to this library.

from adbutils.

codeskyblue avatar codeskyblue commented on May 12, 2024

@ReenigneArcher https://github.com/openatx/adbutils/invitations

from adbutils.

ReenigneArcher avatar ReenigneArcher commented on May 12, 2024

I'm still struggling to find a good solution to this, as port scanning is slow. Honestly I do not understand why Android decided to go with the random ports and complicated pairing process.

from adbutils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.