diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index aa0afdcf5..96b4b13c9 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -194,7 +194,7 @@ The CLI is in `meshtastic/__main__.py`. When adding new CLI commands: ### Optional (extras) - `cli` extra: `pyqrcode`, `print-color`, `dotmap`, `argcomplete` -- `tunnel` extra: `pytap2` +- `tunnel` extra: `pytap3` - `analysis` extra: `dash`, `pandas` ## Important Notes diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index b26d0cb71..8fe61dbbf 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -1171,8 +1171,7 @@ def setSimpleConfig(modem_preset): if args.listen: closeNow = False - have_tunnel = platform.system() == "Linux" - if have_tunnel and args.tunnel: + if args.tunnel: if args.dest != BROADCAST_ADDR: print("A tunnel can only be created using the local node.") return @@ -1801,11 +1800,10 @@ def common(): # We assume client is fully connected now onConnected(client) - have_tunnel = platform.system() == "Linux" if ( args.noproto or args.reply - or (have_tunnel and args.tunnel) + or args.tunnel or args.listen ): # loop until someone presses ctrlc try: @@ -2592,22 +2590,20 @@ def initParser(): "--gpio-watch", help="Start watching a GPIO mask for changes (ex: '0x10')" ) - have_tunnel = platform.system() == "Linux" - if have_tunnel: - tunnelArgs = parser.add_argument_group( - "Tunnel", "Arguments related to establishing a tunnel device over the mesh." - ) - tunnelArgs.add_argument( - "--tunnel", - action="store_true", - help="Create a TUN tunnel device for forwarding IP packets over the mesh", - ) - tunnelArgs.add_argument( - "--subnet", - dest="tunnel_net", - help="Sets the local-end subnet address for the TUN IP bridge. (ex: 10.115' which is the default)", - default=None, - ) + tunnelArgs = parser.add_argument_group( + "Tunnel", "Arguments related to establishing a tunnel device over the mesh." + ) + tunnelArgs.add_argument( + "--tunnel", + action="store_true", + help="Create a TUN tunnel device for forwarding IP packets over the mesh", + ) + tunnelArgs.add_argument( + "--subnet", + dest="tunnel_net", + help="Sets the local-end subnet address for the TUN IP bridge. (ex: 10.115' which is the default)", + default=None, + ) parser.set_defaults(deprecated=None) diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index e2e917e5b..0199c8f51 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -10,27 +10,12 @@ from ..tcp_interface import TCPInterface try: - # Depends upon pytap2, not installed by default + # Depends upon pytap3, not installed by default from ..tunnel import Tunnel, onTunnelReceive except ImportError: pytest.skip("Can't import Tunnel or onTunnelReceive", allow_module_level=True) -@pytest.mark.unit -@patch("platform.system") -def test_Tunnel_on_non_linux_system(mock_platform_system): - """Test that we cannot instantiate a Tunnel on a non Linux system""" - a_mock = MagicMock() - a_mock.return_value = "notLinux" - mock_platform_system.side_effect = a_mock - with patch("socket.socket") as mock_socket: - with pytest.raises(Tunnel.TunnelError) as pytest_wrapped_e: - iface = TCPInterface(hostname="localhost", noProto=True) - Tunnel(iface) - assert pytest_wrapped_e.type == Tunnel.TunnelError - assert mock_socket.called - - @pytest.mark.unit @patch("platform.system") def test_Tunnel_without_interface(mock_platform_system): diff --git a/meshtastic/tunnel.py b/meshtastic/tunnel.py index 46e3a2e0d..7013f13b4 100644 --- a/meshtastic/tunnel.py +++ b/meshtastic/tunnel.py @@ -1,7 +1,7 @@ """Code for IP tunnel over a mesh # Note python-pytuntap was too buggy -# using pip3 install pytap2 +# using pip3 install pytap3 # make sure to "sudo setcap cap_net_admin+eip /usr/bin/python3.8" so python can access tun device without being root # sudo ip tuntap del mode tun tun0 # sudo bin/run.sh --port /dev/ttyUSB0 --setch-shortfast @@ -20,7 +20,7 @@ import threading from pubsub import pub # type: ignore[import-untyped] -from pytap2 import TapDevice +from pytap3 import TapDevice from meshtastic.protobuf import portnums_pb2 from meshtastic import mt_config @@ -64,9 +64,6 @@ def __init__(self, iface, subnet: str="10.115", netmask: str="255.255.0.0") -> N self.iface = iface self.subnetPrefix = subnet - if platform.system() != "Linux": - raise Tunnel.TunnelError("Tunnel() can only be run instantiated on a Linux system") - mt_config.tunnelInstance = self """A list of chatty UDP services we should never accidentally @@ -109,6 +106,7 @@ def __init__(self, iface, subnet: str="10.115", netmask: str="255.255.0.0") -> N logger.debug("creating TUN device with MTU=200") # FIXME - figure out real max MTU, it should be 240 - the overhead bytes for SubPacket and Data + # Windows would like an MTU >= 576 bytes, but in practice the tunnel works without issues. self.tun = None if self.iface.noProto: logger.warning( diff --git a/poetry.lock b/poetry.lock index 9a7fbc5d4..6f30151e0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3684,16 +3684,15 @@ files = [ cp2110 = ["hidapi"] [[package]] -name = "pytap2" -version = "2.3.0" -description = "Object-oriented wrapper around the Linux Tun/Tap device" +name = "pytap3" +version = "3.1.2" +description = "Cross-platform tunnel interface for Python" optional = true python-versions = ">=3.8" groups = ["main"] markers = "extra == \"tunnel\"" files = [ - {file = "pytap2-2.3.0-py3-none-any.whl", hash = "sha256:a1edc287cf25c61f8fa8415fb6b61e50ac119ef5cd758ce15f2105d2c69f24ef"}, - {file = "pytap2-2.3.0.tar.gz", hash = "sha256:5a90d7b7c7107a438e53c7b27c1baadffe72889ada6024c02d19801fece2c383"}, + {file = "pytap3-3.1.2.tar.gz", hash = "sha256:c260e3d00e6af57b1486e01ec45e5bb26299a827c8803a92b1acc4ef2201efe4"}, ] [[package]] @@ -5034,7 +5033,7 @@ type = ["pytest-mypy"] [extras] analysis = ["dash", "dash-bootstrap-components", "pandas", "pandas", "pandas-stubs"] cli = ["argcomplete", "dotmap", "print-color", "pyqrcode", "wcwidth"] -tunnel = ["pytap2"] +tunnel = ["pytap3"] [metadata] lock-version = "2.1" diff --git a/pyproject.toml b/pyproject.toml index b57615585..bd10379eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ pyqrcode = { version = "^1.2.1", optional = true } dotmap = { version = "^1.3.30", optional = true } print-color = { version = "^0.4.6", optional = true } dash = { version = "^2.17.1", optional = true } -pytap2 = { version = "^2.3.0", optional = true } +pytap3 = { version = ">=3.1.2", optional = true } dash-bootstrap-components = { version = "^1.6.0", optional = true } pandas = [ { version = ">=2.2.2,<3", optional = true, python = "<3.13" }, @@ -78,7 +78,7 @@ jupyterlab-widgets = "^3.0.11" [tool.poetry.extras] cli = ["pyqrcode", "print-color", "dotmap", "argcomplete", "wcwidth"] -tunnel = ["pytap2"] +tunnel = ["pytap3"] analysis = ["dash", "dash-bootstrap-components", "pandas", "pandas-stubs"] [tool.poetry.scripts] diff --git a/tests/tuntest.py b/tests/tuntest.py index 38f623124..3e0d3a35f 100644 --- a/tests/tuntest.py +++ b/tests/tuntest.py @@ -1,6 +1,6 @@ # delete me eventually # Note python-pytuntap was too buggy -# using pip3 install pytap2 +# using pip3 install pytap3 # make sure to "sudo setcap cap_net_admin+eip /usr/bin/python3.8" so python can access tun device without being root # sudo ip tuntap del mode tun tun0 @@ -11,7 +11,7 @@ import logging from _thread import start_new_thread -from pytap2 import TapDevice +from pytap3 import TapDevice """A list of chatty UDP services we should never accidentally forward to our slow network"""