Files
frp-panel/common/response.go
VaalaCat 373276633f feat: proxy list [新增代理列表实体]
refactor: change client manager structure [重构:更改客户端管理器结构适配影子客户端]

feat: add proxy config table and dao [添加代理配置独立数据表和DAO层]

feat: new proxy config entity [新建的代理配置实体]

feat: update and delete proxy config [更新和删除代理配置接口]

feat: get config api and beautify proxy item [更新获取配置API并美化代理项]

feat: change proxy form style [美化修改代理的表单样式]

fix: client edit [修复:编辑客户端的问题]

fix: shadow copy status error [修复:影子客户端复制状态错误]

fix: http proxy bug [修复:HTTP代理类型的错误]

fix: cannot update client [修复:无法更新客户端]

feat: record trigger refetch [自动重新获取表格内的数据]

fix: filter string length [修复:过滤字符串长度]

fix: add client error [修复:添加客户端错误]

fix: do not notify client when stopped [修复:停止时不通知客户端]

fix: delete when proxy duplicate [修复:代理重复时删除]

feat: add http proxy location [添加HTTP代理路由路径]

chore: edit style [编辑样式美化]

fix: remove expired client [修复:自动移除过期客户端]

feat: proxy status [新增代理状态提示]

fix: build [修复:构建]

fix: refetch trigger [修复:重新获取数据的问题]

fix: remove all expired client [修复:移除所有过期客户端]

feat: i18n for proxy [代理页面的国际化翻译]
2024-12-20 12:18:28 +00:00

101 lines
3.1 KiB
Go

package common
import (
"fmt"
"net/http"
"github.com/VaalaCat/frp-panel/pb"
"github.com/gin-gonic/gin"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
type RespType interface {
pb.UpdateFRPCResponse | pb.RemoveFRPCResponse |
pb.UpdateFRPSResponse | pb.RemoveFRPSResponse |
pb.CommonResponse | pb.RegisterResponse | pb.LoginResponse |
pb.InitClientResponse | pb.ListClientsResponse | pb.GetClientResponse |
pb.DeleteClientResponse |
pb.InitServerResponse | pb.ListServersResponse | pb.GetServerResponse |
pb.DeleteServerResponse |
pb.GetUserInfoResponse | pb.UpdateUserInfoResponse |
pb.GetPlatformInfoResponse | pb.GetClientsStatusResponse |
pb.GetClientCertResponse |
pb.StartFRPCResponse | pb.StopFRPCResponse | pb.StartFRPSResponse | pb.StopFRPSResponse |
pb.GetProxyStatsByClientIDResponse | pb.GetProxyStatsByServerIDResponse |
pb.CreateProxyConfigResponse | pb.ListProxyConfigsResponse | pb.UpdateProxyConfigResponse |
pb.DeleteProxyConfigResponse | pb.GetProxyConfigResponse
}
func OKResp[T RespType](c *gin.Context, origin *T) {
c.Header(TraceIDKey, c.GetString(TraceIDKey))
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusOK, origin)
} else {
c.JSON(http.StatusOK, OK(ReqSuccess).WithBody(origin))
}
}
func ErrResp[T RespType](c *gin.Context, origin *T, err string) {
c.Header(TraceIDKey, c.GetString(TraceIDKey))
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusInternalServerError, origin)
} else {
c.JSON(http.StatusOK, Err(err).WithBody(origin))
}
}
func ErrUnAuthorized(c *gin.Context, err string) {
c.Header(TraceIDKey, c.GetString(TraceIDKey))
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusUnauthorized,
&pb.CommonResponse{Status: &pb.Status{Code: pb.RespCode_RESP_CODE_UNAUTHORIZED, Message: err}})
} else {
c.JSON(http.StatusOK, Err(err))
}
}
func ProtoResp[T RespType](origin *T) (*pb.ClientMessage, error) {
event, msg, err := getEvent(origin)
if err != nil {
return nil, err
}
rawData, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return &pb.ClientMessage{
Event: event,
Data: rawData,
}, nil
}
func getEvent(origin interface{}) (pb.Event, protoreflect.ProtoMessage, error) {
switch ptr := any(origin).(type) {
case *pb.CommonResponse:
return pb.Event_EVENT_DATA, ptr, nil
case *pb.UpdateFRPCResponse:
return pb.Event_EVENT_UPDATE_FRPC, ptr, nil
case *pb.RemoveFRPCResponse:
return pb.Event_EVENT_REMOVE_FRPC, ptr, nil
case *pb.UpdateFRPSResponse:
return pb.Event_EVENT_UPDATE_FRPC, ptr, nil
case *pb.RemoveFRPSResponse:
return pb.Event_EVENT_REMOVE_FRPC, ptr, nil
case *pb.StartFRPCResponse:
return pb.Event_EVENT_START_FRPC, ptr, nil
case *pb.StopFRPCResponse:
return pb.Event_EVENT_STOP_FRPC, ptr, nil
case *pb.StartFRPSResponse:
return pb.Event_EVENT_START_FRPS, ptr, nil
case *pb.StopFRPSResponse:
return pb.Event_EVENT_STOP_FRPS, ptr, nil
case *pb.GetProxyConfigResponse:
return pb.Event_EVENT_GET_PROXY_INFO, ptr, nil
default:
return 0, nil, fmt.Errorf("cannot unmarshal unknown type: %T", origin)
}
}