mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 15:06:37 +08:00
33b4123444
Migrate Agent/Relay/Tunnel handlers from compat {success,message,data}
to response.OK and response.Abort* with real HTTP status codes. Remove
the compat package and update openflare-agent, openflare-relay, and
openflared clients to parse {error_msg,data}.
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package flared
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/apps/openflare/apiutil"
|
|
ofws "github.com/Rain-kl/Wavelet/internal/apps/openflare/websocket"
|
|
"github.com/Rain-kl/Wavelet/internal/common/response"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PostHeartbeat handles POST /tunnel/heartbeat.
|
|
func PostHeartbeat(c *gin.Context) {
|
|
var payload HeartbeatPayload
|
|
if !apiutil.BindJSON(c, &payload) {
|
|
return
|
|
}
|
|
|
|
authNode, ok := c.Get(ctxFlaredNodeKey)
|
|
if !ok {
|
|
response.AbortUnauthorized(c, errTunnelTokenInvalid)
|
|
return
|
|
}
|
|
node := authNode.(*model.OpenFlareNode)
|
|
|
|
result, err := Heartbeat(c.Request.Context(), node, payload)
|
|
if apiutil.AbortBadRequestOnError(c, err) {
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response.OK(result))
|
|
}
|
|
|
|
// GetActiveConfig handles GET /tunnel/config/active.
|
|
func GetActiveConfig(c *gin.Context) {
|
|
authNode, ok := c.Get(ctxFlaredNodeKey)
|
|
if !ok {
|
|
response.AbortUnauthorized(c, errTunnelTokenInvalid)
|
|
return
|
|
}
|
|
node := authNode.(*model.OpenFlareNode)
|
|
|
|
config, err := GetTunnelConfig(c.Request.Context(), node)
|
|
if apiutil.AbortBadRequestOnError(c, err) {
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response.OK(config))
|
|
}
|
|
|
|
// PostApplyLog handles POST /tunnel/apply-log.
|
|
func PostApplyLog(c *gin.Context) {
|
|
var payload ApplyLogPayload
|
|
if !apiutil.BindJSON(c, &payload) {
|
|
return
|
|
}
|
|
if authNode, ok := c.Get(ctxFlaredNodeKey); ok {
|
|
payload.NodeID = authNode.(*model.OpenFlareNode).NodeID
|
|
}
|
|
|
|
log, err := ReportApplyLog(c.Request.Context(), payload)
|
|
if apiutil.AbortBadRequestOnError(c, err) {
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response.OK(log))
|
|
}
|
|
|
|
// GetWebSocket handles GET /tunnel/ws.
|
|
func GetWebSocket(c *gin.Context) {
|
|
authNode, ok := c.Get(ctxFlaredNodeKey)
|
|
if !ok {
|
|
response.AbortUnauthorized(c, errTunnelTokenInvalid)
|
|
return
|
|
}
|
|
node := authNode.(*model.OpenFlareNode)
|
|
ofws.ServeFlared(c, node.NodeID)
|
|
} |