forked from crow-misia/go-push-receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instanceid.go
131 lines (115 loc) · 3.54 KB
/
instanceid.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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 (
"bytes"
"context"
"fmt"
pb "github.com/crow-misia/go-push-receiver/pb/checkin"
"github.com/golang/protobuf/proto"
"github.com/google/uuid"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type checkInOption struct {
androidID uint64
securityToken uint64
}
type gcmRegisterResponse struct {
token string
androidID uint64
securityToken uint64
appID string
}
func (c *Client) registerGCM(ctx context.Context) (*gcmRegisterResponse, error) {
checkInResp, err := c.checkIn(ctx, &checkInOption{})
if err != nil {
return nil, err
}
return c.doRegister(ctx, *checkInResp.AndroidId, *checkInResp.SecurityToken)
}
func (c *Client) checkIn(ctx context.Context, opt *checkInOption) (*pb.AndroidCheckinResponse, error) {
id := int64(opt.androidID)
r := &pb.AndroidCheckinRequest{
Checkin: &pb.AndroidCheckinProto{
ChromeBuild: &pb.ChromeBuildProto{
Platform: pb.ChromeBuildProto_PLATFORM_LINUX.Enum(),
ChromeVersion: proto.String(chromeVersion),
Channel: pb.ChromeBuildProto_CHANNEL_STABLE.Enum(),
},
Type: pb.DeviceType_DEVICE_CHROME_BROWSER.Enum(),
UserNumber: proto.Int32(0),
},
Fragment: proto.Int32(0),
Version: proto.Int32(3),
UserSerialNumber: proto.Int32(0),
Id: &id,
SecurityToken: &opt.securityToken,
}
message, err := proto.Marshal(r)
if err != nil {
return nil, errors.Wrap(err, "marshal GCM checkin request")
}
res, err := c.post(ctx, checkinURL, bytes.NewReader(message), func(header *http.Header) {
header.Set("Content-Type", "application/x-protobuf")
})
if err != nil {
return nil, errors.Wrap(err, "request GCM checkin")
}
defer closeResponse(res)
// unauthorized error
if res.StatusCode == http.StatusUnauthorized {
return nil, ErrGcmAuthorization
}
if res.StatusCode < 200 || res.StatusCode > 299 {
return nil, errors.Errorf("server error: %s", res.Status)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "read GCM checkin response")
}
var responseProto pb.AndroidCheckinResponse
err = proto.Unmarshal(data, &responseProto)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal GCM checkin response")
}
return &responseProto, nil
}
func (c *Client) doRegister(ctx context.Context, androidID uint64, securityToken uint64) (*gcmRegisterResponse, error) {
appID := fmt.Sprintf("wp:receiver.push.com#%s", uuid.New())
values := url.Values{}
values.Set("app", "org.chromium.linux")
values.Set("X-subtype", appID)
values.Set("device", fmt.Sprint(androidID))
values.Set("sender", fcmServerKey)
res, err := c.post(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", androidID, securityToken))
})
if err != nil {
return nil, errors.Wrap(err, "request GCM register")
}
defer closeResponse(res)
data, err := ioutil.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 &gcmRegisterResponse{
token: token,
androidID: androidID,
securityToken: securityToken,
appID: appID,
}, nil
}