2022-10-24 15:06:49 +00:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-05-08 08:39:41 +00:00
|
|
|
Comment string
|
|
|
|
Listens []*ServerListen
|
2022-10-24 15:06:49 +00:00
|
|
|
Directives []IDirective
|
2022-11-30 09:33:30 +00:00
|
|
|
Line int
|
2022-10-24 15:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(directive IDirective) (*Server, error) {
|
|
|
|
server := &Server{}
|
|
|
|
if block := directive.GetBlock(); block != nil {
|
2022-11-30 09:33:30 +00:00
|
|
|
server.Line = directive.GetBlock().GetLine()
|
2022-10-24 15:06:49 +00:00
|
|
|
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-11-30 09:33:30 +00:00
|
|
|
server.Listens = append(server.Listens, NewServerListen(dir.GetParameters(), dir.GetLine()))
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:33:30 +00:00
|
|
|
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)...)
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 15:08:28 +00:00
|
|
|
if directiveName == "listen" {
|
|
|
|
for _, listen := range s.Listens {
|
|
|
|
params := []string{listen.Bind}
|
|
|
|
params = append(params, listen.Parameters...)
|
|
|
|
if listen.DefaultServer != "" {
|
|
|
|
params = append(params, DefaultServer)
|
|
|
|
}
|
|
|
|
directives = append(directives, &Directive{
|
|
|
|
Name: "listen",
|
|
|
|
Parameters: params,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 09:33:30 +00:00
|
|
|
return directives
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UpdateDirective(key string, params []string) {
|
|
|
|
if key == "" || len(params) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-01-03 08:56:36 +00:00
|
|
|
if key == "listen" {
|
|
|
|
defaultServer := false
|
2023-05-12 15:08:28 +00:00
|
|
|
paramLen := len(params)
|
|
|
|
if paramLen > 0 && params[paramLen-1] == "default_server" {
|
|
|
|
params = params[:paramLen-1]
|
2023-01-03 08:56:36 +00:00
|
|
|
defaultServer = true
|
|
|
|
}
|
2023-05-12 15:08:28 +00:00
|
|
|
s.UpdateListen(params[0], defaultServer, params[1:]...)
|
2023-01-03 08:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:40:05 +00:00
|
|
|
directives := s.Directives
|
2022-11-30 09:33:30 +00:00
|
|
|
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) {
|
2022-11-30 13:40:05 +00:00
|
|
|
directives := s.Directives
|
2022-11-30 09:33:30 +00:00
|
|
|
var newDirectives []IDirective
|
|
|
|
for _, dir := range directives {
|
2023-04-23 10:15:38 +00:00
|
|
|
if dir.GetName() == key {
|
2023-06-29 10:42:11 +00:00
|
|
|
if len(params) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
oldParams := dir.GetParameters()
|
|
|
|
if key == "location" {
|
|
|
|
if len(params) == len(oldParams) {
|
|
|
|
exist := true
|
|
|
|
for i := range params {
|
|
|
|
if params[i] != oldParams[i] {
|
|
|
|
exist = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-23 10:15:38 +00:00
|
|
|
if oldParams[0] == params[0] {
|
2022-11-30 09:33:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newDirectives = append(newDirectives, dir)
|
|
|
|
}
|
|
|
|
s.Directives = newDirectives
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetLine() int {
|
|
|
|
return s.Line
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:06:49 +00:00
|
|
|
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) {
|
2022-11-30 09:33:30 +00:00
|
|
|
s.UpdateDirective("server_name", names)
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 08:28:51 +00:00
|
|
|
func (s *Server) UpdateRoot(path string) {
|
2022-11-30 09:33:30 +00:00
|
|
|
s.UpdateDirective("root", []string{path})
|
2022-11-21 08:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UpdateRootLocation() {
|
|
|
|
newDir := Directive{
|
|
|
|
Name: "location",
|
|
|
|
Parameters: []string{"/"},
|
|
|
|
Block: &Block{},
|
|
|
|
}
|
|
|
|
block := &Block{}
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "root",
|
|
|
|
Parameters: []string{"index.html"},
|
|
|
|
})
|
|
|
|
newDir.Block = block
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:04:57 +00:00
|
|
|
func (s *Server) UpdateRootProxy(proxy []string) {
|
2022-11-21 08:28:51 +00:00
|
|
|
newDir := Directive{
|
|
|
|
Name: "location",
|
|
|
|
Parameters: []string{"/"},
|
|
|
|
Block: &Block{},
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
2022-11-21 08:28:51 +00:00
|
|
|
block := &Block{}
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "proxy_pass",
|
|
|
|
Parameters: proxy,
|
|
|
|
})
|
|
|
|
newDir.Block = block
|
|
|
|
s.UpdateDirectiveBySecondKey("location", "/", newDir)
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 10:54:04 +00:00
|
|
|
func (s *Server) UpdatePHPProxy(proxy []string, localPath string) {
|
2023-04-03 09:47:23 +00:00
|
|
|
newDir := Directive{
|
|
|
|
Name: "location",
|
|
|
|
Parameters: []string{"~ [^/]\\.php(/|$)"},
|
|
|
|
Block: &Block{},
|
|
|
|
}
|
|
|
|
block := &Block{}
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "fastcgi_pass",
|
|
|
|
Parameters: proxy,
|
|
|
|
})
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "include",
|
|
|
|
Parameters: []string{"fastcgi-php.conf"},
|
|
|
|
})
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "include",
|
|
|
|
Parameters: []string{"fastcgi_params"},
|
|
|
|
})
|
2023-04-04 10:54:04 +00:00
|
|
|
if localPath == "" {
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "fastcgi_param",
|
|
|
|
Parameters: []string{"SCRIPT_FILENAME", "$document_root$fastcgi_script_name"},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "fastcgi_param",
|
|
|
|
Parameters: []string{"SCRIPT_FILENAME", localPath},
|
|
|
|
})
|
|
|
|
}
|
2023-04-03 09:47:23 +00:00
|
|
|
newDir.Block = block
|
|
|
|
s.UpdateDirectiveBySecondKey("location", "~ [^/]\\.php(/|$)", newDir)
|
|
|
|
}
|
|
|
|
|
2022-11-08 07:42:31 +00:00
|
|
|
func (s *Server) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {
|
|
|
|
directives := s.Directives
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
s.Directives = directives
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:06:49 +00:00
|
|
|
func (s *Server) RemoveListenByBind(bind string) {
|
2022-12-28 08:07:43 +00:00
|
|
|
var listens []*ServerListen
|
2022-10-24 15:06:49 +00:00
|
|
|
for _, listen := range s.Listens {
|
2023-07-22 14:04:17 +00:00
|
|
|
if listen.Bind != bind {
|
2022-12-28 08:07:43 +00:00
|
|
|
listens = append(listens, listen)
|
2022-10-24 15:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Listens = listens
|
|
|
|
}
|
2022-12-28 08:07:43 +00:00
|
|
|
|
|
|
|
func (s *Server) AddHTTP2HTTPS() {
|
|
|
|
newDir := Directive{
|
|
|
|
Name: "if",
|
|
|
|
Parameters: []string{"($scheme = http)"},
|
|
|
|
Block: &Block{},
|
|
|
|
}
|
|
|
|
block := &Block{}
|
|
|
|
block.Directives = append(block.Directives, &Directive{
|
|
|
|
Name: "return",
|
|
|
|
Parameters: []string{"301", "https://$host$request_uri"},
|
|
|
|
})
|
|
|
|
newDir.Block = block
|
|
|
|
s.UpdateDirectiveBySecondKey("if", "($scheme", newDir)
|
|
|
|
}
|