2024-08-14 01:52:35 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package wshutil
|
|
|
|
|
|
|
|
import (
|
2024-09-05 09:21:08 +02:00
|
|
|
"context"
|
2024-08-14 01:52:35 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-17 01:49:49 +02:00
|
|
|
"log"
|
2024-08-14 01:52:35 +02:00
|
|
|
"sync"
|
2024-09-05 09:21:08 +02:00
|
|
|
"time"
|
2024-08-14 01:52:35 +02:00
|
|
|
|
2024-10-24 07:43:17 +02:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
2024-08-14 01:52:35 +02:00
|
|
|
)
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
const DefaultRoute = "wavesrv"
|
2024-10-24 07:43:17 +02:00
|
|
|
const UpstreamRoute = "upstream"
|
2024-08-17 01:49:49 +02:00
|
|
|
const SysRoute = "sys" // this route doesn't exist, just a placeholder for system messages
|
2024-09-18 08:10:09 +02:00
|
|
|
const ElectronRoute = "electron"
|
2024-08-17 01:49:49 +02:00
|
|
|
|
|
|
|
// this works like a network switch
|
|
|
|
|
2024-08-24 03:12:40 +02:00
|
|
|
// TODO maybe move the wps integration here instead of in wshserver
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
type routeInfo struct {
|
|
|
|
RpcId string
|
|
|
|
SourceRouteId string
|
|
|
|
DestRouteId string
|
|
|
|
}
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
type msgAndRoute struct {
|
|
|
|
msgBytes []byte
|
|
|
|
fromRouteId string
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
type WshRouter struct {
|
2024-10-24 07:43:17 +02:00
|
|
|
Lock *sync.Mutex
|
|
|
|
RouteMap map[string]AbstractRpcClient // routeid => client
|
|
|
|
UpstreamClient AbstractRpcClient // upstream client (if we are not the terminal router)
|
|
|
|
AnnouncedRoutes map[string]string // routeid => local routeid
|
|
|
|
RpcMap map[string]*routeInfo // rpcid => routeinfo
|
|
|
|
SimpleRequestMap map[string]chan *RpcMessage // simple reqid => response channel
|
|
|
|
InputCh chan msgAndRoute
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 03:45:45 +02:00
|
|
|
func MakeConnectionRouteId(connId string) string {
|
|
|
|
return "conn:" + connId
|
|
|
|
}
|
|
|
|
|
2024-08-19 06:26:44 +02:00
|
|
|
func MakeControllerRouteId(blockId string) string {
|
|
|
|
return "controller:" + blockId
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func MakeProcRouteId(procId string) string {
|
|
|
|
return "proc:" + procId
|
|
|
|
}
|
|
|
|
|
2024-10-17 23:34:02 +02:00
|
|
|
func MakeTabRouteId(tabId string) string {
|
|
|
|
return "tab:" + tabId
|
|
|
|
}
|
|
|
|
|
2024-10-17 23:50:36 +02:00
|
|
|
func MakeFeBlockRouteId(blockId string) string {
|
|
|
|
return "feblock:" + blockId
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
var DefaultRouter = NewWshRouter()
|
|
|
|
|
|
|
|
func NewWshRouter() *WshRouter {
|
|
|
|
rtn := &WshRouter{
|
2024-10-24 07:43:17 +02:00
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
RouteMap: make(map[string]AbstractRpcClient),
|
|
|
|
AnnouncedRoutes: make(map[string]string),
|
|
|
|
RpcMap: make(map[string]*routeInfo),
|
|
|
|
SimpleRequestMap: make(map[string]chan *RpcMessage),
|
|
|
|
InputCh: make(chan msgAndRoute, DefaultInputChSize),
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
go rtn.runServer()
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func noRouteErr(routeId string) error {
|
|
|
|
if routeId == "" {
|
|
|
|
return errors.New("no default route")
|
|
|
|
}
|
|
|
|
return fmt.Errorf("no route for %q", routeId)
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:03:55 +02:00
|
|
|
func (router *WshRouter) SendEvent(routeId string, event wps.WaveEvent) {
|
2024-08-14 03:19:29 +02:00
|
|
|
rpc := router.GetRpc(routeId)
|
|
|
|
if rpc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg := RpcMessage{
|
2024-08-20 23:56:48 +02:00
|
|
|
Command: wshrpc.Command_EventRecv,
|
2024-08-17 01:49:49 +02:00
|
|
|
Route: routeId,
|
2024-08-14 03:19:29 +02:00
|
|
|
Data: event,
|
|
|
|
}
|
|
|
|
msgBytes, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
// nothing to do
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rpc.SendRpcMessage(msgBytes)
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
func (router *WshRouter) handleNoRoute(msg RpcMessage) {
|
|
|
|
nrErr := noRouteErr(msg.Route)
|
|
|
|
if msg.ReqId == "" {
|
|
|
|
if msg.Command == wshrpc.Command_Message {
|
|
|
|
// to prevent infinite loops
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// no response needed, but send message back to source
|
|
|
|
respMsg := RpcMessage{Command: wshrpc.Command_Message, Route: msg.Source, Data: wshrpc.CommandMessageData{Message: nrErr.Error()}}
|
|
|
|
respBytes, _ := json.Marshal(respMsg)
|
2024-08-17 01:49:49 +02:00
|
|
|
router.InputCh <- msgAndRoute{msgBytes: respBytes, fromRouteId: SysRoute}
|
2024-08-14 01:52:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// send error response
|
|
|
|
response := RpcMessage{
|
|
|
|
ResId: msg.ReqId,
|
|
|
|
Error: nrErr.Error(),
|
|
|
|
}
|
|
|
|
respBytes, _ := json.Marshal(response)
|
2024-09-04 06:08:51 +02:00
|
|
|
router.sendRoutedMessage(respBytes, msg.Source)
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) registerRouteInfo(rpcId string, sourceRouteId string, destRouteId string) {
|
2024-08-17 01:49:49 +02:00
|
|
|
if rpcId == "" {
|
|
|
|
return
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
router.RpcMap[rpcId] = &routeInfo{RpcId: rpcId, SourceRouteId: sourceRouteId, DestRouteId: destRouteId}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) unregisterRouteInfo(rpcId string) {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
delete(router.RpcMap, rpcId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) getRouteInfo(rpcId string) *routeInfo {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
return router.RpcMap[rpcId]
|
|
|
|
}
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
func (router *WshRouter) handleAnnounceMessage(msg RpcMessage, input msgAndRoute) {
|
|
|
|
// if we have an upstream, send it there
|
|
|
|
// if we don't (we are the terminal router), then add it to our announced route map
|
|
|
|
upstream := router.GetUpstreamClient()
|
|
|
|
if upstream != nil {
|
|
|
|
upstream.SendRpcMessage(input.msgBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if msg.Source == input.fromRouteId {
|
|
|
|
// not necessary to save the id mapping
|
|
|
|
return
|
|
|
|
}
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
router.AnnouncedRoutes[msg.Source] = input.fromRouteId
|
|
|
|
}
|
|
|
|
|
2024-09-16 20:59:39 +02:00
|
|
|
func (router *WshRouter) handleUnannounceMessage(msg RpcMessage) {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
delete(router.AnnouncedRoutes, msg.Source)
|
|
|
|
}
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
func (router *WshRouter) getAnnouncedRoute(routeId string) string {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
return router.AnnouncedRoutes[routeId]
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if message was sent, false if failed
|
|
|
|
func (router *WshRouter) sendRoutedMessage(msgBytes []byte, routeId string) bool {
|
|
|
|
rpc := router.GetRpc(routeId)
|
|
|
|
if rpc != nil {
|
|
|
|
rpc.SendRpcMessage(msgBytes)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
upstream := router.GetUpstreamClient()
|
|
|
|
if upstream != nil {
|
|
|
|
upstream.SendRpcMessage(msgBytes)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
// we are the upstream, so consult our announced routes map
|
|
|
|
localRouteId := router.getAnnouncedRoute(routeId)
|
|
|
|
rpc := router.GetRpc(localRouteId)
|
|
|
|
if rpc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rpc.SendRpcMessage(msgBytes)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
func (router *WshRouter) runServer() {
|
2024-08-17 01:49:49 +02:00
|
|
|
for input := range router.InputCh {
|
|
|
|
msgBytes := input.msgBytes
|
2024-08-14 01:52:35 +02:00
|
|
|
var msg RpcMessage
|
|
|
|
err := json.Unmarshal(msgBytes, &msg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error unmarshalling message: ", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-17 01:49:49 +02:00
|
|
|
routeId := msg.Route
|
2024-09-16 20:59:39 +02:00
|
|
|
if msg.Command == wshrpc.Command_RouteAnnounce {
|
2024-08-17 01:49:49 +02:00
|
|
|
router.handleAnnounceMessage(msg, input)
|
|
|
|
continue
|
|
|
|
}
|
2024-09-16 20:59:39 +02:00
|
|
|
if msg.Command == wshrpc.Command_RouteUnannounce {
|
|
|
|
router.handleUnannounceMessage(msg)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
if msg.Command != "" {
|
|
|
|
// new comand, setup new rpc
|
2024-08-17 01:49:49 +02:00
|
|
|
ok := router.sendRoutedMessage(msgBytes, routeId)
|
|
|
|
if !ok {
|
2024-08-14 01:52:35 +02:00
|
|
|
router.handleNoRoute(msg)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-17 01:49:49 +02:00
|
|
|
router.registerRouteInfo(msg.ReqId, msg.Source, routeId)
|
2024-08-14 01:52:35 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// look at reqid or resid to route correctly
|
|
|
|
if msg.ReqId != "" {
|
|
|
|
routeInfo := router.getRouteInfo(msg.ReqId)
|
|
|
|
if routeInfo == nil {
|
|
|
|
// no route info, nothing to do
|
|
|
|
continue
|
|
|
|
}
|
2024-08-17 01:49:49 +02:00
|
|
|
// no need to check the return value here (noop if failed)
|
|
|
|
router.sendRoutedMessage(msgBytes, routeInfo.DestRouteId)
|
2024-08-14 01:52:35 +02:00
|
|
|
continue
|
|
|
|
} else if msg.ResId != "" {
|
2024-10-24 07:43:17 +02:00
|
|
|
ok := router.trySimpleResponse(&msg)
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
routeInfo := router.getRouteInfo(msg.ResId)
|
|
|
|
if routeInfo == nil {
|
|
|
|
// no route info, nothing to do
|
|
|
|
continue
|
|
|
|
}
|
2024-08-17 01:49:49 +02:00
|
|
|
router.sendRoutedMessage(msgBytes, routeInfo.SourceRouteId)
|
2024-08-14 01:52:35 +02:00
|
|
|
if !msg.Cont {
|
|
|
|
router.unregisterRouteInfo(msg.ResId)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// this is a bad message (no command, reqid, or resid)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 09:21:08 +02:00
|
|
|
func (router *WshRouter) WaitForRegister(ctx context.Context, routeId string) error {
|
|
|
|
for {
|
|
|
|
if router.GetRpc(routeId) != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-time.After(30 * time.Millisecond):
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
// this will also consume the output channel of the abstract client
|
2024-10-24 07:43:17 +02:00
|
|
|
func (router *WshRouter) RegisterRoute(routeId string, rpc AbstractRpcClient, shouldAnnounce bool) {
|
|
|
|
if routeId == SysRoute || routeId == UpstreamRoute {
|
2024-08-17 01:49:49 +02:00
|
|
|
// cannot register sys route
|
2024-10-24 07:43:17 +02:00
|
|
|
log.Printf("error: WshRouter cannot register %s route\n", routeId)
|
2024-08-17 01:49:49 +02:00
|
|
|
return
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
log.Printf("[router] registering wsh route %q\n", routeId)
|
2024-08-14 01:52:35 +02:00
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
2024-10-18 21:05:20 +02:00
|
|
|
alreadyExists := router.RouteMap[routeId] != nil
|
|
|
|
if alreadyExists {
|
|
|
|
log.Printf("[router] warning: route %q already exists (replacing)\n", routeId)
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
router.RouteMap[routeId] = rpc
|
|
|
|
go func() {
|
2024-08-17 01:49:49 +02:00
|
|
|
// announce
|
2024-10-24 07:43:17 +02:00
|
|
|
if shouldAnnounce && !alreadyExists && router.GetUpstreamClient() != nil {
|
2024-09-16 20:59:39 +02:00
|
|
|
announceMsg := RpcMessage{Command: wshrpc.Command_RouteAnnounce, Source: routeId}
|
2024-08-17 01:49:49 +02:00
|
|
|
announceBytes, _ := json.Marshal(announceMsg)
|
|
|
|
router.GetUpstreamClient().SendRpcMessage(announceBytes)
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
for {
|
|
|
|
msgBytes, ok := rpc.RecvRpcMessage()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var rpcMsg RpcMessage
|
|
|
|
err := json.Unmarshal(msgBytes, &rpcMsg)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rpcMsg.Command != "" {
|
2024-08-17 01:49:49 +02:00
|
|
|
if rpcMsg.Source == "" {
|
|
|
|
rpcMsg.Source = routeId
|
|
|
|
}
|
|
|
|
if rpcMsg.Route == "" {
|
|
|
|
rpcMsg.Route = DefaultRoute
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
msgBytes, err = json.Marshal(rpcMsg)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 01:49:49 +02:00
|
|
|
router.InputCh <- msgAndRoute{msgBytes: msgBytes, fromRouteId: routeId}
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) UnregisterRoute(routeId string) {
|
2024-08-20 23:56:48 +02:00
|
|
|
log.Printf("[router] unregistering wsh route %q\n", routeId)
|
2024-08-14 01:52:35 +02:00
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
delete(router.RouteMap, routeId)
|
2024-09-16 20:59:39 +02:00
|
|
|
// clear out announced routes
|
|
|
|
for routeId, localRouteId := range router.AnnouncedRoutes {
|
|
|
|
if localRouteId == routeId {
|
|
|
|
delete(router.AnnouncedRoutes, routeId)
|
|
|
|
}
|
|
|
|
}
|
2024-08-24 03:12:40 +02:00
|
|
|
go func() {
|
|
|
|
wps.Broker.UnsubscribeAll(routeId)
|
2024-10-17 23:50:36 +02:00
|
|
|
wps.Broker.Publish(wps.WaveEvent{Event: wps.Event_RouteGone, Scopes: []string{routeId}})
|
2024-08-24 03:12:40 +02:00
|
|
|
}()
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
// this may return nil (returns default only for empty routeId)
|
|
|
|
func (router *WshRouter) GetRpc(routeId string) AbstractRpcClient {
|
2024-08-14 01:52:35 +02:00
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
2024-08-17 01:49:49 +02:00
|
|
|
return router.RouteMap[routeId]
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 01:49:49 +02:00
|
|
|
func (router *WshRouter) SetUpstreamClient(rpc AbstractRpcClient) {
|
2024-08-14 01:52:35 +02:00
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
2024-08-17 01:49:49 +02:00
|
|
|
router.UpstreamClient = rpc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) GetUpstreamClient() AbstractRpcClient {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
return router.UpstreamClient
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
2024-10-24 07:43:17 +02:00
|
|
|
|
|
|
|
func (router *WshRouter) InjectMessage(msgBytes []byte, fromRouteId string) {
|
|
|
|
router.InputCh <- msgAndRoute{msgBytes: msgBytes, fromRouteId: fromRouteId}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) registerSimpleRequest(reqId string) chan *RpcMessage {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
rtn := make(chan *RpcMessage, 1)
|
|
|
|
router.SimpleRequestMap[reqId] = rtn
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) trySimpleResponse(msg *RpcMessage) bool {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
respCh := router.SimpleRequestMap[msg.ResId]
|
|
|
|
if respCh == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
respCh <- msg
|
|
|
|
delete(router.SimpleRequestMap, msg.ResId)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) clearSimpleRequest(reqId string) {
|
|
|
|
router.Lock.Lock()
|
|
|
|
defer router.Lock.Unlock()
|
|
|
|
delete(router.SimpleRequestMap, reqId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) RunSimpleRawCommand(ctx context.Context, msg RpcMessage, fromRouteId string) (*RpcMessage, error) {
|
|
|
|
if msg.Command == "" {
|
|
|
|
return nil, errors.New("no command")
|
|
|
|
}
|
|
|
|
msgBytes, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var respCh chan *RpcMessage
|
|
|
|
if msg.ReqId != "" {
|
|
|
|
respCh = router.registerSimpleRequest(msg.ReqId)
|
|
|
|
}
|
|
|
|
router.InjectMessage(msgBytes, fromRouteId)
|
|
|
|
if respCh == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
router.clearSimpleRequest(msg.ReqId)
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case resp := <-respCh:
|
|
|
|
if resp.Error != "" {
|
|
|
|
return nil, errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (router *WshRouter) HandleProxyAuth(jwtTokenAny any) (*wshrpc.CommandAuthenticateRtnData, error) {
|
|
|
|
if jwtTokenAny == nil {
|
|
|
|
return nil, errors.New("no jwt token")
|
|
|
|
}
|
|
|
|
jwtToken, ok := jwtTokenAny.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("jwt token not a string")
|
|
|
|
}
|
|
|
|
if jwtToken == "" {
|
|
|
|
return nil, errors.New("empty jwt token")
|
|
|
|
}
|
|
|
|
msg := RpcMessage{
|
|
|
|
Command: wshrpc.Command_Authenticate,
|
|
|
|
ReqId: uuid.New().String(),
|
|
|
|
Data: jwtToken,
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeoutMs*time.Millisecond)
|
|
|
|
defer cancelFn()
|
|
|
|
resp, err := router.RunSimpleRawCommand(ctx, msg, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
return nil, errors.New("no data in authenticate response")
|
|
|
|
}
|
|
|
|
var respData wshrpc.CommandAuthenticateRtnData
|
|
|
|
err = utilfn.ReUnmarshal(&respData, resp.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error unmarshalling authenticate response: %v", err)
|
|
|
|
}
|
|
|
|
if respData.AuthToken == "" {
|
|
|
|
return nil, errors.New("no auth token in authenticate response")
|
|
|
|
}
|
|
|
|
return &respData, nil
|
|
|
|
}
|