File size: 11,160 Bytes
32e4bbf |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
#!/usr/bin/env python3
"""
Cloudflare Manager Web Interface
For deployment on Hugging Face Spaces
"""
import gradio as gr
import os
from cloudflare_manager import CloudflareManager, CloudflareAccount
def test_connection(email, token):
"""Test Cloudflare API connection"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
if cf.account.account_id:
return f"β Connected!\n\nAccount: {cf.account.name}\nID: {cf.account.account_id}"
else:
return "β Failed to connect. Please check your credentials."
except Exception as e:
return f"β Error: {str(e)}"
def list_projects(email, token):
"""List all Pages projects"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
projects = cf.list_pages_projects()
if not projects:
return "No projects found."
result = f"Found {len(projects)} project(s):\n\n"
for project in projects:
result += f"π¦ {project['name']}\n"
result += f" URL: https://{project.get('subdomain', 'N/A')}\n"
result += f" Created: {project.get('created_on', 'N/A')}\n\n"
return result
except Exception as e:
return f"β Error: {str(e)}"
def create_project(email, token, project_name, branch):
"""Create a new Pages project"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
project = cf.create_pages_project(project_name, branch)
if project:
return f"β Project created!\n\nName: {project['name']}\nURL: https://{project.get('subdomain')}\n"
else:
return "β Failed to create project"
except Exception as e:
return f"β Error: {str(e)}"
def list_zones(email, token):
"""List all zones"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
zones = cf.list_zones()
if not zones:
return "No zones found."
result = f"Found {len(zones)} zone(s):\n\n"
for zone in zones:
result += f"π {zone['name']}\n"
result += f" Zone ID: {zone['id']}\n"
result += f" Status: {zone.get('status', 'unknown')}\n"
nameservers = zone.get('name_servers', [])
if nameservers:
result += f" Nameservers:\n"
for ns in nameservers:
result += f" - {ns}\n"
result += "\n"
return result
except Exception as e:
return f"β Error: {str(e)}"
def create_zone_and_get_ns(email, token, domain_name):
"""Create zone and get nameservers"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
zone = cf.create_zone(domain_name)
if zone:
nameservers = zone.get('name_servers', [])
result = f"β Zone created for {domain_name}\n\n"
result += f"Zone ID: {zone['id']}\n\n"
result += "π Add these nameservers to your domain registrar:\n\n"
for ns in nameservers:
result += f" {ns}\n"
return result
else:
return "β Failed to create zone"
except Exception as e:
return f"β Error: {str(e)}"
def bind_domain(email, token, project_name, domain_name):
"""Bind domain to Pages project"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
result_data = cf.add_pages_domain(project_name, domain_name)
if result_data:
result = f"β Domain bound to project!\n\n"
result += f"Domain: {result_data.get('name')}\n"
result += f"Status: {result_data.get('status')}\n\n"
if result_data.get('validation_data'):
val = result_data['validation_data']
result += f"DNS Validation Required:\n"
result += f" Type: {val.get('type')}\n"
result += f" Name: {val.get('name')}\n"
result += f" Value: {val.get('value')}\n"
return result
else:
return "β Failed to bind domain"
except Exception as e:
return f"β Error: {str(e)}"
def create_worker_route(email, token, zone_id, pattern, script_name):
"""Create worker route"""
try:
account = CloudflareAccount(email=email, token=token, use_api_key=True)
cf = CloudflareManager(account)
route = cf.create_worker_route(zone_id, pattern, script_name)
if route:
return f"β Worker route created!\n\nRoute ID: {route.get('id')}\nPattern: {pattern}\nScript: {script_name}"
else:
return "β Failed to create route"
except Exception as e:
return f"β Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="Cloudflare Manager") as demo:
gr.Markdown("""
# π Cloudflare Multi-Account Manager
Manage Cloudflare Pages, Domains, and Workers through a simple web interface.
## Features
- β
Pages project management
- β
Domain binding
- β
Nameserver lookup
- β
Worker route configuration
""")
with gr.Row():
with gr.Column():
email_input = gr.Textbox(
label="Cloudflare Email",
placeholder="your-email@example.com",
value=os.getenv("CLOUDFLARE_EMAIL", "")
)
token_input = gr.Textbox(
label="API Token",
type="password",
placeholder="Your API Token",
value=os.getenv("CLOUDFLARE_TOKEN", "")
)
with gr.Tabs():
# Tab 1: Connection Test
with gr.Tab("π Connection Test"):
test_btn = gr.Button("Test Connection", variant="primary")
test_output = gr.Textbox(label="Result", lines=5)
test_btn.click(
test_connection,
inputs=[email_input, token_input],
outputs=test_output
)
# Tab 2: Pages Projects
with gr.Tab("π¦ Pages Projects"):
with gr.Row():
with gr.Column():
gr.Markdown("### List Projects")
list_projects_btn = gr.Button("List Projects")
list_projects_output = gr.Textbox(label="Projects", lines=10)
list_projects_btn.click(
list_projects,
inputs=[email_input, token_input],
outputs=list_projects_output
)
with gr.Column():
gr.Markdown("### Create Project")
project_name = gr.Textbox(label="Project Name", placeholder="my-website")
project_branch = gr.Textbox(label="Branch", value="main")
create_project_btn = gr.Button("Create Project", variant="primary")
create_project_output = gr.Textbox(label="Result", lines=10)
create_project_btn.click(
create_project,
inputs=[email_input, token_input, project_name, project_branch],
outputs=create_project_output
)
# Tab 3: Domains & Zones
with gr.Tab("π Domains & Zones"):
with gr.Row():
with gr.Column():
gr.Markdown("### List Zones")
list_zones_btn = gr.Button("List Zones")
list_zones_output = gr.Textbox(label="Zones", lines=10)
list_zones_btn.click(
list_zones,
inputs=[email_input, token_input],
outputs=list_zones_output
)
with gr.Column():
gr.Markdown("### Create Zone & Get Nameservers")
zone_domain = gr.Textbox(label="Domain Name", placeholder="example.com")
create_zone_btn = gr.Button("Create Zone", variant="primary")
create_zone_output = gr.Textbox(label="Result", lines=10)
create_zone_btn.click(
create_zone_and_get_ns,
inputs=[email_input, token_input, zone_domain],
outputs=create_zone_output
)
# Tab 4: Domain Binding
with gr.Tab("π Bind Domain"):
gr.Markdown("### Bind Domain to Pages Project")
with gr.Row():
bind_project = gr.Textbox(label="Project Name", placeholder="my-website")
bind_domain_name = gr.Textbox(label="Domain", placeholder="example.com")
bind_btn = gr.Button("Bind Domain", variant="primary")
bind_output = gr.Textbox(label="Result", lines=10)
bind_btn.click(
bind_domain,
inputs=[email_input, token_input, bind_project, bind_domain_name],
outputs=bind_output
)
# Tab 5: Worker Routes
with gr.Tab("β‘ Worker Routes"):
gr.Markdown("### Create Worker Route")
worker_zone_id = gr.Textbox(label="Zone ID", placeholder="abc123...")
worker_pattern = gr.Textbox(label="Route Pattern", placeholder="example.com/api/*")
worker_script = gr.Textbox(label="Script Name", placeholder="my-worker")
worker_btn = gr.Button("Create Route", variant="primary")
worker_output = gr.Textbox(label="Result", lines=10)
worker_btn.click(
create_worker_route,
inputs=[email_input, token_input, worker_zone_id, worker_pattern, worker_script],
outputs=worker_output
)
gr.Markdown("""
---
### π Documentation
- [Quick Start Guide](GET_STARTED.md)
- [Usage Guide](USAGE_GUIDE.md)
- [API Reference](API_REFERENCE.md)
### π API Token Permissions
Your API token needs:
- Account > Cloudflare Pages > Edit
- Zone > DNS > Edit
- Zone > Workers Routes > Edit
### π‘ Tips
1. Get your API token from: https://dash.cloudflare.com/profile/api-tokens
2. After creating a zone, add the nameservers to your domain registrar
3. DNS propagation takes 5-30 minutes
""")
if __name__ == "__main__":
# Run with sharing enabled for Hugging Face Spaces
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
|