This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
apns.go
184 lines (157 loc) · 3.84 KB
/
apns.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
package apns
import (
"bytes"
"crypto/tls"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"strings"
"time"
)
type Notification struct {
DeviceToken string
Identifier uint32
ExpireAfterSeconds int
Payload *Payload
}
// An Apn contain a ErrorChan channle when connected to apple server. When a notification sent wrong, you can get the error infomation from this channel.
type Apn struct {
ErrorChan <-chan error
server string
conf *tls.Config
conn *tls.Conn
timeout time.Duration
sendChan chan *sendArg
errorChan chan error
}
// New Apn with cert_filename and key_filename.
func New(cert_filename, key_filename, server string, timeout time.Duration) (*Apn, error) {
echan := make(chan error)
cert, err := tls.LoadX509KeyPair(cert_filename, key_filename)
if err != nil {
return nil, err
}
nameport := strings.Split(server, ":")
certificate := []tls.Certificate{cert}
conf := &tls.Config{
Certificates: certificate,
ServerName: nameport[0],
}
ret := &Apn{
ErrorChan: echan,
server: server,
conf: conf,
timeout: timeout,
sendChan: make(chan *sendArg),
errorChan: echan,
}
go sendLoop(ret)
return ret, err
}
func (a *Apn) GetErrorChan() <-chan error {
return a.ErrorChan
}
// Send a notification to iOS
func (a *Apn) Send(notification *Notification) error {
err := make(chan error)
arg := &sendArg{
n: notification,
err: err,
}
a.sendChan <- arg
return <-err
}
type sendArg struct {
n *Notification
err chan<- error
}
func (a *Apn) Close() error {
if a.conn == nil {
return nil
}
conn := a.conn
a.conn = nil
return conn.Close()
}
func (a *Apn) connect() (<-chan int, error) {
// make sure last readError(...) will fail when reading.
err := a.Close()
if err != nil {
return nil, fmt.Errorf("close last connection failed: %s", err)
}
conn, err := net.Dial("tcp", a.server)
if err != nil {
return nil, fmt.Errorf("connect to server error: %d", err)
}
var client_conn *tls.Conn = tls.Client(conn, a.conf)
err = client_conn.Handshake()
if err != nil {
return nil, fmt.Errorf("handshake server error: %s", err)
}
a.conn = client_conn
quit := make(chan int)
go readError(client_conn, quit, a.errorChan)
return quit, nil
}
func (a *Apn) send(notification *Notification) error {
tokenbin, err := hex.DecodeString(notification.DeviceToken)
if err != nil {
return fmt.Errorf("convert token to hex error: %s", err)
}
payloadbyte, _ := json.Marshal(notification.Payload)
expiry := time.Now().Add(time.Duration(notification.ExpireAfterSeconds) * time.Second).Unix()
buffer := bytes.NewBuffer([]byte{})
binary.Write(buffer, binary.BigEndian, uint8(1))
binary.Write(buffer, binary.BigEndian, uint32(notification.Identifier))
binary.Write(buffer, binary.BigEndian, uint32(expiry))
binary.Write(buffer, binary.BigEndian, uint16(len(tokenbin)))
binary.Write(buffer, binary.BigEndian, tokenbin)
binary.Write(buffer, binary.BigEndian, uint16(len(payloadbyte)))
binary.Write(buffer, binary.BigEndian, payloadbyte)
pushPackage := buffer.Bytes()
_, err = a.conn.Write(pushPackage)
if err != nil {
return fmt.Errorf("write socket error: %s", err)
}
return nil
}
func sendLoop(apn *Apn) {
for {
arg := <-apn.sendChan
quit, err := apn.connect()
if err != nil {
arg.err <- err
continue
}
arg.err <- apn.send(arg.n)
for connected := true; connected; {
select {
case <-quit:
connected = false
case <-time.After(apn.timeout):
connected = false
case arg := <-apn.sendChan:
arg.err <- apn.send(arg.n)
}
}
err = apn.Close()
if err != nil {
e := NewNotificationError(nil, err)
apn.errorChan <- e
}
}
}
func readError(conn *tls.Conn, quit chan<- int, c chan<- error) {
p := make([]byte, 6, 6)
for {
n, err := conn.Read(p)
e := NewNotificationError(p[:n], err)
c <- e
if err != nil {
quit <- 1
return
}
}
}