Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
qtvhao committed Aug 3, 2023
1 parent d657519 commit b5a2a77
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ RUN apt update && apt install -y \
RUN wget https://get.docker.com -O /tmp/get-docker.sh && \
sh /tmp/get-docker.sh

RUN apt install -y docker-compose-plugin

WORKDIR /app
COPY get-tunnel.sh /app/get-tunnel.sh
COPY get-exist-tunnel.sh /app/get-exist-tunnel.sh
COPY start-tunnel.sh /app/start-tunnel.sh
COPY docker-compose.yml /app/docker-compose.yml
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
docker build -t qtvhao/trycloudflare-docker .
docker push qtvhao/trycloudflare-docker

TUNNEL=$(docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock qtvhao/trycloudflare-docker \
bash get-tunnel.sh docker-compose.yml trycloudflare-docker)

echo "Tunnel: $TUNNEL"
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,3 @@ services:
cloudflared-tunnel:
image: cloudflare/cloudflared
command: tunnel --url 172.17.0.1:8000
cloudflare_created:
build: .
command: sleep infinity
volumes:
- ./:/app
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- cloudflared-tunnel
29 changes: 29 additions & 0 deletions get-exist-tunnel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
docker_compose_file="$1"
project_name="$2"

set -e
tunnel_container=$(docker compose -f $docker_compose_file -p $project_name ps -q cloudflared-tunnel)
docker inspect -f '{{.State.Running}}' $tunnel_container >/dev/null
if [ $? -eq 0 ]; then
true
else
exit 1
fi
matcher=".trycloudflare.com"
while true; do
logs=$(docker compose -f $docker_compose_file -p $project_name logs cloudflared-tunnel)
echo "$logs" | grep -oP "Unauthorized" > /dev/null && echo "Unauthorized" && exit 1
echo "$logs" | grep -oP '(?<=https://).*(?=.trycloudflare.com)' | head -n 1 > /dev/null
if [ $? -eq 0 ]; then
TRYCLOUDFLARE_DOMAIN="$(echo "$logs" | grep -oP '(?<=https://).*(?=.trycloudflare.com)' | head -n 1)"
if [ -z "$TRYCLOUDFLARE_DOMAIN" ]; then
continue;
fi
echo "$TRYCLOUDFLARE_DOMAIN""$matcher"
break;
else
echo "Not ready yet..."
sleep 1
continue
fi
done
4 changes: 2 additions & 2 deletions get-tunnel.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
docker_compose_file="$1"
project_name="$2"
# docker compose -f $docker_compose_file -p $project_name logs
sh get-tunnel-admin-domain.sh "$docker_compose_file" "$project_name"
sh get-exist-tunnel.sh "$docker_compose_file" "$project_name" > /dev/null || sh start-tunnel.sh "$docker_compose_file" "$project_name" > /dev/null
sh get-exist-tunnel.sh "$docker_compose_file" "$project_name"
3 changes: 3 additions & 0 deletions reset-tunnel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker_compose_file="$1"
project_name="$2"
docker compose -f $docker_compose_file -p $project_name down
7 changes: 7 additions & 0 deletions start-tunnel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
docker_compose_file="$1"
project_name="$2"

logs=$(docker compose -f $docker_compose_file -p $project_name logs cloudflared-tunnel)
echo "$logs" | grep -oP "Unauthorized" > /dev/null && bash reset-tunnel.sh

docker compose -f $docker_compose_file -p $project_name up -d

0 comments on commit b5a2a77

Please sign in to comment.