-
Notifications
You must be signed in to change notification settings - Fork 44
/
provider.go
180 lines (157 loc) · 4.37 KB
/
provider.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package nanopush
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/micromdm/nanomdm/mdm"
"github.com/micromdm/nanomdm/push"
"golang.org/x/net/http2"
)
// Doer is ostensibly an *http.Client
type Doer interface {
Do(req *http.Request) (*http.Response, error)
}
const (
Development = "https://api.development.push.apple.com"
Development2197 = "https://api.development.push.apple.com:2197"
Production = "https://api.push.apple.com"
Production2197 = "https://api.push.apple.com:2197"
)
// Provider sends pushes to Apple's APNs servers.
type Provider struct {
client Doer
expiration time.Duration
workers int
baseURL string
}
// JSONPushError is a JSON error returned from the APNs service.
type JSONPushError struct {
Reason string `json:"reason"`
Timestamp int64 `json:"timestamp"`
}
func (e *JSONPushError) Error() string {
s := "APNs push error"
if e == nil {
return s + ": nil"
}
if e.Reason != "" {
s += ": " + e.Reason
}
if e.Timestamp > 0 {
s += ": timestamp " + strconv.FormatInt(e.Timestamp, 10)
}
return s
}
func newError(body io.Reader, statusCode int) error {
var err error = new(JSONPushError)
if decodeErr := json.NewDecoder(body).Decode(err); decodeErr != nil {
err = fmt.Errorf("decoding JSON push error: %w", decodeErr)
}
return fmt.Errorf("push HTTP status: %d: %w", statusCode, err)
}
// do performs the HTTP push request
func (p *Provider) do(ctx context.Context, pushInfo *mdm.Push) *push.Response {
jsonPayload := []byte(`{"mdm":"` + pushInfo.PushMagic + `"}`)
url := p.baseURL + "/3/device/" + pushInfo.Token.String()
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonPayload))
if err != nil {
return &push.Response{Err: err}
}
req.Header.Set("Content-Type", "application/json")
if p.expiration > 0 {
exp := time.Now().Add(p.expiration)
req.Header.Set("apns-expiration", strconv.FormatInt(exp.Unix(), 10))
}
r, err := p.client.Do(req)
var goAwayErr http2.GoAwayError
if errors.As(err, &goAwayErr) {
body := strings.NewReader(goAwayErr.DebugData)
return &push.Response{Err: newError(body, r.StatusCode)}
} else if err != nil {
return &push.Response{Err: err}
}
defer r.Body.Close()
response := &push.Response{Id: r.Header.Get("apns-id")}
if r.StatusCode != http.StatusOK {
response.Err = newError(r.Body, r.StatusCode)
}
return response
}
// pushSerial performs APNs pushes serially.
func (p *Provider) pushSerial(ctx context.Context, pushInfos []*mdm.Push) (map[string]*push.Response, error) {
ret := make(map[string]*push.Response)
for _, pushInfo := range pushInfos {
if pushInfo == nil {
continue
}
ret[pushInfo.Token.String()] = p.do(ctx, pushInfo)
}
return ret, nil
}
// pushConcurrent performs APNs pushes concurrently.
// It spawns worker goroutines and feeds them from the list of pushInfos.
func (p *Provider) pushConcurrent(ctx context.Context, pushInfos []*mdm.Push) (map[string]*push.Response, error) {
// don't start more workers than we have pushes to send
workers := p.workers
if len(pushInfos) > workers {
workers = len(pushInfos)
}
// response associates push.Response with token
type response struct {
token string
response *push.Response
}
jobs := make(chan *mdm.Push)
results := make(chan response)
var wg sync.WaitGroup
// start our workers
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
for pushInfo := range jobs {
results <- response{
token: pushInfo.Token.String(),
response: p.do(ctx, pushInfo),
}
}
}()
}
// start the "feeder" (queue source)
go func() {
for _, pushInfo := range pushInfos {
jobs <- pushInfo
}
close(jobs)
}()
// watch for our workers finishing (they should after feeding is done)
// stop the collector when the workers have finished.
go func() {
wg.Wait()
close(results)
}()
// collect our results
ret := make(map[string]*push.Response)
for r := range results {
ret[r.token] = r.response
}
return ret, nil
}
// Push sends APNs pushes to MDM enrollments.
func (p *Provider) Push(ctx context.Context, pushInfos []*mdm.Push) (map[string]*push.Response, error) {
if len(pushInfos) < 1 {
return nil, errors.New("no push data provided")
} else if len(pushInfos) == 1 {
return p.pushSerial(ctx, pushInfos)
} else {
return p.pushConcurrent(ctx, pushInfos)
}
}