refactor(frpc): replace ini with toml for config handling and improve proxy management

- Replace ini package with toml for more reliable config parsing
- Refactor proxy management to use structured config instead of string manipulation
- Add proper error handling for config operations
- Clean up unused imports and improve code organization
- Update go.mod dependencies accordingly
This commit is contained in:
2026-03-19 22:40:12 +08:00
parent c7fc0136b0
commit ab2e0567a9
8 changed files with 232 additions and 224 deletions

209
frpAct.go
View File

@@ -12,15 +12,14 @@ import (
"super-frpc/postLog"
"time"
"gopkg.in/ini.v1"
"github.com/BurntSushi/toml"
)
type InstanceInfo struct {
Name string `json:"name"`
ServerAddr string `json:"serverAddr"`
ServerPort string `json:"serverPort"`
AuthMethod string `json:"auth_method"`
// BootAtStart bool `json:"bootAtStart"`
Name string `json:"name"`
ServerAddr string `json:"serverAddr"`
ServerPort string `json:"serverPort"`
AuthMethod string `json:"auth_method"`
RunUser string `json:"runUser"`
Additional map[string]interface{} `json:"additionalProperties"`
}
@@ -40,6 +39,12 @@ type CreateInstanceRequest struct {
Additional map[string]interface{} `json:"additionalProperties"`
}
type FrpcConfig struct {
Common map[string]interface{} `toml:"common"`
Proxies []map[string]interface{} `toml:"proxies"`
Additional map[string]interface{} `toml:"-"`
}
var frpcDB *sql.DB
func CloseFrpcDatabase() error {
@@ -64,7 +69,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
// 先解析为map处理类型不匹配的情况
var reqMap map[string]interface{}
if err := json.Unmarshal(body, &reqMap); err != nil {
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to unmarshal request body: %v", err))
@@ -72,7 +76,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
// 处理bootAtStart字段
bootAtStart := false
if bas, ok := reqMap["bootAtStart"]; ok {
switch v := bas.(type) {
@@ -85,7 +88,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
}
}
// 处理instanceInfo字段
instanceInfoMap, ok := reqMap["instanceInfo"].(map[string]interface{})
if !ok {
SendErrorResponse(w, http.StatusBadRequest, "Invalid instanceInfo format")
@@ -100,12 +102,10 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
RunUser: getStringFromMap(reqMap, "runUser"),
}
// 处理additionalProperties字段
if additional, ok := reqMap["additionalProperties"].(map[string]interface{}); ok {
instanceInfo.Additional = additional
}
// 从Header中验证token和timeStamp
userID, _, err := ValidateRequestWithHeader(w, r)
if err != nil {
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to validate request header: %v", err))
@@ -113,7 +113,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
// 构建请求结构体
req := CreateInstanceRequest{
InstanceInfo: instanceInfo,
BootAtStart: bootAtStart,
@@ -152,7 +151,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Add frpc instance
configFileName := fmt.Sprintf("superfrpc_%s_%s.toml", user.Username, req.InstanceInfo.Name)
configPath := filepath.Join(configDir, configFileName)
@@ -190,7 +188,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
// Finish add frpc instance
SendSuccessResponse(w, "Instance created successfully", map[string]interface{}{
"name": req.InstanceInfo.Name,
@@ -248,12 +245,6 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) {
}
var instance FrpcInstance
// err = frpcDB.QueryRow(`
// SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt
// FROM frpcInstances WHERE userID = ? AND name = ?
// `, userID, instanceName).Scan(
// &instance.ID, &instance.UserID, &instance.Name, &instance.ServerAddr, &instance.ServerPort,
// &instance.AuthMethod, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &instance.CreatedAt)
instance, err = DBQueryFrpcInstance(userID, instanceName)
if err == sql.ErrNoRows {
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
@@ -334,7 +325,7 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
if modifyType != "configFile" && modifyType != "systemConfig" { // Detect valid modify type
if modifyType != "configFile" && modifyType != "systemConfig" {
SendErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("Unknown modify type %s", modifyType))
return
}
@@ -345,7 +336,6 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
// 从Header中验证token和timeStamp
userID, _, err := ValidateRequestWithHeader(w, r)
if err != nil {
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to validate request header: %v", err))
@@ -378,7 +368,6 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
// 验证 instanceID 是否匹配
if fmt.Sprintf("%d", instance.ID) != instanceID {
SendErrorResponse(w, http.StatusBadRequest, "instanceID does not match instanceName")
return
@@ -394,7 +383,6 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifiedData map[string]interface{}, username string) {
configPath := instance.ConfigPath
// Read current config file content
configContent, err := os.ReadFile(configPath)
if err != nil {
postLog.Error(fmt.Sprintf("[handleConfigFileModify] Failed to read config file %s: %v", configPath, err))
@@ -402,7 +390,6 @@ func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifi
return
}
// Parse config file content
updatedConfig, err := updateCommonSection(string(configContent), modifiedData)
if err != nil {
postLog.Error(fmt.Sprintf("[handleConfigFileModify] Failed to update common section: %v", err))
@@ -410,14 +397,12 @@ func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifi
return
}
// Write updated config file content back to file
if err := os.WriteFile(configPath, []byte(updatedConfig), 0644); err != nil {
postLog.Error(fmt.Sprintf("[handleConfigFileModify] Failed to write config file %s: %v", configPath, err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to write config file")
return
}
// Update instance fields in database
if v, ok := modifiedData["server_addr"].(string); ok && v != "" {
instance.ServerAddr = v
}
@@ -443,44 +428,23 @@ func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifi
}
func updateCommonSection(configContent string, modifiedData map[string]interface{}) (string, error) {
cfg, err := ini.Load([]byte(configContent))
if err != nil {
var config FrpcConfig
if _, err := toml.Decode(configContent, &config); err != nil {
return "", fmt.Errorf("failed to parse config: %w", err)
}
commonSection := cfg.Section("common")
if commonSection == nil {
return "", fmt.Errorf("common section not found")
}
for key, value := range modifiedData {
commonSection.Key(key).SetValue(formatConfigValue(value))
config.Common[key] = value
}
var buf strings.Builder
if _, err := cfg.WriteTo(&buf); err != nil {
if err := toml.NewEncoder(&buf).Encode(config); err != nil {
return "", fmt.Errorf("failed to write config: %w", err)
}
return buf.String(), nil
}
func formatConfigValue(value interface{}) string {
switch v := value.(type) {
case string:
return v
case bool:
return fmt.Sprintf("%t", v)
case float64:
if v == float64(int64(v)) {
return fmt.Sprintf("%d", int64(v))
}
return fmt.Sprintf("%f", v)
default:
return fmt.Sprintf("%v", v)
}
}
func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance FrpcInstance, modifiedData map[string]interface{}, user *User) {
newName := instance.Name
newRunUser := instance.RunUser
@@ -501,7 +465,6 @@ func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance F
oldConfigPath := instance.ConfigPath
var newConfigPath string
// If instance name or run user changed, need to rename config file
if newName != instance.Name || newRunUser != instance.RunUser {
configDir, err := GetConfigDir()
if err != nil {
@@ -526,7 +489,6 @@ func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance F
newConfigPath = oldConfigPath
}
// Update instance fields in database
instance.Name = newName
instance.RunUser = newRunUser
instance.BootAtStart = newBootAtStart
@@ -538,7 +500,6 @@ func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance F
return
}
// Handle boot service creation and removal
if oldBootAtStart && !newBootAtStart {
if err := removeBootService(user.Username, instance.Name); err != nil {
postLog.Error(fmt.Sprintf("[handleSystemConfigModify] Failed to remove boot service: %v", err))
@@ -574,78 +535,27 @@ func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance F
data["bootServiceError"] = bootServiceError
}
SendSuccessResponse(w, "System config modified successfully", data)
postLog.Info(fmt.Sprintf("[handleSystemConfigModify] System config for instance %s modified successfully: bootAtStart=%v, runUser=%s", newName, newBootAtStart, newRunUser))
}
func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
userID, _, err := ValidateRequestWithHeader(w, r)
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to validate request: %v", err))
SendErrorResponse(w, http.StatusUnauthorized, "Failed to validate request")
return
}
userType, err := GetUserType(userID)
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to get user type: %v", err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
return
}
instanceList, err := DBListFrpcInstances()
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to query instances: %v", err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instances")
return
}
var responseInstances []map[string]interface{}
for _, instance := range instanceList {
instanceData := map[string]interface{}{
"instanceID": instance.ID,
"name": instance.Name,
"serverAddr": instance.ServerAddr,
"serverPort": instance.ServerPort,
"auth_method": instance.AuthMethod,
"bootAtStart": instance.BootAtStart,
"runUser": instance.RunUser,
"configPath": instance.ConfigPath,
"createdAt": instance.CreatedAt,
"createdBy": instance.CreatedBy,
}
if userType == "visitor" {
delete(instanceData, "serverAddr")
delete(instanceData, "serverPort")
delete(instanceData, "auth_method")
}
responseInstances = append(responseInstances, instanceData)
}
if responseInstances == nil {
responseInstances = []map[string]interface{}{}
}
SendSuccessResponse(w, "Instances retrieved successfully", responseInstances)
}
func generateFrpcConfig(info InstanceInfo) string {
var sb strings.Builder
sb.WriteString("[common]\n")
sb.WriteString(fmt.Sprintf("server_addr = %s\n", info.ServerAddr))
sb.WriteString(fmt.Sprintf("server_port = %s\n", info.ServerPort))
sb.WriteString(fmt.Sprintf("auth_method = %s\n", info.AuthMethod))
for key, value := range info.Additional {
sb.WriteString(fmt.Sprintf("%s = %v\n", key, value))
config := FrpcConfig{
Common: make(map[string]interface{}),
}
sb.WriteString("\n")
return sb.String()
config.Common["server_addr"] = info.ServerAddr
config.Common["server_port"] = info.ServerPort
config.Common["auth_method"] = info.AuthMethod
for key, value := range info.Additional {
config.Common[key] = value
}
var buf strings.Builder
if err := toml.NewEncoder(&buf).Encode(config); err != nil {
return ""
}
return buf.String()
}
func GetUserInstances(userID int) ([]FrpcInstance, error) {
@@ -681,3 +591,56 @@ func getStringFromMap(m map[string]interface{}, key string) string {
}
return ""
}
func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
postLog.Debug(fmt.Sprintf("[ListInstancesHandler] Invalid request method: %s", r.Method))
return
}
userID, _, err := ValidateRequestWithHeader(w, r)
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to validate request header: %v", err))
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
return
}
userType, err := GetUserType(userID)
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to get user type: %v", err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
return
}
instances, err := GetUserInstances(userID)
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to get user instances: %v", err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get instances")
return
}
instanceList := make([]map[string]interface{}, len(instances))
for i, inst := range instances {
instanceData := map[string]interface{}{
"instanceID": inst.ID,
"name": inst.Name,
"bootAtStart": inst.BootAtStart,
"runUser": inst.RunUser,
"configPath": inst.ConfigPath,
"createdAt": inst.CreatedAt,
"createdBy": inst.CreatedBy,
}
if userType == "admin" || userType == "superuser" {
instanceData["serverAddr"] = inst.ServerAddr
instanceData["serverPort"] = inst.ServerPort
instanceData["auth_method"] = inst.AuthMethod
}
instanceList[i] = instanceData
}
SendSuccessResponse(w, "Instances retrieved successfully", instanceList)
postLog.Info(fmt.Sprintf("[ListInstancesHandler] Retrieved %d instances for user %d (type: %s)", len(instances), userID, userType))
}