-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlApns.go
228 lines (201 loc) · 4.43 KB
/
jlApns.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
package jlApns
// doc: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
import (
"crypto/tls"
"errors"
"log"
"net"
"strings"
"time"
)
const (
StateDisconnect = iota
StateConncting
StateConncted
)
var ApplePushResponses = map[uint8]string{
0: "NO_ERRORS",
1: "PROCESSING_ERROR",
2: "MISSING_DEVICE_TOKEN",
3: "MISSING_TOPIC",
4: "MISSING_PAYLOAD",
5: "INVALID_TOKEN_SIZE",
6: "INVALID_TOPIC_SIZE",
7: "INVALID_PAYLOAD_SIZE",
8: "INVALID_TOKEN",
10: "SHUTDOWN",
255: "UNKNOWN",
}
type APNSession struct {
Gateway string // APNS gateway
CertificateFile string
KeyFile string
CertificateBase64 string
KeyBase64 string
conn net.Conn
tlsConn *tls.Conn
ResponseChan chan *FailInfo
StateChangeChan chan int
state int
}
func (a *APNSession) State() int {
return a.state
}
func (a *APNSession) setState(newState int) {
if a.state != newState {
a.state = newState
if len(a.StateChangeChan) > 8 {
for i := 0; i < 7; i++ {
<-a.StateChangeChan // 倾倒
}
}
a.StateChangeChan <- newState
}
}
func NewAPNSession(gateway, certificateFile, keyFile string, responseChan chan *FailInfo) (a *APNSession) {
a = new(APNSession)
a.Gateway = gateway
a.CertificateFile = certificateFile
a.KeyFile = keyFile
a.state = StateDisconnect
a.ResponseChan = responseChan
a.StateChangeChan = make(chan int, 10)
return
}
// ----------
func (a *APNSession) Send(sendIdentifier int32, payload *Payload, token string, expiration uint32) error {
if payload == nil || len(token) != 32*2 {
return errors.New("Invalid args")
}
if a.state != StateConncted {
return errors.New("Not Connect")
}
payloadData, err := payload.toJsonData()
if err != nil {
return err
}
buf, err := EncodePushNotificationToData(sendIdentifier, expiration, token, payloadData, 10)
if err != nil {
return err
}
_, err = a.tlsConn.Write(buf)
if err != nil {
return err
}
return nil
}
func (a *APNSession) RecvRespnose() {
if a.ResponseChan == nil || a.state != StateConncted {
return
}
buffer := make([]byte, 6, 6)
for {
readCount, err := a.tlsConn.Read(buffer)
if err == nil && buffer[0] == 8 {
log.Printf("send data len:%d\n", readCount)
info := NewFailInfoFromByte(buffer)
if info.Code == 10 {
a.Close()
break
}
if info != nil && a.ResponseChan != nil {
a.ResponseChan <- info
}
} else {
log.Printf("send data err:%v\n", err)
a.Close()
break
}
}
}
// ----------
func (a *APNSession) Close() {
a.setState(StateDisconnect)
if a.conn != nil {
a.conn.Close()
a.conn = nil
}
if a.tlsConn != nil {
a.tlsConn.Close()
a.tlsConn = nil
}
}
func (a *APNSession) Connect() (err error) {
if a.state != StateDisconnect {
if a.state == StateConncted {
return
}
return errors.New("connecting")
}
a.setState(StateConncting)
defer func() {
if err == nil {
a.setState(StateConncted)
} else {
a.setState(StateDisconnect)
}
}()
var cert tls.Certificate
if len(a.CertificateBase64) == 0 || len(a.KeyBase64) == 0 {
// The user did not specify raw block contents, so check the filesystem.
cert, err = tls.LoadX509KeyPair(a.CertificateFile, a.KeyFile)
} else {
// The user provided the raw block contents, so use that.
cert, err = tls.X509KeyPair([]byte(a.CertificateBase64), []byte(a.KeyBase64))
}
if err != nil {
return err
}
gatewayParts := strings.Split(a.Gateway, ":")
if len(gatewayParts) != 2 {
return errors.New("Invalid args")
}
conf := &tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: gatewayParts[0],
}
conncetedChannel := make(chan error, 1)
go func() {
a.conn, err = net.Dial("tcp", a.Gateway)
if err != nil {
a.conn = nil
}
conncetedChannel <- err
}()
select {
case e := <-conncetedChannel:
if e != nil {
err = e
}
case <-time.After(time.Second * 20):
err = errors.New("Timeout connect")
}
if err != nil {
return err
}
go func() {
a.tlsConn = tls.Client(a.conn, conf)
err = a.tlsConn.Handshake()
if err != nil {
a.tlsConn.Close()
a.tlsConn = nil
}
conncetedChannel <- err
}()
select {
case e := <-conncetedChannel:
if e != nil {
err = e
}
case <-time.After(time.Second * 20):
err = errors.New("Timeout Handshake")
}
if err != nil {
return err
}
log.Printf("Connect success\n")
if a.ResponseChan != nil {
go a.RecvRespnose()
}
return
}