diff --git a/internal/application/frpc/service.go b/internal/application/frpc/service.go index a86bd85..627d80f 100644 --- a/internal/application/frpc/service.go +++ b/internal/application/frpc/service.go @@ -145,7 +145,7 @@ func (fs *Service) genCommonCfgs(id *string) (*v1.ClientCommonConfig, error) { } func (fs *Service) genProxyCfgs(serverId *string) ([]v1.ProxyConfigurer, error) { - proxies, err := fs.proxyRepo.FindByServerID(*serverId) + proxies, err := fs.proxyRepo.FindEnabledByServerID(*serverId) if err != nil { fs.app.Logger().Error("genProxyCfgs", "err", err) return nil, err diff --git a/internal/domain/proxy/repository.go b/internal/domain/proxy/repository.go index ae90332..6ecc25f 100644 --- a/internal/domain/proxy/repository.go +++ b/internal/domain/proxy/repository.go @@ -9,4 +9,5 @@ type Repository interface { UpdateBootStatusByServerID(serverID string, status ProxyBootStatus) error ResetAllBootStatus() error FindByServerID(serverID string) ([]Proxy, error) + FindEnabledByServerID(serverID string) ([]Proxy, error) } diff --git a/internal/infrastructure/persistence/proxy_repository.go b/internal/infrastructure/persistence/proxy_repository.go index d624d07..6c0e395 100644 --- a/internal/infrastructure/persistence/proxy_repository.go +++ b/internal/infrastructure/persistence/proxy_repository.go @@ -151,3 +151,23 @@ func (r *ProxyRepository) FindByServerID(serverID string) ([]proxy.Proxy, error) return proxies, nil } + +func (r *ProxyRepository) FindEnabledByServerID(serverID string) ([]proxy.Proxy, error) { + var proxies []proxy.Proxy + err := r.app.DB(). + Select("fh_proxies.*"). + From("fh_proxies"). + Where(dbx.And( + dbx.HashExp{"serverId": serverID}, + dbx.HashExp{"status": string(proxy.ProxyStatusEnabled)}, + )). + All(&proxies) + + if err != nil { + r.app.Logger().Error("Failed to find enabled proxies by server id", "serverID", serverID, "error", err) + return nil, err + } + + return proxies, nil +} + diff --git a/internal/interfaces/http/frpc_handler.go b/internal/interfaces/http/frpc_handler.go index 8f73b25..d510796 100644 --- a/internal/interfaces/http/frpc_handler.go +++ b/internal/interfaces/http/frpc_handler.go @@ -84,4 +84,29 @@ func (h *FrpcHandler) RegisterHandlers(e *core.ServeEvent) { } return e.JSON(200, map[string]string{"message": "frpc terminated"}) })) + + e.Router.POST("/api/frpc/reload", requireAuth(func(e *core.RequestEvent) error { + data := struct { + ID string `json:"id"` + }{} + if err := e.BindBody(&data); err != nil { + return e.JSON(400, map[string]string{"error": "invalid request body"}) + } + id := data.ID + if id == "" { + return e.JSON(400, map[string]string{"error": "id is required"}) + } + if !validID.MatchString(id) { + return e.JSON(400, map[string]string{"error": "invalid id format"}) + } + // Only reload if the server is currently running + if !h.service.IsServerRunning(id) { + return e.JSON(200, map[string]string{"message": "server not running, skipped"}) + } + err := h.service.ReloadFrpc(&id) + if err != nil { + return e.JSON(500, map[string]string{"error": err.Error()}) + } + return e.JSON(200, map[string]string{"message": "frpc reloaded"}) + })) } diff --git a/site/src/pages/Proxies/useProxies.ts b/site/src/pages/Proxies/useProxies.ts index 2542c6b..612dea4 100644 --- a/site/src/pages/Proxies/useProxies.ts +++ b/site/src/pages/Proxies/useProxies.ts @@ -1,5 +1,6 @@ import { useState, useEffect, useCallback } from "react"; import pb from "../../lib/pocketbase"; +import { apiPost } from "../../lib/api"; import { toast } from "sonner"; export interface Proxy { @@ -67,10 +68,15 @@ export function useProxies() { }, [fetchProxies]); const deleteProxy = async (id: string) => { + const proxy = proxies.find((p) => p.id === id); try { await pb.collection("fh_proxies").delete(id); await fetchProxies(); toast.success("Proxy deleted successfully"); + // Reload frp config if we know which server this proxy belonged to + if (proxy?.serverId) { + await apiPost("/api/frpc/reload", { id: proxy.serverId }); + } } catch (err) { toast.error(err instanceof Error ? err.message : "Failed to delete proxy"); } @@ -84,6 +90,8 @@ export function useProxies() { ); try { await pb.collection("fh_proxies").update(proxy.id, { status: newStatus }); + // Reload frp config so the change takes effect immediately + await apiPost("/api/frpc/reload", { id: proxy.serverId }); } catch (err) { // Rollback on failure setProxies((prev) =>