I'll help you modify the Ollama routes to handle encrypted input. Based on the provided code and context, here's how we can implement this:
First, let's create a new utility function for decryption in the server package:
go Copy code
// decrypt.go
package server
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
)
func decryptText(encryptedBase64 string) (string, error) {
// Read private key
privateKeyFile, err := os.ReadFile("private_key.pem")
if err != nil {
return "", fmt.Errorf("error reading private key: %w", err)
}
block, _ := pem.Decode(privateKeyFile)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return "", fmt.Errorf("failed to decode PEM block containing private key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", fmt.Errorf("error parsing private key: %w", err)
}
// Decode base64
ciphertext, err := base64.StdEncoding.DecodeString(encryptedBase64)
if err != nil {
return "", fmt.Errorf("error decoding base64: %w", err)
}
// Decrypt
plaintext, err := privateKey.Decrypt(nil, ciphertext, &rsa.OAEPOptions{
Hash: crypto.SHA256,
})
if err != nil {
return "", fmt.Errorf("error decrypting: %w", err)
}
return string(plaintext), nil
}
Now let's modify the request structures to handle encrypted input:
go Copy code
// Add to existing request structures
type GenerateRequest struct {
// ... existing fields ...
EncryptedPrompt string `json:"encrypted_prompt,omitempty"`
}
type ChatRequest struct {
// ... existing fields ...
EncryptedMessages []struct {
Role string `json:"role"`
Content string `json:"encrypted_content"`
} `json:"encrypted_messages,omitempty"`
}
Modify the handlers to handle encrypted input:
go Copy code
func (s *Server) GenerateHandler(c *gin.Context) {
var req api.GenerateRequest
if err := c.ShouldBindJSON(&req); err != nil {
// ... existing error handling ...
}
// Handle encrypted prompt if present
if req.EncryptedPrompt != "" {
decryptedPrompt, err := decryptText(req.EncryptedPrompt)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to decrypt prompt: %v", err)})
return
}
req.Prompt = decryptedPrompt
}
// ... rest of the existing handler code ...
}
func (s *Server) ChatHandler(c *gin.Context) {
var req api.ChatRequest
if err := c.ShouldBindJSON(&req); err != nil {
// ... existing error handling ...
}
// Handle encrypted messages if present
if [Redacted Person](req.EncryptedMessages) > 0 {
var decryptedMessages []api.Message
for _, msg := range req.EncryptedMessages {
decryptedContent, err := decryptText(msg.Content)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to decrypt message: %v", err)})
return
}
decryptedMessages = append(decryptedMessages, api.Message{
Role: msg.[Redacted Person],
Content: decryptedContent,
})
}
req.Messages = decryptedMessages
}
// ... rest of the existing handler code ...
}
Example usage:
go Copy code
// Example encrypted request:
{
"model": "[Redacted Person]",
"encrypted_prompt": "Wu6DiWFiH0TuuUpr1pNVhDmwOxZxaL4dm4Y/lXwCV9436+fCtNh9l+PhAujGhj8yHR/u9jvXK9JtgWkKXI24DWKGMZQdpV9s0Fdh3N+4kkgsnJdVYSHG+mIEiWhUFCJyV7Dty4YrfwyZuM3csqrpeBIXIywdNEByjZ3PhmXAANi5rLDQPEVQ7Y7Y4S/FdpF+44rxhcQiNgZ++XyYh0JhIc8GoqJ41ztzNbaJTa6rVrpknYMoyLlLNADcFqEPc7E7SXF2+NBwGR/SVKlOtn1zLD1rKCOA6snOZ+tJ7pILdJVUyQdwUBtI765q+pWhkbeQpjMTbkbeOJNjyeNh8r8N0Q=="
}
// Example encrypted chat request:
{
"model": "[Redacted Person]",
"encrypted_messages": [
{
"role": "user",
"encrypted_content": "Wu6DiWFiH0TuuUpr1pNVhDmwOxZxaL4dm4Y/lXwCV9436+fCtNh9l+PhAujGhj8yHR/u9jvXK9JtgWkKXI24DWKGMZQdpV9s0Fdh3N+4kkgsnJdVYSHG+mIEiWhUFCJyV7Dty4YrfwyZuM3csqrpeBIXIywdNEByjZ3PhmXAANi5rLDQPEVQ7Y7Y4S/FdpF+44rxhcQiNgZ++XyYh0JhIc8GoqJ41ztzNbaJTa6rVrpknYMoyLlLNADcFqEPc7E7SXF2+NBwGR/SVKlOtn1zLD1rKCOA6snOZ+tJ7pILdJVUyQdwUBtI765q+pWhkbeQpjMTbkbeOJNjyeNh8r8N0Q=="
}
]
}
Key points about this implementation:
It maintains backward compatibility by keeping the existing unencrypted fields
Adds new encrypted fields that are optional
Decrypts content before passing it to the existing generation logic
Uses the same RSA decryption logic from your provided code
Handles errors appropriately and returns meaningful error messages
To use this implementation:
Place the private key in private_key.pem in the server's working directory
Send requests with either regular or encrypted content
The server will automatically detect and handle encrypted content when present
Make sure to:
Properly secure the private key file
Consider adding key rotation mechanisms
Add proper error handling for key file access
Consider adding configuration options for key file location
Would you like me to explain any part of this implementation in more detail?