This is a mess

Nat Traversal failed
Bad urls? Bad response to get urls or requestprobeinitext?
Will need updating to latest versions of nex-go and nex-protocols-go
This commit is contained in:
SuperMarioDaBom 2022-08-29 08:44:51 -07:00
parent 5d8beeb5e9
commit 25dcb37f47
16 changed files with 715 additions and 26 deletions

View File

@ -19,7 +19,8 @@ func connect(packet *nex.PacketV1) {
checkDataDecrypted := kerberos.Decrypt(checkData)
checkDataStream := nex.NewStreamIn(checkDataDecrypted, nexServer)
_ = checkDataStream.ReadUInt32LE() // User PID
userPID := checkDataStream.ReadUInt32LE() // User PID
packet.Sender().SetPID(userPID)
_ = checkDataStream.ReadUInt32LE() //CID of secure server station url
responseCheck := checkDataStream.ReadUInt32LE()
@ -33,4 +34,12 @@ func connect(packet *nex.PacketV1) {
packet.Sender().UpdateRC4Key(sessionKey)
packet.Sender().SetSessionKey(sessionKey)
if !doesUserExist(userPID) {
addNewUser(userPID)
}
if !isUserAllowed(userPID) {
nexServer.Kick(packet.Sender())
return
}
}

View File

@ -4,6 +4,7 @@ import (
"math/rand"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)

205
database.go Normal file
View File

@ -0,0 +1,205 @@
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mongoClient *mongo.Client
var mongoContext context.Context
var accountDatabase *mongo.Database
var doorsDatabase *mongo.Database
var pnidCollection *mongo.Collection
var nexAccountsCollection *mongo.Collection
var regionsCollection *mongo.Collection
var usersCollection *mongo.Collection
var sessionsCollection *mongo.Collection
var callsCollection *mongo.Collection
var tourneysCollection *mongo.Collection
func connectMongo() {
mongoClient, _ = mongo.NewClient(options.Client().ApplyURI("mongodb://143.198.126.113:27017/"))
mongoContext, _ = context.WithTimeout(context.Background(), 10*time.Second)
_ = mongoClient.Connect(mongoContext)
accountDatabase = mongoClient.Database("pretendo")
pnidCollection = accountDatabase.Collection("pnids")
nexAccountsCollection = accountDatabase.Collection("nexaccounts")
doorsDatabase = mongoClient.Database("doors")
usersCollection = doorsDatabase.Collection("users")
sessionsCollection = doorsDatabase.Collection("sessions")
callsCollection = doorsDatabase.Collection("calls")
sessionsCollection.DeleteMany(context.TODO(), bson.D{})
callsCollection.DeleteMany(context.TODO(), bson.D{})
}
func getUsernameFromPID(pid uint32) string {
var result bson.M
err := pnidCollection.FindOne(context.TODO(), bson.D{{Key: "pid", Value: pid}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return ""
}
panic(err)
}
return result["username"].(string)
}
func addNewUser(pid uint32) {
_, err := usersCollection.InsertOne(context.TODO(), bson.D{{"pid", pid}, {"missed_calls", bson.A{""}}, {"username", getUsernameFromPID(pid)}, {"status", "unallowed"}})
if err != nil {
panic(err)
}
}
func isUserAllowed(pid uint32) bool {
var result bson.M
err := usersCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return false
} else {
panic(err)
}
} else {
data := result["status"].(string)
if data == "allowed" {
return true
} else {
return false
}
}
}
func doesUserExist(pid uint32) bool {
var result bson.M
err := usersCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return false
} else {
panic(err)
}
} else {
return true
}
}
func addPlayerSession(pid uint32, urls []string, ip string, port string) {
_, err := sessionsCollection.InsertOne(context.TODO(), bson.D{{"pid", pid}, {"urls", urls}, {"ip", ip}, {"port", port}})
if err != nil {
panic(err)
}
}
func getAllSessionPIDs() []uint32 {
var result []bson.M
output := []uint32{}
c, _ := sessionsCollection.Find(context.TODO(), bson.D{})
c.All(context.TODO(), &result)
for _, i := range result {
output = append(output, uint32(i["pid"].(int64)))
}
return output
}
func doesSessionExist(pid uint32) bool {
var result bson.M
err := sessionsCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return false
} else {
panic(err)
}
} else {
return true
}
}
func updatePlayerSessionAll(pid uint32, urls []string, ip string, port string) {
_, err := sessionsCollection.UpdateOne(context.TODO(), bson.D{{"pid", pid}}, bson.D{{"$set", bson.D{{"pid", pid}, {"urls", urls}, {"ip", ip}, {"port", port}}}})
if err != nil {
panic(err)
}
}
func updatePlayerSessionUrl(pid uint32, oldurl string, newurl string) {
var result bson.M
err := sessionsCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
panic(err)
}
oldurlArray := result["urls"].(bson.A)
newurlArray := make([]string, len(oldurlArray))
for i := 0; i < len(oldurlArray); i++ {
if oldurlArray[i].(string) == oldurl {
newurlArray[i] = newurl
} else {
newurlArray[i] = oldurlArray[i].(string)
}
}
_, err = sessionsCollection.UpdateOne(context.TODO(), bson.D{{"pid", pid}}, bson.D{{"$set", bson.D{{"urls", newurlArray}}}})
if err != nil {
panic(err)
}
}
func deletePlayerSession(pid uint32) {
_, err := sessionsCollection.DeleteOne(context.TODO(), bson.D{{"pid", pid}})
if err != nil {
panic(err)
}
}
func getPlayerUrls(pid uint32) []string {
var result bson.M
err := sessionsCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
panic(err)
}
oldurlArray := result["urls"].(bson.A)
newurlArray := make([]string, len(oldurlArray))
for i := 0; i < len(oldurlArray); i++ {
newurlArray[i] = oldurlArray[i].(string)
}
return newurlArray
}
func getPlayerSessionAddress(pid uint32) string {
var result bson.M
fmt.Println(pid)
err := sessionsCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return "127.0.0.1:9999"
} else {
panic(err)
}
}
return result["ip"].(string) + ":" + result["port"].(string)
}

62
database_calls.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func newCall(caller uint32, target uint32) {
_, err := callsCollection.InsertOne(context.TODO(), bson.D{{"caller_pid", caller}, {"target_pid", target}, {"ringing", true}})
if err != nil {
panic(err)
}
}
func getCallInfoByCaller(caller uint32) (uint32, uint32, bool) { // caller pid, target pid, ringing
var result bson.M
filter := bson.D{{"caller_pid", caller}}
err := callsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return 0, 0, false
} else {
panic(err)
}
} else {
return uint32(result["caller_pid"].(int64)), uint32(result["target_pid"].(int64)), result["ringing"].(bool)
}
}
func getCallInfoByTarget(target uint32) (uint32, uint32, bool) { // caller pid, target pid, ringing
var result bson.M
filter := bson.D{{"target_pid", target}}
err := callsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return 0, 0, false
} else {
panic(err)
}
} else {
return uint32(result["caller_pid"].(int64)), uint32(result["target_pid"].(int64)), result["ringing"].(bool)
}
}
func endCallRinging(caller uint32) {
_, err := callsCollection.UpdateOne(context.TODO(), bson.D{{"caller_pid", caller}}, bson.D{{"$set", bson.D{{"ringing", false}}}})
if err != nil {
panic(err)
}
}
func endCall(caller uint32) {
_, err := callsCollection.DeleteOne(context.TODO(), bson.D{{"caller_pid", caller}})
if err != nil {
panic(err)
}
}

67
gathering.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
nexproto "github.com/PretendoNetwork/nex-protocols-go"
nex "github.com/PretendoNetwork/nex-go"
)
func unregisterGathering(err error, client *nex.Client, callID uint32, idGathering uint32) {
endCall(idGathering)
rmcResponse := nex.NewRMCResponse(nexproto.MatchMakingMethodUnregisterGathering, callID)
rmcResponse.SetSuccess(nexproto.MatchMakingProtocolID, []byte{0x01})
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}
func findBySingleID(err error, client *nex.Client, callID uint32, id uint32) {
caller, _, _ := getCallInfoByCaller(id)
gathering := nexproto.NewGathering()
gathering.ID = id
gathering.OwnerPID = caller
gathering.HostPID = caller
gathering.MinimumParticipants = 2
gathering.MaximumParticipants = 2
gathering.Description = "Doors Invite Request"
outStream := nex.NewStreamOut(nexServer)
outStream.WriteBool(true)
outStream.WriteString("Gathering")
b := gathering.Bytes(nex.NewStreamOut(nexServer))
outStream.WriteUInt32LE(uint32(4) + uint32(len(b)))
outStream.WriteBuffer(b)
rmcResponse := nex.NewRMCResponse(nexproto.MatchMakingMethodFindBySingleID, callID)
rmcResponse.SetSuccess(nexproto.MatchMakingProtocolID, outStream.Bytes())
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}

View File

@ -1,7 +1,10 @@
package main
import (
"fmt"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
@ -9,8 +12,8 @@ import (
type NotificationEvent struct {
sourcePID uint32
typeParameter uint32
parameter1 uint64
parameter2 uint64
parameter1 uint32
parameter2 uint32
stringParameter string
nex.Structure
}
@ -19,8 +22,8 @@ type NotificationEvent struct {
func (notificationEvent *NotificationEvent) Bytes(stream *nex.StreamOut) []byte {
stream.WriteUInt32LE(notificationEvent.sourcePID)
stream.WriteUInt32LE(notificationEvent.typeParameter)
stream.WriteUInt64LE(notificationEvent.parameter1)
stream.WriteUInt64LE(notificationEvent.parameter2)
stream.WriteUInt32LE(notificationEvent.parameter1)
stream.WriteUInt32LE(notificationEvent.parameter2)
stream.WriteString(notificationEvent.stringParameter)
return stream.Bytes()
@ -28,28 +31,21 @@ func (notificationEvent *NotificationEvent) Bytes(stream *nex.StreamOut) []byte
func getFriendNotificationData(err error, client *nex.Client, callID uint32, uiType int32) {
fmt.Printf("uiType: %d\r\n", uiType)
rmcResponseStream := nex.NewStreamOut(nexServer)
// List<NotificationEvent>
notificationEvent := &NotificationEvent{}
notificationEvent.sourcePID = 1743126339 // Sender PID
notificationEvent.sourcePID = 1337825003 // Sender PID
notificationEvent.typeParameter = 102000 // Notification type
notificationEvent.parameter1 = 1 // Gathering ID
notificationEvent.parameter2 = 1730592963 // Recipient PID
notificationEvent.parameter2 = 1513547864 // Recipient PID
notificationEvent.stringParameter = "Invite Cancellation" // Unknown
notificationEvent2 := &NotificationEvent{}
notificationEvent2.sourcePID = 1743126339 // Sender PID
notificationEvent2.typeParameter = 101000 // Notification type
notificationEvent2.parameter1 = 1 // Gathering ID
notificationEvent2.parameter2 = 1730592963 // Recipient PID
notificationEvent2.stringParameter = "Invite Request" // Unknown
rmcResponseStream.WriteUInt32LE(2)
rmcResponseStream.WriteStructure(notificationEvent)
rmcResponseStream.WriteStructure(notificationEvent2)
rmcResponseStream.WriteUInt32LE(0)
//rmcResponseStream.WriteStructure(notificationEvent)
rmcResponseBody := rmcResponseStream.Bytes()
@ -70,4 +66,11 @@ func getFriendNotificationData(err error, client *nex.Client, callID uint32, uiT
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
//// HANDLE INCOMING CALL ////
caller, target, ringing := getCallInfoByTarget(client.PID())
if (caller != 0) && (target == client.PID()) && (ringing == true) {
sendCallNotification(caller, target, callID)
}
}

22
go.mod Normal file
View File

@ -0,0 +1,22 @@
module wiiu-chat-secure
go 1.18
require (
go.mongodb.org/mongo-driver v1.10.1
)
require (
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/superwhiskers/crunch/v3 v3.5.6 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect
)

55
go.sum Normal file
View File

@ -0,0 +1,55 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/superwhiskers/crunch/v3 v3.5.6 h1:gxAMb9Ga/DerwN8jg3U1OMAEPLww3WCW465R07owRUQ=
github.com/superwhiskers/crunch/v3 v3.5.6/go.mod h1:4ub2EKgF1MAhTjoOCTU4b9uLMsAweHEa89aRrfAypXA=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.10.1 h1:NujsPveKwHaWuKUer/ceo9DzEe7HIj1SlJ6uvXZG0S4=
go.mongodb.org/mongo-driver v1.10.1/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

5
init.go Normal file
View File

@ -0,0 +1,5 @@
package main
func init() {
connectMongo()
}

68
join_session.go Normal file
View File

@ -0,0 +1,68 @@
package main
import (
"fmt"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
"github.com/PretendoNetwork/nex-go"
)
func joinMatchmakeSessionEx(err error, client *nex.Client, callID uint32, gid uint32, strMessage string, dontCareMyBlockList bool, participationCount uint16) {
fmt.Printf("gid: %d, strMessage: %s, dontCareMyBlockList: %t, participationCount: %d\r\n", gid, strMessage, dontCareMyBlockList, participationCount)
endCallRinging(gid)
output := nex.NewStreamOut(nexServer)
output.WriteBuffer(make([]byte, 32))
rmcResponse := nex.NewRMCResponse(nexproto.MatchmakeExtensionProtocolID, callID)
rmcResponse.SetSuccess(nexproto.MatchmakeExtensionMethodJoinMatchmakeSessionEx, output.Bytes())
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}
func getSessionUrls(err error, client *nex.Client, callID uint32, gid uint32) {
var stationUrlStrings []string
hostpid, _, _ := getCallInfoByCaller(gid)
stationUrlStrings = getPlayerUrls(hostpid)
rmcResponseStream := nex.NewStreamOut(nexServer)
rmcResponseStream.WriteListString(stationUrlStrings)
rmcResponseBody := rmcResponseStream.Bytes()
// Build response packet
rmcResponse := nex.NewRMCResponse(nexproto.MatchMakingProtocolID, callID)
rmcResponse.SetSuccess(nexproto.MatchMakingMethodGetSessionURLs, rmcResponseBody)
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}

21
main.go
View File

@ -4,6 +4,7 @@ import (
"fmt"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
@ -27,16 +28,34 @@ func main() {
fmt.Println("======================")
})
nexServer.On("Kick", func(packet *nex.PacketV1) {
fmt.Println("Kick client event called")
deletePlayerSession(packet.Sender().PID())
})
nexServer.On("Disconnect", func(packet *nex.PacketV1) {
fmt.Println("Disconnect client event called")
deletePlayerSession(packet.Sender().PID())
})
secureServer = nexproto.NewSecureProtocol(nexServer)
matchmakeExtensionServer := nexproto.NewMatchmakeExtensionProtocol(nexServer)
natTraversalServer := nexproto.NewNATTraversalProtocol(nexServer)
natTraversalServer := nexproto.NewNatTraversalProtocol(nexServer)
matchMakingServer := nexproto.NewMatchMakingProtocol(nexServer)
matchmakeExtensionServer.OpenParticipation(openParticipation)
matchmakeExtensionServer.CreateMatchmakeSession(createMatchmakeSession)
matchmakeExtensionServer.UpdateNotificationData(updateNotificationData)
matchmakeExtensionServer.GetFriendNotificationData(getFriendNotificationData)
matchmakeExtensionServer.JoinMatchmakeSessionEx(joinMatchmakeSessionEx)
natTraversalServer.ReportNATProperties(reportNATProperties)
natTraversalServer.RequestProbeInitiationExt(requestProbeInitiationExt)
natTraversalServer.ReportNATTraversalResult(reportNatTraversalResult)
matchMakingServer.UnregisterGathering(unregisterGathering)
matchMakingServer.FindBySingleID(findBySingleID)
matchMakingServer.GetSessionURLs(getSessionUrls)
// Handle PRUDP CONNECT packet (not an RMC method)
nexServer.On("Connect", connect)

View File

@ -2,6 +2,7 @@ package main
import (
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)

View File

@ -1,20 +1,41 @@
package main
import (
"strconv"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
func register(err error, client *nex.Client, callID uint32, stationUrls []*nex.StationURL) {
localStation := stationUrls[0]
localStationURL := localStation.EncodeToString()
connectionId := uint32(secureServer.ConnectionIDCounter.Increment())
client.SetConnectionId(connectionId)
client.SetLocalStationUrl(localStationURL)
address := client.Address().IP.String()
port := string(client.Address().Port)
port := strconv.Itoa(client.Address().Port)
natf := "0"
natm := "0"
type_ := "3"
pid := strconv.Itoa(int(client.PID()))
localStation.SetAddress(&address)
localStation.SetPort(&port)
localStation.SetNatf(&natf)
localStation.SetNatm(&natm)
localStation.SetType(&type_)
localStation.SetPid(&pid)
localStationURL := localStation.EncodeToString()
globalStationURL := localStation.EncodeToString()
if !doesSessionExist(client.PID()) {
addPlayerSession(client.PID(), []string{localStationURL, globalStationURL}, address, port)
} else {
updatePlayerSessionAll(client.PID(), []string{localStationURL, globalStationURL}, address, port)
}
rmcResponseStream := nex.NewStreamOut(nexServer)

View File

@ -1,16 +1,110 @@
package main
import (
"fmt"
"strconv"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
func reportNATProperties(err error, client *nex.Client, callID uint32, natmapping uint32, natfiltering uint32, rtt uint32) {
// TODO: Implement this
func reportNATProperties(err error, client *nex.Client, callID uint32, natm uint32, natf uint32, rtt uint32) {
stationUrlsStrings := getPlayerUrls(client.PID())
stationUrls := make([]nex.StationURL, len(stationUrlsStrings))
pid := strconv.FormatUint(uint64(client.PID()), 10)
rvcid := strconv.FormatUint(uint64(client.ConnectionId()), 10)
// Build response packet
rmcResponse := nex.NewRMCResponse(nexproto.NATTraversalProtocolID, callID)
rmcResponse.SetSuccess(nexproto.NATTraversalMethodReportNATProperties, nil)
for i := 0; i < len(stationUrlsStrings); i++ {
stationUrls[i] = *nex.NewStationURL(stationUrlsStrings[i])
if stationUrls[i].Type() == "3" {
natm_s := strconv.FormatUint(uint64(natm), 10)
natf_s := strconv.FormatUint(uint64(natf), 10)
stationUrls[i].SetNatm(&natm_s)
stationUrls[i].SetNatf(&natf_s)
}
stationUrls[i].SetPid(&pid)
stationUrls[i].SetRVCID(&rvcid)
updatePlayerSessionUrl(client.PID(), stationUrlsStrings[i], stationUrls[i].EncodeToString())
}
rmcResponse := nex.NewRMCResponse(nexproto.NatTraversalProtocolID, callID)
rmcResponse.SetSuccess(nexproto.NatTraversalMethodReportNATProperties, nil)
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}
func requestProbeInitiationExt(err error, client *nex.Client, callID uint32, targetList []string, stationToProbe string) {
fmt.Println(targetList)
fmt.Println(stationToProbe)
rmcResponse := nex.NewRMCResponse(nexproto.NatTraversalProtocolID, callID)
rmcResponse.SetSuccess(nexproto.NatTraversalMethodRequestProbeInitiationExt, nil)
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
rmcMessage := nex.RMCRequest{}
rmcMessage.SetProtocolID(nexproto.NatTraversalProtocolID)
rmcMessage.SetCallID(0xffff0000 + callID)
rmcMessage.SetMethodID(nexproto.NatTraversalMethodInitiateProbe)
rmcRequestStream := nex.NewStreamOut(nexServer)
rmcRequestStream.WriteString(stationToProbe)
rmcRequestBody := rmcRequestStream.Bytes()
rmcMessage.SetParameters(rmcRequestBody)
rmcMessageBytes := rmcMessage.Bytes()
for _, target := range targetList {
targetUrl := nex.NewStationURL(target)
targetClient := nexServer.GetClient(targetUrl.Address() + ":" + targetUrl.Port())
fmt.Println(targetClient)
if targetClient != nil {
messagePacket, _ := nex.NewPacketV1(targetClient, nil)
messagePacket.SetVersion(1)
messagePacket.SetSource(0xA1)
messagePacket.SetDestination(0xAF)
messagePacket.SetType(nex.DataPacket)
messagePacket.SetPayload(rmcMessageBytes)
messagePacket.AddFlag(nex.FlagNeedsAck)
messagePacket.AddFlag(nex.FlagReliable)
nexServer.Send(messagePacket)
}
}
}
func reportNatTraversalResult(err error, client *nex.Client, callID uint32, cid uint32, result bool, rtt uint32) {
fmt.Println("DID NAT TRAVERSAL SUCCEED?")
fmt.Println(result)
rmcResponse := nex.NewRMCResponse(nexproto.NatTraversalProtocolID, callID)
rmcResponse.SetSuccess(nexproto.NatTraversalMethodReportNATTraversalResult, nil)
rmcResponseBytes := rmcResponse.Bytes()

39
send_call_notification.go Normal file
View File

@ -0,0 +1,39 @@
package main
import nex "github.com/PretendoNetwork/nex-go"
func sendCallNotification(caller uint32, target uint32, callID uint32) {
event := &NotificationEvent{}
event.sourcePID = caller // Sender PID
event.typeParameter = 101000 // Notification type
event.parameter1 = caller // Gathering ID
event.parameter2 = target // Recipient PID
event.stringParameter = "Invite Request" // Unknown
eventObject := nex.NewStreamOut(nexServer)
eventObject.WriteStructure(event)
rmcRequest, _ := nex.NewRMCRequest([]byte{})
rmcRequest.SetProtocolID(14)
rmcRequest.SetCallID(0xffff + callID)
rmcRequest.SetMethodID(1)
rmcRequest.SetParameters(eventObject.Bytes())
rmcRequestBytes := rmcRequest.Bytes()
clientAddr := getPlayerSessionAddress(target)
requestPacket, _ := nex.NewPacketV1(nexServer.GetClient(clientAddr), nil)
requestPacket.SetVersion(1)
requestPacket.SetSource(0xA1)
requestPacket.SetDestination(0xAF)
requestPacket.SetType(nex.DataPacket)
requestPacket.SetPayload(rmcRequestBytes)
requestPacket.AddFlag(nex.FlagNeedsAck)
requestPacket.AddFlag(nex.FlagReliable)
nexServer.Send(requestPacket)
}

View File

@ -1,12 +1,30 @@
package main
import (
"fmt"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
func updateNotificationData(err error, client *nex.Client, callID uint32, uiType uint32, uiParam1 uint32, uiParam2 uint32, strParam string) {
// TODO: Implement this
fmt.Printf("uiType: %d, uiParam1: %d, uiParam2: %d, strParam: %s\r\n", uiType, uiParam1, uiParam2, strParam)
// kick player if invite cancellation to prevent app hanging indefinitely
if uiType == 102 {
endCall(uiParam1)
nexServer.Kick(client)
return
}
if uiType == 101 {
newCall(client.PID(), uiParam2)
if doesSessionExist(uiParam2) {
sendCallNotification(client.PID(), uiParam2, callID)
}
}
// Build response packet
rmcResponse := nex.NewRMCResponse(nexproto.MatchmakeExtensionProtocolID, callID)