| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 | package self_checkimport (	"fmt"	"os"	"strings"	"time"	"github.com/0xJacky/Nginx-UI/internal/nginx"	"github.com/tufanbarisyildirim/gonginx/config"	"github.com/tufanbarisyildirim/gonginx/dumper"	"github.com/tufanbarisyildirim/gonginx/parser")// CheckNginxConfIncludeSites checks if nginx.conf include sites-enabledfunc CheckNginxConfIncludeSites() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find http block	for _, v := range c.Block.Directives {		if v.GetName() == "http" {			// find include sites-enabled			for _, directive := range v.GetBlock().GetDirectives() {				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&					directive.GetParameters()[0].Value == nginx.GetConfPath("sites-enabled/*") {					return nil				}			}			return ErrNginxConfNotIncludeSitesEnabled		}	}	return ErrNginxConfNoHttpBlock}// CheckNginxConfIncludeStreams checks if nginx.conf include streams-enabledfunc CheckNginxConfIncludeStreams() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find http block	for _, v := range c.Block.Directives {		if v.GetName() == "stream" {			// find include sites-enabled			for _, directive := range v.GetBlock().GetDirectives() {				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&					directive.GetParameters()[0].Value == nginx.GetConfPath("streams-enabled/*") {					return nil				}			}			return ErrNginxConfNotIncludeStreamEnabled		}	}	return ErrNginxConfNoStreamBlock}// FixNginxConfIncludeSites attempts to fix nginx.conf include sites-enabledfunc FixNginxConfIncludeSites() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// create a backup file (+.bak.timestamp)	backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())	err = os.WriteFile(backupPath, content, 0644)	if err != nil {		return ErrFailedToCreateBackup	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find http block	for _, v := range c.Block.Directives {		if v.GetName() == "http" {			// add include sites-enabled/* to http block			includeDirective := &config.Directive{				Name:       "include",				Parameters: []config.Parameter{{Value: nginx.GetConfPath("sites-enabled/*")}},			}			realBlock := v.GetBlock().(*config.HTTP)			realBlock.Directives = append(realBlock.Directives, includeDirective)			// write to file			return os.WriteFile(path, []byte(dumper.DumpBlock(c.Block, dumper.IndentedStyle)), 0644)		}	}	// if no http block, append http block with include sites-enabled/*	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("sites-enabled/*"))...)	return os.WriteFile(path, content, 0644)}// FixNginxConfIncludeStreams attempts to fix nginx.conf include streams-enabledfunc FixNginxConfIncludeStreams() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// create a backup file (+.bak.timestamp)	backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())	err = os.WriteFile(backupPath, content, 0644)	if err != nil {		return ErrFailedToCreateBackup	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find stream block	for _, v := range c.Block.Directives {		if v.GetName() == "stream" {			// add include streams-enabled/* to stream block			includeDirective := &config.Directive{				Name:       "include",				Parameters: []config.Parameter{{Value: nginx.GetConfPath("streams-enabled/*")}},			}			realBlock := v.GetBlock().(*config.Block)			realBlock.Directives = append(realBlock.Directives, includeDirective)			// write to file			return os.WriteFile(path, []byte(dumper.DumpBlock(c.Block, dumper.IndentedStyle)), 0644)		}	}	// if no stream block, append stream block with include streams-enabled/*	content = append(content, fmt.Appendf(nil, "\nstream {\n\tinclude %s;\n}\n", nginx.GetConfPath("streams-enabled/*"))...)	return os.WriteFile(path, content, 0644)}// CheckNginxConfIncludeConfD checks if nginx.conf includes conf.d directoryfunc CheckNginxConfIncludeConfD() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find http block	for _, v := range c.Block.Directives {		if v.GetName() == "http" {			// find include conf.d			for _, directive := range v.GetBlock().GetDirectives() {				if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&					strings.HasPrefix(directive.GetParameters()[0].Value, nginx.GetConfPath("conf.d")) {					return nil				}			}			return ErrNginxConfNotIncludeConfD		}	}	return ErrNginxConfNoHttpBlock}// FixNginxConfIncludeConfD attempts to fix nginx.conf to include conf.d directoryfunc FixNginxConfIncludeConfD() error {	path := nginx.GetConfEntryPath()	content, err := os.ReadFile(path)	if err != nil {		return ErrFailedToReadNginxConf	}	// create a backup file (+.bak.timestamp)	backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())	err = os.WriteFile(backupPath, content, 0644)	if err != nil {		return ErrFailedToCreateBackup	}	// parse nginx.conf	p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())	c, err := p.Parse()	if err != nil {		return ErrParseNginxConf	}	// find http block	for _, v := range c.Block.Directives {		if v.GetName() == "http" {			// add include conf.d/*.conf to http block			includeDirective := &config.Directive{				Name:       "include",				Parameters: []config.Parameter{{Value: nginx.GetConfPath("conf.d/*.conf")}},			}			realBlock := v.GetBlock().(*config.HTTP)			realBlock.Directives = append(realBlock.Directives, includeDirective)			// write to file			return os.WriteFile(path, []byte(dumper.DumpBlock(c.Block, dumper.IndentedStyle)), 0644)		}	}	// if no http block, append http block with include conf.d/*.conf	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("conf.d/*.conf"))...)	return os.WriteFile(path, content, 0644)}
 |