refactor(frpc): restructure instance management and config handling

- Move instance-related structs and functions to config.go
- Remove serverAddr, serverPort, and authMethod from database schema
- Implement new config parsing and encoding with nested key support
- Update service management to use instanceID instead of username/name
- Add GetServiceNameByInstanceID helper function
- Update API documentation for auth.method field change
This commit is contained in:
2026-03-25 20:00:34 +08:00
parent da729b44ff
commit 92a0e24db7
8 changed files with 491 additions and 281 deletions

View File

@@ -7,17 +7,11 @@ import (
"net/http"
"os"
"strconv"
"strings"
"super-frpc/postLog"
"github.com/BurntSushi/toml"
)
type CreateProxyRequest struct {
InstanceID string `json:"instanceID"`
ProxyInfo FrpcProxyInfo `json:"proxyInfo"`
}
func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
@@ -129,30 +123,6 @@ func CreateProxyHandler(w http.ResponseWriter, r *http.Request) {
postLog.Info(fmt.Sprintf("[CreateProxyHandler] Proxy %s created successfully for instance %d", proxyInfo.Name, instance.ID))
}
func addFrpcProxy(configContent string, info FrpcProxyInfo) (string, error) {
var config FrpcConfig
if _, err := toml.Decode(configContent, &config); err != nil {
return "", fmt.Errorf("failed to parse config: %w", err)
}
proxy := map[string]interface{}{
"name": info.Name,
"type": info.Type,
"localIP": info.LocalIP,
"localPort": info.LocalPort,
"remotePort": info.RemotePort,
}
config.Proxies = append(config.Proxies, proxy)
var buf strings.Builder
if err := toml.NewEncoder(&buf).Encode(config); err != nil {
return "", fmt.Errorf("failed to write config: %w", err)
}
return buf.String(), nil
}
func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
@@ -249,36 +219,6 @@ func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
postLog.Info(fmt.Sprintf("[DeleteProxyHandler] Proxy %s deleted successfully from instance %d", proxyName, instance.ID))
}
func removeFrpcProxy(configContent string, proxyName string) (string, error) {
var config FrpcConfig
if _, err := toml.Decode(configContent, &config); err != nil {
return "", fmt.Errorf("failed to parse config: %w", err)
}
var found bool
var newProxies []map[string]interface{}
for _, proxy := range config.Proxies {
if name, ok := proxy["name"].(string); ok && name == proxyName {
found = true
continue
}
newProxies = append(newProxies, proxy)
}
if !found {
return "", fmt.Errorf("proxy %s not found", proxyName)
}
config.Proxies = newProxies
var buf strings.Builder
if err := toml.NewEncoder(&buf).Encode(config); err != nil {
return "", fmt.Errorf("failed to write config: %w", err)
}
return buf.String(), nil
}
func ListProxiesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")