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

optimized phpfpm.go add webmail proxy.

Jacka9389659ea0b
diff --git a/core/internal/cmd/cmd.go b/core/internal/cmd/cmd.go
index 09cb6c44..daefde6b 100644
--- a/core/internal/cmd/cmd.go
+++ b/core/internal/cmd/cmd.go
@@ -22,7 +22,8 @@ var (
hello.NewV1(),
)
})
- s.BindHandler("/php-fpm/*any", phpfpm.PHPFpm)
+
+ s.BindHandler("/php-fpm/*any", phpfpm.PHPFpmHandlerFactory("unix", "../php-sock/php-fpm.sock", "/var/www/html"))
s.Run()
return nil
},
diff --git a/core/internal/service/phpfpm/phpfpm.go b/core/internal/service/phpfpm/phpfpm.go
index b4649ea6..45a920c2 100644
--- a/core/internal/service/phpfpm/phpfpm.go
+++ b/core/internal/service/phpfpm/phpfpm.go
@@ -4,77 +4,93 @@ import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/glog"
+ "github.com/gogf/gf/v2/util/gconv"
"io"
+ "path/filepath"
+ "strings"
+ "time"
"github.com/tomasen/fcgi_client"
)
-// Transfer PHP-FPM requests to PHP-FPM server via FastCGI
-func PHPFpm(r *ghttp.Request) {
- // Get the requested file path
- filePath := r.URL.Path
-
- // Configure FastCGI client
- fcgiAddr := "/var/run/php/php-fpm.sock" // Default PHP-FPM address, adjust as needed
-
- // Create environment variables for FastCGI
- env := map[string]string{
- "SCRIPT_FILENAME": "/var/www/html" + filePath, // Adjust document root as needed
- "REQUEST_METHOD": r.Method,
- "SCRIPT_NAME": filePath,
- "REQUEST_URI": r.RequestURI,
- "QUERY_STRING": r.URL.RawQuery,
- "CONTENT_TYPE": r.Header.Get("Content-Type"),
- "CONTENT_LENGTH": r.Header.Get("Content-Length"),
- "REMOTE_ADDR": r.RemoteAddr,
- "SERVER_NAME": r.Host,
- "SERVER_PORT": r.Request.Header.Get("X-Forwarded-Port"),
- "SERVER_PROTOCOL": r.Proto,
- "HTTP_HOST": r.Host,
- "HTTP_USER_AGENT": r.Header.Get("User-Agent"),
- "HTTP_ACCEPT": r.Header.Get("Accept"),
- }
+// PHPFpm Handler Factory
+func PHPFpmHandlerFactory(network, fcgiAddr, rootPath string) ghttp.HandlerFunc {
+ return func(r *ghttp.Request) {
+ // Get the requested file path
+ filePath := "/" + r.Get("any").String()
- // Add HTTP headers to environment
- for key, values := range r.Header {
- if len(values) > 0 {
- env["HTTP_"+key] = values[0]
+ if !strings.HasSuffix(filePath, ".php") {
+ r.Response.ServeFile(filepath.Join("../webmail-data", filePath))
+ return
}
- }
- fc, err := fcgiclient.Dial("unix", fcgiAddr)
+ https := "off"
- if err != nil {
- glog.Error(context.Background(), err)
- return
- }
+ if r.GetSchema() == "https" {
+ https = "on"
+ }
- defer fc.Close()
+ // Create environment variables for FastCGI
+ env := map[string]string{
+ "SCRIPT_FILENAME": filepath.Join(rootPath, filePath), // Adjust document root as needed
+ "REQUEST_METHOD": r.Method,
+ "SCRIPT_NAME": filePath,
+ "REQUEST_URI": r.RequestURI,
+ "QUERY_STRING": r.URL.RawQuery,
+ "CONTENT_TYPE": r.Header.Get("Content-Type"),
+ "CONTENT_LENGTH": r.Header.Get("Content-Length"),
+ "REMOTE_ADDR": r.RemoteAddr,
+ "SERVER_NAME": r.Host,
+ "SERVER_PORT": r.Request.Header.Get("X-Forwarded-Port"),
+ "SERVER_PROTOCOL": r.Proto,
+ "HTTPS": https,
+ "REQUEST_TIME": gconv.String(time.Now().Unix()),
+ "HTTP_HOST": r.Host,
+ "HTTP_USER_AGENT": r.Header.Get("User-Agent"),
+ "HTTP_ACCEPT": r.Header.Get("Accept"),
+ }
- resp, err := fc.Request(env, r.Body)
+ // Add HTTP headers to environment
+ for key, values := range r.Header {
+ if len(values) > 0 {
+ env["HTTP_"+key] = values[0]
+ }
+ }
- if err != nil {
- glog.Error(context.Background(), err)
- return
- }
+ fc, err := fcgiclient.Dial(network, fcgiAddr)
- defer resp.Body.Close()
+ if err != nil {
+ glog.Error(context.Background(), err)
+ return
+ }
- // Copy headers from PHP response to our response
- for key, values := range resp.Header {
- for _, value := range values {
- r.Response.Header().Add(key, value)
+ defer fc.Close()
+
+ resp, err := fc.Request(env, r.Body)
+
+ if err != nil {
+ glog.Error(context.Background(), err)
+ return
}
- }
- // Set status code
- r.Response.WriteStatus(resp.StatusCode)
+ defer resp.Body.Close()
- // Copy response body
- _, err = io.Copy(r.Response.Writer, resp.Body)
+ // Copy headers from PHP response to our response
+ for key, values := range resp.Header {
+ for _, value := range values {
+ r.Response.Header().Add(key, value)
+ }
+ }
- if err != nil {
- glog.Error(context.Background(), err)
- return
+ // Set status code
+ r.Response.WriteStatus(resp.StatusCode)
+
+ // Copy response body
+ _, err = io.Copy(r.Response.Writer, resp.Body)
+
+ if err != nil {
+ glog.Error(context.Background(), err)
+ return
+ }
}
}