2024-07-20 19:58:54 +08:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-03-01 15:22:51 +08:00
|
|
|
"fmt"
|
|
|
|
|
2024-10-24 00:13:16 +08:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
2025-03-01 15:22:51 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
|
2024-10-31 20:38:50 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2025-03-01 15:22:51 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/web/exts"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-12-03 21:24:16 +08:00
|
|
|
"github.com/rs/zerolog/log"
|
2025-03-01 15:22:51 +08:00
|
|
|
"github.com/samber/lo"
|
2024-07-20 19:58:54 +08:00
|
|
|
)
|
|
|
|
|
2024-10-27 12:50:07 +08:00
|
|
|
func (v *App) BroadcastEvent(ctx context.Context, request *proto.EventInfo) (*proto.EventResponse, error) {
|
2024-12-03 21:49:41 +08:00
|
|
|
log.Debug().Str("event", request.GetEvent()).
|
|
|
|
Msg("Got a broadcasting event...")
|
2025-03-01 15:22:51 +08:00
|
|
|
|
2024-07-20 19:58:54 +08:00
|
|
|
switch request.GetEvent() {
|
2025-03-01 15:22:51 +08:00
|
|
|
// Last seen at
|
2024-10-24 00:13:16 +08:00
|
|
|
case "ws.client.register":
|
2024-07-20 19:58:54 +08:00
|
|
|
// No longer need update user online status
|
|
|
|
// Based on realtime sever connection status
|
|
|
|
break
|
2024-10-24 00:13:16 +08:00
|
|
|
case "ws.client.unregister":
|
2024-07-20 19:58:54 +08:00
|
|
|
// Update user last seen at
|
2024-10-24 00:13:16 +08:00
|
|
|
data := nex.DecodeMap(request.GetData())
|
2024-12-03 21:24:16 +08:00
|
|
|
err := services.SetAccountLastSeen(uint(data["user"].(float64)))
|
|
|
|
log.Debug().Err(err).Any("event", data).Msg("Setting account last seen...")
|
2024-07-20 19:58:54 +08:00
|
|
|
}
|
|
|
|
|
2024-10-24 00:13:16 +08:00
|
|
|
return &proto.EventResponse{}, nil
|
2024-07-20 19:58:54 +08:00
|
|
|
}
|
2025-03-01 15:22:51 +08:00
|
|
|
|
|
|
|
func (v *App) PushStream(_ context.Context, request *proto.PushStreamRequest) (*proto.PushStreamResponse, error) {
|
|
|
|
sc := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
|
|
|
|
|
|
|
|
var in nex.WebSocketPackage
|
|
|
|
if err := jsoniter.Unmarshal(request.GetBody(), &in); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch in.Action {
|
|
|
|
// PaKex (Key Exchange)
|
|
|
|
case "kex.ask":
|
|
|
|
var data struct {
|
|
|
|
UserID uint `json:"user_id" validate:"required"`
|
|
|
|
KeypairID string `json:"keypair_id" validate:"required"`
|
2025-03-04 00:38:16 +08:00
|
|
|
ClientID string `json:"client_id" validate:"required"`
|
2025-03-01 15:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err := jsoniter.Unmarshal(in.RawPayload(), &data)
|
|
|
|
if request.ClientId != nil {
|
|
|
|
data.ClientID = *request.ClientId
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = exts.ValidateStruct(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_, _ = sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
|
|
|
ClientId: request.ClientId,
|
|
|
|
Body: nex.WebSocketPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable parse payload: %v", err),
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward ask request
|
|
|
|
sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
|
|
|
UserId: lo.ToPtr(uint64(data.UserID)),
|
2025-03-04 00:50:31 +08:00
|
|
|
Body: nex.WebSocketPackage{
|
|
|
|
Action: "kex.ask",
|
|
|
|
Payload: data,
|
|
|
|
}.Marshal(),
|
2025-03-01 15:22:51 +08:00
|
|
|
})
|
|
|
|
case "kex.ack":
|
|
|
|
var data struct {
|
|
|
|
UserID uint `json:"user_id" validate:"required"`
|
|
|
|
KeypairID string `json:"keypair_id" validate:"required"`
|
|
|
|
PublicKey string `json:"public_key"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
2025-03-04 00:38:16 +08:00
|
|
|
ClientID string `json:"client_id" validate:"required"`
|
2025-03-01 15:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err := jsoniter.Unmarshal(in.RawPayload(), &data)
|
|
|
|
if request.ClientId != nil {
|
|
|
|
data.ClientID = *request.ClientId
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = exts.ValidateStruct(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_, _ = sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
|
|
|
ClientId: request.ClientId,
|
|
|
|
Body: nex.WebSocketPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable parse payload: %v", err),
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(data.PublicKey) == 0 && len(data.PrivateKey) == 0 {
|
|
|
|
_, _ = sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
|
|
|
ClientId: request.ClientId,
|
|
|
|
Body: nex.WebSocketPackage{
|
|
|
|
Action: "error",
|
|
|
|
Message: "one of public key and private key is required",
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward ack request
|
|
|
|
sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
|
|
|
ClientId: request.ClientId,
|
2025-03-04 00:50:31 +08:00
|
|
|
Body: nex.WebSocketPackage{
|
|
|
|
Action: "kex.ack",
|
|
|
|
Payload: data,
|
|
|
|
}.Marshal(),
|
2025-03-01 15:22:51 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.PushStreamResponse{}, nil
|
|
|
|
}
|