forked from Wattpad/apns
/
client.go
348 lines (299 loc) · 8.32 KB
/
client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package apns
import (
"bytes"
"crypto/tls"
"encoding/binary"
"io"
"log"
"net"
"strings"
"time"
)
var (
MAX_SEND_Q = 10000
TIME_OUT = time.Minute // dial & write timeout, avoid infinite block
ErrChannel = make(chan *SendErr)
)
type SendErr struct {
Pn *PushNotification
Res *errResponse
}
type opSend struct {
Pn *PushNotification
ChErr chan error
}
const (
signal_STOP = iota
)
// Client contains the fields necessary to communicate
// with Apple, such as the gateway to use and your
// certificate contents.
//
// You'll need to provide your own CertificateFile
// and KeyFile to send notifications. Ideally, you'll
// just set the CertificateFile and KeyFile fields to
// a location on drive where the certs can be loaded,
// but if you prefer you can use the CertificateBase64
// and KeyBase64 fields to store the actual contents.
type Client struct {
Gateway string
CertificateFile string
CertificateBase64 string
KeyFile string
KeyBase64 string
certificate tls.Certificate
apnsConnection *tls.Conn
chErrResponse chan *errResponse
chSend chan *opSend
chSignal chan int
chConnect chan chan error
chConnectionErr chan *tls.Conn
sentQ *pnQueue
counter int32
}
// BareClient can be used to set the contents of your
// certificate and key blocks manually.
func BareClient(gateway, certificateBase64, keyBase64 string) (c *Client) {
c = create(gateway)
c.CertificateBase64 = certificateBase64
c.KeyBase64 = keyBase64
return
}
// NewClient assumes you'll be passing in paths that
// point to your certificate and key.
func NewClient(gateway, certificateFile, keyFile string) (c *Client) {
c = create(gateway)
c.CertificateFile = certificateFile
c.KeyFile = keyFile
return
}
func create(gateway string) (c *Client) {
c = new(Client)
c.Gateway = gateway
c.chErrResponse = make(chan *errResponse, 10)
c.chSend = make(chan *opSend)
c.chConnect = make(chan chan error)
c.chConnectionErr = make(chan *tls.Conn)
c.chSignal = make(chan int)
c.sentQ = newPnQueue(MAX_SEND_Q)
return c
}
func (p *Client) run() {
defer log.Printf("client %p stop running \f", p)
for {
select {
case res := <-p.chErrResponse:
p.handleErrResponse(res)
case op := <-p.chSend:
op.ChErr <- p.innerSend(op.Pn)
case ch := <-p.chConnect:
if p.apnsConnection == nil {
ch <- p.openConnection()
} else {
ch <- nil
}
case conn := <-p.chConnectionErr:
if p.apnsConnection == conn {
p.innerClose()
} else {
go conn.Close()
}
case <-p.chSignal:
p.innerClose()
//final stop
return
}
}
}
func (client *Client) handleErrResponse(res *errResponse) {
if res.Command == 0 {
//no error
return
}
errPn, reSend := client.sentQ.Tail(res.Identifier)
log.Printf("handle err response %d, %##v\n", res.Identifier, errPn)
if errPn == nil {
log.Println("[warn] maybe MAX_SEND_Q is too short:", MAX_SEND_Q)
return
}
go func() {
ErrChannel <- &SendErr{Pn: errPn, Res: res}
}()
client.sentQ.Clear()
if len(reSend) == 0 {
return
}
go func(l []*PushNotification) {
for _, pn := range l {
if err := client.Send(pn); err != nil {
log.Println("re-send err", err, pn.Identifier)
}
}
}(reSend)
}
func (client *Client) Send(pn *PushNotification) error {
op := &opSend{Pn: pn, ChErr: make(chan error)}
client.chSend <- op
return <-op.ChErr
}
// Send connects to the APN service and sends your push notification.
// Remember that if the submission is successful, Apple won't reply.
func (client *Client) innerSend(pn *PushNotification) error {
pn.Identifier = client.counter
client.counter = (client.counter + 1) % IdentifierUbound
payload, err := pn.ToBytes()
if err != nil {
return err
}
err = client.connectAndWrite(payload)
if err == nil {
client.sentQ.Append(pn)
} else {
client.apnsConnection = nil
go func() {
ErrChannel <- &SendErr{Pn: pn, Res: nil}
}()
}
return err
}
func (client *Client) Connect() error {
go client.run()
op := make(chan error)
client.chConnect <- op
return <-op
}
// ConnectAndWrite establishes the connection to Apple and handles the
// transmission of your push notification, as well as waiting for a reply.
//
// In lieu of a timeout (which would be available in Go 1.1)
// we use a timeout channel pattern instead. We start two goroutines,
// one of which just sleeps for TimeoutSeconds seconds, while the other
// waits for a response from the Apple servers.
//
// Whichever channel puts data on first is the "winner". As such, it's
// possible to get a false positive if Apple takes a long time to respond.
// It's probably not a deal-breaker, but something to be aware of.
func (client *Client) connectAndWrite(payload []byte) error {
if client.apnsConnection == nil {
if err := client.openConnection(); err != nil {
return err
}
}
if err := client.apnsConnection.SetWriteDeadline(time.Now().Add(TIME_OUT)); err != nil {
return err
}
_, err := client.apnsConnection.Write(payload)
if err != nil {
log.Println("write error ", err, "try again")
// if err != io.EOF && err.Error() != "use of closed network connection" && err != syscall.EPIPE {
// return err
// }
// log.Println("try again")
// If the connection is closed, reconnect
if err := client.openConnection(); err != nil {
return err
}
if err := client.apnsConnection.SetWriteDeadline(time.Now().Add(TIME_OUT)); err != nil {
return err
}
if _, err := client.apnsConnection.Write(payload); err != nil {
return err
}
}
return err
}
// Opens a connection to the Apple APNS server
// The connection is created and persisted to the client's apnsConnection property
// to save on the overhead of the crypto libraries.
func (client *Client) openConnection() error {
log.Printf("open connection %p\n", client)
err := client.getCertificate()
if err != nil {
log.Println("cert err", err)
return err
}
conf := &tls.Config{
Certificates: []tls.Certificate{client.certificate},
ServerName: strings.Split(client.Gateway, ":")[0],
MinVersion: tls.VersionTLS10,
}
conn, err := net.DialTimeout("tcp", client.Gateway, TIME_OUT)
if err != nil {
log.Println("open connection err", err)
return err
}
tlsConn := tls.Client(conn, conf)
//add handshake timeout
if err := tlsConn.SetDeadline(time.Now().Add(TIME_OUT)); err != nil {
return err
}
err = tlsConn.Handshake()
if err != nil {
log.Println("tls handshake err", err)
return err
}
//clear read timeout
if err := tlsConn.SetReadDeadline(time.Time{}); err != nil {
return err
}
client.apnsConnection = tlsConn
go read(client, tlsConn)
return nil
}
func (p *Client) tryReset(conn *tls.Conn) {
if p.apnsConnection == conn {
p.apnsConnection = nil
}
}
func read(client *Client, conn *tls.Conn) {
buffer := make([]byte, ERR_RESPONSE_LEN)
if _, err := conn.Read(buffer); err != nil {
log.Printf("read err %v, %v, %p\n", err, err == io.EOF, client)
client.chConnectionErr <- conn
return
}
errRsp := &errResponse{
Command: uint8(buffer[0]),
Status: uint8(buffer[1]),
}
if err := binary.Read(bytes.NewBuffer(buffer[2:]), binary.BigEndian, &errRsp.Identifier); err != nil {
log.Println("read identifier err", err)
return
}
if errRsp.Command != ERR_RESPONSE_CMD {
log.Println("unknown err response", buffer)
return
}
errMsg, ok := ApplePushResponses[errRsp.Status]
if !ok {
log.Println("unknown err status", buffer)
return
}
log.Printf("get err response : %##v, %s\n", errRsp, errMsg)
client.chErrResponse <- errRsp
}
// Returns a certificate to use to send the notification.
// The certificate is only created once to save on
// the overhead of the crypto libraries.
func (client *Client) getCertificate() error {
var err error
if client.certificate.PrivateKey == nil {
if len(client.CertificateBase64) == 0 && len(client.KeyBase64) == 0 {
// The user did not specify raw block contents, so check the filesystem.
client.certificate, err = tls.LoadX509KeyPair(client.CertificateFile, client.KeyFile)
} else {
// The user provided the raw block contents, so use that.
client.certificate, err = tls.X509KeyPair([]byte(client.CertificateBase64), []byte(client.KeyBase64))
}
}
return err
}
func (p *Client) Stop() {
p.chSignal <- signal_STOP
}
func (client *Client) innerClose() {
if client.apnsConnection != nil {
go client.apnsConnection.Close()
client.apnsConnection = nil
}
}