feat(proxy): add modify proxy API endpoint and functionality
Implement proxy modification feature including: - New modifyFrpcProxy function in config.go - New ModifyProxyHandler in frpcProxyAct.go - New API endpoint in router.go - Updated API documentation in docs/api.md
This commit is contained in:
@@ -107,6 +107,101 @@ 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 ModifyProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ModifyProxyHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to read request body: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Failed to read request body")
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var reqMap map[string]interface{}
|
||||
if err := json.Unmarshal(body, &reqMap); err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to unmarshal request body: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||||
return
|
||||
}
|
||||
|
||||
instanceID := getStringFromMap(reqMap, "instanceID")
|
||||
if instanceID == "" {
|
||||
postLog.Error("[ModifyProxyHandler] instanceID is required")
|
||||
SendErrorResponse(w, http.StatusBadRequest, "instanceID is required")
|
||||
return
|
||||
}
|
||||
|
||||
proxyInfoMap, ok := reqMap["proxyInfo"].(map[string]interface{})
|
||||
if !ok {
|
||||
postLog.Error("[ModifyProxyHandler] Invalid proxyInfo format")
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid proxyInfo format")
|
||||
return
|
||||
}
|
||||
|
||||
proxyInfo := FrpcProxyInfo{
|
||||
Name: getStringFromMap(proxyInfoMap, "name"),
|
||||
Type: getStringFromMap(proxyInfoMap, "type"),
|
||||
LocalIP: getStringFromMap(proxyInfoMap, "localIP"),
|
||||
LocalPort: getStringFromMap(proxyInfoMap, "localPort"),
|
||||
RemotePort: getStringFromMap(proxyInfoMap, "remotePort"),
|
||||
}
|
||||
|
||||
if proxyInfo.Name == "" || proxyInfo.Type == "" || proxyInfo.LocalIP == "" ||
|
||||
proxyInfo.LocalPort == "" || proxyInfo.RemotePort == "" {
|
||||
postLog.Error("[ModifyProxyHandler] Missing required fields in proxyInfo")
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Missing required fields in proxyInfo")
|
||||
return
|
||||
}
|
||||
|
||||
var instance FrpcInstance
|
||||
instanceIDInt, _ := strconv.Atoi(instanceID)
|
||||
instance, err = DBQueryFrpcInstanceByID(instanceIDInt)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to query instance: %v", err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instance")
|
||||
return
|
||||
}
|
||||
|
||||
if instance.UserID != userID {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Instance not found for user %d", userID))
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
return
|
||||
}
|
||||
|
||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to read config file %s: %v", instance.ConfigPath, err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to read config file")
|
||||
return
|
||||
}
|
||||
|
||||
updatedContent, err := modifyFrpcProxy(string(configContent), proxyInfo)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to modify proxy: %v", err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to modify proxy")
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.WriteFile(instance.ConfigPath, []byte(updatedContent), 0644); err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyProxyHandler] Failed to write config file %s: %v", instance.ConfigPath, err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to write config file")
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Proxy modified successfully", map[string]interface{}{
|
||||
"instanceID": instance.ID,
|
||||
"configPath": instance.ConfigPath,
|
||||
"proxyName": proxyInfo.Name,
|
||||
})
|
||||
postLog.Info(fmt.Sprintf("[ModifyProxyHandler] Proxy %s modified successfully for instance %d", proxyInfo.Name, instance.ID))
|
||||
}
|
||||
|
||||
func DeleteProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user