mirror of
https://github.com/PretendoNetwork/wiiu-chat-secure.git
synced 2026-09-09 11:45:23 -05:00
Update gRPC to not repeatedly make new connections
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
pb "github.com/PretendoNetwork/grpc-go/friends"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
"github.com/PretendoNetwork/plogger-go"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
var Logger = plogger.NewLogger()
|
||||
var NEXServer *nex.Server
|
||||
var GRPCFriendsClientConnection *grpc.ClientConn
|
||||
var GRPCFriendsClient pb.FriendsClient
|
||||
var GRPCFriendsCommonMetadata metadata.MD
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
pb "github.com/PretendoNetwork/grpc-go/friends"
|
||||
nex "github.com/PretendoNetwork/nex-go"
|
||||
@@ -13,26 +12,11 @@ import (
|
||||
nintendo_notifications "github.com/PretendoNetwork/nex-protocols-go/nintendo-notifications"
|
||||
nintendo_notifications_types "github.com/PretendoNetwork/nex-protocols-go/nintendo-notifications/types"
|
||||
"github.com/PretendoNetwork/wiiu-chat-secure/globals"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func SendIncomingCallNotification(caller uint32, target uint32) {
|
||||
// Connect to Friends gRPC.
|
||||
conn, err := grpc.Dial(os.Getenv("FRIENDS_GRPC_ADDRESS"), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
log.Fatalf("Connection to Friends gRPC failed! : %v", err)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
c := pb.NewFriendsClient(conn)
|
||||
|
||||
md := metadata.Pairs(
|
||||
"X-API-Key", os.Getenv("FRIENDS_GRPC_API_KEY"),
|
||||
)
|
||||
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), globals.GRPCFriendsCommonMetadata)
|
||||
|
||||
presence := friends_wiiu_types.NewNintendoPresenceV2()
|
||||
|
||||
@@ -63,7 +47,7 @@ func SendIncomingCallNotification(caller uint32, target uint32) {
|
||||
stream := nex.NewStreamOut(globals.NEXServer)
|
||||
eventObjectBytes := eventObject.Bytes(stream)
|
||||
|
||||
_, err = c.SendUserNotificationWiiU(ctx, &pb.SendUserNotificationWiiURequest{Pid: target, NotificationData: eventObjectBytes})
|
||||
_, err := globals.GRPCFriendsClient.SendUserNotificationWiiU(ctx, &pb.SendUserNotificationWiiURequest{Pid: target, NotificationData: eventObjectBytes})
|
||||
if err != nil {
|
||||
log.Fatalf("Greeting Friends gRPC failed! : %v", err)
|
||||
}
|
||||
|
||||
47
init.go
47
init.go
@@ -1,9 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
pb "github.com/PretendoNetwork/grpc-go/friends"
|
||||
"github.com/PretendoNetwork/plogger-go"
|
||||
"github.com/PretendoNetwork/wiiu-chat-secure/database"
|
||||
"github.com/PretendoNetwork/wiiu-chat-secure/globals"
|
||||
"github.com/joho/godotenv"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
var logger = plogger.NewLogger()
|
||||
@@ -14,5 +24,42 @@ func init() {
|
||||
logger.Warning("Error loading .env file")
|
||||
}
|
||||
|
||||
friendsGRPCHost := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_HOST")
|
||||
friendsGRPCPort := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_PORT")
|
||||
friendsGRPCAPIKey := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY")
|
||||
|
||||
if strings.TrimSpace(friendsGRPCHost) == "" {
|
||||
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_HOST environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(friendsGRPCPort) == "" {
|
||||
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_PORT environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(friendsGRPCPort); err != nil {
|
||||
globals.Logger.Errorf("PN_WIIU_CHAT_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
|
||||
os.Exit(0)
|
||||
} else if port < 0 || port > 65535 {
|
||||
globals.Logger.Errorf("PN_WIIU_CHAT_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(friendsGRPCAPIKey) == "" {
|
||||
globals.Logger.Warning("Insecure gRPC server detected. PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY environment variable not set")
|
||||
}
|
||||
|
||||
globals.GRPCFriendsClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", friendsGRPCHost, friendsGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
globals.Logger.Criticalf("Failed to connect to friends gRPC server: %v", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
globals.GRPCFriendsClient = pb.NewFriendsClient(globals.GRPCFriendsClientConnection)
|
||||
globals.GRPCFriendsCommonMetadata = metadata.Pairs(
|
||||
"X-API-Key", friendsGRPCAPIKey,
|
||||
)
|
||||
|
||||
database.ConnectAll()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user