mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
feat: add stub_status_port configuration for Nginx settings
This commit is contained in:
parent
2d56914af1
commit
509443a6e7
9 changed files with 88 additions and 40 deletions
|
@ -1,6 +1,7 @@
|
|||
package nginx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -8,8 +9,10 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
@ -31,7 +34,6 @@ type StubStatusData struct {
|
|||
}
|
||||
|
||||
const (
|
||||
StubStatusPort = 51828
|
||||
StubStatusPath = "/stub_status"
|
||||
StubStatusHost = "127.0.0.1"
|
||||
StubStatusProtocol = "http"
|
||||
|
@ -54,7 +56,7 @@ func GetStubStatusData() (bool, *StubStatusData, error) {
|
|||
|
||||
// Get the stub_status status information
|
||||
enabled, statusURL := IsStubStatusEnabled()
|
||||
logger.Info("GetStubStatusData", "enabled", enabled, "statusURL", statusURL)
|
||||
logger.Debug("GetStubStatusData", "enabled", enabled, "statusURL", statusURL)
|
||||
if !enabled {
|
||||
return false, result, fmt.Errorf("stub_status is not enabled")
|
||||
}
|
||||
|
@ -131,12 +133,12 @@ func IsStubStatusEnabled() (bool, string) {
|
|||
for _, server := range ngxConfig.Servers {
|
||||
protocol := StubStatusProtocol
|
||||
host := StubStatusHost
|
||||
port := strconv.Itoa(StubStatusPort)
|
||||
port := settings.NginxSettings.StubStatusPort
|
||||
|
||||
for _, location := range server.Locations {
|
||||
// Check if the location content contains stub_status
|
||||
if strings.Contains(location.Content, "stub_status") {
|
||||
stubStatusURL := fmt.Sprintf("%s://%s:%s%s", protocol, host, port, StubStatusPath)
|
||||
stubStatusURL := fmt.Sprintf("%s://%s:%d%s", protocol, host, port, StubStatusPath)
|
||||
return true, stubStatusURL
|
||||
}
|
||||
}
|
||||
|
@ -169,23 +171,54 @@ func DisableStubStatus() error {
|
|||
func CreateStubStatusConfig() error {
|
||||
httpConfPath := GetConfPath("conf.d", StubStatusConfigName)
|
||||
|
||||
stubStatusConfig := `
|
||||
const stubStatusTemplate = `
|
||||
# DO NOT EDIT THIS FILE, IT IS AUTO GENERATED BY NGINX-UI
|
||||
# Nginx stub_status configuration for Nginx-UI
|
||||
# Modified at ` + time.Now().Format("2006-01-02 15:04:05") + `
|
||||
# Modified at {{.ModifiedTime}}
|
||||
|
||||
server {
|
||||
listen 51828; # Use non-standard port to avoid conflicts
|
||||
server_name localhost;
|
||||
listen {{.Port}}; # Use non-standard port to avoid conflicts
|
||||
server_name {{.ServerName}};
|
||||
|
||||
# Status monitoring interface
|
||||
location /stub_status {
|
||||
location {{.StatusPath}} {
|
||||
stub_status;
|
||||
allow 127.0.0.1; # Only allow local access
|
||||
deny all;
|
||||
allow {{.AllowIP}}; # Only allow local access
|
||||
deny {{.DenyAccess}};
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
type StubStatusTemplateData struct {
|
||||
ModifiedTime string
|
||||
Port uint
|
||||
ServerName string
|
||||
StatusPath string
|
||||
AllowIP string
|
||||
DenyAccess string
|
||||
}
|
||||
|
||||
data := StubStatusTemplateData{
|
||||
ModifiedTime: time.Now().Format(time.DateTime),
|
||||
Port: settings.NginxSettings.StubStatusPort,
|
||||
ServerName: "localhost",
|
||||
StatusPath: StubStatusPath,
|
||||
AllowIP: StubStatusAllow,
|
||||
DenyAccess: StubStatusDeny,
|
||||
}
|
||||
|
||||
tmpl, err := template.New("stub_status").Parse(stubStatusTemplate)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to parse template")
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, data); err != nil {
|
||||
return errors.Wrap(err, "failed to execute template")
|
||||
}
|
||||
|
||||
stubStatusConfig := buf.String()
|
||||
|
||||
ngxConfig, err := ParseNgxConfigByContent(stubStatusConfig)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to parse new nginx config")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue