Spaces:
Sleeping
Sleeping
| import json | |
| import gradio as gr | |
| from cryptography import x509 | |
| from cryptography.hazmat.backends import default_backend | |
| from decode import decode_ssl_certificate | |
| def decode(cert) -> str: | |
| try: | |
| # Ensure cert is a byte string | |
| if isinstance(cert, str): | |
| cert = cert.encode() | |
| try: | |
| decoded_cert = None | |
| try: | |
| decoded_cert = x509.load_pem_x509_certificate(cert, default_backend()) | |
| except Exception: | |
| decoded_cert = x509.load_pem_x509_csr(cert, default_backend()) | |
| if decoded_cert: | |
| decoded_data = decode_ssl_certificate(decoded_cert) | |
| response = { | |
| "status": "Success", | |
| "message": "Certificate decoded successfully.", | |
| "data": decoded_data | |
| } | |
| else: | |
| response = { | |
| "status": "Failed", | |
| "message": "Invalid certificate format.", | |
| "data": None | |
| } | |
| except Exception as e: | |
| response = { | |
| "status": "Failed", | |
| "message": "Failed to decode certificate. Please upload a valid certificate file.", | |
| "error": str(e) # Convert Exception to a string | |
| } | |
| except Exception as e: | |
| response = { | |
| "status": "Failed", | |
| "message": "Unexpected error while processing the certificate.", | |
| "error": str(e) | |
| } | |
| print(response) | |
| return json.dumps(response, indent=4) # Always return a JSON string | |
| def app(): | |
| with gr.Blocks(title="Project Gatekeeper - Get free SSL Certificates") as webui: | |
| with gr.Row(): | |
| ssl = gr.Textbox(label="Enter Domains", type="text", interactive=True) | |
| with gr.Row(): | |
| decoded_data = gr.Textbox(label="Decoded SSL Data", type="text", interactive=False, show_copy_button=True) | |
| btn = gr.Button(value="Generate SSL Certificate") | |
| btn.click(decode, inputs=ssl, outputs=decoded_data) | |
| try: | |
| webui.queue(default_concurrency_limit=15).launch() | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| if __name__ == "__main__": | |
| app() |