mirror of
https://github.com/PretendoNetwork/friends.git
synced 2026-09-13 13:55:18 -05:00
Merge authentication and secure servers
This commit is contained in:
@@ -2,11 +2,19 @@ package globals
|
||||
|
||||
import (
|
||||
"github.com/PretendoNetwork/friends-secure/types"
|
||||
pb "github.com/PretendoNetwork/grpc-go/account"
|
||||
"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 KerberosPassword = "password" // * Default password
|
||||
var AuthenticationServer *nex.Server
|
||||
var SecureServer *nex.Server
|
||||
var ConnectedUsers map[uint32]*types.ConnectedUser
|
||||
var AESKey []byte
|
||||
var GRPCAccountClientConnection *grpc.ClientConn
|
||||
var GRPCAccountClient pb.AccountClient
|
||||
var GRPCAccountCommonMetadata metadata.MD
|
||||
|
||||
22
globals/password_from_pid.go
Normal file
22
globals/password_from_pid.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "github.com/PretendoNetwork/grpc-go/account"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
"github.com/PretendoNetwork/nex-protocols-go/globals"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func PasswordFromPID(pid uint32) (string, uint32) {
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), GRPCAccountCommonMetadata)
|
||||
|
||||
response, err := GRPCAccountClient.GetNEXPassword(ctx, &pb.GetNEXPasswordRequest{Pid: pid})
|
||||
if err != nil {
|
||||
globals.Logger.Error(err.Error())
|
||||
return "", nex.Errors.RendezVous.InvalidUsername
|
||||
}
|
||||
|
||||
return response.Password, 0
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package grpc
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
pb "github.com/PretendoNetwork/grpc-go/friends"
|
||||
"google.golang.org/grpc"
|
||||
@@ -13,7 +14,7 @@ type gRPCFriendsServer struct {
|
||||
}
|
||||
|
||||
func StartGRPCServer() {
|
||||
listener, err := net.Listen("tcp", ":50051")
|
||||
listener, err := net.Listen("tcp", ":"+os.Getenv("PN_FRIENDS_GRPC_SERVER_PORT"))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (s *gRPCFriendsServer) SendUserNotificationWiiU(ctx context.Context, in *pb
|
||||
requestPacket.AddFlag(nex.FlagNeedsAck)
|
||||
requestPacket.AddFlag(nex.FlagReliable)
|
||||
|
||||
globals.NEXServer.Send(requestPacket)
|
||||
globals.SecureServer.Send(requestPacket)
|
||||
}
|
||||
|
||||
return &empty.Empty{}, nil
|
||||
|
||||
5
main.go
5
main.go
@@ -10,10 +10,11 @@ import (
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func main() {
|
||||
wg.Add(2)
|
||||
wg.Add(3)
|
||||
|
||||
go grpc.StartGRPCServer()
|
||||
go nex.StartNEXServer()
|
||||
go nex.StartAuthenticationServer()
|
||||
go nex.StartSecureServer()
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
38
nex/authentication.go
Normal file
38
nex/authentication.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package nex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
)
|
||||
|
||||
var serverBuildString string
|
||||
|
||||
func StartAuthenticationServer() {
|
||||
globals.AuthenticationServer = nex.NewServer()
|
||||
globals.AuthenticationServer.SetPRUDPVersion(0)
|
||||
globals.AuthenticationServer.SetPRUDPProtocolMinorVersion(0) // TODO: Figure out what to put here
|
||||
globals.AuthenticationServer.SetDefaultNEXVersion(&nex.NEXVersion{
|
||||
Major: 1,
|
||||
Minor: 0, // TODO: Figure out what to put here
|
||||
Patch: 0,
|
||||
})
|
||||
globals.AuthenticationServer.SetKerberosKeySize(16)
|
||||
globals.AuthenticationServer.SetKerberosPassword(globals.KerberosPassword)
|
||||
globals.AuthenticationServer.SetAccessKey("ridfebb9")
|
||||
|
||||
globals.AuthenticationServer.On("Data", func(packet *nex.PacketV1) {
|
||||
request := packet.RMCRequest()
|
||||
|
||||
fmt.Println("==Friends - Auth==")
|
||||
fmt.Printf("Protocol ID: %#v\n", request.ProtocolID())
|
||||
fmt.Printf("Method ID: %#v\n", request.MethodID())
|
||||
fmt.Println("===============")
|
||||
})
|
||||
|
||||
registerCommonAuthenticationServerProtocols()
|
||||
|
||||
globals.AuthenticationServer.Listen(fmt.Sprintf(":%s", os.Getenv("PN_FRIENDS_AUTHENTICATION_SERVER_PORT")))
|
||||
}
|
||||
@@ -10,17 +10,17 @@ func connect(packet *nex.PacketV0) {
|
||||
packet.Sender().SetClientConnectionSignature(packet.ConnectionSignature())
|
||||
|
||||
payload := packet.Payload()
|
||||
stream := nex.NewStreamIn(payload, globals.NEXServer)
|
||||
stream := nex.NewStreamIn(payload, globals.SecureServer)
|
||||
|
||||
ticketData, _ := stream.ReadBuffer()
|
||||
requestData, _ := stream.ReadBuffer()
|
||||
|
||||
serverKey := nex.DeriveKerberosKey(2, []byte(globals.NEXServer.KerberosPassword()))
|
||||
serverKey := nex.DeriveKerberosKey(2, []byte(globals.SecureServer.KerberosPassword()))
|
||||
|
||||
// TODO: use random key from auth server
|
||||
ticketDataEncryption, _ := nex.NewKerberosEncryption(serverKey)
|
||||
decryptedTicketData := ticketDataEncryption.Decrypt(ticketData)
|
||||
ticketDataStream := nex.NewStreamIn(decryptedTicketData, globals.NEXServer)
|
||||
ticketDataStream := nex.NewStreamIn(decryptedTicketData, globals.SecureServer)
|
||||
|
||||
_, _ = ticketDataStream.ReadUInt64LE() // expiration time
|
||||
_, _ = ticketDataStream.ReadUInt32LE() // User PID
|
||||
@@ -28,22 +28,22 @@ func connect(packet *nex.PacketV0) {
|
||||
|
||||
requestDataEncryption, _ := nex.NewKerberosEncryption(sessionKey)
|
||||
decryptedRequestData := requestDataEncryption.Decrypt(requestData)
|
||||
requestDataStream := nex.NewStreamIn(decryptedRequestData, globals.NEXServer)
|
||||
requestDataStream := nex.NewStreamIn(decryptedRequestData, globals.SecureServer)
|
||||
|
||||
userPID, _ := requestDataStream.ReadUInt32LE() // User PID
|
||||
|
||||
_, _ = requestDataStream.ReadUInt32LE() //CID of secure server station url
|
||||
responseCheck, _ := requestDataStream.ReadUInt32LE()
|
||||
|
||||
responseValueStream := nex.NewStreamOut(globals.NEXServer)
|
||||
responseValueStream := nex.NewStreamOut(globals.SecureServer)
|
||||
responseValueStream.WriteUInt32LE(responseCheck + 1)
|
||||
|
||||
responseValueBufferStream := nex.NewStreamOut(globals.NEXServer)
|
||||
responseValueBufferStream := nex.NewStreamOut(globals.SecureServer)
|
||||
responseValueBufferStream.WriteBuffer(responseValueStream.Bytes())
|
||||
|
||||
packet.Sender().UpdateRC4Key(sessionKey)
|
||||
|
||||
globals.NEXServer.AcknowledgePacket(packet, responseValueBufferStream.Bytes())
|
||||
globals.SecureServer.AcknowledgePacket(packet, responseValueBufferStream.Bytes())
|
||||
|
||||
packet.Sender().SetPID(userPID)
|
||||
|
||||
|
||||
28
nex/register_common_authentication_server_protocols.go
Normal file
28
nex/register_common_authentication_server_protocols.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package nex
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/PretendoNetwork/friends-secure/globals"
|
||||
nex "github.com/PretendoNetwork/nex-go"
|
||||
ticket_granting "github.com/PretendoNetwork/nex-protocols-common-go/ticket-granting"
|
||||
)
|
||||
|
||||
func registerCommonAuthenticationServerProtocols() {
|
||||
ticketGrantingProtocol := ticket_granting.NewCommonTicketGrantingProtocol(globals.AuthenticationServer)
|
||||
|
||||
secureStationURL := nex.NewStationURL("")
|
||||
secureStationURL.SetScheme("prudps")
|
||||
secureStationURL.SetAddress(os.Getenv("PN_FRIENDS_SECURE_SERVER_HOST"))
|
||||
secureStationURL.SetPort(os.Getenv("PN_FRIENDS_SECURE_SERVER_PORT"))
|
||||
secureStationURL.SetCID("1")
|
||||
secureStationURL.SetPID("2")
|
||||
secureStationURL.SetSID("1")
|
||||
secureStationURL.SetStream("10")
|
||||
secureStationURL.SetType("2")
|
||||
|
||||
ticketGrantingProtocol.SetSecureStationURL(secureStationURL)
|
||||
ticketGrantingProtocol.SetBuildName(serverBuildString)
|
||||
|
||||
globals.AuthenticationServer.SetPasswordFromPIDFunction(globals.PasswordFromPID)
|
||||
}
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
nex_friends_wiiu "github.com/PretendoNetwork/friends-secure/nex/friends-wiiu"
|
||||
nex_secure_connection "github.com/PretendoNetwork/friends-secure/nex/secure-connection"
|
||||
account_management "github.com/PretendoNetwork/nex-protocols-go/account-management"
|
||||
friends_3ds "github.com/PretendoNetwork/nex-protocols-go/friends/3ds"
|
||||
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends/wiiu"
|
||||
friends_3ds "github.com/PretendoNetwork/nex-protocols-go/friends-3ds"
|
||||
friends_wiiu "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu"
|
||||
secure_connection "github.com/PretendoNetwork/nex-protocols-go/secure-connection"
|
||||
)
|
||||
|
||||
func registerNEXProtocols() {
|
||||
secureConnectionProtocol := secure_connection.NewSecureConnectionProtocol(globals.NEXServer)
|
||||
accountManagementProtocol := account_management.NewAccountManagementProtocol(globals.NEXServer)
|
||||
friendsWiiUProtocol := friends_wiiu.NewFriendsWiiUProtocol(globals.NEXServer)
|
||||
friends3DSProtocol := friends_3ds.NewFriends3DSProtocol(globals.NEXServer)
|
||||
secureConnectionProtocol := secure_connection.NewSecureConnectionProtocol(globals.SecureServer)
|
||||
accountManagementProtocol := account_management.NewAccountManagementProtocol(globals.SecureServer)
|
||||
friendsWiiUProtocol := friends_wiiu.NewFriendsWiiUProtocol(globals.SecureServer)
|
||||
friends3DSProtocol := friends_3ds.NewFriends3DSProtocol(globals.SecureServer)
|
||||
|
||||
// Account Management protocol handles
|
||||
accountManagementProtocol.NintendoCreateAccount(nex_account_management.NintendoCreateAccount)
|
||||
|
||||
@@ -19,10 +19,10 @@ func Register(err error, client *nex.Client, callID uint32, stationUrls []*nex.S
|
||||
|
||||
localStationURL := localStation.EncodeToString()
|
||||
|
||||
rmcResponseStream := nex.NewStreamOut(globals.NEXServer)
|
||||
rmcResponseStream := nex.NewStreamOut(globals.SecureServer)
|
||||
|
||||
rmcResponseStream.WriteUInt32LE(0x10001) // Success
|
||||
rmcResponseStream.WriteUInt32LE(globals.NEXServer.ConnectionIDCounter().Increment())
|
||||
rmcResponseStream.WriteUInt32LE(globals.SecureServer.ConnectionIDCounter().Increment())
|
||||
rmcResponseStream.WriteString(localStationURL)
|
||||
|
||||
rmcResponseBody := rmcResponseStream.Bytes()
|
||||
@@ -44,5 +44,5 @@ func Register(err error, client *nex.Client, callID uint32, stationUrls []*nex.S
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
|
||||
globals.NEXServer.Send(responsePacket)
|
||||
globals.SecureServer.Send(responsePacket)
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ func RegisterEx(err error, client *nex.Client, callID uint32, stationUrls []*nex
|
||||
|
||||
localStationURL := localStation.EncodeToString()
|
||||
|
||||
rmcResponseStream := nex.NewStreamOut(globals.NEXServer)
|
||||
rmcResponseStream := nex.NewStreamOut(globals.SecureServer)
|
||||
|
||||
rmcResponseStream.WriteUInt32LE(0x10001) // Success
|
||||
rmcResponseStream.WriteUInt32LE(globals.NEXServer.ConnectionIDCounter().Increment())
|
||||
rmcResponseStream.WriteUInt32LE(globals.SecureServer.ConnectionIDCounter().Increment())
|
||||
rmcResponseStream.WriteString(localStationURL)
|
||||
|
||||
rmcResponseBody := rmcResponseStream.Bytes()
|
||||
@@ -65,5 +65,5 @@ func RegisterEx(err error, client *nex.Client, callID uint32, stationUrls []*nex
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
|
||||
globals.NEXServer.Send(responsePacket)
|
||||
globals.SecureServer.Send(responsePacket)
|
||||
}
|
||||
|
||||
@@ -15,16 +15,16 @@ import (
|
||||
_ "github.com/PretendoNetwork/nex-protocols-go"
|
||||
)
|
||||
|
||||
func StartNEXServer() {
|
||||
globals.NEXServer = nex.NewServer()
|
||||
globals.NEXServer.SetFragmentSize(900)
|
||||
globals.NEXServer.SetPRUDPVersion(0)
|
||||
globals.NEXServer.SetKerberosKeySize(16)
|
||||
globals.NEXServer.SetKerberosPassword(os.Getenv("PN_FRIENDS_CONFIG_KERBEROS_PASSWORD"))
|
||||
globals.NEXServer.SetPingTimeout(20) // Maybe too long?
|
||||
globals.NEXServer.SetAccessKey("ridfebb9")
|
||||
func StartSecureServer() {
|
||||
globals.SecureServer = nex.NewServer()
|
||||
globals.SecureServer.SetFragmentSize(900)
|
||||
globals.SecureServer.SetPRUDPVersion(0)
|
||||
globals.SecureServer.SetKerberosKeySize(16)
|
||||
globals.SecureServer.SetKerberosPassword(os.Getenv("PN_FRIENDS_CONFIG_KERBEROS_PASSWORD"))
|
||||
globals.SecureServer.SetPingTimeout(20) // Maybe too long?
|
||||
globals.SecureServer.SetAccessKey("ridfebb9")
|
||||
|
||||
globals.NEXServer.On("Data", func(packet *nex.PacketV0) {
|
||||
globals.SecureServer.On("Data", func(packet *nex.PacketV0) {
|
||||
request := packet.RMCRequest()
|
||||
|
||||
fmt.Println("==Friends - Secure==")
|
||||
@@ -33,7 +33,7 @@ func StartNEXServer() {
|
||||
fmt.Println("====================")
|
||||
})
|
||||
|
||||
globals.NEXServer.On("Kick", func(packet *nex.PacketV0) {
|
||||
globals.SecureServer.On("Kick", func(packet *nex.PacketV0) {
|
||||
pid := packet.Sender().PID()
|
||||
|
||||
if globals.ConnectedUsers[pid] == nil {
|
||||
@@ -56,13 +56,13 @@ func StartNEXServer() {
|
||||
fmt.Println("Leaving (Kick)")
|
||||
})
|
||||
|
||||
globals.NEXServer.On("Disconnect", func(packet *nex.PacketV0) {
|
||||
globals.SecureServer.On("Disconnect", func(packet *nex.PacketV0) {
|
||||
fmt.Println("Leaving (Disconnect)")
|
||||
})
|
||||
|
||||
globals.NEXServer.On("Connect", connect)
|
||||
globals.SecureServer.On("Connect", connect)
|
||||
|
||||
registerNEXProtocols()
|
||||
|
||||
globals.NEXServer.Listen(":60001")
|
||||
globals.SecureServer.Listen(":" + os.Getenv("PN_FRIENDS_SECURE_SERVER_PORT"))
|
||||
}
|
||||
Reference in New Issue
Block a user