diff --git a/device.py b/device.py index 7a2416f..d61928f 100644 --- a/device.py +++ b/device.py @@ -13,6 +13,7 @@ from device_names import device_names from freeport import freeport from utils import current_ip +from utils import current_ip_network STATUS_INIT = "init" @@ -33,6 +34,7 @@ def __init__(self, serial: str, callback=nop_callback): self._serial = serial self._procs = [] self._current_ip = current_ip() + self._current_ip_network = current_ip_network() self._device = adbclient.device(serial) self._callback = callback @@ -150,6 +152,7 @@ def port2addr(port): "atxAgentAddress": port2addr(self._atx_proxy_port), "remoteConnectAddress": port2addr(self._adb_remote_port), "whatsInputAddress": port2addr(self._whatsinput_port), + "remoteConnectAddressNetwork": self._current_ip_network } def adb_call(self, *args): diff --git a/requirements.txt b/requirements.txt index 352063a..a5e909a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ adbutils>=0.3.4,<0.4 tornado requests apkutils-patch>=0.5.4,<0.6 -# uiautomator2==0.2.* \ No newline at end of file +# uiautomator2==0.2.* +netifaces \ No newline at end of file diff --git a/utils.py b/utils.py index 3f1f759..081b1a5 100644 --- a/utils.py +++ b/utils.py @@ -6,7 +6,7 @@ import re import socket import string - +import netifaces def current_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -15,6 +15,28 @@ def current_ip(): s.close() return ip +def current_ip_network(): + def calc_network(netmask): + result = "" + for num in netmask.split('.'): + temp = str(bin(int(num)))[2:] + result += temp + return str(len([n for n in result if n != '0'])) + + machine_nick_name = netifaces.gateways()['default'][netifaces.AF_INET][1] + match_info = [netifaces.ifaddresses(interface)[netifaces.AF_INET] for interface in netifaces.interfaces() if interface == machine_nick_name] + ip = current_ip() + ip_and_network = ip + '/' + 'unknow' + # normally, if will match + if match_info: + try: + # addr = match_info[0][0]['addr'] + netmask = match_info[0][0]['netmask'] + net = calc_network(netmask) + ip_and_network = ip + '/' + net + except Exception as e: + pass + return ip_and_network def update_recursive(d: dict, u: dict) -> dict: for k, v in u.items():