fix(proxy): add "oldName" && "newName" paras to fix unable to modify proxy name

This commit is contained in:
2026-05-05 12:52:04 +08:00
parent e72c142b16
commit a2869c1e0e
3 changed files with 32 additions and 11 deletions
+14 -3
View File
@@ -23,6 +23,8 @@ type InstanceInfo struct {
type FrpcProxyInfo struct { type FrpcProxyInfo struct {
Name string `json:"name"` Name string `json:"name"`
OldName string `json:"oldName"`
NewName string `json:"newName"`
Type string `json:"type"` Type string `json:"type"`
LocalIP string `json:"local_ip"` LocalIP string `json:"local_ip"`
LocalPort int `json:"local_port"` LocalPort int `json:"local_port"`
@@ -349,11 +351,20 @@ func ModifyFrpcProxy(configContent string, info FrpcProxyInfo) (string, error) {
return "", fmt.Errorf("failed to parse config: %w", err) return "", fmt.Errorf("failed to parse config: %w", err)
} }
searchName := info.OldName
if searchName == "" {
searchName = info.Name
}
var found bool var found bool
for i, proxy := range config.Proxies { for i, proxy := range config.Proxies {
if name, ok := proxy["name"].(string); ok && name == info.Name { if name, ok := proxy["name"].(string); ok && name == searchName {
proxyName := info.NewName
if proxyName == "" {
proxyName = info.Name
}
config.Proxies[i] = map[string]interface{}{ config.Proxies[i] = map[string]interface{}{
"name": info.Name, "name": proxyName,
"type": info.Type, "type": info.Type,
"localIP": info.LocalIP, "localIP": info.LocalIP,
"localPort": info.LocalPort, "localPort": info.LocalPort,
@@ -365,7 +376,7 @@ func ModifyFrpcProxy(configContent string, info FrpcProxyInfo) (string, error) {
} }
if !found { if !found {
return "", fmt.Errorf("proxy %s not found", info.Name) return "", fmt.Errorf("proxy %s not found", searchName)
} }
result, err := EncodeFrpcConfig(config) result, err := EncodeFrpcConfig(config)
+6 -4
View File
@@ -1063,7 +1063,8 @@ X-Timestamp: 1704067200000
{ {
"instanceID": "1", "instanceID": "1",
"proxyInfo": { "proxyInfo": {
"name": "ssh_proxy", "oldName": "ssh_proxy",
"newName": "ssh_proxy123",
"type": "tcp", "type": "tcp",
"localIP": "127.0.0.1", "localIP": "127.0.0.1",
"localPort": "22", "localPort": "22",
@@ -1080,7 +1081,8 @@ X-Timestamp: 1704067200000
| Field | Type | Required | Description | | Field | Type | Required | Description |
|-------|------|----------|-------------| |-------|------|----------|-------------|
| instanceID | string | Yes | Instance ID (the ID of the frpc instance) | | instanceID | string | Yes | Instance ID (the ID of the frpc instance) |
| proxyInfo.name | string | Yes | Proxy name (used to identify which proxy to modify) | | proxyInfo.oldName | string | Yes | Proxy old name (used to identify which proxy to modify) |
| proxyInfo.newName | string | Yes | Proxy new name (used to identify which proxy to modify to) |
| proxyInfo.type | string | Yes | Proxy type (e.g., tcp, udp, http, https) | | proxyInfo.type | string | Yes | Proxy type (e.g., tcp, udp, http, https) |
| proxyInfo.localIP | string | Yes | Local IP address to forward to | | proxyInfo.localIP | string | Yes | Local IP address to forward to |
| proxyInfo.localPort | string | Yes | Local port to forward from | | proxyInfo.localPort | string | Yes | Local port to forward from |
@@ -1094,7 +1096,7 @@ X-Timestamp: 1704067200000
"data": { "data": {
"instanceID": 1, "instanceID": 1,
"configPath": "./configs/superfrpc_user_my_frpc.toml", "configPath": "./configs/superfrpc_user_my_frpc.toml",
"proxyName": "ssh_proxy" "proxyName": "ssh_proxy123"
} }
} }
``` ```
@@ -1111,7 +1113,7 @@ The proxy configuration in the config file will be updated to the following form
```toml ```toml
[[proxies]] [[proxies]]
name = ssh_proxy name = ssh_proxy123
type = tcp type = tcp
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 22 local_port = 22
+12 -4
View File
@@ -10,8 +10,8 @@ import (
"super-frpc/config" "super-frpc/config"
"super-frpc/database" "super-frpc/database"
"super-frpc/utils"
"super-frpc/postLog" "super-frpc/postLog"
"super-frpc/utils"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
@@ -149,6 +149,8 @@ func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
} }
proxyInfo := config.FrpcProxyInfo{ proxyInfo := config.FrpcProxyInfo{
OldName: getStringFromMap(proxyInfoMap, "oldName"),
NewName: getStringFromMap(proxyInfoMap, "newName"),
Name: getStringFromMap(proxyInfoMap, "name"), Name: getStringFromMap(proxyInfoMap, "name"),
Type: getStringFromMap(proxyInfoMap, "type"), Type: getStringFromMap(proxyInfoMap, "type"),
LocalIP: getStringFromMap(proxyInfoMap, "localIP"), LocalIP: getStringFromMap(proxyInfoMap, "localIP"),
@@ -156,7 +158,13 @@ func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
RemotePort: getNumFromMap(proxyInfoMap, "remotePort"), RemotePort: getNumFromMap(proxyInfoMap, "remotePort"),
} }
if proxyInfo.Name == "" || proxyInfo.Type == "" || proxyInfo.LocalIP == "" || if proxyInfo.OldName == "" {
postLog.Error("[ModifyProxyHandler] oldName is required")
utils.SendErrorResponse(w, http.StatusBadRequest, "oldName is required")
return
}
if proxyInfo.NewName == "" || proxyInfo.Type == "" || proxyInfo.LocalIP == "" ||
proxyInfo.LocalPort == 0 || proxyInfo.RemotePort == 0 { proxyInfo.LocalPort == 0 || proxyInfo.RemotePort == 0 {
postLog.Error("[ModifyProxyHandler] Missing required fields in proxyInfo") postLog.Error("[ModifyProxyHandler] Missing required fields in proxyInfo")
utils.SendErrorResponse(w, http.StatusBadRequest, "Missing required fields in proxyInfo") utils.SendErrorResponse(w, http.StatusBadRequest, "Missing required fields in proxyInfo")
@@ -201,9 +209,9 @@ func ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
utils.SendSuccessResponse(w, "Proxy modified successfully", map[string]interface{}{ utils.SendSuccessResponse(w, "Proxy modified successfully", map[string]interface{}{
"instanceID": instance.ID, "instanceID": instance.ID,
"configPath": instance.ConfigPath, "configPath": instance.ConfigPath,
"proxyName": proxyInfo.Name, "proxyName": proxyInfo.NewName,
}) })
postLog.Info(fmt.Sprintf("[ModifyProxyHandler] Proxy %s modified successfully for instance %d", proxyInfo.Name, instance.ID)) postLog.Info(fmt.Sprintf("[ModifyProxyHandler] Proxy %s modified successfully for instance %d", proxyInfo.NewName, instance.ID))
} }
func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) { func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {