|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Simple server for KaraKeep Homedash with preference persistence |
4 | | -This is optional - you can use any static file server if you don't need |
5 | | -cross-device preference syncing. |
| 3 | +Simple server for KaraKeep Homedash with preference persistence and API proxy |
| 4 | +This server also proxies API requests to KaraKeep to avoid CORS issues. |
6 | 5 | """ |
7 | 6 |
|
8 | 7 | from http.server import HTTPServer, SimpleHTTPRequestHandler |
9 | 8 | import json |
10 | 9 | import os |
11 | 10 | from urllib.parse import urlparse |
| 11 | +import urllib.request |
| 12 | +import urllib.error |
| 13 | + |
| 14 | +# Try to import ssl, but don't fail if it's not available |
| 15 | +try: |
| 16 | + import ssl |
| 17 | + SSL_AVAILABLE = True |
| 18 | +except ImportError: |
| 19 | + SSL_AVAILABLE = False |
| 20 | + print("Warning: SSL module not available. HTTPS connections may fail.") |
12 | 21 |
|
13 | 22 | DEFAULT_CONFIG = { |
14 | 23 | "karakeepUrl": "http://localhost:3000", |
@@ -40,9 +49,71 @@ def do_GET(self): |
40 | 49 | if self.path == '/config.json': |
41 | 50 | self.path = '/config/config.json' |
42 | 51 |
|
| 52 | + # Proxy API requests to KaraKeep |
| 53 | + elif self.path.startswith('/api/karakeep/'): |
| 54 | + self.proxy_to_karakeep('GET') |
| 55 | + return |
| 56 | + |
43 | 57 | # Serve files normally |
44 | 58 | return super().do_GET() |
45 | 59 |
|
| 60 | + def proxy_to_karakeep(self, method): |
| 61 | + """Proxy requests to KaraKeep API""" |
| 62 | + try: |
| 63 | + # Load config to get KaraKeep URL and API key |
| 64 | + config_path = 'config/config.json' |
| 65 | + with open(config_path, 'r') as f: |
| 66 | + config = json.load(f) |
| 67 | + |
| 68 | + karakeep_url = config.get('karakeepUrl', 'http://localhost:3000') |
| 69 | + api_key = config.get('apiKey', '') |
| 70 | + |
| 71 | + # Remove /api/karakeep prefix and construct the actual API path |
| 72 | + api_path = self.path.replace('/api/karakeep', '/api', 1) |
| 73 | + full_url = f"{karakeep_url}{api_path}" |
| 74 | + |
| 75 | + # Create request with auth header |
| 76 | + req = urllib.request.Request(full_url) |
| 77 | + req.add_header('Authorization', f'Bearer {api_key}') |
| 78 | + req.add_header('Content-Type', 'application/json') |
| 79 | + |
| 80 | + # Create SSL context that handles self-signed certificates |
| 81 | + # Note: This accepts any certificate, including self-signed ones |
| 82 | + # In production with proper certificates, you may want to remove these lines |
| 83 | + ssl_context = None |
| 84 | + if SSL_AVAILABLE and full_url.startswith('https://'): |
| 85 | + ssl_context = ssl.create_default_context() |
| 86 | + ssl_context.check_hostname = False |
| 87 | + ssl_context.verify_mode = ssl.CERT_NONE |
| 88 | + |
| 89 | + # Make the request |
| 90 | + if ssl_context: |
| 91 | + response = urllib.request.urlopen(req, context=ssl_context) |
| 92 | + else: |
| 93 | + response = urllib.request.urlopen(req) |
| 94 | + data = response.read() |
| 95 | + |
| 96 | + # Send response back to client |
| 97 | + self.send_response(response.getcode()) |
| 98 | + self.send_header('Content-Type', 'application/json') |
| 99 | + self.end_headers() |
| 100 | + self.wfile.write(data) |
| 101 | + |
| 102 | + except urllib.error.HTTPError as e: |
| 103 | + # Forward HTTP errors |
| 104 | + self.send_response(e.code) |
| 105 | + self.send_header('Content-Type', 'application/json') |
| 106 | + self.end_headers() |
| 107 | + error_data = e.read() |
| 108 | + self.wfile.write(error_data if error_data else json.dumps({"error": str(e)}).encode()) |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + # Handle other errors |
| 112 | + self.send_response(500) |
| 113 | + self.send_header('Content-Type', 'application/json') |
| 114 | + self.end_headers() |
| 115 | + self.wfile.write(json.dumps({"error": str(e)}).encode()) |
| 116 | + |
46 | 117 | def do_POST(self): |
47 | 118 | """Handle POST requests for saving preferences""" |
48 | 119 | if self.path == '/api/preferences': |
@@ -105,6 +176,7 @@ def do_OPTIONS(self): |
105 | 176 | httpd = HTTPServer(server_address, KaraKeepHandler) |
106 | 177 |
|
107 | 178 | print(f"KaraKeep HomeDash Server running on http://localhost:{port}") |
| 179 | + print("API requests will be proxied to avoid CORS issues") |
108 | 180 | print("Press Ctrl+C to stop") |
109 | 181 |
|
110 | 182 | try: |
|
0 commit comments