refactor(config): improve config file handling and error messages
- Replace manual address formatting with net.JoinHostPort in tcpClient - Simplify debug/error messages in IsInstanceRunning by removing redundant output - Fix incorrect map reference for RunUser in CreateInstanceHandler - Extract config file creation logic into handleConfigFileCreate - Add auth_token to response data in GetInstanceInfoHandler - Remove redundant AuthToken field from InstanceInfo struct
This commit is contained in:
@@ -27,7 +27,6 @@ type InstanceInfo struct {
|
|||||||
ServerAddr string `json:"serverAddr"`
|
ServerAddr string `json:"serverAddr"`
|
||||||
ServerPort string `json:"serverPort"`
|
ServerPort string `json:"serverPort"`
|
||||||
AuthMethod string `json:"auth_method"`
|
AuthMethod string `json:"auth_method"`
|
||||||
AuthToken string `json:"auth_token"`
|
|
||||||
RunUser string `json:"runUser"`
|
RunUser string `json:"runUser"`
|
||||||
Additional map[string]interface{} `json:"additionalProperties"`
|
Additional map[string]interface{} `json:"additionalProperties"`
|
||||||
}
|
}
|
||||||
@@ -231,26 +230,64 @@ func encodeFrpcConfig(config FrpcConfig) (string, error) {
|
|||||||
return strings.TrimSuffix(buf.String(), "\n"), nil
|
return strings.TrimSuffix(buf.String(), "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateFrpcConfig(info InstanceInfo) string {
|
func handleConfigFileCreate(configPath string, info InstanceInfo) error {
|
||||||
config := FrpcConfig{
|
config := FrpcConfig{
|
||||||
Global: make(map[string]interface{}),
|
Global: make(map[string]interface{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Global["serverAddr"] = info.ServerAddr
|
|
||||||
config.Global["serverPort"] = info.ServerPort
|
|
||||||
config.Global["auth.method"] = info.AuthMethod
|
|
||||||
config.Global["auth.token"] = info.AuthToken
|
|
||||||
|
|
||||||
for key, value := range info.Additional {
|
|
||||||
config.Global[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := encodeFrpcConfig(config)
|
result, err := encodeFrpcConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return fmt.Errorf("failed to encode empty config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
if err := os.WriteFile(configPath, []byte(result), 0644); err != nil {
|
||||||
|
return fmt.Errorf("failed to create config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configData := make(map[string]interface{})
|
||||||
|
configData["serverAddr"] = info.ServerAddr
|
||||||
|
configData["serverPort"] = info.ServerPort
|
||||||
|
configData["auth.method"] = info.AuthMethod
|
||||||
|
|
||||||
|
if authToken, ok := info.Additional["auth_token"]; ok {
|
||||||
|
configData["auth.token"] = authToken
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range info.Additional {
|
||||||
|
if key == "auth_token" || key == "auth_method" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
configData[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range configData {
|
||||||
|
var configValue string
|
||||||
|
if key == "serverPort" {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case float64:
|
||||||
|
configValue = strconv.Itoa(int(v))
|
||||||
|
case int:
|
||||||
|
configValue = strconv.Itoa(v)
|
||||||
|
case string:
|
||||||
|
var intVal int
|
||||||
|
if _, err := fmt.Sscanf(v, "%d", &intVal); err == nil {
|
||||||
|
configValue = strconv.Itoa(intVal)
|
||||||
|
} else {
|
||||||
|
configValue = v
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
configValue = fmt.Sprintf("%v", value)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
configValue = fmt.Sprintf("%v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := setKeyText(configPath, key, "", configValue); err != nil {
|
||||||
|
return fmt.Errorf("failed to set key %s: %w", key, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addFrpcProxy(configContent string, info FrpcProxyInfo) (string, error) {
|
func addFrpcProxy(configContent string, info FrpcProxyInfo) (string, error) {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
ServerAddr: getStringFromMap(instanceInfoMap, "serverAddr"),
|
ServerAddr: getStringFromMap(instanceInfoMap, "serverAddr"),
|
||||||
ServerPort: getStringFromMap(instanceInfoMap, "serverPort"),
|
ServerPort: getStringFromMap(instanceInfoMap, "serverPort"),
|
||||||
AuthMethod: getStringFromMap(instanceInfoMap, "auth_method"),
|
AuthMethod: getStringFromMap(instanceInfoMap, "auth_method"),
|
||||||
RunUser: getStringFromMap(reqMap, "runUser"),
|
RunUser: getStringFromMap(instanceInfoMap, "runUser"),
|
||||||
}
|
}
|
||||||
|
|
||||||
if additional, ok := reqMap["additionalProperties"].(map[string]interface{}); ok {
|
if additional, ok := reqMap["additionalProperties"].(map[string]interface{}); ok {
|
||||||
@@ -111,8 +111,7 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
configFileName := fmt.Sprintf("superfrpc_%s_%s.toml", user.Username, req.InstanceInfo.Name)
|
configFileName := fmt.Sprintf("superfrpc_%s_%s.toml", user.Username, req.InstanceInfo.Name)
|
||||||
configPath := filepath.Join(configDir, configFileName)
|
configPath := filepath.Join(configDir, configFileName)
|
||||||
|
|
||||||
configContent := generateFrpcConfig(req.InstanceInfo)
|
if err := handleConfigFileCreate(configPath, req.InstanceInfo); err != nil {
|
||||||
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
|
|
||||||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to create config file %s: %v", configPath, err))
|
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to create config file %s: %v", configPath, err))
|
||||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to create config file")
|
SendErrorResponse(w, http.StatusInternalServerError, "Failed to create config file")
|
||||||
return
|
return
|
||||||
@@ -1002,6 +1001,9 @@ func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if authMethod, ok := config.Global["auth.method"]; ok {
|
if authMethod, ok := config.Global["auth.method"]; ok {
|
||||||
responseData["auth_method"] = authMethod
|
responseData["auth_method"] = authMethod
|
||||||
}
|
}
|
||||||
|
if authToken, ok := config.Global["auth.token"]; ok {
|
||||||
|
responseData["auth_token"] = authToken
|
||||||
|
}
|
||||||
|
|
||||||
if userType == "admin" || userType == "superuser" {
|
if userType == "admin" || userType == "superuser" {
|
||||||
if serverAddr, ok := config.Global["serverAddr"]; ok {
|
if serverAddr, ok := config.Global["serverAddr"]; ok {
|
||||||
|
|||||||
@@ -445,15 +445,15 @@ func IsInstanceRunning(instanceID int) error {
|
|||||||
return fmt.Errorf("service %s is not running", serviceName)
|
return fmt.Errorf("service %s is not running", serviceName)
|
||||||
}
|
}
|
||||||
if exitCode == 3 {
|
if exitCode == 3 {
|
||||||
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is not running: %s, output: %s", serviceName, outputStr))
|
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is not running: %s", serviceName, outputStr))
|
||||||
return fmt.Errorf("service %s is not running", serviceName)
|
return fmt.Errorf("service %s is not running", serviceName)
|
||||||
}
|
}
|
||||||
if exitCode == 4 {
|
if exitCode == 4 {
|
||||||
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist: %s, output: %s", serviceName, outputStr))
|
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist: %s", serviceName, outputStr))
|
||||||
return fmt.Errorf("service %s does not exist", serviceName)
|
return fmt.Errorf("service %s does not exist", serviceName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s, output: %s", err, outputStr))
|
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s", err))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func tcpConnect(ipaddr string, port int) error {
|
|||||||
return fmt.Errorf("already connected")
|
return fmt.Errorf("already connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
address := fmt.Sprintf("%s:%d", ipaddr, port)
|
address := net.JoinHostPort(ipaddr, fmt.Sprintf("%d", port))
|
||||||
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
|
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to %s: %v", address, err)
|
return fmt.Errorf("failed to connect to %s: %v", address, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user