mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40:12 +00:00
feat(agentcompat): expose dashboard capability routes
Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
//go:build agentcompat
|
||||
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/nezhahq/nezha/model"
|
||||
"github.com/nezhahq/nezha/pkg/agentcompatcontract"
|
||||
serviceRPC "github.com/nezhahq/nezha/service/rpc"
|
||||
)
|
||||
|
||||
type natCapabilityLease struct {
|
||||
active bool
|
||||
publicationOwned bool
|
||||
streamLease *serviceRPC.AgentCompatNATStreamLease
|
||||
access serviceRPC.AgentCompatCapabilityAccess
|
||||
handle serviceRPC.AgentCompatNATPublishHandle
|
||||
}
|
||||
|
||||
func prepareNATCapability(request *http.Request, natConfig *model.NAT) (natCapabilityLease, error) {
|
||||
values := request.Header.Values(agentcompatcontract.IOStreamCapabilityHeader)
|
||||
if len(values) == 0 {
|
||||
request.Header.Del("Authorization")
|
||||
return natCapabilityLease{}, nil
|
||||
}
|
||||
request.Header.Del(agentcompatcontract.IOStreamCapabilityHeader)
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
request.Header.Del("Authorization")
|
||||
return natCapabilityLease{}, errors.New("invalid NAT capability")
|
||||
}
|
||||
access, handle, err := serviceRPC.NezhaHandlerSingleton.ConsumeAgentCompatNATCapabilityForProfile(values[0], natConfig.ServerID, natConfig.ID)
|
||||
request.Header.Del("Authorization")
|
||||
if err != nil {
|
||||
return natCapabilityLease{}, errors.New("invalid NAT capability")
|
||||
}
|
||||
return natCapabilityLease{active: true, access: access, handle: handle}, nil
|
||||
}
|
||||
|
||||
func (lease natCapabilityLease) cleanup(handler *serviceRPC.NezhaHandler) {
|
||||
if !lease.active {
|
||||
return
|
||||
}
|
||||
_ = handler.CancelAgentCompatIOStreamCapability(lease.access)
|
||||
_ = handler.CloseAgentCompatNATStreamLease(lease.streamLease)
|
||||
_ = handler.UnregisterAgentCompatIOStreamCapability(lease.access)
|
||||
}
|
||||
Reference in New Issue
Block a user