mirror of
https://github.com/yhl452493373/frpc-panel.git
synced 2026-04-04 06:17:00 +08:00
support update proxies
This commit is contained in:
@@ -3,11 +3,8 @@ package controller
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
ginI18n "github.com/gin-contrib/i18n"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/vaughan0/go-ini"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
@@ -59,7 +56,7 @@ func (c *HandleController) MakeIndexFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
context.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"version": c.Version,
|
||||
"OldNameKey": OldNameKey,
|
||||
"OriginalNameKey": OriginalNameKey,
|
||||
"showExit": trimString(c.CommonInfo.AdminUser) != "" && trimString(c.CommonInfo.AdminPwd) != "",
|
||||
"FrpcPanel": ginI18n.MustGetMessage(context, "Frpc Panel"),
|
||||
"ClientInfo": ginI18n.MustGetMessage(context, "Client Info"),
|
||||
@@ -143,66 +140,8 @@ func (c *HandleController) MakeLangFunc() func(context *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HandleController) MakeAddProxyFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
proxy := ini.Section{}
|
||||
|
||||
response := OperationResponse{
|
||||
Success: true,
|
||||
Code: Success,
|
||||
Message: "proxy add success",
|
||||
}
|
||||
|
||||
err := context.BindJSON(&proxy)
|
||||
if err != nil {
|
||||
response.Success = false
|
||||
response.Code = ParamError
|
||||
response.Message = fmt.Sprintf("proxy add failed, param error : %v", err)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
name := proxy[NameKey]
|
||||
|
||||
if trimString(name) == "" {
|
||||
response.Success = false
|
||||
response.Code = ParamError
|
||||
response.Message = fmt.Sprintf("proxy add failed, proxy name invalid")
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
if _, exist := clientProxies[name]; exist {
|
||||
response.Success = false
|
||||
response.Code = ProxyExist
|
||||
response.Message = fmt.Sprintf("proxy add failed, proxy exist")
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
clientProxies[name] = proxy
|
||||
|
||||
res := c.UpdateFrpcConfig()
|
||||
if !res.Success {
|
||||
response.Success = false
|
||||
response.Code = res.Code
|
||||
response.Message = fmt.Sprintf("proxy add failed, error : %v", res.Message)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
context.JSON(0, &response)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
proxyType := context.Query("type")
|
||||
proxy := v1.NewProxyConfigurerByType(v1.ProxyType(proxyType))
|
||||
|
||||
response := OperationResponse{
|
||||
Success: true,
|
||||
@@ -210,27 +149,15 @@ func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
||||
Message: "proxy update success",
|
||||
}
|
||||
|
||||
var b = make([]byte, context.Request.ContentLength)
|
||||
context.Request.Body.Read(b)
|
||||
var s = string(b)
|
||||
log.Print(s)
|
||||
toml.Unmarshal(b, &proxy)
|
||||
data, _ := context.GetRawData()
|
||||
|
||||
err := context.BindJSON(&proxy)
|
||||
if err != nil {
|
||||
response.Success = false
|
||||
response.Code = ParamError
|
||||
response.Message = fmt.Sprintf("update failed, param error : %v", err)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
c.UpdateFrpcConfig(data)
|
||||
|
||||
context.JSON(200, &response)
|
||||
|
||||
//proxy.GetBaseConfig()
|
||||
//
|
||||
//oldName := proxy[OldNameKey]
|
||||
//oldName := proxy[OriginalNameKey]
|
||||
//name := proxy[NameKey]
|
||||
//
|
||||
//if trimString(oldName) == "" || trimString(name) == "" {
|
||||
@@ -279,69 +206,6 @@ func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HandleController) MakeRemoveProxyFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
var proxies []ini.Section
|
||||
|
||||
response := OperationResponse{
|
||||
Success: true,
|
||||
Code: Success,
|
||||
Message: "proxy remove success",
|
||||
}
|
||||
|
||||
err := context.BindJSON(&proxies)
|
||||
if err != nil {
|
||||
response.Success = false
|
||||
response.Code = ParamError
|
||||
response.Message = fmt.Sprintf("proxy remove failed, param error : %v", err)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
tempProxyNames := make([]string, len(proxies))
|
||||
for index, proxy := range proxies {
|
||||
name := proxy[NameKey]
|
||||
|
||||
if trimString(name) == "" {
|
||||
response.Success = false
|
||||
response.Code = ParamError
|
||||
response.Message = fmt.Sprintf("proxy remove failed, proxy %v name invalid", name)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
if _, exist := clientProxies[name]; !exist {
|
||||
response.Success = false
|
||||
response.Code = ProxyExist
|
||||
response.Message = fmt.Sprintf("proxy remove failed, proxy %v not exist", name)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
tempProxyNames[index] = name
|
||||
}
|
||||
|
||||
for _, name := range tempProxyNames {
|
||||
delete(clientProxies, name)
|
||||
}
|
||||
|
||||
res := c.UpdateFrpcConfig()
|
||||
if !res.Success {
|
||||
response.Success = false
|
||||
response.Code = res.Code
|
||||
response.Message = fmt.Sprintf("proxy remvoe failed, error : %v", res.Message)
|
||||
log.Printf(response.Message)
|
||||
context.JSON(http.StatusOK, &response)
|
||||
return
|
||||
}
|
||||
|
||||
context.JSON(http.StatusOK, &response)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HandleController) MakeProxyFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
res := ProxyResponse{}
|
||||
@@ -378,11 +242,11 @@ func (c *HandleController) MakeProxyFunc() func(context *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HandleController) UpdateFrpcConfig() ProxyResponse {
|
||||
func (c *HandleController) UpdateFrpcConfig(tomlStr []byte) ProxyResponse {
|
||||
res := ProxyResponse{}
|
||||
|
||||
requestUrl := c.buildRequestUrl("/api/config")
|
||||
request, _ := http.NewRequest("PUT", requestUrl, bytes.NewReader(serializeSections()))
|
||||
request, _ := http.NewRequest("PUT", requestUrl, bytes.NewReader(tomlStr))
|
||||
response, err := c.getClientResponse(request, c.buildClient())
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -39,8 +39,6 @@ func (c *HandleController) Register(rootDir string, engine *gin.Engine) {
|
||||
group = engine.Group("/")
|
||||
}
|
||||
group.GET("/", c.MakeIndexFunc())
|
||||
group.POST("/add", c.MakeAddProxyFunc())
|
||||
group.POST("/update", c.MakeUpdateProxyFunc())
|
||||
group.POST("/remove", c.MakeRemoveProxyFunc())
|
||||
group.GET("/proxy/*serverApi", c.MakeProxyFunc())
|
||||
}
|
||||
|
||||
@@ -5,11 +5,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/fatedier/frp/pkg/config"
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/vaughan0/go-ini"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -22,44 +20,6 @@ func equalIgnoreCase(source string, target string) bool {
|
||||
return strings.ToUpper(source) == strings.ToUpper(target)
|
||||
}
|
||||
|
||||
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 := range sortSectionKeys(clientCommon) {
|
||||
build.WriteString(fmt.Sprintf("%s = %s\n", key, clientCommon[key]))
|
||||
}
|
||||
build.WriteString("\n")
|
||||
|
||||
sections := Sections{clientProxies}
|
||||
|
||||
for _, sectionInfo := range sections.sort() {
|
||||
name := sectionInfo.Name
|
||||
build.WriteString(fmt.Sprintf("[%s]\n", name))
|
||||
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")
|
||||
}
|
||||
|
||||
return []byte(build.String())
|
||||
}
|
||||
|
||||
func (c *HandleController) buildRequestUrl(serverApi string) string {
|
||||
var protocol string
|
||||
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/vaughan0/go-ini"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Success int = iota
|
||||
ParamError
|
||||
@@ -17,7 +11,7 @@ const (
|
||||
|
||||
const (
|
||||
NameKey = "name"
|
||||
OldNameKey = "_old_name"
|
||||
OriginalNameKey = "_original_name"
|
||||
SessionName = "GOSESSION"
|
||||
AuthName = "_PANEL_AUTH"
|
||||
LoginUrl = "/login"
|
||||
@@ -26,16 +20,6 @@ const (
|
||||
LogoutSuccessUrl = "/login"
|
||||
)
|
||||
|
||||
var (
|
||||
clientCommon ini.Section
|
||||
clientProxies map[string]ini.Section
|
||||
)
|
||||
|
||||
func init() {
|
||||
clientCommon = ini.Section{}
|
||||
clientProxies = make(map[string]ini.Section)
|
||||
}
|
||||
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
Err error
|
||||
@@ -75,38 +59,3 @@ type ProxyResponse struct {
|
||||
OperationResponse
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
type ClientProxies struct {
|
||||
Proxy ini.Section `json:"proxy"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user