Skip to content

Commit

Permalink
Merge pull request #26 from PretendoNetwork/nex-go-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniElectra authored Apr 7, 2024
2 parents 8acdf1c + dfe7dba commit 112d279
Show file tree
Hide file tree
Showing 78 changed files with 2,889 additions and 3,746 deletions.
70 changes: 39 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NEX Protocols Common Go
## NEX protocols used by many games with premade handlers and a high level API

[![GoDoc](https://godoc.org/github.com/PretendoNetwork/nex-protocols-common-go?status.svg)](https://godoc.org/github.com/PretendoNetwork/nex-protocols-common-go)
[![GoDoc](https://godoc.org/github.com/PretendoNetwork/nex-protocols-common-go/v2?status.svg)](https://godoc.org/github.com/PretendoNetwork/nex-protocols-common-go/v2)

### Other NEX libraries
[nex-go](https://github.com/PretendoNetwork/nex-go) - Barebones NEX/PRUDP server implementation
Expand All @@ -10,14 +10,14 @@

### Install

`go get github.com/PretendoNetwork/nex-protocols-common-go`
`go get github.com/PretendoNetwork/nex-protocols-common-go/v2`

### Usage

`nex-protocols-common-go` provides a higher level API than the [NEX Protocols Go module](https://github.com/PretendoNetwork/nex-protocols-go). This module handles many of the more common protcols and methods used shared by many servers. Instead of working directly with the NEX server, this module exposes an API for defining helper functions to provide the module with the data it needs to run

### Example, friends (Wii U) authentication server
### For a complete example, see the complete [Friends Authentication Server](https://github.com/PretendoNetwork/friends-authentication), and other game servers
### For a complete example, see the complete [Friends Server](https://github.com/PretendoNetwork/friends), and other game servers

```go
package main
Expand All @@ -26,44 +26,52 @@ import (
"fmt"
"os"

"github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/nex-protocols-common-go/authentication"
"github.com/PretendoNetwork/nex-go/v2"
ticket_granting "github.com/PretendoNetwork/nex-protocols-go/v2/ticket-granting"
common_ticket_granting "github.com/PretendoNetwork/nex-protocols-common-go/v2/ticket-granting"
)

var nexServer *nex.Server
var nexServer *nex.PRUDPServer

func main() {
nexServer = nex.NewServer()
nexServer.SetPRUDPVersion(0)
nexServer.SetKerberosKeySize(16)
nexServer.SetKerberosPassword(os.Getenv("KERBEROS_PASSWORD"))
nexServer.SetAccessKey("ridfebb9")
nexServer := nex.NewPRUDPServer()

nexServer.On("Data", func(packet *nex.PacketV0) {
request := packet.RMCRequest()
endpoint := nex.NewPRUDPEndPoint(1)
endpoint.ServerAccount = nex.NewAccount(types.NewPID(1), "Quazal Authentication", "password"))
endpoint.AccountDetailsByPID = accountDetailsByPID
endpoint.AccountDetailsByUsername = accountDetailsByUsername

endpoint.OnData(func(packet nex.PacketInterface) {
request := packet.RMCMessage()

fmt.Println("==Friends - Auth==")
fmt.Printf("Protocol ID: %#v\n", request.ProtocolID())
fmt.Printf("Method ID: %#v\n", request.MethodID())
fmt.Printf("Protocol ID: %#v\n", request.ProtocolID)
fmt.Printf("Method ID: %#v\n", request.MethodID)
fmt.Println("==================")
})

authenticationProtocol := authentication.NewCommonAuthenticationProtocol(nexServer)
nexServer.SetFragmentSize(962)
nexServer.LibraryVersions.SetDefault(nex.NewLibraryVersion(1, 1, 0))
nexServer.SessionKeyLength = 16
nexServer.AccessKey = "ridfebb9"

ticketGrantingProtocol := ticket_granting.NewProtocol(endpoint)
endpoint.RegisterServiceProtocol(ticketGrantingProtocol)
commonTicketGrantingProtocol := common_ticket_granting.NewCommonProtocol(ticketGrantingProtocol)

secureStationURL := nex.NewStationURL("")
secureStationURL.SetScheme("prudps")
secureStationURL.SetAddress(os.Getenv("SECURE_SERVER_LOCATION"))
secureStationURL.SetPort(os.Getenv("SECURE_SERVER_PORT"))
secureStationURL.SetCID("1")
secureStationURL.SetPID("2")
secureStationURL.SetSID("1")
secureStationURL.SetStream("10")
secureStationURL.SetType("2")

authenticationProtocol.SetSecureStationURL(secureStationURL)
authenticationProtocol.SetBuildName("Pretendo Friends Auth")
authenticationProtocol.SetPasswordFromPIDFunction(passwordFromPID)

nexServer.Listen(":60000")
secureStationURL.Scheme = "prudps"
secureStationURL.Fields.Set("address", os.Getenv("SECURE_SERVER_LOCATION"))
secureStationURL.Fields.Set("port", os.Getenv("SECURE_SERVER_PORT"))
secureStationURL.Fields.Set("CID", "1")
secureStationURL.Fields.Set("PID", "2")
secureStationURL.Fields.Set("sid", "1")
secureStationURL.Fields.Set("stream", "10")
secureStationURL.Fields.Set("type", "2")

commonTicketGrantingProtocol.SecureStationURL = secureStationURL
commonTicketGrantingProtocol.BuildName = "Pretendo Friends Auth"

nexServer.Listen(60000)
}
```
```
97 changes: 41 additions & 56 deletions datastore/change_meta.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,81 @@
package datastore

import (
nex "github.com/PretendoNetwork/nex-go"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/globals"
datastore "github.com/PretendoNetwork/nex-protocols-go/datastore"
datastore_types "github.com/PretendoNetwork/nex-protocols-go/datastore/types"
"github.com/PretendoNetwork/nex-go/v2"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/v2/globals"
datastore "github.com/PretendoNetwork/nex-protocols-go/v2/datastore"
datastore_types "github.com/PretendoNetwork/nex-protocols-go/v2/datastore/types"
)

func changeMeta(err error, packet nex.PacketInterface, callID uint32, param *datastore_types.DataStoreChangeMetaParam) uint32 {
if commonDataStoreProtocol.getObjectInfoByDataIDHandler == nil {
func (commonProtocol *CommonProtocol) changeMeta(err error, packet nex.PacketInterface, callID uint32, param *datastore_types.DataStoreChangeMetaParam) (*nex.RMCMessage, *nex.Error) {
if commonProtocol.GetObjectInfoByDataID == nil {
common_globals.Logger.Warning("GetObjectInfoByDataID not defined")
return nex.Errors.Core.NotImplemented
return nil, nex.NewError(nex.ResultCodes.Core.NotImplemented, "change_error")
}

if commonDataStoreProtocol.updateObjectPeriodByDataIDWithPasswordHandler == nil {
if commonProtocol.UpdateObjectPeriodByDataIDWithPassword == nil {
common_globals.Logger.Warning("UpdateObjectPeriodByDataIDWithPassword not defined")
return nex.Errors.Core.NotImplemented
return nil, nex.NewError(nex.ResultCodes.Core.NotImplemented, "change_error")
}

if commonDataStoreProtocol.updateObjectMetaBinaryByDataIDWithPasswordHandler == nil {
if commonProtocol.UpdateObjectMetaBinaryByDataIDWithPassword == nil {
common_globals.Logger.Warning("UpdateObjectMetaBinaryByDataIDWithPassword not defined")
return nex.Errors.Core.NotImplemented
return nil, nex.NewError(nex.ResultCodes.Core.NotImplemented, "change_error")
}

if commonDataStoreProtocol.updateObjectDataTypeByDataIDWithPasswordHandler == nil {
if commonProtocol.UpdateObjectDataTypeByDataIDWithPassword == nil {
common_globals.Logger.Warning("UpdateObjectDataTypeByDataIDWithPassword not defined")
return nex.Errors.Core.NotImplemented
return nil, nex.NewError(nex.ResultCodes.Core.NotImplemented, "change_error")
}

if err != nil {
common_globals.Logger.Error(err.Error())
return nex.Errors.DataStore.Unknown
return nil, nex.NewError(nex.ResultCodes.DataStore.Unknown, "change_error")
}

client := packet.Sender()
connection := packet.Sender()
endpoint := connection.Endpoint()

metaInfo, errCode := commonDataStoreProtocol.getObjectInfoByDataIDHandler(param.DataID)
if errCode != 0 {
return errCode
metaInfo, errCode := commonProtocol.GetObjectInfoByDataID(param.DataID)
if errCode != nil {
return nil, errCode
}

// TODO - Is this the right permission?
errCode = commonDataStoreProtocol.VerifyObjectPermission(metaInfo.OwnerID, client.PID(), metaInfo.DelPermission)
if errCode != 0 {
return errCode
errCode = commonProtocol.VerifyObjectPermission(metaInfo.OwnerID, connection.PID(), metaInfo.DelPermission)
if errCode != nil {
return nil, errCode
}

if param.ModifiesFlag&0x08 != 0 {
errCode = commonDataStoreProtocol.updateObjectPeriodByDataIDWithPasswordHandler(param.DataID, param.Period, param.UpdatePassword)
if errCode != 0 {
return errCode
if param.ModifiesFlag.PAND(0x08) != 0 {
errCode = commonProtocol.UpdateObjectPeriodByDataIDWithPassword(param.DataID, param.Period, param.UpdatePassword)
if errCode != nil {
return nil, errCode
}
}

if param.ModifiesFlag&0x10 != 0 {
errCode = commonDataStoreProtocol.updateObjectMetaBinaryByDataIDWithPasswordHandler(param.DataID, param.MetaBinary, param.UpdatePassword)
if errCode != 0 {
return errCode
if param.ModifiesFlag.PAND(0x10) != 0 {
errCode = commonProtocol.UpdateObjectMetaBinaryByDataIDWithPassword(param.DataID, param.MetaBinary, param.UpdatePassword)
if errCode != nil {
return nil, errCode
}
}

if param.ModifiesFlag&0x80 != 0 {
errCode = commonDataStoreProtocol.updateObjectDataTypeByDataIDWithPasswordHandler(param.DataID, param.DataType, param.UpdatePassword)
if errCode != 0 {
return errCode
if param.ModifiesFlag.PAND(0x80) != 0 {
errCode = commonProtocol.UpdateObjectDataTypeByDataIDWithPassword(param.DataID, param.DataType, param.UpdatePassword)
if errCode != nil {
return nil, errCode
}
}

rmcResponse := nex.NewRMCResponse(datastore.ProtocolID, callID)
rmcResponse.SetSuccess(datastore.MethodChangeMeta, nil)
rmcResponse := nex.NewRMCSuccess(endpoint, nil)
rmcResponse.ProtocolID = datastore.ProtocolID
rmcResponse.MethodID = datastore.MethodChangeMeta
rmcResponse.CallID = callID

rmcResponseBytes := rmcResponse.Bytes()

var responsePacket nex.PacketInterface

if commonDataStoreProtocol.server.PRUDPVersion() == 0 {
responsePacket, _ = nex.NewPacketV0(client, nil)
responsePacket.SetVersion(0)
} else {
responsePacket, _ = nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
if commonProtocol.OnAfterChangeMeta != nil {
go commonProtocol.OnAfterChangeMeta(packet, param)
}

responsePacket.SetSource(packet.Destination())
responsePacket.SetDestination(packet.Source())
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)

responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)

commonDataStoreProtocol.server.Send(responsePacket)

return 0
return rmcResponse, nil
}
Loading

0 comments on commit 112d279

Please sign in to comment.