feat: 反向代理增加编辑源文功能

This commit is contained in:
zhengkunwang223
2023-04-24 17:48:22 +08:00
committed by zhengkunwang223
parent 2dd88364f8
commit acf64dcf25
14 changed files with 355 additions and 25 deletions

View File

@@ -1,8 +1,10 @@
package components
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type Location struct {
@@ -17,6 +19,7 @@ type Location struct {
Directives []IDirective
Line int
Parameters []string
Replaces map[string]string
}
func NewLocation(directive IDirective) *Location {
@@ -60,6 +63,11 @@ func NewLocation(directive IDirective) *Location {
}
}
}
case "sub_filter":
if location.Replaces == nil {
location.Replaces = make(map[string]string, 0)
}
location.Replaces[strings.Trim(params[0], "\"")] = strings.Trim(params[1], "\"")
}
}
@@ -176,6 +184,7 @@ func (l *Location) ChangePath(Modifier string, Match string) {
func (l *Location) AddCache(cacheTime int, cacheUint string) {
l.RemoveDirective("add_header", []string{"Cache-Control", "no-cache"})
l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"})
directives := l.GetDirectives()
newDir := &Directive{
Name: "if",
@@ -212,3 +221,20 @@ func (l *Location) RemoveCache() {
l.CacheUint = ""
l.Cache = false
}
func (l *Location) AddSubFilter(subFilters map[string]string) {
l.RemoveDirective("sub_filter", []string{})
l.Replaces = subFilters
for k, v := range subFilters {
l.UpdateDirective("sub_filter", []string{fmt.Sprintf(`"%s"`, k), fmt.Sprintf(`"%s"`, v)})
}
l.UpdateDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.UpdateDirective("sub_filter_once", []string{"off"})
}
func (l *Location) RemoveSubFilter() {
l.RemoveDirective("sub_filter", []string{})
l.RemoveDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.RemoveDirective("sub_filter_once", []string{"off"})
l.Replaces = nil
}