-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtunnel-app.sh
More file actions
executable file
·198 lines (173 loc) · 5 KB
/
tunnel-app.sh
File metadata and controls
executable file
·198 lines (173 loc) · 5 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CACHE_DIR="${HOME}/.cache/worm"
mkdir -p "$CACHE_DIR"
REFRESH=false
SEARCH=""
R_ARG=""
V_ARG=""
O_ARG=""
CONTAINER=""
IGNORE_FILE="${SCRIPT_DIR}/ignore-hosts.txt"
# --- Parse arguments ---
while [[ $# -gt 0 ]]; do
case "$1" in
--refresh)
REFRESH=true
shift
;;
-o|--other)
O_ARG="-o"
shift
;;
-v|--verbose)
V_ARG="-v"
shift
;;
-r|--remote-port)
R_ARG="-r $2"
shift 2
;;
-c|--container)
CONTAINER="$2"
shift 2
;;
*)
SEARCH="$1"
shift
;;
esac
done
# --- Collect hosts from SSH config ---
ALL_HOSTS=($(grep -E '^Host ' ~/.ssh/config | awk '{print $2}'))
# Remove ignored hosts if ignore file exists (exact match only)
if [[ -f "$IGNORE_FILE" ]]; then
IGNORED=($(<"$IGNORE_FILE"))
FILTERED_HOSTS=()
for host in "${ALL_HOSTS[@]}"; do
[[ -z "$host" ]] && continue
skip=false
for ignore in "${IGNORED[@]}"; do
if [[ "$host" == "$ignore" ]]; then
skip=true
break
fi
done
$skip || FILTERED_HOSTS+=("$host")
done
ALL_HOSTS=("${FILTERED_HOSTS[@]}")
fi
# --- Step 1: Select host (single select) ---
if command -v fzf >/dev/null 2>&1; then
selected_host=$(printf "%s\n" "${ALL_HOSTS[@]}" | fzf --prompt="Select host > " --height=40%)
else
echo "Available hosts:"
printf " %s\n" "${ALL_HOSTS[@]}"
echo ""
read -p "Enter host name: " selected_host
fi
if [[ -z "$selected_host" ]]; then
echo "No host selected, exiting."
exit 0
fi
echo "Selected host: $selected_host"
echo ""
# --- Step 2: Fetch or use cached projects for selected host ---
cache_file="${CACHE_DIR}/${selected_host}.txt"
if [[ "$REFRESH" == true || ! -f "$cache_file" ]]; then
echo "Fetching Docker Compose projects from $selected_host..."
# Get all unique Docker Compose project names from running containers
projects=$(ssh -n "$selected_host" "docker ps --filter 'label=com.docker.compose.project' --format '{{.Label \"com.docker.compose.project\"}}' 2>/dev/null | sort -u" || true)
if [[ -z "$projects" ]]; then
echo "No Docker Compose projects found on $selected_host"
exit 1
fi
echo "$projects" > "$cache_file"
echo "Cached projects for $selected_host"
echo ""
else
# Calculate cache age
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
cache_mod_time=$(stat -f %m "$cache_file" 2>/dev/null)
else
# Linux
cache_mod_time=$(stat -c %Y "$cache_file" 2>/dev/null)
fi
if [[ -n "$cache_mod_time" ]]; then
current_time=$(date +%s)
cache_age=$((current_time - cache_mod_time))
cache_age_minutes=$((cache_age / 60))
cache_age_hours=$((cache_age / 3600))
if [[ $cache_age_hours -gt 0 ]]; then
echo "Using cached projects for $selected_host (${cache_age_hours}h old)"
else
echo "Using cached projects for $selected_host (${cache_age_minutes}m old)"
fi
# Show refresh hint if cache is older than 1 hour
if [[ $cache_age -gt 3600 ]]; then
echo "Tip: Use --refresh to update the cache"
fi
else
echo "Using cached projects for $selected_host"
fi
projects=$(<"$cache_file")
echo ""
fi
# --- Step 3: Build project options ---
options=()
for project in $projects; do
[[ -z "$project" ]] && continue
options+=("$project")
done
# Apply search filter if provided
if [[ -n "$SEARCH" ]]; then
options=($(printf "%s\n" "${options[@]}" | grep -i "$SEARCH" || true))
fi
# Handle no matches
if [[ ${#options[@]} -eq 0 ]]; then
echo "No matches found."
exit 1
fi
# --- Step 4: Select project ---
if [[ -z "$SEARCH" ]]; then
# No search term - always show fzf
if command -v fzf >/dev/null 2>&1; then
selected_project=$(printf "%s\n" "${options[@]}" | fzf --prompt="Select project > " --height=40%)
else
echo "Available projects:"
printf " %s\n" "${options[@]}"
echo ""
read -p "Enter project name: " selected_project
fi
else
# Search term provided - auto-select if only one match
if [[ ${#options[@]} -eq 1 ]]; then
selected_project="${options[0]}"
echo "Auto-selected: $selected_project"
else
if command -v fzf >/dev/null 2>&1; then
selected_project=$(printf "%s\n" "${options[@]}" | fzf --prompt="Select project > " --height=40%)
else
echo "Available projects:"
printf " %s\n" "${options[@]}"
echo ""
read -p "Enter project name: " selected_project
fi
fi
fi
if [[ -z "$selected_project" ]]; then
echo "No project selected, exiting."
exit 0
fi
# --- Step 5: Connect ---
echo "Connecting to $selected_host, project: $selected_project ${R_ARG:+$R_ARG} ${V_ARG:+verbose mode} ${O_ARG:+choose service} ${CONTAINER:+container: $CONTAINER}"
# Build args array to avoid empty string arguments
args=()
[[ -n "$V_ARG" ]] && args+=("$V_ARG")
[[ -n "$O_ARG" ]] && args+=("$O_ARG")
[[ -n "$R_ARG" ]] && args+=("$R_ARG")
args+=("$selected_host" "$selected_project")
[[ -n "$CONTAINER" ]] && args+=("$CONTAINER")
$SCRIPT_DIR/container-tunnel-caddy "${args[@]}"