forked from PretendoNetwork/super-mario-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_post_object.go
107 lines (77 loc) · 2.95 KB
/
prepare_post_object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"math/rand"
"os"
"strconv"
"time"
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
func preparePostObject(err error, client *nex.Client, callID uint32, param *nexproto.DataStorePreparePostParam) {
rand.Seed(time.Now().UnixNano())
nodeID := rand.Intn(len(dataStoreIDGenerators))
dataStoreIDGenerator := dataStoreIDGenerators[nodeID]
for dataStoreIDGenerator.InUse() {
// Do nothing, wait until it's done
// There has to be a better way to do this
}
//dataStoreIDGenerator.SetInUse(true)
dataID := dataStoreIDGenerator.Next()
setDataStoreIDGeneratorLastID(nodeID, dataStoreIDGenerator.Value)
initializeCourseData(dataID, client.PID(), param.Size, param.Name, param.Flag, param.ExtraData, param.DataType, param.Period)
if param.DataType != 1 { // 1 is Mii data, assume other values are course meta data
updateCourseMetaBinary(dataID, param.MetaBinary)
}
//dataStoreIDGenerator.SetInUse(false)
key := fmt.Sprintf("course/%d.bin", dataID)
bucket := "pds-amaj-d1"
date := strconv.Itoa(int(time.Now().Unix()))
pid := strconv.Itoa(int(client.PID()))
data := pid + bucket + key + date
hmac := hmac.New(sha256.New, hmacSecret)
hmac.Write([]byte(data))
signature := hex.EncodeToString(hmac.Sum(nil))
fieldBucket := nexproto.NewDataStoreKeyValue()
fieldBucket.Key = "bucket"
fieldBucket.Value = bucket
fieldKey := nexproto.NewDataStoreKeyValue()
fieldKey.Key = "key"
fieldKey.Value = key
fieldACL := nexproto.NewDataStoreKeyValue()
fieldACL.Key = "acl"
fieldACL.Value = "private"
fieldPID := nexproto.NewDataStoreKeyValue()
fieldPID.Key = "pid"
fieldPID.Value = pid
fieldDate := nexproto.NewDataStoreKeyValue()
fieldDate.Key = "date"
fieldDate.Value = date
fieldSignature := nexproto.NewDataStoreKeyValue()
fieldSignature.Key = "signature"
fieldSignature.Value = signature
rmcResponseStream := nex.NewStreamOut(nexServer)
pReqPostInfo := nexproto.NewDataStoreReqPostInfo()
pReqPostInfo.DataID = dataID
pReqPostInfo.URL = os.Getenv("DATASTORE_UPLOAD_URL")
pReqPostInfo.RequestHeaders = []*nexproto.DataStoreKeyValue{}
pReqPostInfo.FormFields = []*nexproto.DataStoreKeyValue{fieldBucket, fieldKey, fieldACL, fieldPID, fieldDate, fieldSignature}
pReqPostInfo.RootCACert = []byte{}
rmcResponseStream.WriteStructure(pReqPostInfo)
rmcResponseBody := rmcResponseStream.Bytes()
rmcResponse := nex.NewRMCResponse(nexproto.DataStoreProtocolID, callID)
rmcResponse.SetSuccess(nexproto.DataStoreMethodPreparePostObject, 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)
}