-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-port-detection.sh
More file actions
executable file
·150 lines (136 loc) · 4.75 KB
/
Copy pathdebug-port-detection.sh
File metadata and controls
executable file
·150 lines (136 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
usage: ./debug-port-detection.sh [--live-only|--samples-only|--command <text>]
Options:
--live-only Show only current process snapshot.
--samples-only Run only sample-command port extraction.
--command TEXT Run extraction for one explicit command string.
-h, --help Show this help text.
USAGE
}
extract_ports() {
local command="$1"
local extracted
extracted=$(
printf '%s\n' "$command" | perl -ne '
while (/(?:^|\s)[A-Z_]*PORT=(?:[\x27"]?)([0-9]{1,5})(?:[\x27"]?)(?:\D|$)/g) { print "$1\n" }
while (/(?:^|\s)[A-Z_]*PORT=\$\{[^}:]+:-(?:[\x27"]?)([0-9]{1,5})(?:[\x27"]?)\}(?:\D|$)/g) { print "$1\n" }
while (/(?:^|\s)[A-Z_]*PORT=\$\{[^}:]+:=(?:[\x27"]?)([0-9]{1,5})(?:[\x27"]?)\}(?:\D|$)/g) { print "$1\n" }
while (/(?:^|\s)[A-Z_]*PORT=\$\{[^}-]+-(?:[\x27"]?)([0-9]{1,5})(?:[\x27"]?)\}(?:\D|$)/g) { print "$1\n" }
while (/(?:^|\s)[A-Z_]*PORT=\$\{[^}=]+=(?:[\x27"]?)([0-9]{1,5})(?:[\x27"]?)\}(?:\D|$)/g) { print "$1\n" }
while (/(?:--(?:port|http-port|https-port)\s*=?\s*(?:[^:\s]+|\[[^\]]+\]):)([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/(?:--(?:port|http-port|https-port)\s*=?\s*)([0-9]{1,5})(?![.:])(?:\D|$)/g) { print "$1\n" }
while (/(?:--inspect-port\s*=?\s*(?:[^:\s]+|\[[^\]]+\]):)([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/(?:--inspect-port\s*=?\s*)([0-9]{1,5})(?![.:])(?:\D|$)/g) { print "$1\n" }
while (/(?:--(?:inspect|inspect-brk|inspect-wait)\s*=?\s*)([0-9]{1,5})(?![.:])(?:\D|$)/g) { print "$1\n" }
while (/(?:--(?:inspect|inspect-brk|inspect-wait)\s*=?\s*(?:[^:\s]+:))([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/(?:--(?:inspect|inspect-brk|inspect-wait)\s*=?\s*)(?:\[[^\]]+\]|localhost|0\.0\.0\.0|127(?:\.\d{1,3}){3}|::1)(?=[\s,;]|$)/g) { print "9229\n" }
while (/(?:--(?:listen|listen-address|addr|address|bind|socket)\s*=?\s*(?:[^:\s]+|\[[^\]]+\]):)([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/(?:https?:\/\/[^:\s]+:)([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0):([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/\[[^\]]+\]:([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
while (/\*:([0-9]{1,5})(?:\D|$)/g) { print "$1\n" }
' | awk '$1 >= 1 && $1 <= 65535'
)
if [[ -z "$extracted" ]]; then
echo " (inga port-träffar)"
return
fi
printf '%s\n' "$extracted" | awk '!seen[$0]++' | sort -n | sed 's/^/ :/'
}
show_live_processes() {
echo "=== Live process snapshot (Node/dev wrappers) ==="
ps -axo pid=,command= \
| awk '
$0 ~ /^[[:space:]]*[0-9]+ / &&
$0 ~ /(node|npm|pnpm|yarn|npx|bun|deno)([[:space:]]|$)/ {
print
}
' \
| head -20 || true
}
show_samples() {
local samples=(
"npm run dev -- --port 3001"
"node server.js --port 8080"
"vite --port 5173 --host 0.0.0.0"
"PORT=4173 npm run preview"
"PORT=\"3002\" npm run dev"
"WEB_PORT=\${WEB_PORT:-4174} pnpm dev"
"API_PORT=\${API_PORT:=4200} pnpm dev"
"INSPECT_PORT=\${INSPECT_PORT=9333} node --inspect app.js"
"node --inspect=9229 app.js"
"node --inspect=127.0.0.1:9229 app.js"
"node --inspect-brk=localhost app.js"
"node --inspect-port=127.0.0.1:9230 app.js"
"node --inspect-wait=127.0.0.1:9330 app.js"
"node --inspect-wait=::1 app.js"
"next dev --hostname [::1] --port=3000"
"bun --hot server.ts --port 3002"
"bun --watch --inspect-wait=127.0.0.1:9330 server.ts"
"deno serve --listen 0.0.0.0:8787"
"PORT=8788 deno task dev"
"deno task dev -- --listen 127.0.0.1:8789"
)
echo "=== Sample command extraction ==="
for command in "${samples[@]}"; do
echo ""
echo "Command: $command"
extract_ports "$command"
done
}
MODE="all"
CUSTOM_COMMAND=""
while [[ $# -gt 0 ]]; do
case "$1" in
--live-only)
MODE="live"
;;
--samples-only)
MODE="samples"
;;
--command)
shift
if [[ $# -eq 0 ]]; then
echo "❌ Missing value for --command" >&2
usage >&2
exit 2
fi
MODE="command"
CUSTOM_COMMAND="$1"
;;
--command=*)
MODE="command"
CUSTOM_COMMAND="${1#--command=}"
;;
-h|--help)
usage
exit 0
;;
*)
echo "❌ Unknown option: ${1}" >&2
usage >&2
exit 2
;;
esac
shift
done
if [[ "$MODE" == "command" ]]; then
if [[ -z "$CUSTOM_COMMAND" ]]; then
echo "❌ --command requires a non-empty value" >&2
exit 2
fi
echo "=== Single command extraction ==="
echo "Command: $CUSTOM_COMMAND"
extract_ports "$CUSTOM_COMMAND"
exit 0
fi
if [[ "$MODE" == "all" || "$MODE" == "live" ]]; then
show_live_processes
fi
if [[ "$MODE" == "all" || "$MODE" == "samples" ]]; then
echo ""
show_samples
fi