1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

feat: 修改 nginx util 增加空行处理

This commit is contained in:
zhengkunwang223 2022-11-30 17:33:30 +08:00 committed by zhengkunwang223
parent cd9bd9940c
commit 5b63dc42a2
12 changed files with 294 additions and 210 deletions

View File

@ -1,6 +1,7 @@
package components package components
type Block struct { type Block struct {
Line int
Comment string Comment string
Directives []IDirective Directives []IDirective
} }
@ -13,6 +14,10 @@ func (b *Block) GetComment() string {
return b.Comment return b.Comment
} }
func (b *Block) GetLine() int {
return b.Line
}
func (b *Block) FindDirectives(directiveName string) []IDirective { func (b *Block) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0) directives := make([]IDirective, 0)
for _, directive := range b.GetDirectives() { for _, directive := range b.GetDirectives() {
@ -27,57 +32,101 @@ func (b *Block) FindDirectives(directiveName string) []IDirective {
return directives return directives
} }
func (b *Block) UpdateDirectives(directiveName string, directive Directive) { //func (b *Block) UpdateDirectives(directiveName string, directive Directive) {
// directives := b.GetDirectives()
// index := -1
// for i, dir := range directives {
// if dir.GetName() == directiveName {
// index = i
// break
// }
// }
// if index > -1 {
// directives[index] = &directive
// } else {
// directives = append(directives, &directive)
// }
// b.Directives = directives
//}
func (b *Block) UpdateDirective(key string, params []string) {
if key == "" || len(params) == 0 {
return
}
directives := b.GetDirectives() directives := b.GetDirectives()
index := -1 index := -1
for i, dir := range directives { for i, dir := range directives {
if dir.GetName() == directiveName { if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i index = i
break break
} }
} }
if index > -1 { newDirective := &Directive{
directives[index] = &directive Name: key,
} else { Parameters: params,
directives = append(directives, &directive)
}
b.Directives = directives
}
func (b *Block) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {
directives := b.GetDirectives()
index := -1
for i, dir := range directives {
if dir.GetName() == name && dir.GetParameters()[0] == key {
index = i
break
}
} }
if index > -1 { if index > -1 {
directives[index] = &directive directives[index] = newDirective
} else { } else {
directives = append(directives, &directive) directives = append(directives, newDirective)
} }
b.Directives = directives b.Directives = directives
} }
func (b *Block) AddDirectives(directive Directive) { //func (b *Block) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {
directives := append(b.GetDirectives(), &directive) //
b.Directives = directives // directives := b.GetDirectives()
} //
// index := -1
// for i, dir := range directives {
// if dir.GetName() == name && dir.GetParameters()[0] == key {
// index = i
// break
// }
// }
// if index > -1 {
// directives[index] = &directive
// } else {
// directives = append(directives, &directive)
// }
// b.Directives = directives
//}
func (b *Block) RemoveDirectives(names []string) { //func (b *Block) RemoveDirectives(names []string) {
nameMaps := make(map[string]struct{}, len(names)) // nameMaps := make(map[string]struct{}, len(names))
for _, name := range names { // for _, name := range names {
nameMaps[name] = struct{}{} // nameMaps[name] = struct{}{}
} // }
// directives := b.GetDirectives()
// var newDirectives []IDirective
// for _, dir := range directives {
// if _, ok := nameMaps[dir.GetName()]; ok {
// continue
// }
// newDirectives = append(newDirectives, dir)
// }
// b.Directives = newDirectives
//}
func (b *Block) RemoveDirective(key string, params []string) {
directives := b.GetDirectives() directives := b.GetDirectives()
var newDirectives []IDirective var newDirectives []IDirective
for _, dir := range directives { for _, dir := range directives {
if _, ok := nameMaps[dir.GetName()]; ok { if dir.GetName() == key {
continue if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if len(oldParams) > 0 && oldParams[0] == params[0] {
continue
}
} else {
continue
}
} }
newDirectives = append(newDirectives, dir) newDirectives = append(newDirectives, dir)
} }

View File

@ -2,6 +2,7 @@ package components
type Comment struct { type Comment struct {
Detail string Detail string
Line int
} }
func (c *Comment) GetName() string { func (c *Comment) GetName() string {
@ -19,3 +20,7 @@ func (c *Comment) GetBlock() IBlock {
func (c *Comment) GetComment() string { func (c *Comment) GetComment() string {
return c.Detail return c.Detail
} }
func (c *Comment) GetLine() int {
return c.Line
}

View File

@ -36,3 +36,17 @@ func (c *Config) FindHttp() *Http {
return http return http
} }
var repeatKeys = map[string]struct {
}{
"limit_conn": {},
"limit_conn_zone": {},
"set": {},
}
func IsRepeatKey(key string) bool {
if _, ok := repeatKeys[key]; ok {
return true
}
return false
}

View File

@ -1,6 +1,7 @@
package components package components
type Directive struct { type Directive struct {
Line int
Block IBlock Block IBlock
Name string Name string
Comment string Comment string
@ -22,3 +23,7 @@ func (d *Directive) GetParameters() []string {
func (d *Directive) GetBlock() IBlock { func (d *Directive) GetBlock() IBlock {
return d.Block return d.Block
} }
func (d *Directive) GetLine() int {
return d.Line
}

View File

@ -8,6 +8,7 @@ type Http struct {
Comment string Comment string
Servers []*Server Servers []*Server
Directives []IDirective Directives []IDirective
Line int
} }
func (h *Http) GetComment() string { func (h *Http) GetComment() string {
@ -17,11 +18,11 @@ func (h *Http) GetComment() string {
func NewHttp(directive IDirective) (*Http, error) { func NewHttp(directive IDirective) (*Http, error) {
if block := directive.GetBlock(); block != nil { if block := directive.GetBlock(); block != nil {
http := &Http{ http := &Http{
Line: directive.GetBlock().GetLine(),
Servers: []*Server{}, Servers: []*Server{},
Directives: []IDirective{}, Directives: []IDirective{},
Comment: block.GetComment(), Comment: block.GetComment(),
} }
for _, directive := range block.GetDirectives() { for _, directive := range block.GetDirectives() {
if server, ok := directive.(*Server); ok { if server, ok := directive.(*Server); ok {
http.Servers = append(http.Servers, server) http.Servers = append(http.Servers, server)
@ -66,38 +67,49 @@ func (h *Http) FindDirectives(directiveName string) []IDirective {
return directives return directives
} }
func (h *Http) UpdateDirectives(directiveName string, directive Directive) { func (h *Http) UpdateDirective(key string, params []string) {
directives := h.Directives if key == "" || len(params) == 0 {
return
}
directives := h.GetDirectives()
index := -1 index := -1
for i, dir := range directives { for i, dir := range directives {
if dir.GetName() == directiveName { if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i index = i
break break
} }
} }
newDirective := &Directive{
Name: key,
Parameters: params,
}
if index > -1 { if index > -1 {
directives[index] = &directive directives[index] = newDirective
} else { } else {
directives = append(directives, &directive) directives = append(directives, newDirective)
} }
h.Directives = directives h.Directives = directives
} }
func (h *Http) AddDirectives(directive Directive) { func (h *Http) RemoveDirective(key string, params []string) {
directives := append(h.GetDirectives(), &directive)
h.Directives = directives
}
func (h *Http) RemoveDirectives(names []string) {
nameMaps := make(map[string]struct{}, len(names))
for _, name := range names {
nameMaps[name] = struct{}{}
}
directives := h.GetDirectives() directives := h.GetDirectives()
var newDirectives []IDirective var newDirectives []IDirective
for _, dir := range directives { for _, dir := range directives {
if _, ok := nameMaps[dir.GetName()]; ok { if dir.GetName() == key {
continue if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if len(oldParams) > 0 && oldParams[0] == params[0] {
continue
}
} else {
continue
}
} }
newDirectives = append(newDirectives, dir) newDirectives = append(newDirectives, dir)
} }
@ -108,19 +120,6 @@ func (h *Http) GetBlock() IBlock {
return h return h
} }
func (h *Http) UpdateDirectiveBySecondKey(name string, key string, directive Directive) { func (h *Http) GetLine() int {
directives := h.Directives return h.Line
index := -1
for i, dir := range directives {
if dir.GetName() == name && dir.GetParameters()[0] == key {
index = i
break
}
}
if index > -1 {
directives[index] = &directive
} else {
directives = append(directives, &directive)
}
h.Directives = directives
} }

View File

@ -8,18 +8,19 @@ type Server struct {
Comment string Comment string
Listens []*ServerListen Listens []*ServerListen
Directives []IDirective Directives []IDirective
Line int
} }
func NewServer(directive IDirective) (*Server, error) { func NewServer(directive IDirective) (*Server, error) {
server := &Server{} server := &Server{}
if block := directive.GetBlock(); block != nil { if block := directive.GetBlock(); block != nil {
server.Line = directive.GetBlock().GetLine()
server.Comment = block.GetComment() server.Comment = block.GetComment()
directives := block.GetDirectives() directives := block.GetDirectives()
for _, dir := range directives { for _, dir := range directives {
switch dir.GetName() { switch dir.GetName() {
case "listen": case "listen":
server.Listens = append(server.Listens, NewServerListen(dir.GetParameters())) server.Listens = append(server.Listens, NewServerListen(dir.GetParameters(), dir.GetLine()))
default: default:
server.Directives = append(server.Directives, dir) server.Directives = append(server.Directives, dir)
} }
@ -54,6 +55,73 @@ func (s *Server) GetDirectives() []IDirective {
return directives return directives
} }
func (s *Server) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0)
for _, directive := range s.Directives {
if directive.GetName() == directiveName {
directives = append(directives, directive)
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
func (s *Server) UpdateDirective(key string, params []string) {
if key == "" || len(params) == 0 {
return
}
directives := s.GetDirectives()
index := -1
for i, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i
break
}
}
newDirective := &Directive{
Name: key,
Parameters: params,
}
if index > -1 {
directives[index] = newDirective
} else {
directives = append(directives, newDirective)
}
s.Directives = directives
}
func (s *Server) RemoveDirective(key string, params []string) {
directives := s.GetDirectives()
var newDirectives []IDirective
for _, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if len(oldParams) > 0 && oldParams[0] == params[0] {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)
}
s.Directives = newDirectives
}
func (s *Server) GetLine() int {
return s.Line
}
func (s *Server) AddListen(bind string, defaultServer bool, params ...string) { func (s *Server) AddListen(bind string, defaultServer bool, params ...string) {
listen := &ServerListen{ listen := &ServerListen{
Bind: bind, Bind: bind,
@ -120,19 +188,11 @@ func (s *Server) AddServerName(name string) {
} }
func (s *Server) UpdateServerName(names []string) { func (s *Server) UpdateServerName(names []string) {
serverNameDirective := Directive{ s.UpdateDirective("server_name", names)
Name: "server_name",
Parameters: names,
}
s.UpdateDirectives("server_name", serverNameDirective)
} }
func (s *Server) UpdateRoot(path string) { func (s *Server) UpdateRoot(path string) {
rootDir := Directive{ s.UpdateDirective("root", []string{path})
Name: "root",
Parameters: []string{path},
}
s.UpdateDirectives("root", rootDir)
} }
func (s *Server) UpdateRootLocation() { func (s *Server) UpdateRootLocation() {
@ -192,55 +252,3 @@ func (s *Server) RemoveListenByBind(bind string) {
} }
s.Listens = listens s.Listens = listens
} }
func (s *Server) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0)
for _, directive := range s.Directives {
if directive.GetName() == directiveName {
directives = append(directives, directive)
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
func (s *Server) UpdateDirectives(directiveName string, directive Directive) {
directives := s.Directives
index := -1
for i, dir := range directives {
if dir.GetName() == directiveName {
index = i
break
}
}
if index > -1 {
directives[index] = &directive
} else {
directives = append(directives, &directive)
}
s.Directives = directives
}
func (s *Server) AddDirectives(directive Directive) {
directives := append(s.Directives, &directive)
s.Directives = directives
}
func (s *Server) RemoveDirectives(names []string) {
nameMaps := make(map[string]struct{}, len(names))
for _, name := range names {
nameMaps[name] = struct{}{}
}
directives := s.Directives
var newDirectives []IDirective
for _, dir := range directives {
if _, ok := nameMaps[dir.GetName()]; ok {
continue
}
newDirectives = append(newDirectives, dir)
}
s.Directives = newDirectives
}

View File

@ -12,11 +12,13 @@ type ServerListen struct {
DefaultServer string DefaultServer string
Parameters []string Parameters []string
Comment string Comment string
Line int
} }
func NewServerListen(params []string) *ServerListen { func NewServerListen(params []string, line int) *ServerListen {
server := &ServerListen{ server := &ServerListen{
Parameters: []string{}, Parameters: []string{},
Line: line,
} }
for _, param := range params { for _, param := range params {
if isBind(param) { if isBind(param) {
@ -66,3 +68,7 @@ func (sl *ServerListen) AddDefaultServer() {
func (sl *ServerListen) RemoveDefaultServe() { func (sl *ServerListen) RemoveDefaultServe() {
sl.DefaultServer = "" sl.DefaultServer = ""
} }
func (sl *ServerListen) GetLine() int {
return sl.Line
}

View File

@ -3,10 +3,10 @@ package components
type IBlock interface { type IBlock interface {
GetDirectives() []IDirective GetDirectives() []IDirective
FindDirectives(directiveName string) []IDirective FindDirectives(directiveName string) []IDirective
UpdateDirectives(directiveName string, directive Directive) RemoveDirective(name string, params []string)
AddDirectives(directive Directive) UpdateDirective(name string, params []string)
RemoveDirectives(names []string)
GetComment() string GetComment() string
GetLine() int
} }
type IDirective interface { type IDirective interface {
@ -14,12 +14,5 @@ type IDirective interface {
GetParameters() []string GetParameters() []string
GetBlock() IBlock GetBlock() IBlock
GetComment() string GetComment() string
} GetLine() int
type FileDirective interface {
isFileDirective()
}
type IncludeDirective interface {
FileDirective
} }

View File

@ -9,6 +9,7 @@ type Upstream struct {
UpstreamServers []*UpstreamServer UpstreamServers []*UpstreamServer
Directives []IDirective Directives []IDirective
Comment string Comment string
Line int
} }
func (us *Upstream) GetName() string { func (us *Upstream) GetName() string {
@ -33,7 +34,6 @@ func (us *Upstream) GetDirectives() []IDirective {
for _, uss := range us.UpstreamServers { for _, uss := range us.UpstreamServers {
directives = append(directives, uss) directives = append(directives, uss)
} }
return directives return directives
} }
@ -41,6 +41,7 @@ func NewUpstream(directive IDirective) (*Upstream, error) {
parameters := directive.GetParameters() parameters := directive.GetParameters()
us := &Upstream{ us := &Upstream{
UpstreamName: parameters[0], UpstreamName: parameters[0],
Line: directive.GetLine(),
} }
if block := directive.GetBlock(); block != nil { if block := directive.GetBlock(); block != nil {
@ -76,35 +77,55 @@ func (us *Upstream) FindDirectives(directiveName string) []IDirective {
return directives return directives
} }
func (us *Upstream) UpdateDirectives(directiveName string, directive Directive) { func (us *Upstream) UpdateDirective(key string, params []string) {
directives := make([]IDirective, 0) if key == "" || len(params) == 0 {
for _, dir := range us.GetDirectives() { return
if dir.GetName() == directiveName { }
directives = append(directives, &directive) directives := us.GetDirectives()
} else { index := -1
directives = append(directives, dir) for i, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i
break
} }
} }
us.Directives = directives newDirective := &Directive{
} Name: key,
Parameters: params,
func (us *Upstream) AddDirectives(directive Directive) {
directives := append(us.GetDirectives(), &directive)
us.Directives = directives
}
func (us *Upstream) RemoveDirectives(names []string) {
nameMaps := make(map[string]struct{}, len(names))
for _, name := range names {
nameMaps[name] = struct{}{}
} }
if index > -1 {
directives[index] = newDirective
} else {
directives = append(directives, newDirective)
}
us.Directives = directives
}
func (us *Upstream) RemoveDirective(key string, params []string) {
directives := us.GetDirectives() directives := us.GetDirectives()
var newDirectives []IDirective var newDirectives []IDirective
for _, dir := range directives { for _, dir := range directives {
if _, ok := nameMaps[dir.GetName()]; ok { if dir.GetName() == key {
continue if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if len(oldParams) > 0 && oldParams[0] == params[0] {
continue
}
} else {
continue
}
} }
newDirectives = append(newDirectives, dir) newDirectives = append(newDirectives, dir)
} }
us.Directives = newDirectives us.Directives = newDirectives
} }
func (us *Upstream) GetLine() int {
return us.Line
}

View File

@ -11,6 +11,7 @@ type UpstreamServer struct {
Address string Address string
Flags []string Flags []string
Parameters map[string]string Parameters map[string]string
Line int
} }
func (uss *UpstreamServer) GetName() string { func (uss *UpstreamServer) GetName() string {
@ -58,6 +59,7 @@ func NewUpstreamServer(directive IDirective) *UpstreamServer {
Comment: directive.GetComment(), Comment: directive.GetComment(),
Flags: make([]string, 0), Flags: make([]string, 0),
Parameters: make(map[string]string, 0), Parameters: make(map[string]string, 0),
Line: directive.GetLine(),
} }
for i, parameter := range directive.GetParameters() { for i, parameter := range directive.GetParameters() {
@ -75,3 +77,7 @@ func NewUpstreamServer(directive IDirective) *UpstreamServer {
return uss return uss
} }
func (uss *UpstreamServer) GetLine() int {
return uss.Line
}

View File

@ -3,62 +3,27 @@ package nginx
import ( import (
"bytes" "bytes"
"fmt" "fmt"
components "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" "github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"io/ioutil" "io/ioutil"
"sort"
"strings" "strings"
) )
var ( var (
//NoIndentStyle default style
NoIndentStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 0,
}
//IndentedStyle default style
IndentedStyle = &Style{ IndentedStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
}
//NoIndentSortedStyle default style
NoIndentSortedStyle = &Style{
SortDirectives: true,
StartIndent: 0,
Indent: 0,
}
//NoIndentSortedSpaceStyle default style
NoIndentSortedSpaceStyle = &Style{
SortDirectives: true,
SpaceBeforeBlocks: true, SpaceBeforeBlocks: true,
StartIndent: 0, StartIndent: 0,
Indent: 0, Indent: 4,
} }
) )
type Style struct { type Style struct {
SortDirectives bool
SpaceBeforeBlocks bool SpaceBeforeBlocks bool
StartIndent int StartIndent int
Indent int Indent int
} }
func NewStyle() *Style {
style := &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
}
return style
}
func (s *Style) Iterate() *Style { func (s *Style) Iterate() *Style {
newStyle := &Style{ newStyle := &Style{
SortDirectives: s.SortDirectives,
SpaceBeforeBlocks: s.SpaceBeforeBlocks, SpaceBeforeBlocks: s.SpaceBeforeBlocks,
StartIndent: s.StartIndent + s.Indent, StartIndent: s.StartIndent + s.Indent,
Indent: s.Indent, Indent: s.Indent,
@ -91,23 +56,33 @@ func DumpDirective(d components.IDirective, style *Style) string {
buf.WriteString(d.GetComment()) buf.WriteString(d.GetComment())
} }
buf.WriteString("\n") buf.WriteString("\n")
buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate())) buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate(), d.GetBlock().GetLine()))
buf.WriteString(fmt.Sprintf("\n%s}", strings.Repeat(" ", style.StartIndent))) buf.WriteString(fmt.Sprintf("\n%s}", strings.Repeat(" ", style.StartIndent)))
} }
return buf.String() return buf.String()
} }
func DumpBlock(b components.IBlock, style *Style) string { func DumpBlock(b components.IBlock, style *Style, startLine int) string {
var buf bytes.Buffer var buf bytes.Buffer
line := startLine
directives := b.GetDirectives() if b.GetLine() > startLine {
if style.SortDirectives { for i := 0; i < b.GetLine()-startLine; i++ {
sort.SliceStable(directives, func(i, j int) bool { buf.WriteString("\n")
return directives[i].GetName() < directives[j].GetName() }
}) line = b.GetLine()
} }
directives := b.GetDirectives()
for i, directive := range directives { for i, directive := range directives {
if directive.GetLine() > line {
for i := 0; i < b.GetLine()-line; i++ {
buf.WriteString("\n")
}
line = b.GetLine()
}
buf.WriteString(DumpDirective(directive, style)) buf.WriteString(DumpDirective(directive, style))
if i != len(directives)-1 { if i != len(directives)-1 {
buf.WriteString("\n") buf.WriteString("\n")
@ -117,7 +92,7 @@ func DumpBlock(b components.IBlock, style *Style) string {
} }
func DumpConfig(c *components.Config, style *Style) string { func DumpConfig(c *components.Config, style *Style) string {
return DumpBlock(c.Block, style) return DumpBlock(c.Block, style, 1)
} }
func WriteConfig(c *components.Config, style *Style) error { func WriteConfig(c *components.Config, style *Style) error {

View File

@ -88,6 +88,7 @@ func (p *Parser) parseBlock() *components.Block {
context := &components.Block{ context := &components.Block{
Comment: "", Comment: "",
Directives: make([]components.IDirective, 0), Directives: make([]components.IDirective, 0),
Line: p.currentToken.Line,
} }
parsingloop: parsingloop:
@ -100,6 +101,7 @@ parsingloop:
case p.curTokenIs(flag.Comment): case p.curTokenIs(flag.Comment):
context.Directives = append(context.Directives, &components.Comment{ context.Directives = append(context.Directives, &components.Comment{
Detail: p.currentToken.Literal, Detail: p.currentToken.Literal,
Line: p.currentToken.Line,
}) })
} }
p.nextToken() p.nextToken()
@ -111,6 +113,7 @@ parsingloop:
func (p *Parser) parseStatement() components.IDirective { func (p *Parser) parseStatement() components.IDirective {
d := &components.Directive{ d := &components.Directive{
Name: p.currentToken.Literal, Name: p.currentToken.Literal,
Line: p.currentToken.Line,
} }
for p.nextToken(); p.currentToken.IsParameterEligible(); p.nextToken() { for p.nextToken(); p.currentToken.IsParameterEligible(); p.nextToken() {