2022-10-24 15:06:49 +00:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Comment string
|
|
|
|
Listens []*ServerListen
|
|
|
|
Directives []IDirective
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(directive IDirective) (*Server, error) {
|
|
|
|
server := &Server{}
|
|
|
|
if block := directive.GetBlock(); block != nil {
|
|
|
|
server.Comment = block.GetComment()
|
|
|
|
directives := block.GetDirectives()
|
|
|
|
for _, dir := range directives {
|
2022-10-28 09:04:57 +00:00
|
|
|
|
|
|
|
switch dir.GetName() {
|
|
|
|
case "listen":
|
2022-10-24 15:06:49 +00:00
|
|
|
server.Listens = append(server.Listens, NewServerListen(dir.GetParameters()))
|
2022-10-28 09:04:57 +00:00
|
|
|
default:
|
2022-10-24 15:06:49 +00:00
|
|
|
server.Directives = append(server.Directives, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("server directive must have a block")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetName() string {
|
|
|
|
return "server"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetParameters() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetBlock() IBlock {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComment() string {
|
|
|
|
return s.Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetDirectives() []IDirective {
|
|
|
|
directives := make([]IDirective, 0)
|
|
|
|
for _, ls := range s.Listens {
|
|
|
|
directives = append(directives, ls)
|
|
|
|
}
|
|
|
|
directives = append(directives, s.Directives...)
|
|
|
|
return directives
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) AddListen(bind string, defaultServer bool, params ...string) {
|
|
|
|
listen := &ServerListen{
|
|
|
|
Bind: bind,
|
|
|
|
Parameters: params,
|
|
|
|
}
|
|
|
|
if defaultServer {
|
|
|
|
listen.DefaultServer = DefaultServer
|
|
|
|
}
|
|
|
|
s.Listens = append(s.Listens, listen)
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:04:57 +00:00
|
|
|
func (s *Server) UpdateListen(bind string, defaultServer bool, params ...string) {
|
|
|
|
listen := &ServerListen{
|
|
|
|
Bind: bind,
|
|
|
|
Parameters: params,
|
|
|
|
}
|
|
|
|
if defaultServer {
|
|
|
|
listen.DefaultServer = DefaultServer
|
|
|
|
}
|
|
|
|
var newListens []*ServerListen
|
2022-11-03 09:06:48 +00:00
|
|
|
exist := false
|
2022-10-28 09:04:57 +00:00
|
|
|
for _, li := range s.Listens {
|
|
|
|
if li.Bind == bind {
|
2022-11-03 09:06:48 +00:00
|
|
|
exist = true
|
2022-10-28 09:04:57 +00:00
|
|
|
newListens = append(newListens, listen)
|
|
|
|
} else {
|
|
|
|
newListens = append(newListens, li)
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 09:06:48 +00:00
|
|
|
if !exist {
|
|
|
|
newListens = append(newListens, listen)
|
|
|
|
}
|
2022-10-28 09:04:57 +00:00
|
|
|
|
|
|
|
s.Listens = newListens
|
|
|
|
}
|
|
|
|
|
2022-11-03 09:06:48 +00:00
|
|
|
func (s *Server) DeleteListen(bind string) {
|
|
|
|
var newListens []*ServerListen
|
|
|
|
for _, li := range s.Listens {
|
|
|
|
if li.Bind != bind {
|
|
|
|
newListens = append(newListens, li)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Listens = newListens
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DeleteServerName(name string) {
|
|
|
|
var names []string
|
|
|
|
dirs := s.FindDirectives("server_name")
|
|
|
|
params := dirs[0].GetParameters()
|
|
|
|
for _, param := range params {
|
|
|
|
if param != name {
|
|
|
|
names = append(names, param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.UpdateServerName(names)
|
|
|
|
}
|
|
|
|
|
2022-11-03 10:02:07 +00:00
|
|
|
func (s *Server) AddServerName(name string) {
|
|
|
|
dirs := s.FindDirectives("server_name")
|
|
|
|
params := dirs[0].GetParameters()
|
|
|
|
params = append(params, name)
|
|
|
|
s.UpdateServerName(params)
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:04:57 +00:00
|
|
|
func (s *Server) UpdateServerName(names []string) {
|
|
|
|
serverNameDirective := Directive{
|
|
|
|
Name: "server_name",
|
|
|
|
Parameters: names,
|
|
|
|
}
|
|
|
|
s.UpdateDirectives("server_name", serverNameDirective)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UpdateRootProxy(proxy []string) {
|
|
|
|
//TODD 根据ID修改
|
|
|
|
dirs := s.FindDirectives("location")
|
|
|
|
for _, dir := range dirs {
|
|
|
|
location, ok := dir.(*Location)
|
|
|
|
if ok && location.Match == "/" {
|
|
|
|
newDir := Directive{
|
|
|
|
Name: "location",
|
|
|
|
Parameters: []string{"/"},
|
|
|
|
Block: &Block{},
|
|
|
|
}
|
|
|
|
block := &Block{}
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "proxy_pass",
|
|
|
|
Parameters: proxy,
|
|
|
|
})
|
|
|
|
newDir.Block = block
|
|
|
|
s.UpdateDirectives("location", newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:06:49 +00:00
|
|
|
func (s *Server) RemoveListenByBind(bind string) {
|
|
|
|
index := 0
|
|
|
|
listens := s.Listens
|
|
|
|
for _, listen := range s.Listens {
|
|
|
|
if listen.Bind != bind || len(listen.Parameters) > 0 {
|
|
|
|
listens[index] = listen
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 := make([]IDirective, 0)
|
|
|
|
for _, dir := range s.Directives {
|
|
|
|
if dir.GetName() == directiveName {
|
|
|
|
directives = append(directives, &directive)
|
|
|
|
} else {
|
|
|
|
directives = append(directives, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Directives = directives
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) AddDirectives(directive Directive) {
|
|
|
|
directives := append(s.Directives, &directive)
|
|
|
|
s.Directives = directives
|
|
|
|
}
|
2022-10-28 09:04:57 +00:00
|
|
|
|
|
|
|
func (s *Server) RemoveDirectives(names []string) {
|
|
|
|
nameMaps := make(map[string]struct{}, len(names))
|
|
|
|
for _, name := range names {
|
|
|
|
nameMaps[name] = struct{}{}
|
|
|
|
}
|
|
|
|
directives := s.GetDirectives()
|
|
|
|
var newDirectives []IDirective
|
|
|
|
for _, dir := range directives {
|
|
|
|
if _, ok := nameMaps[dir.GetName()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newDirectives = append(newDirectives, dir)
|
|
|
|
}
|
|
|
|
s.Directives = newDirectives
|
|
|
|
}
|