Skip to content

Commit 1510f82

Browse files
committed
fix: handle accounts
1 parent 0a38b4a commit 1510f82

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

globals/accounts.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package globals
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/PretendoNetwork/nex-go/v2"
7+
"github.com/PretendoNetwork/nex-go/v2/types"
8+
)
9+
10+
var AuthenticationServerAccount *nex.Account
11+
var SecureServerAccount *nex.Account
12+
13+
func InitAccounts() {
14+
AuthenticationServerAccount = nex.NewAccount(types.NewPID(1), "Quazal Authentication", KerberosPassword)
15+
SecureServerAccount = nex.NewAccount(types.NewPID(2), "Quazal Rendez-Vous", KerberosPassword)
16+
}
17+
18+
func AccountDetailsByPID(pid *types.PID) (*nex.Account, *nex.Error) {
19+
if pid.Equals(AuthenticationServerAccount.PID) {
20+
return AuthenticationServerAccount, nil
21+
}
22+
23+
if pid.Equals(SecureServerAccount.PID) {
24+
return SecureServerAccount, nil
25+
}
26+
27+
password, errorCode := PasswordFromPID(pid)
28+
if errorCode != 0 {
29+
return nil, nex.NewError(errorCode, "Failed to get password from PID")
30+
}
31+
32+
account := nex.NewAccount(pid, strconv.Itoa(int(pid.LegacyValue())), password)
33+
34+
return account, nil
35+
}
36+
37+
func AccountDetailsByUsername(username string) (*nex.Account, *nex.Error) {
38+
if username == AuthenticationServerAccount.Username {
39+
return AuthenticationServerAccount, nil
40+
}
41+
42+
if username == SecureServerAccount.Username {
43+
return SecureServerAccount, nil
44+
}
45+
46+
pidInt, err := strconv.Atoi(username)
47+
if err != nil {
48+
Logger.Error(err.Error())
49+
return nil, nex.NewError(nex.ResultCodes.RendezVous.InvalidUsername, "Invalid username")
50+
}
51+
52+
pid := types.NewPID(uint64(pidInt))
53+
54+
password, errorCode := PasswordFromPID(pid)
55+
if errorCode != 0 {
56+
Logger.Errorf("Password err: %v", errorCode)
57+
return nil, nex.NewError(errorCode, "Failed to get password from PID")
58+
}
59+
60+
account := nex.NewAccount(pid, username, password)
61+
62+
return account, nil
63+
}

globals/globals.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
var Logger = plogger.NewLogger()
12+
var KerberosPassword = "password" // * Default password
1213
var SecureServer *nex.PRUDPServer
1314
var SecureEndpoint *nex.PRUDPEndPoint
1415
var GRPCFriendsClientConnection *grpc.ClientConn

globals/password_from_pid.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package globals
2+
3+
import (
4+
"context"
5+
6+
"github.com/PretendoNetwork/nex-go/v2/types"
7+
8+
pb "github.com/PretendoNetwork/grpc-go/account"
9+
"github.com/PretendoNetwork/nex-go/v2"
10+
"google.golang.org/grpc/metadata"
11+
)
12+
13+
func PasswordFromPID(pid *types.PID) (string, uint32) {
14+
ctx := metadata.NewOutgoingContext(context.Background(), GRPCAccountCommonMetadata)
15+
16+
response, err := GRPCAccountClient.GetNEXPassword(ctx, &pb.GetNEXPasswordRequest{Pid: pid.LegacyValue()})
17+
if err != nil {
18+
Logger.Error(err.Error())
19+
return "", nex.ResultCodes.RendezVous.InvalidUsername
20+
}
21+
22+
return response.Password, 0
23+
}

init.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ func init() {
2424
logger.Warning("Error loading .env file")
2525
}
2626

27+
kerberosPassword := os.Getenv("PN_WIIU_CHAT_KERBEROS_PASSWORD")
2728
friendsGRPCHost := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_HOST")
2829
friendsGRPCPort := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_PORT")
2930
friendsGRPCAPIKey := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY")
3031

32+
if strings.TrimSpace(kerberosPassword) == "" {
33+
globals.Logger.Warningf("PN_KERBEROS_PASSWORD environment variable not set. Using default password: %q", globals.KerberosPassword)
34+
} else {
35+
globals.KerberosPassword = kerberosPassword
36+
}
37+
3138
if strings.TrimSpace(friendsGRPCHost) == "" {
3239
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_HOST environment variable not set")
3340
os.Exit(0)
@@ -61,5 +68,6 @@ func init() {
6168
"X-API-Key", friendsGRPCAPIKey,
6269
)
6370

71+
globals.InitAccounts()
6472
database.ConnectAll()
6573
}

nex/server.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ func StartNEXServer() {
1313

1414
globals.SecureEndpoint = nex.NewPRUDPEndPoint(1)
1515
globals.SecureEndpoint.IsSecureEndPoint = true
16-
globals.SecureEndpoint.SetAccessKey("e7a47214")
17-
globals.SecureServer.LibraryVersions.SetDefault(nex.NewLibraryVersion(3, 4, 2))
16+
globals.SecureEndpoint.AccountDetailsByPID = globals.AccountDetailsByPID
17+
globals.SecureEndpoint.AccountDetailsByUsername = globals.AccountDetailsByUsername
18+
globals.SecureEndpoint.ServerAccount = globals.SecureServerAccount
1819
globals.SecureServer.BindPRUDPEndPoint(globals.SecureEndpoint)
1920
// globals.SecureServer.SetPingTimeout(65535) // TODO: what to replace with
2021

22+
globals.SecureServer.LibraryVersions.SetDefault(nex.NewLibraryVersion(3, 4, 2))
23+
globals.SecureEndpoint.SetAccessKey("e7a47214")
24+
2125
globals.SecureEndpoint.OnData(func(packet nex.PacketInterface) {
2226
request := packet.RMCMessage()
2327

0 commit comments

Comments
 (0)