diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 9e9f13f4..2f568b5c 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -70,6 +70,8 @@ from .util import ( expand_osenv_noop, expand_osenv_s, has_resource, + list_ips, + list_nics, load_resource, lprint, min_ex, @@ -634,6 +636,9 @@ def get_sects(): \033[32m-i fd:\033[33m3\033[0m uses the socket passed to copyparty on file descriptor 3 \033[33m-p\033[0m (tcp ports) is ignored for unix-sockets and FDs + + \033[33m--list-nics\033[0m shows all network adapters (also offline ones); + \033[33m--list-ips\033[0m shows all LAN IPs """ ), ], @@ -1358,6 +1363,8 @@ def add_network(ap): ap2.add_argument("--s-wr-slp", metavar="SEC", type=float, default=0.0, help="debug: socket write delay in seconds") ap2.add_argument("--rsp-slp", metavar="SEC", type=float, default=0.0, help="debug: response delay in seconds") ap2.add_argument("--rsp-jtr", metavar="SEC", type=float, default=0.0, help="debug: response delay, random duration 0..\033[33mSEC\033[0m") + ap2.add_argument("--list-nics", action="store_true", help="debug: list detected network adapters") + ap2.add_argument("--list-ips", action="store_true", help="debug: list detected LAN IPs") def add_tls(ap, cert_path): @@ -2124,6 +2131,14 @@ def main(argv: Optional[list[str]] = None) -> None: print("\n".join("%8s %s" % (k, v) for k, v in sorted(MIMES.items()))) sys.exit(0) + if "--list-ips" in argv: + print("\n".join(str(x) for x in sorted(list_ips()))) + sys.exit(0) + + if "--list-nics" in argv: + print("\n".join(str(x) for x in sorted(list_nics(True).items()))) + sys.exit(0) + if EXE: print("pybin: {}\n".format(pybin), end="") diff --git a/copyparty/tcpsrv.py b/copyparty/tcpsrv.py index 73fc7448..1518381d 100644 --- a/copyparty/tcpsrv.py +++ b/copyparty/tcpsrv.py @@ -23,6 +23,7 @@ from .util import ( atomic_move, chkcmd, get_adapters, + list_nics, min_ex, sunpack, termsize, @@ -461,23 +462,7 @@ class TcpSrv(object): def detect_interfaces(self, listen_ips: list[str]) -> dict[str, Netdev]: listen_ips = [x for x in listen_ips if not x.startswith(("unix:", "fd:"))] - nics = get_adapters(True) - eps: dict[str, Netdev] = {} - for nic in nics: - for nip in nic.ips: - ipa = nip.ip[0] if ":" in str(nip.ip) else nip.ip - sip = "{}/{}".format(ipa, nip.network_prefix) - nd = Netdev(sip, nic.index or 0, nic.nice_name, "") - eps[sip] = nd - try: - idx = socket.if_nametoindex(nd.name) - if idx and idx != nd.idx: - t = "netdev idx mismatch; ifaddr={} cpython={}" - self.log("tcpsrv", t.format(nd.idx, idx), 3) - nd.idx = idx - except: - pass - + eps = list_nics() netlist = str(sorted(eps.items())) if netlist == self.netlist and self.netdevs: return {} diff --git a/copyparty/util.py b/copyparty/util.py index 6138aeae..c4eb0766 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -3176,6 +3176,31 @@ def list_ips() -> list[str]: return list(ret) +def list_nics(alll: bool = False) -> dict[str, Netdev]: + nics = get_adapters(alll) + eps: dict[str, Netdev] = {} + for nic in nics: + name = nic.nice_name + try: + idx = socket.if_nametoindex(name) + if idx and idx != nic.index: + LOG[0]("#", "nic-idx mismatch; ifaddr=%r libc=%r" % (nic.index, idx), 3) + except: + idx = nic.index + + for nip in nic.ips: + ipa = nip.ip[0] if ":" in str(nip.ip) else nip.ip + sip = "%s/%s" % (ipa, nip.network_prefix) + nd = Netdev(sip, idx or 0, name, "") + eps[sip] = nd + + if alll and not nic.ips: + zs = "no-ip-%s" % (idx,) + eps[zs] = Netdev(zs, idx or 0, name, "") + + return eps + + def build_netmap(csv: str, defer_mutex: bool = False): csv = csv.lower().strip()