forked from Coccodrillo/apns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_binary_provider_api.go
More file actions
275 lines (239 loc) · 6.34 KB
/
client_binary_provider_api.go
File metadata and controls
275 lines (239 loc) · 6.34 KB
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
package apns
import (
"crypto/tls"
"errors"
"golang.org/x/net/context"
"net"
"net/url"
"strings"
// "sync"
"time"
)
// the binary provider api tends to be of the following format, url wise:
//
// gateway.push.apple.com:2195
// gateway.sandbox.push.apple.com:2195
func getURL(rawurl string) (u *url.URL, err error) {
if !strings.HasPrefix(rawurl, "https://") {
rawurl = "https://" + rawurl
}
u, err = url.Parse(rawurl)
return
}
// GatewayClient 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 GatewayClient struct {
gateway *url.URL
certificateFile string
certificateBase64 string
keyFile string
keyBase64 string
idleConn chan *tls.Conn
connCount int
maxIdle int
maxOpen int
maxLifetime time.Duration
}
const (
defaultMaxIdle = 10
defaultMaxOpen = 20
)
// BareGatewayClient can be used to set the contents of your
// certificate and key blocks manually.
func BareGatewayClient(gateway, certificateBase64, keyBase64 string) (c Client) {
uri, err := getURL(gateway)
if err != nil {
return nil
}
client := &GatewayClient{
gateway: uri,
certificateBase64: certificateBase64,
keyBase64: keyBase64,
maxIdle: defaultMaxIdle,
maxOpen: defaultMaxOpen,
idleConn: make(chan *tls.Conn, defaultMaxIdle),
}
return client
}
// NewGatewayClient assumes you'll be passing in paths that
// point to your certificate and key.
func NewGatewayClient(gateway, certificateFile, keyFile string) (c Client, err error) {
uri, err := getURL(gateway)
if err != nil {
return nil, err
}
client := &GatewayClient{
gateway: uri,
certificateFile: certificateFile,
keyFile: keyFile,
maxIdle: defaultMaxIdle,
maxOpen: defaultMaxOpen,
idleConn: make(chan *tls.Conn, defaultMaxIdle),
}
return client, nil
}
// SetMaxConns will set the maximum number of connections usable within the
// connection pool
func (client *GatewayClient) SetMaxConns(m int) {
client.maxOpen = m
}
// SetMaxIdleConns will set the maximum number of Idle Connections to remain
// open at a given time
func (client *GatewayClient) SetMaxIdleConns(m int) {
client.maxIdle = m
previousIdleConns := client.idleConn
client.idleConn = make(chan *tls.Conn, m)
i := 0
for c := range previousIdleConns {
if i < m {
client.idleConn <- c
i++
} else {
// close this idle connection
client.connCount--
c.Close()
}
}
close(previousIdleConns)
}
const (
getConnAttemptCount int = 2
)
// gets a connection from the pool, or opens a new one
func (client *GatewayClient) getConn() (*tls.Conn, error) {
vlogf("attempting to get a connection")
var i = 0
for i < getConnAttemptCount {
select {
case c := <-client.idleConn:
vlogf("Found an idle connection")
return c, nil
default:
if client.connCount < client.maxOpen {
c, err := client.dial()
if err != nil {
// we had an issue opening a connection
i++
vlogf("Unable to dial: %s", err)
continue
}
return c, nil
}
}
time.Sleep(time.Millisecond)
}
err := errors.New("Unable to retrieve a connection")
vlogf("error: %s", err)
return nil, err
}
// put's the connection back in the pool
func (client *GatewayClient) putConn(c *tls.Conn) {
client.idleConn <- c
}
// dial will attempt to establish a new tls.Conn
func (client *GatewayClient) dial() (*tls.Conn, error) {
vlogf("Attempting to Open a new connection")
var cert tls.Certificate
var err error
if len(client.certificateBase64) == 0 && len(client.keyBase64) == 0 {
// The user did not specify raw block contents, so check the filesystem.
cert, err = tls.LoadX509KeyPair(client.certificateFile, client.keyFile)
} else {
// The user provided the raw block contents, so use that.
cert, err = tls.X509KeyPair([]byte(client.certificateBase64), []byte(client.keyBase64))
}
if err != nil {
vlogf("Error loading certs: %s", err)
return nil, err
}
var host, port string
if strings.Contains(client.gateway.Host, ":") {
host, port, err = net.SplitHostPort(client.gateway.Host)
if err != nil {
vlogf("Erro splitting Host and port: %s\n", err)
return nil, err
}
}
if port == "" {
port = "2195"
}
conf := &tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: host,
}
conn, err := tls.Dial("tcp", host+":"+port, conf)
if err != nil {
vlogf("Error opening connection: %s", err)
return nil, err
}
client.connCount++
return conn, nil
}
// Send connects to the APN service and sends your push notification.
// Remember that if the submission is successful, Apple won't reply.
func (client *GatewayClient) Send(pn *PushNotification) (resp *PushNotificationResponse) {
resp = new(PushNotificationResponse)
conn, err := client.getConn()
if err != nil {
vlogf("Error retrieving a connection: %s", err)
return &PushNotificationResponse{
Error: err,
Success: false,
}
}
bytes, err := pn.ToBytes()
if err != nil {
vlogf("Error converting payload: %s", err)
resp.Success = false
resp.Error = err
return
}
w := 0
for w < len(bytes) {
i, err := conn.Write(bytes[w:])
if err != nil {
vlogf("Error writing to connection: %s", err)
return &PushNotificationResponse{
Error: err,
Success: false,
}
}
w += i
time.Sleep(time.Millisecond)
}
defer client.putConn(conn)
responseChannel := make(chan []byte, 1)
defer close(responseChannel)
go func() {
b := make([]byte, 6)
r := 0
for r < len(b) {
i, err := conn.Read(b[r:])
if err != nil {
vlogf("Unable to Read from Connection: %s\n", err)
return // well... crap... must have some big error.
}
r += i
}
responseChannel <- b
}()
ctx, _ := context.WithTimeout(context.Background(), TimeoutSeconds*time.Second)
select {
case b := <-responseChannel:
vlogf("Apple Binary API Error: %s", AppleResponseCode(b[1]))
resp.AppleResponse = AppleResponseCode(b[1])
resp.Error = AppleResponseCode(b[1])
resp.Success = false
case <-ctx.Done():
// deadline exceeded...
}
return
}