sort by proxy type and name when reloading frpc config

This commit is contained in:
杨黄林
2023-09-27 01:24:22 +08:00
parent 07cbf8d6a3
commit e8d64b9db2
3 changed files with 62 additions and 12 deletions

View File

@@ -179,8 +179,6 @@ func (c *HandleController) MakeAddProxyFunc() func(context *gin.Context) {
return
}
delete(proxy, NameKey)
delete(proxy, OldNameKey)
clientProxies[name] = proxy
res := c.UpdateFrpcConfig()
@@ -249,8 +247,6 @@ func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
}
}
delete(proxy, NameKey)
delete(proxy, OldNameKey)
delete(clientProxies, oldName)
clientProxies[name] = proxy
@@ -369,8 +365,9 @@ func (c *HandleController) MakeProxyFunc() func(context *gin.Context) {
func (c *HandleController) UpdateFrpcConfig() ProxyResponse {
res := ProxyResponse{}
requestUrl := c.buildRequestUrl("/api/config")
request, _ := http.NewRequest("PUT", requestUrl, bytes.NewReader(serializeSectionsToString()))
request, _ := http.NewRequest("PUT", requestUrl, bytes.NewReader(serializeSections()))
response, err := c.getClientResponse(request, c.buildClient())
if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"io"
"log"
"net/http"
"sort"
"strconv"
"strings"
)
@@ -16,17 +17,36 @@ func trimString(str string) string {
return strings.TrimSpace(str)
}
func serializeSectionsToString() []byte {
func sortSectionKeys(object ini.Section) []string {
var keys []string
for key := range object {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func serializeSections() []byte {
var build strings.Builder
build.WriteString("[common]\n")
for key, value := range clientCommon {
build.WriteString(fmt.Sprintf("%s = %s\n", key, value))
for _, key := range sortSectionKeys(clientCommon) {
build.WriteString(fmt.Sprintf("%s = %s\n", key, clientCommon[key]))
}
build.WriteString("\n")
for name, section := range clientProxies {
sections := Sections{clientProxies}
for _, sectionInfo := range sections.sort() {
name := sectionInfo.Name
build.WriteString(fmt.Sprintf("[%s]\n", name))
for key, value := range section {
section := sectionInfo.Section
for _, key := range sortSectionKeys(section) {
value := section[key]
if key == NameKey || key == OldNameKey || trimString(value) == "" {
continue
}
build.WriteString(fmt.Sprintf("%s = %s\n", key, value))
}
build.WriteString("\n")

View File

@@ -2,6 +2,8 @@ package controller
import (
"github.com/vaughan0/go-ini"
"sort"
"strings"
)
const (
@@ -39,6 +41,10 @@ type HTTPError struct {
Err error
}
func (e *HTTPError) Error() string {
return e.Err.Error()
}
type Common struct {
Common CommonInfo
}
@@ -74,6 +80,33 @@ type ClientProxies struct {
Proxy ini.Section `json:"proxy"`
}
func (e *HTTPError) Error() string {
return e.Err.Error()
type SectionInfo struct {
Name string
Section ini.Section
}
type Sections struct {
sections map[string]ini.Section
}
func (s *Sections) sort() []SectionInfo {
sectionInfos := make([]SectionInfo, 0)
for key, value := range s.sections {
sectionInfos = append(sectionInfos, SectionInfo{Name: key, Section: value})
}
sort.Slice(sectionInfos, func(i, j int) bool {
typeCompare := strings.Compare(sectionInfos[i].Section["type"], sectionInfos[j].Section["type"])
if typeCompare == -1 {
return true
} else if typeCompare == 0 {
nameCompare := strings.Compare(sectionInfos[i].Name, sectionInfos[j].Name)
return nameCompare == -1
} else {
return false
}
})
return sectionInfos
}