From ada92715a838056b72db00c3df2d8ebf4deee4b3 Mon Sep 17 00:00:00 2001 From: Adam Outler Date: Fri, 3 Oct 2025 22:12:42 +0000 Subject: [PATCH] all debugging online. --- .devcontainer/Dockerfile | 2 +- .devcontainer/devcontainer.json | 79 +++++++++---------- .../services/config/php/conf.d/99-xdebug.ini | 2 +- .../config/php/php-fpm.d/99-xdebug.ini | 2 +- .devcontainer/scripts/isDevContainer.sh | 2 +- .venv_import_check.py | 23 ++++++ .vscode/tasks.json | 19 +++++ Dockerfile | 2 +- back/speedtest-cli | 3 +- docs/DEV_PORTS_HOST_MODE.md | 44 +++++++++++ scripts/list-ports.sh | 19 +++++ 11 files changed, 151 insertions(+), 46 deletions(-) create mode 100644 .venv_import_check.py create mode 100644 docs/DEV_PORTS_HOST_MODE.md create mode 100755 scripts/list-ports.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index fcd0da04..482ac79d 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -14,7 +14,7 @@ RUN apk add --no-cache bash shadow python3 python3-dev gcc musl-dev libffi-dev o # Enable venv ENV PATH="/opt/venv/bin:$PATH" -RUN pip install openwrt-luci-rpc asusrouter asyncio aiohttp graphene flask flask-cors unifi-sm-api tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros yattag zeroconf git+https://github.com/foreign-sub/aiofreepybox.git +RUN pip install openwrt-luci-rpc asusrouter asyncio aiohttp graphene flask flask-cors unifi-sm-api tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros yattag zeroconf simplejson future six urllib3 httplib2 git+https://github.com/foreign-sub/aiofreepybox.git RUN chmod -R u-rwx,g-rwx /opt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 003d4fc3..e1c275b7 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,27 +1,49 @@ { "name": "NetAlertX DevContainer", "remoteUser": "netalertx", + "workspaceFolder": "/workspaces/NetAlertX", "build": { - "dockerfile": "./Dockerfile", - "context": "../", + "dockerfile": "./Dockerfile", // Dockerfile generated by script + "context": "../", // Context is the root of the repository "target": "netalertx-devcontainer" }, - "workspaceFolder": "/workspaces/NetAlertX", - "runArgs": [ - "--add-host=host.docker.internal:host-gateway", - "--security-opt", "apparmor=unconfined" // for alowing ramdisk mounts - ], - "capAdd": [ "SYS_ADMIN", // For mounting ramdisks "NET_ADMIN", // For network interface configuration - "NET_RAW" // For raw packet manipulation + "NET_RAW" // For raw packet manipulation ], - - - + "runArgs": [ + "--security-opt", + "apparmor=unconfined" // for allowing ramdisk mounts + + // Uncomment --network=host to run full NetAlertX scanning capabilities of network scanning in + // container. This runs too slowly in a large network to be practical for development purposes. + // You can start services such as avahi on the host, in other containers within the network, or + // even within this container and connect to them as needed. + // "--network=host", + ], + // ATTENTION: If running with --network=host, COMMENT `forwardPorts` OR ELSE THERE WILL BE NO WEBUI! + "forwardPorts": [20211, 20212, 9003, 9000, 5678], + "portsAttributes": { // the ports we care about + "20211": { + "label": "Frontend:Nginx+PHP" + }, + "20212": { + "label": "Backend:GraphQL" + }, + "9003": { + "label": "PHP Debug:Xdebug" + }, + "9000": { + "label": "PHP-FPM:FastCGI" + }, + "5678": { + "label": "Python Debug:debugpy" + } + }, + "postStartCommand": "${containerWorkspaceFolder}/.devcontainer/scripts/setup.sh", - + "customizations": { "vscode": { "extensions": [ @@ -43,16 +65,13 @@ "eamodio.gitlens", "alexcvzz.vscode-sqlite", "yzhang.markdown-all-in-one" - ] - , + ], "settings": { "terminal.integrated.cwd": "${containerWorkspaceFolder}", // Python testing configuration "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false, - "python.testing.pytestArgs": [ - "test" - ], + "python.testing.pytestArgs": ["test"], // Make sure we discover tests and import server correctly "python.analysis.extraPaths": [ "/workspaces/NetAlertX", @@ -63,26 +82,6 @@ } } }, - "forwardPorts": [5678, 9000, 9003, 20211, 20212], - "portsAttributes": { - "20211": { - "label": "Frontend:Nginx+PHP" - }, - "20212": { - "label": "Backend:GraphQL" - }, - "9003": { - "label": "PHP Debug:Xdebug" - }, - "9000": { - "label": "PHP-FPM:FastCGI" - }, - "5678": { - "label": "Python Debug:debugpy" - } - }, - - // Optional: ensures compose services are stopped when you close the window - "shutdownAction": "stopContainer" -} \ No newline at end of file + "shutdownAction": "stopContainer" // stop container when VSCode is closed +} diff --git a/.devcontainer/resources/devcontainer-overlay/services/config/php/conf.d/99-xdebug.ini b/.devcontainer/resources/devcontainer-overlay/services/config/php/conf.d/99-xdebug.ini index 7779c22b..d9a4ac5a 100644 --- a/.devcontainer/resources/devcontainer-overlay/services/config/php/conf.d/99-xdebug.ini +++ b/.devcontainer/resources/devcontainer-overlay/services/config/php/conf.d/99-xdebug.ini @@ -5,7 +5,7 @@ extension_dir="/services/php/modules" xdebug.mode=develop,debug xdebug.log=/app/log/xdebug.log xdebug.log_level=7 -xdebug.client_host=host.docker.internal +xdebug.client_host=127.0.0.1 xdebug.client_port=9003 xdebug.start_with_request=yes xdebug.discover_client_host=1 diff --git a/.devcontainer/resources/devcontainer-overlay/services/config/php/php-fpm.d/99-xdebug.ini b/.devcontainer/resources/devcontainer-overlay/services/config/php/php-fpm.d/99-xdebug.ini index 37452d58..2bbbf083 100755 --- a/.devcontainer/resources/devcontainer-overlay/services/config/php/php-fpm.d/99-xdebug.ini +++ b/.devcontainer/resources/devcontainer-overlay/services/config/php/php-fpm.d/99-xdebug.ini @@ -2,7 +2,7 @@ zend_extension="xdebug.so" [xdebug] xdebug.mode=develop,debug xdebug.log_level=0 -xdebug.client_host=host.docker.internal +xdebug.client_host=127.0.0.1 xdebug.client_port=9003 xdebug.start_with_request=yes xdebug.discover_client_host=1 diff --git a/.devcontainer/scripts/isDevContainer.sh b/.devcontainer/scripts/isDevContainer.sh index 408c757d..ca86e3da 100755 --- a/.devcontainer/scripts/isDevContainer.sh +++ b/.devcontainer/scripts/isDevContainer.sh @@ -5,4 +5,4 @@ if [ ! -d /workspaces/NetAlertX/.devcontainer ]; then echo "Not in a devcontainer, exiting..." echo --------------------------------------------------- exit 255 -fi \ No newline at end of file +fi diff --git a/.venv_import_check.py b/.venv_import_check.py new file mode 100644 index 00000000..4f3a643e --- /dev/null +++ b/.venv_import_check.py @@ -0,0 +1,23 @@ +import sys, importlib +mods = [ + 'json', 'simplejson', + 'httplib', 'http.client', + 'urllib2', 'urllib.request', + 'Queue', 'queue', + 'cStringIO', 'StringIO', 'io', + 'md5', 'hashlib', + 'ssl' +] +print('PYTHON_EXE:' + sys.executable) +print('PYTHON_VER:' + sys.version.replace('\n', ' ')) +for m in mods: + try: + mod = importlib.import_module(m) + ver = getattr(mod, '__version__', None) + if ver is None: + # try common attributes + ver = getattr(mod, 'version', None) + info = (' version=' + str(ver)) if ver is not None else '' + print('OK %s%s' % (m, info)) + except Exception as e: + print('MISSING %s %s: %s' % (m, e.__class__.__name__, e)) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f84cb2f1..e226c9ba 100755 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -146,6 +146,25 @@ "id": "debug-stop", "color": "terminal.ansiRed" } + }, + { + "label": "[Dev Container] List NetAlertX Ports", + "type": "shell", + "command": "./scripts/list-ports.sh", + "options": { + "cwd": "${workspaceFolder:NetAlertX}" + }, + "presentation": { + "echo": true, + "reveal": "always", + "panel": "shared", + "showReuseMessage": false + }, + "problemMatcher": [], + "icon": { + "id": "output", + "color": "terminal.ansiBlue" + } } ] } diff --git a/Dockerfile b/Dockerfile index c43b4261..6f3cdc35 100755 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ RUN apk add --no-cache bash shadow python3 python3-dev gcc musl-dev libffi-dev o # Enable venv ENV PATH="/opt/venv/bin:$PATH" -RUN pip install openwrt-luci-rpc asusrouter asyncio aiohttp graphene flask flask-cors unifi-sm-api tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros yattag zeroconf git+https://github.com/foreign-sub/aiofreepybox.git +RUN pip install openwrt-luci-rpc asusrouter asyncio aiohttp graphene flask flask-cors unifi-sm-api tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros yattag zeroconf simplejson future six urllib3 httplib2 git+https://github.com/foreign-sub/aiofreepybox.git RUN chmod -R u-rwx,g-rwx /opt diff --git a/back/speedtest-cli b/back/speedtest-cli index 186b5292..84d06194 100755 --- a/back/speedtest-cli +++ b/back/speedtest-cli @@ -957,7 +957,8 @@ class SpeedtestResults(object): self.client = client or {} self._share = None - self.timestamp = '%sZ' % datetime.datetime.utcnow().isoformat() + # Use timezone-aware UTC datetime (utcnow() deprecated for removal) + self.timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00', 'Z') self.bytes_received = 0 self.bytes_sent = 0 diff --git a/docs/DEV_PORTS_HOST_MODE.md b/docs/DEV_PORTS_HOST_MODE.md new file mode 100644 index 00000000..0738b752 --- /dev/null +++ b/docs/DEV_PORTS_HOST_MODE.md @@ -0,0 +1,44 @@ +# Dev Ports in Host Network Mode + +When using `"--network=host"` in the devcontainer, VS Code's normal port forwarding model doesn't apply. All container ports are already on the host network namespace, so: + +- Listing ports in `forwardPorts` can cause VS Code to pre-bind or reserve them (conflicts with startup scripts waiting for a free port). +- The PORTS panel will not auto-detect services reliably, because forwarding isn't occurring. +- Debugger ports (e.g. Xdebug `9003`, Python debugpy `5678`) can still be listed safely. + +## Recommended Pattern + +1. Only include debugger ports in `forwardPorts`: + ```jsonc + "forwardPorts": [5678, 9003] + ``` +2. Do NOT list application service ports (e.g. 20211, 20212) there when in host mode. +3. Use the helper task to enumerate current bindings: + - Run task: `> Tasks: Run Task` → `[Dev Container] List NetAlertX Ports` + +## Port Enumeration Script +Script: `scripts/list-ports.sh` +Outputs binding address, PID (if resolvable) and process name for key ports. + +You can edit the PORTS variable inside that script to add/remove watched ports. + +## Xdebug Notes +Set in `99-xdebug.ini`: +```ini +xdebug.client_host=127.0.0.1 +xdebug.client_port=9003 +xdebug.discover_client_host=1 +``` +Ensure your IDE is listening on 9003. + +## Troubleshooting +| Symptom | Cause | Fix | +|---------|-------|-----| +| `Waiting for port 20211 to free...` repeats | VS Code pre-bound the port via `forwardPorts` | Remove the port from `forwardPorts`, rebuild, retry | +| PHP request hangs at start | Xdebug trying to connect to unresolved host (`host.docker.internal`) | Use `127.0.0.1` or rely on discovery | +| PORTS panel empty | Expected in host mode | Use the port enumeration task | + +## Future Improvements +- Optional: add a small web status endpoint summarizing runtime ports. +- Optional: detect host mode in `setup.sh` and skip the wait loop if the PID using port is the intended process. + diff --git a/scripts/list-ports.sh b/scripts/list-ports.sh new file mode 100755 index 00000000..d7197b36 --- /dev/null +++ b/scripts/list-ports.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Quick port/service enumerator for host-network dev container. +# Shows which processes are bound to key NetAlertX ports. +PORTS="20211 20212 9003 9000 5678" +printf "%-8s %-22s %-8s %s\n" PORT BIND LISTEN_PID COMMAND +for p in $PORTS; do + line=$(ss -ltnp 2>/dev/null | awk -v P=":${p}" '$4 ~ P {print $0; exit}') + if [ -n "$line" ]; then + addr=$(echo "$line" | awk '{print $4}') + pid=$(echo "$line" | sed -n 's/.*pid=\([0-9]*\).*/\1/p') + cmd="$( [ -n "$pid" ] && ps -o comm= -p "$pid" 2>/dev/null)" + printf "%-8s %-22s %-8s %s\n" "$p" "$addr" "${pid:-?}" "${cmd:-?}" + else + printf "%-8s %-22s %-8s %s\n" "$p" "(not listening)" "-" "-" + fi +done + +# Show any other NetAlertX-related listeners (nginx, php-fpm, python backend) +ss -ltnp 2>/dev/null | egrep 'nginx|php-fpm|python' || true