-
Notifications
You must be signed in to change notification settings - Fork 12
/
register.go
72 lines (61 loc) · 1.7 KB
/
register.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
package utils
import (
"encoding/json"
"time"
)
func Register(teamToken string) ([]byte, string, error) {
var err error
var output, body, store []byte
var response Response
var publicKey, privateKey string
if privateKey, publicKey, err = GenerateKey(); err != nil {
panic(err)
}
installID := RandStringRunes(22, nil)
fcmtoken := RandStringRunes(134, nil)
payload := []byte(
`{
"key":"` + publicKey + `",
"install_id":"` + installID + `",
"fcm_token":"` + installID + `:APA91b` + fcmtoken + `",
"tos":"` + time.Now().UTC().Format("2006-01-02T15:04:05.999Z") + `",
"model":"Android",
"serial_number":"` + installID + `"
}`,
)
if teamToken != "" {
if body, err = request(payload, teamToken, "", "registerTeam"); err != nil {
panic(err)
}
} else {
if body, err = request(payload, "", "", "register"); err != nil {
panic(err)
}
}
if err = json.Unmarshal(body, &response); err != nil {
panic(err)
}
response.Config.ReservedDec, response.Config.ReservedHex = clientIDtoReserved(response.Config.ClientID)
jsonIn := RegisterOutput{
Endpoint: struct {
V4 string `json:"v4"`
V6 string `json:"v6"`
}{
V4: response.Config.Peers[0].Endpoint.V4,
V6: response.Config.Peers[0].Endpoint.V6,
},
ReservedStr: response.Config.ClientID,
ReservedHex: response.Config.ReservedHex,
ReservedDec: response.Config.ReservedDec,
PrivateKey: privateKey,
PublicKey: response.Config.Peers[0].PublicKey,
Addresses: response.Config.Interface.Addresses,
}
if output, err = json.MarshalIndent(jsonIn, "", " "); err != nil {
panic(err)
}
if store, err = json.MarshalIndent(response, "", " "); err != nil {
panic(err)
}
return store, string(output), nil
}