Deployment with cloudflare works now

pull/313/head
abawi 8 months ago
parent 06bd2d7195
commit 8ca568ffd6

@ -31,6 +31,7 @@ TUNNEL_NAME="filepizza"
HTTP_SERVICE_URL="http://localhost:8080" HTTP_SERVICE_URL="http://localhost:8080"
HOSTNAME="$HOST_DOMAIN" HOSTNAME="$HOST_DOMAIN"
CREDENTIALS_DIR=~/.cloudflared CREDENTIALS_DIR=~/.cloudflared
CONFIG_FILE="$CREDENTIALS_DIR/config.yml"
# Color codes for better readability # Color codes for better readability
GREEN='\033[0;32m' GREEN='\033[0;32m'
@ -60,10 +61,10 @@ if ! command -v cloudflared &> /dev/null; then
rm cloudflared.rpm rm cloudflared.rpm
else else
# Generic Linux # Generic Linux
mkdir -p ~/.cloudflared mkdir -p $CREDENTIALS_DIR
curl -L --output ~/.cloudflared/cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 curl -L --output $CREDENTIALS_DIR/cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64
chmod +x ~/.cloudflared/cloudflared chmod +x $CREDENTIALS_DIR/cloudflared
echo 'export PATH=$PATH:~/.cloudflared' >> ~/.bashrc echo "export PATH=\$PATH:$CREDENTIALS_DIR" >> ~/.bashrc
source ~/.bashrc source ~/.bashrc
fi fi
elif [[ "$OSTYPE" == "darwin"* ]]; then elif [[ "$OSTYPE" == "darwin"* ]]; then
@ -77,125 +78,102 @@ if ! command -v cloudflared &> /dev/null; then
echo -e "${GREEN}cloudflared installed successfully!${NC}" echo -e "${GREEN}cloudflared installed successfully!${NC}"
fi fi
# Function to create/update config file # Authenticate with Cloudflare if needed
function CREATE_CONFIG_FILE { if [ ! -f $CREDENTIALS_DIR/cert.pem ]; then
CONFIG_FILE="$CREDENTIALS_DIR/config.yml" echo -e "${YELLOW}Authenticating with Cloudflare...${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}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
echo -e "${YELLOW}This will open a browser window. Please log in and authorize cloudflared.${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 cloudflared tunnel login
# Check if login was successful if [ ! -f $CREDENTIALS_DIR/cert.pem ]; then
if [ ! -f ~/.cloudflared/cert.pem ]; then
echo -e "${RED}Authentication failed. cert.pem not found.${NC}" echo -e "${RED}Authentication failed. cert.pem not found.${NC}"
exit 1 exit 1
fi fi
echo -e "${GREEN}Authentication successful!${NC}"
echo -e "${GREEN}Authentication successful! Certificate created at ~/.cloudflared/cert.pem${NC}" else
echo -e "${GREEN}Using existing Cloudflare credentials${NC}"
fi fi
# Make sure the credentials directory exists # Make sure credentials directory exists
mkdir -p $CREDENTIALS_DIR mkdir -p $CREDENTIALS_DIR
# Check if tunnel already exists # Check if tunnel exists
echo -e "${YELLOW}Checking if tunnel already exists: $TUNNEL_NAME...${NC}" echo -e "${YELLOW}Checking if tunnel already exists: $TUNNEL_NAME...${NC}"
EXISTING_TUNNEL=$(cloudflared tunnel list | grep $TUNNEL_NAME | awk '{print $1}') 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}" echo -e "${GREEN}Tunnel already exists with ID: $EXISTING_TUNNEL${NC}"
TUNNEL_ID=$EXISTING_TUNNEL TUNNEL_ID=$EXISTING_TUNNEL
# Check if credentials file exists for this tunnel # Delete existing tunnel if credentials file is missing
CREDS_FILE="$CREDENTIALS_DIR/$TUNNEL_ID.json" if [ ! -f "$CREDENTIALS_DIR/$TUNNEL_ID.json" ]; then
if [ ! -f "$CREDS_FILE" ]; then echo -e "${YELLOW}Credentials file missing. Recreating tunnel...${NC}"
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}"
cloudflared tunnel delete $TUNNEL_ID cloudflared tunnel delete $TUNNEL_ID
# Create a new tunnel # Create new tunnel
echo -e "${YELLOW}Creating new tunnel: $TUNNEL_NAME...${NC}" 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 if [ -z "$TUNNEL_ID" ]; then
echo -e "${RED}Failed to create tunnel.${NC}" 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 exit 1
fi fi
fi
echo -e "${GREEN}New tunnel created with ID: $TUNNEL_ID${NC}" echo -e "${GREEN}New tunnel created with ID: $TUNNEL_ID${NC}"
# Create the updated config file # Route DNS
CREATE_CONFIG_FILE
# Route the tunnel to your domain
echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}" echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}"
cloudflared tunnel route dns $TUNNEL_ID $HOSTNAME 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 fi
else else
# Create a new tunnel # Create new tunnel
echo -e "${YELLOW}Creating new tunnel: $TUNNEL_NAME...${NC}" 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 if [ -z "$TUNNEL_ID" ]; then
echo -e "${RED}Failed to create tunnel.${NC}" 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 exit 1
fi fi
fi
echo -e "${GREEN}Tunnel created with ID: $TUNNEL_ID${NC}" echo -e "${GREEN}Tunnel created with ID: $TUNNEL_ID${NC}"
# Create the config file # Route DNS
CREATE_CONFIG_FILE
# Route the tunnel to your domain
echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}" echo -e "${YELLOW}Routing tunnel to your domain: $HOSTNAME...${NC}"
cloudflared tunnel route dns $TUNNEL_ID $HOSTNAME cloudflared tunnel route dns $TUNNEL_ID $HOSTNAME
fi fi
# Check if the credentials file exists now # Create config file
CREDS_FILE="$CREDENTIALS_DIR/$TUNNEL_ID.json" echo -e "${YELLOW}Creating config file...${NC}"
if [ ! -f "$CREDS_FILE" ]; then cat > "$CONFIG_FILE" << EOF
echo -e "${RED}Credentials file still not found at: $CREDS_FILE${NC}" tunnel: $TUNNEL_ID
echo -e "${RED}This is unexpected. Please ensure your Cloudflare account has proper permissions.${NC}" 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 exit 1
fi fi
# Copy the tunnel ID JSON file to filepizza.json # Run the tunnel
cp "$CREDENTIALS_DIR/$TUNNEL_ID.json" "$CREDENTIALS_DIR/filepizza.json" echo -e "${GREEN}Starting tunnel to $HOSTNAME...${NC}"
# 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}"
echo -e "${YELLOW}Your FilePizza server is now accessible at: https://$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}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 cloudflared tunnel --config="$CONFIG_FILE" run
Loading…
Cancel
Save