|
| 1 | +/* |
| 2 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + port string |
| 18 | + allowedOrigin string |
| 19 | +) |
| 20 | + |
| 21 | +// serverCmd represents the server command |
| 22 | +var serverCmd = &cobra.Command{ |
| 23 | + Use: "server", |
| 24 | + Short: "Golang Backend Server as a Proxy to overcome CORS errors for the Jumble Nostr client", |
| 25 | + Long: `This application is a proxy server used by the Jumble Nostr client as a workaround to fix CORS erros, |
| 26 | +so that the client can show the URL preview from links' Open Graph data.`, |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + if port == "" { |
| 29 | + port = "8000" |
| 30 | + } |
| 31 | + |
| 32 | + http.HandleFunc("GET /sites/{site}", proxyHandler) |
| 33 | + if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil { |
| 34 | + fmt.Fprintf(os.Stderr, "%s\n", err) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + log.Printf("server listening on port %s\n", port) |
| 39 | + |
| 40 | + return nil |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func proxyHandler(w http.ResponseWriter, r *http.Request) { |
| 45 | + if allowedOrigin == "" { |
| 46 | + allowedOrigin = "*" |
| 47 | + } |
| 48 | + |
| 49 | + w.Header().Set("Access-Control-Allow-Origin", allowedOrigin) |
| 50 | + w.Header().Set("Access-Control-Allow-Methods", "GET") |
| 51 | + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
| 52 | + |
| 53 | + site := r.PathValue("site") |
| 54 | + |
| 55 | + // Send request to the target site. |
| 56 | + req, err := http.NewRequest(r.Method, site, r.Body) |
| 57 | + if err != nil { |
| 58 | + http.Error(w, "Failed to create request", http.StatusInternalServerError) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Copy headers from original request. |
| 63 | + for header, values := range r.Header { |
| 64 | + for _, value := range values { |
| 65 | + req.Header.Add(header, value) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Perform the proxy request. |
| 70 | + client := &http.Client{} |
| 71 | + resp, err := client.Do(req) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("Proxy request failed %v", err) |
| 74 | + http.Error(w, "Proxy request failed", http.StatusBadGateway) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + defer resp.Body.Close() |
| 79 | + |
| 80 | + // Copy the response headers. |
| 81 | + for header, values := range resp.Header { |
| 82 | + for _, value := range values { |
| 83 | + w.Header().Add(header, value) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Set the status code and write the response body. |
| 88 | + w.WriteHeader(resp.StatusCode) |
| 89 | + body, _ := io.ReadAll(resp.Body) |
| 90 | + w.Write(body) |
| 91 | +} |
| 92 | + |
| 93 | +func init() { |
| 94 | + rootCmd.AddCommand(serverCmd) |
| 95 | + |
| 96 | + serverCmd.Flags(). |
| 97 | + StringVarP(&allowedOrigin, "allowed-origin", "a", "", "Restrict access to a specific allowed domain") |
| 98 | + serverCmd.Flags(). |
| 99 | + StringVarP(&port, "port", "p", "", "Server Port") |
| 100 | +} |
0 commit comments