feat(site): index url #736,#310

This commit is contained in:
Jacky 2025-04-05 12:50:46 +00:00
parent 94fcbf6362
commit c013c468a0
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
20 changed files with 559 additions and 355 deletions

View file

@ -19,4 +19,5 @@ type Config struct {
EnvGroup *model.EnvGroup `json:"env_group,omitempty"`
Enabled bool `json:"enabled"`
Dir string `json:"dir"`
Urls []string `json:"urls,omitempty"`
}

View file

@ -35,7 +35,7 @@ func Disable(name string) (err error) {
output := nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf(output)
return fmt.Errorf("%s", output)
}
go syncDisable(name)

View file

@ -37,12 +37,12 @@ func Enable(name string) (err error) {
output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn {
_ = os.Remove(enabledConfigFilePath)
return fmt.Errorf(output)
return fmt.Errorf("%s", output)
}
output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf(output)
return fmt.Errorf("%s", output)
}
go syncEnable(name)

132
internal/site/index.go Normal file
View file

@ -0,0 +1,132 @@
package site
import (
"net"
"path/filepath"
"regexp"
"strings"
"github.com/0xJacky/Nginx-UI/internal/cache"
)
type SiteIndex struct {
Path string
Content string
Urls []string
}
var (
IndexedSites = make(map[string]*SiteIndex)
)
func GetIndexedSite(path string) *SiteIndex {
if site, ok := IndexedSites[path]; ok {
return site
}
return &SiteIndex{}
}
func init() {
cache.RegisterCallback(scanForSite)
}
func scanForSite(configPath string, content []byte) error {
// Regular expressions for server_name and listen directives
serverNameRegex := regexp.MustCompile(`(?m)server_name\s+([^;]+);`)
listenRegex := regexp.MustCompile(`(?m)listen\s+([^;]+);`)
// Find server blocks
serverBlockRegex := regexp.MustCompile(`(?ms)server\s*\{[^\{]*((.*?\{.*?\})*?[^\}]*)\}`)
serverBlocks := serverBlockRegex.FindAllSubmatch(content, -1)
siteIndex := SiteIndex{
Path: configPath,
Content: string(content),
Urls: []string{},
}
// Map to track hosts and their SSL status
hostMap := make(map[string]bool)
for _, block := range serverBlocks {
serverBlockContent := block[0]
// Extract server_name values
serverNameMatches := serverNameRegex.FindSubmatch(serverBlockContent)
if len(serverNameMatches) < 2 {
continue
}
// Get all server names
serverNames := strings.Fields(string(serverNameMatches[1]))
var validServerNames []string
// Filter valid domain names and IPs
for _, name := range serverNames {
// Skip placeholder names
if name == "_" || name == "localhost" {
continue
}
// Check if it's a valid IP
if net.ParseIP(name) != nil {
validServerNames = append(validServerNames, name)
continue
}
// Basic domain validation
if isValidDomain(name) {
validServerNames = append(validServerNames, name)
}
}
if len(validServerNames) == 0 {
continue
}
// Check if SSL is enabled
listenMatches := listenRegex.FindAllSubmatch(serverBlockContent, -1)
hasSSL := false
for _, match := range listenMatches {
if len(match) >= 2 {
listenValue := string(match[1])
if strings.Contains(listenValue, "ssl") || strings.Contains(listenValue, "443") {
hasSSL = true
break
}
}
}
// Update host map with SSL status
for _, name := range validServerNames {
// Only update if this host doesn't have SSL yet
if currentSSL, exists := hostMap[name]; !exists || !currentSSL {
hostMap[name] = hasSSL
}
}
}
// Generate URLs from the host map
for host, hasSSL := range hostMap {
protocol := "http"
if hasSSL {
protocol = "https"
}
url := protocol + "://" + host
siteIndex.Urls = append(siteIndex.Urls, url)
}
// Only store if we found valid URLs
if len(siteIndex.Urls) > 0 {
IndexedSites[filepath.Base(configPath)] = &siteIndex
}
return nil
}
// isValidDomain performs a basic validation of domain names
func isValidDomain(domain string) bool {
// Basic validation: contains at least one dot and no spaces
return strings.Contains(domain, ".") && !strings.Contains(domain, " ")
}

View file

@ -49,13 +49,13 @@ func Rename(oldName string, newName string) (err error) {
// test nginx configuration
output := nginx.TestConf()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf(output)
return fmt.Errorf("%s", output)
}
// reload nginx
output = nginx.Reload()
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf(output)
return fmt.Errorf("%s", output)
}
go syncRename(oldName, newName)