Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/dev-servers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
Shows the number of distinct listening TCP ports owned by your user. **Port range**
defaults to `3000-9999`. The popup lists each port and process and updates every five seconds.
IPv4 and IPv6 sockets from the same process on the same port appear once.
Ports that Docker or OrbStack publish show the container's name instead of the helper process,
when `docker` is on your PATH, its daemon (Docker Desktop or OrbStack) is running, and the
current Docker context points at this Mac. A remote context is never queried.

Click an address to open `http://localhost:PORT`. **Stop** sends `SIGTERM` to that process,
after checking that it still owns a listener on that port. This stops the whole process,
including any other ports it serves. It does not force-kill processes or stop their supervisors.
On a container's row, **Stop** runs `docker stop` on that container instead, which leaves Docker
and its other containers running. **Stop** never stops the Docker or OrbStack helper itself,
since it forwards the ports of every container.

Requires `jq`. The scan uses `lsof` and needs no elevated permissions or network requests.
The range can contain non-HTTP services; opening those in a browser may fail.
2 changes: 1 addition & 1 deletion packages/dev-servers/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "dev-servers",
"name": "Development servers",
"version": "1.0.2",
"version": "1.1.0",
"description": "Lists your listening TCP ports and their processes, with actions to open localhost or stop a process.",
"author": "Seb-GRAF",
"repository": "https://github.com/Seb-GRAF/stablebar-packages",
Expand Down
30 changes: 23 additions & 7 deletions packages/dev-servers/scripts/servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ listeners=$(lsof -nP -a -u "$(id -un)" -iTCP:"$PORTS" -sTCP:LISTEN -Fpcn 2>&1) |
fi
}

printf '%s' "$listeners" | jq -Rns '
# Docker and OrbStack publish ports through a helper process, so map host ports to containers.
# shortcut: only Docker Desktop and OrbStack helpers get names; other runtimes (Colima, Podman) keep process · PID.
# Ask only a daemon on this Mac: a remote Docker context would mean a network request every few seconds.
containers=""
if host=$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null) && [[ $host == unix://* ]]; then
containers=$(docker --host "$host" ps --no-trunc --format '{{.ID}}\t{{.Names}}\t{{.Ports}}' 2>/dev/null) || containers=""
fi

printf '%s' "$listeners" | jq -Rns --arg containers "$containers" --arg host "$host" '
($containers | split("\n") | map(select(length > 0) | split("\t") | {id: .[0], name: .[1]} as $container |
[(.[2] // "") | scan(":([0-9]+)(?:-([0-9]+))?->[0-9-]+/tcp")] |
map(range(.[0] | tonumber; (.[1] // .[0] | tonumber) + 1) | {key: tostring, value: $container})
) | add // [] | from_entries) as $docker |
reduce (inputs | split("\n")[]) as $line ({pid: "", process: "", servers: []};
if $line | startswith("p") then .pid = $line[1:]
elif $line | startswith("c") then .process = $line[1:]
Expand All @@ -22,10 +34,14 @@ printf '%s' "$listeners" | jq -Rns '
text: (map(.port) | unique | length | tostring),
icon: "server.rack",
fields: {hint: (if length == 0 then "No listening ports in this range." else "" end)},
lists: {servers: map({
address: "localhost:\(.port)",
process: "\(.process) · PID \(.pid)",
link: "http://localhost:\(.port)",
stop: ("\"$PACKAGE_DIR/scripts/stop.sh\" " + (.pid | @sh) + " " + (.port | tostring | @sh))
})}
lists: {servers: map(
(if .process | test("^(OrbStack|com\\.docker)") then $docker[.port | tostring] else null end) as $c |
{
address: "localhost:\(.port)",
process: (if $c then "\($c.name) · container" else "\(.process) · PID \(.pid)" end),
link: "http://localhost:\(.port)",
stop: ("\"$PACKAGE_DIR/scripts/stop.sh\" " + (.pid | @sh) + " " + (.port | tostring | @sh)
+ (if $c then " " + ($c.id | @sh) + " " + ($host | @sh) else "" end))
}
)}
}'
22 changes: 20 additions & 2 deletions packages/dev-servers/scripts/stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ set -euo pipefail

pid=$1
port=$2
container=${3-}
host=${4-}

if lsof -nP -a -u "$(id -un)" -p "$pid" -iTCP:"$port" -sTCP:LISTEN -t > /dev/null; then
kill -TERM "$pid"
# The helper process forwards every container's ports, so stop only the container. Its ID, unlike
# its name, can't reach a newer container that took the name after the list was read, and the
# daemon the list was read from still answers if the Docker context changed since.
if [[ -n $container ]]; then
docker --host "$host" stop "$container" > /dev/null
exit
fi

process=$(lsof -nP -a -u "$(id -un)" -p "$pid" -iTCP:"$port" -sTCP:LISTEN -Fc | sed -n 's/^c//p') || exit 0

# A helper's row has no container name when docker couldn't be asked; stopping it would cut every container.
case $process in
OrbStack* | com.docker*)
printf '%s forwards the ports of every container, so Stop leaves it running.\n' "$process" >&2
exit 1
;;
esac

kill -TERM "$pid"
Loading