forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-cloudflare-tunnel.sh
More file actions
executable file
·82 lines (70 loc) · 2.18 KB
/
Copy pathsetup-cloudflare-tunnel.sh
File metadata and controls
executable file
·82 lines (70 loc) · 2.18 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
#!/bin/bash
# Check if script is run with required arguments
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <subdomain> <domain> <local_port>"
echo "Example: $0 image2 pollinations.ai 16384"
exit 1
fi
SUBDOMAIN=$1
DOMAIN=$2
LOCAL_PORT=$3
HOSTNAME="${SUBDOMAIN}.${DOMAIN}"
# Install cloudflared if not present
if ! command -v cloudflared &> /dev/null; then
echo "Installing cloudflared..."
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared.deb
rm cloudflared.deb
fi
# Login to Cloudflare (this will open a browser)
echo "Please login to Cloudflare..."
cloudflared tunnel login
# Create new tunnel
echo "Creating tunnel..."
TUNNEL_NAME="${SUBDOMAIN}.${DOMAIN}"
TUNNEL_ID=$(cloudflared tunnel create "$TUNNEL_NAME" | grep -oP 'Created tunnel \K[a-f0-9-]+')
echo "Tunnel created with ID: $TUNNEL_ID"
# Create config file
echo "Creating config file..."
mkdir -p ~/.cloudflared
cat > ~/.cloudflared/config.yml << EOL
tunnel: ${TUNNEL_ID}
credentials-file: /home/ubuntu/.cloudflared/${TUNNEL_ID}.json
ingress:
- hostname: ${HOSTNAME}
service: http://localhost:${LOCAL_PORT}
- service: http_status:404
EOL
# Create systemd service
echo "Creating systemd service..."
sudo bash -c "cat > /etc/systemd/system/cloudflared.service << EOL
[Unit]
Description=Cloudflare Tunnel
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/usr/local/bin/cloudflared tunnel run ${TUNNEL_ID}
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOL"
# Start and enable the service
echo "Starting cloudflared service..."
sudo systemctl daemon-reload
sudo systemctl enable cloudflared
sudo systemctl start cloudflared
echo "Setup complete!"
echo "Tunnel ID: ${TUNNEL_ID}"
echo ""
echo "Next steps:"
echo "1. In your authoritative DNS (e.g., Netlify), create a CNAME record:"
echo " ${HOSTNAME} -> ${HOSTNAME}.cdn.cloudflare.net"
echo ""
echo "2. In Cloudflare dashboard, create a CNAME record:"
echo " Name: ${SUBDOMAIN}"
echo " Target: ${TUNNEL_ID}.cfargotunnel.com"
echo " Proxy status: Proxied (Orange cloud)"
echo ""
echo "3. Verify your service is running on port ${LOCAL_PORT}"