-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcm.go
107 lines (91 loc) · 2.78 KB
/
gcm.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
/*
* Copyright (c) 2019 Zenichi Amano
*
* This file is part of go-push-receiver, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
package pushreceiver
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type GCMRegistrationOpts struct {
AppID string
InstanceID string
Expiry time.Duration
}
func RegisterGCM(ctx context.Context, authorizationEntity string, creds GCMCredentials, opts *GCMRegistrationOpts) (*FCMCredentials, error) {
values := url.Values{}
var appID string
if opts != nil && opts.AppID != "" {
appID = opts.AppID
} else {
appID = uuid.New().String()
}
if opts != nil && opts.InstanceID != "" {
values.Set("appId", opts.InstanceID)
}
if opts != nil && opts.Expiry != 0 {
ttl := strconv.Itoa(int(opts.Expiry.Seconds()))
values.Set("ttl", ttl)
}
values.Set("app", "org.chromium.linux")
values.Set("scope", "GCM")
values.Set("X-scope", "GCM")
values.Set("X-subtype", appID)
values.Set("device", fmt.Sprint(creds.AndroidID))
values.Set("gmsv", strings.Split(chromeVersion, ".")[0])
values.Set("sender", authorizationEntity)
res, err := postRequest(ctx, registerURL, strings.NewReader(values.Encode()), func(header *http.Header) {
header.Set("Content-Type", "application/x-www-form-urlencoded")
header.Set("Authorization", fmt.Sprintf("AidLogin %d:%d", creds.AndroidID, creds.SecurityToken))
})
if err != nil {
return nil, errors.Wrap(err, "request GCM register")
}
defer closeResponse(res)
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "read GCM register response")
}
subscription, err := url.ParseQuery(string(data))
if err != nil {
return nil, errors.Wrap(err, "parse GCM register URL")
}
token := subscription.Get("token")
return &FCMCredentials{
GCM: creds,
Token: token,
AppID: appID,
}, nil
}
func UnregisterGCM(ctx context.Context, authorizationEntity string, creds GCMCredentials, appID string) error {
values := url.Values{}
values.Set("app", "org.chromium.linux")
values.Set("scope", "GCM")
values.Set("X-scope", "GCM")
values.Set("X-subtype", appID)
values.Set("device", fmt.Sprint(creds.AndroidID))
values.Set("gmsv", strings.Split(chromeVersion, ".")[0])
values.Set("sender", authorizationEntity)
values.Set("delete", "true")
res, err := postRequest(ctx, registerURL, strings.NewReader(values.Encode()), func(header *http.Header) {
header.Set("Content-Type", "application/x-www-form-urlencoded")
header.Set("Authorization", fmt.Sprintf("AidLogin %d:%d", creds.AndroidID, creds.SecurityToken))
})
if err != nil {
return errors.Wrap(err, "failed to unregister with GCM")
}
if res.StatusCode != http.StatusOK {
return errors.New("failed to unregister with GCM")
}
return nil
}