Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Added acme-challenge proxy.

dreambladeflag9cb1da76fc74
diff --git a/core/internal/cmd/cmd.go b/core/internal/cmd/cmd.go
index 5ad2b9fb..7307532e 100644
--- a/core/internal/cmd/cmd.go
+++ b/core/internal/cmd/cmd.go
@@ -18,6 +18,11 @@ import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/glog"
+ "net"
+ "net/http"
+ "net/http/httputil"
+ "net/url"
+ "time"
)
var (
@@ -93,6 +98,38 @@ var (
Static: consts.ROUNDCUBE_ROOT_PATH,
}))
+ // Proxy unix socket for ACME challenge
+ s.BindHandler("/.well-known/acme-challenge/*any", func(r *ghttp.Request) {
+ // Set the backend URL to the Unix socket
+ socketPath := "/tmp/acme-challenge.sock"
+ backendURL, err := url.Parse(r.GetSchema() + "://unix" + socketPath)
+ if err != nil {
+ glog.Error(r.Context(), "Error parsing backend URL:", err)
+ r.Response.WriteStatus(http.StatusInternalServerError)
+ return
+ }
+
+ // Set up the dialer to connect to the Unix socket
+ dialer := &net.Dialer{
+ Timeout: 5 * time.Second,
+ }
+
+ proxy := httputil.NewSingleHostReverseProxy(backendURL)
+ proxy.Transport = &http.Transport{
+ // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
+ return dialer.Dial("unix", socketPath)
+ },
+ }
+
+ // Save the original host header
+ originalHost := r.Host
+ r.Header.Set("X-Forwarded-Host", originalHost)
+
+ // Forward the request to the backend
+ proxy.ServeHTTP(r.Response.Writer, r.Request)
+ })
+
s.Run()
return nil
},
diff --git a/core/internal/service/acme/acme.go b/core/internal/service/acme/acme.go
index 302406c5..57ddce76 100644
--- a/core/internal/service/acme/acme.go
+++ b/core/internal/service/acme/acme.go
@@ -11,12 +11,9 @@ import (
"encoding/pem"
"errors"
"fmt"
- "net/http"
+ srcLog "log"
"os"
"path/filepath"
- "time"
-
- srcLog "log"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
@@ -322,67 +319,6 @@ func CloseLog(logFile *os.File) {
logFile.Close()
}
-/**
- * @brief Start HTTP verification server for Let's Encrypt challenge
- * @param port Port to listen on
- * @return Server and error
- */
-func StartHTTPVerificationServer(port string) (*http.Server, error) {
- // Create a custom HTTP server to handle the challenge
- handler := http.NewServeMux()
-
- // Handle ACME HTTP-01 challenge requests
- handler.HandleFunc("/.well-known/acme-challenge/", func(w http.ResponseWriter, r *http.Request) {
- // The challenge path is managed by the go-acme/lego library
- if r.Method != http.MethodGet {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
-
- // The challenge handler is managed by the go-acme/lego library
- // We don't need to do anything here - lego will set up the responses
- })
-
- // Create a server
- server := &http.Server{
- Addr: ":" + port,
- Handler: handler,
- }
-
- // Start the server in a goroutine
- go func() {
- err := server.ListenAndServe()
- if err != nil && err != http.ErrServerClosed {
- fmt.Printf("HTTP verification server error: %s\n", err)
- }
- }()
-
- // Wait a moment for the server to start
- time.Sleep(100 * time.Millisecond)
-
- return server, nil
-}
-
-/**
- * @brief Get HTTP challenge handler that can be integrated into existing web servers
- * @return http.Handler HTTP handler for ACME challenges
- */
-func GetHTTPChallengeHandler() http.Handler {
- mux := http.NewServeMux()
-
- // Handle ACME HTTP-01 challenge requests
- mux.HandleFunc("/.well-known/acme-challenge/", func(w http.ResponseWriter, r *http.Request) {
- // The actual response will be handled by the lego library
- // We just need to route the request to the correct path
- if r.Method != http.MethodGet {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- })
-
- return mux
-}
-
/**
* @brief Apply for SSL certificate with an existing HTTP server
* @param domains Domain list
@@ -422,7 +358,7 @@ func ApplySSLWithExistingServer(ctx context.Context, domains []string, email str
if vtype == "http" {
// Assume the HTTP server is already running and properly configured
// to handle the challenge requests
- err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "80"))
+ err = client.Challenge.SetHTTP01Provider(http01.NewUnixProviderServer(public.AbsPath("/tmp/acme-challenge.sock"), os.ModePerm))
if err != nil {
return "", "", errors.New(public.LangCtx(ctx, "Failed to set HTTP verification: {}", err.Error()))
}