From 8ca568ffd6207acbdd4193a17162e79e9a55afe4 Mon Sep 17 00:00:00 2001 From: abawi Date: Thu, 24 Apr 2025 23:33:50 +0200 Subject: [PATCH] Deployment with cloudflare works now --- scripts/run_filepizza_cloudflare_tunnel.sh | 138 +++++++++------------ 1 file changed, 58 insertions(+), 80 deletions(-) diff --git a/scripts/run_filepizza_cloudflare_tunnel.sh b/scripts/run_filepizza_cloudflare_tunnel.sh index 2be1212..1507338 100755 --- a/scripts/run_filepizza_cloudflare_tunnel.sh +++ b/scripts/run_filepizza_cloudflare_tunnel.sh @@ -31,6 +31,7 @@ TUNNEL_NAME="filepizza" HTTP_SERVICE_URL="http://localhost:8080" HOSTNAME="$HOST_DOMAIN" CREDENTIALS_DIR=~/.cloudflared +CONFIG_FILE="$CREDENTIALS_DIR/config.yml" # Color codes for better readability GREEN='\033[0;32m' @@ -60,10 +61,10 @@ if ! command -v cloudflared &> /dev/null; then rm cloudflared.rpm else # Generic Linux - mkdir -p ~/.cloudflared - curl -L --output ~/.cloudflared/cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 - chmod +x ~/.cloudflared/cloudflared - echo 'export PATH=$PATH:~/.cloudflared' >> ~/.bashrc + mkdir -p $CREDENTIALS_DIR + curl -L --output $CREDENTIALS_DIR/cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 + chmod +x $CREDENTIALS_DIR/cloudflared + echo "export PATH=\$PATH:$CREDENTIALS_DIR" >> ~/.bashrc source ~/.bashrc fi elif [[ "$OSTYPE" == "darwin"* ]]; then @@ -77,125 +78,102 @@ if ! command -v cloudflared &> /dev/null; then echo -e "${GREEN}cloudflared installed successfully!${NC}" fi -# Function to create/update config file -function CREATE_CONFIG_FILE { - CONFIG_FILE="$CREDENTIALS_DIR/config.yml" - cat > "$CONFIG_FILE" << EOF -tunnel: $TUNNEL_ID -credentials-file: $CREDENTIALS_DIR/$TUNNEL_ID.json -ingress: - - hostname: $HOSTNAME - service: $HTTP_SERVICE_URL - - service: http_status:404 -EOF - - echo -e "${GREEN}Created/updated config file at: $CONFIG_FILE${NC}" -} - -# Authenticate with Cloudflare -echo -e "${YELLOW}Authenticating with Cloudflare...${NC}" - -# Check if authentication was already done previously -if [ -f ~/.cloudflared/cert.pem ]; then - echo -e "${GREEN}Certificate already exists at ~/.cloudflared/cert.pem${NC}" - echo -e "${GREEN}Skipping authentication step...${NC}" -else +# Authenticate with Cloudflare if needed +if [ ! -f $CREDENTIALS_DIR/cert.pem ]; then + echo -e "${YELLOW}Authenticating with Cloudflare...${NC}" echo -e "${YELLOW}This will open a browser window. Please log in and authorize cloudflared.${NC}" - # Run the cloudflared login command - this will open a browser for authentication cloudflared tunnel login - # Check if login was successful - if [ ! -f ~/.cloudflared/cert.pem ]; then + if [ ! -f $CREDENTIALS_DIR/cert.pem ]; then echo -e "${RED}Authentication failed. cert.pem not found.${NC}" exit 1 fi - - echo -e "${GREEN}Authentication successful! Certificate created at ~/.cloudflared/cert.pem${NC}" + echo -e "${GREEN}Authentication successful!${NC}" +else + echo -e "${GREEN}Using existing Cloudflare credentials${NC}" fi -# Make sure the credentials directory exists +# Make sure credentials directory exists mkdir -p $CREDENTIALS_DIR -# Check if tunnel already exists +# Check if tunnel exists echo -e "${YELLOW}Checking if tunnel already exists: $TUNNEL_NAME...${NC}" EXISTING_TUNNEL=$(cloudflared tunnel list | grep $TUNNEL_NAME | awk '{print $1}') -if [ ! -z "$EXISTING_TUNNEL" ]; then +if [ -n "$EXISTING_TUNNEL" ]; then echo -e "${GREEN}Tunnel already exists with ID: $EXISTING_TUNNEL${NC}" TUNNEL_ID=$EXISTING_TUNNEL - # Check if credentials file exists for this tunnel - CREDS_FILE="$CREDENTIALS_DIR/$TUNNEL_ID.json" - if [ ! -f "$CREDS_FILE" ]; then - echo -e "${YELLOW}Credentials file not found for existing tunnel.${NC}" - echo -e "${YELLOW}Generating config file for existing tunnel...${NC}" - - # Since we're missing credentials, we need to recreate the tunnel - echo -e "${YELLOW}Deleting existing tunnel due to missing credentials...${NC}" + # Delete existing tunnel if credentials file is missing + if [ ! -f "$CREDENTIALS_DIR/$TUNNEL_ID.json" ]; then + echo -e "${YELLOW}Credentials file missing. Recreating tunnel...${NC}" cloudflared tunnel delete $TUNNEL_ID - # Create a new tunnel + # Create new tunnel echo -e "${YELLOW}Creating new tunnel: $TUNNEL_NAME...${NC}" - TUNNEL_ID=$(cloudflared tunnel create $TUNNEL_NAME | grep -oP 'Created tunnel \K[a-z0-9-]+') + TUNNEL_OUTPUT=$(cloudflared tunnel create $TUNNEL_NAME) + echo "$TUNNEL_OUTPUT" + TUNNEL_ID=$(echo "$TUNNEL_OUTPUT" | grep -i "created tunnel" | grep -o "[a-f0-9]\{8\}-[a-f0-9]\{4\}-[a-f0-9]\{4\}-[a-f0-9]\{4\}-[a-f0-9]\{12\}") if [ -z "$TUNNEL_ID" ]; then - echo -e "${RED}Failed to create tunnel.${NC}" - exit 1 + echo -e "${RED}Failed to extract tunnel ID automatically.${NC}" + read -p "Please enter the tunnel ID manually from the output above: " TUNNEL_ID + if [ -z "$TUNNEL_ID" ]; then + echo -e "${RED}No tunnel ID provided. Exiting.${NC}" + exit 1 + fi fi echo -e "${GREEN}New tunnel created with ID: $TUNNEL_ID${NC}" - # Create the updated config file - CREATE_CONFIG_FILE - - # Route the tunnel to your domain + # Route DNS echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}" cloudflared tunnel route dns $TUNNEL_ID $HOSTNAME - else - echo -e "${GREEN}Found credentials file for tunnel: $CREDS_FILE${NC}" - # Update the config file with current settings - CREATE_CONFIG_FILE fi else - # Create a new tunnel + # Create new tunnel echo -e "${YELLOW}Creating new tunnel: $TUNNEL_NAME...${NC}" - TUNNEL_ID=$(cloudflared tunnel create $TUNNEL_NAME | grep -oP 'Created tunnel \K[a-z0-9-]+') + TUNNEL_OUTPUT=$(cloudflared tunnel create $TUNNEL_NAME) + echo "$TUNNEL_OUTPUT" + TUNNEL_ID=$(echo "$TUNNEL_OUTPUT" | grep -i "created tunnel" | grep -o "[a-f0-9]\{8\}-[a-f0-9]\{4\}-[a-f0-9]\{4\}-[a-f0-9]\{4\}-[a-f0-9]\{12\}") if [ -z "$TUNNEL_ID" ]; then - echo -e "${RED}Failed to create tunnel.${NC}" - exit 1 + echo -e "${RED}Failed to extract tunnel ID automatically.${NC}" + read -p "Please enter the tunnel ID manually from the output above: " TUNNEL_ID + if [ -z "$TUNNEL_ID" ]; then + echo -e "${RED}No tunnel ID provided. Exiting.${NC}" + exit 1 + fi fi echo -e "${GREEN}Tunnel created with ID: $TUNNEL_ID${NC}" - # Create the config file - CREATE_CONFIG_FILE - - # Route the tunnel to your domain + # Route DNS echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}" cloudflared tunnel route dns $TUNNEL_ID $HOSTNAME fi -# Check if the credentials file exists now -CREDS_FILE="$CREDENTIALS_DIR/$TUNNEL_ID.json" -if [ ! -f "$CREDS_FILE" ]; then - echo -e "${RED}Credentials file still not found at: $CREDS_FILE${NC}" - echo -e "${RED}This is unexpected. Please ensure your Cloudflare account has proper permissions.${NC}" +# Create config file +echo -e "${YELLOW}Creating config file...${NC}" +cat > "$CONFIG_FILE" << EOF +tunnel: $TUNNEL_ID +credentials-file: $CREDENTIALS_DIR/$TUNNEL_ID.json +ingress: + - hostname: $HOSTNAME + service: $HTTP_SERVICE_URL + - service: http_status:404 +EOF +echo -e "${GREEN}Config file created at: $CONFIG_FILE${NC}" + +# Verify credentials file exists +if [ ! -f "$CREDENTIALS_DIR/$TUNNEL_ID.json" ]; then + echo -e "${RED}Warning: Credentials file not found at $CREDENTIALS_DIR/$TUNNEL_ID.json${NC}" + echo -e "${RED}You may need to recreate the tunnel or check permissions.${NC}" exit 1 fi -# Copy the tunnel ID JSON file to filepizza.json -cp "$CREDENTIALS_DIR/$TUNNEL_ID.json" "$CREDENTIALS_DIR/filepizza.json" - -# Run the tunnel with the configuration -echo -e "${GREEN}Setup complete! Running tunnel...${NC}" - -# Run the tunnel with the configuration -echo -e "${GREEN}Setup complete! Running tunnel...${NC}" +# Run the tunnel +echo -e "${GREEN}Starting tunnel to $HOSTNAME...${NC}" echo -e "${YELLOW}Your FilePizza server is now accessible at: https://$HOSTNAME${NC}" echo -e "${YELLOW}Press Ctrl+C to stop the tunnel${NC}" -echo -e "${YELLOW}Note: TURN/STUN services need to be configured separately in another tunnel${NC}" - -# Run the tunnel with the config file -echo -e "${GREEN}Starting tunnel with configuration...${NC}" cloudflared tunnel --config="$CONFIG_FILE" run \ No newline at end of file