@@ -13,6 +13,7 @@ import (
13
13
apns "github.com/sideshow/apns2"
14
14
"github.com/sideshow/apns2/certificate"
15
15
"github.com/sideshow/apns2/payload"
16
+ "github.com/sideshow/apns2/token"
16
17
"golang.org/x/net/http2"
17
18
)
18
19
@@ -31,13 +32,68 @@ func NewAppleNotificationServer(settings ApplePushSettings, logger *Logger, metr
31
32
}
32
33
}
33
34
35
+ func (me * AppleNotificationServer ) setupProxySettings (appleCert * tls.Certificate ) bool {
36
+ // Override the native transport.
37
+ proxyServer := getProxyServer ()
38
+ if proxyServer != "" {
39
+ transport := & http.Transport {
40
+ Proxy : func (request * http.Request ) (* url.URL , error ) {
41
+ return url .Parse (proxyServer )
42
+ },
43
+ IdleConnTimeout : apns .HTTPClientTimeout ,
44
+ }
45
+
46
+ if appleCert != nil {
47
+ transport .TLSClientConfig = & tls.Config {
48
+ Certificates : []tls.Certificate {* appleCert },
49
+ }
50
+ }
51
+
52
+ err := http2 .ConfigureTransport (transport )
53
+ if err != nil {
54
+ me .logger .Errorf ("Transport Error: %v" , err )
55
+ return false
56
+ }
57
+
58
+ me .AppleClient .HTTPClient .Transport = transport
59
+ }
60
+
61
+ if appleCert != nil {
62
+ me .logger .Infof ("Initializing apple notification server for type=%v with PEM certificate" , me .ApplePushSettings .Type )
63
+ } else {
64
+ me .logger .Infof ("Initializing apple notification server for type=%v with AuthKey" , me .ApplePushSettings .Type )
65
+ }
66
+
67
+ return true
68
+ }
69
+
34
70
func (me * AppleNotificationServer ) Initialize () bool {
35
- me .logger .Infof ("Initializing apple notification server for type=%v" , me .ApplePushSettings .Type )
71
+ if me .ApplePushSettings .AppleAuthKeyFile != "" && me .ApplePushSettings .AppleAuthKeyID != "" && me .ApplePushSettings .AppleTeamID != "" {
72
+ authKey , err := token .AuthKeyFromFile (me .ApplePushSettings .AppleAuthKeyFile )
73
+ if err != nil {
74
+ me .logger .Panicf ("Failed to initialize apple notification service with AuthKey file err=%v " , err )
75
+ }
76
+
77
+ appleToken := & token.Token {
78
+ AuthKey : authKey ,
79
+ KeyID : me .ApplePushSettings .AppleAuthKeyID ,
80
+ TeamID : me .ApplePushSettings .AppleTeamID ,
81
+ }
82
+
83
+ if me .ApplePushSettings .ApplePushUseDevelopment {
84
+ me .AppleClient = apns .NewTokenClient (appleToken ).Development ()
85
+ } else {
86
+ me .AppleClient = apns .NewTokenClient (appleToken ).Production ()
87
+ }
88
+
89
+ // Override the native transport.
90
+ return me .setupProxySettings (nil )
91
+ }
36
92
37
93
if me .ApplePushSettings .ApplePushCertPrivate != "" {
38
94
appleCert , appleCertErr := certificate .FromPemFile (me .ApplePushSettings .ApplePushCertPrivate , me .ApplePushSettings .ApplePushCertPassword )
39
95
if appleCertErr != nil {
40
- me .logger .Panicf ("Failed to load the apple pem cert err=%v for type=%v" , appleCertErr , me .ApplePushSettings .Type )
96
+ me .logger .Panicf ("Failed to initialize apple notification service with pem cert err=%v for type=%v" , appleCertErr , me .ApplePushSettings .Type )
41
97
return false
42
98
}
43
99
@@ -48,33 +104,11 @@ func (me *AppleNotificationServer) Initialize() bool {
48
104
}
49
105
50
106
// Override the native transport.
51
- proxyServer := getProxyServer ()
52
- if proxyServer != "" {
53
- tlsConfig := & tls.Config {
54
- Certificates : []tls.Certificate {appleCert },
55
- }
56
-
57
- transport := & http.Transport {
58
- TLSClientConfig : tlsConfig ,
59
- Proxy : func (request * http.Request ) (* url.URL , error ) {
60
- return url .Parse (proxyServer )
61
- },
62
- IdleConnTimeout : apns .HTTPClientTimeout ,
63
- }
64
- err := http2 .ConfigureTransport (transport )
65
- if err != nil {
66
- me .logger .Errorf ("Transport Error: %v" , err )
67
- return false
68
- }
69
-
70
- me .AppleClient .HTTPClient .Transport = transport
71
- }
72
-
73
- return true
74
- } else {
75
- me .logger .Errorf ("Apple push notifications not configured. Missing ApplePushCertPrivate. for type=%v" , me .ApplePushSettings .Type )
76
- return false
107
+ return me .setupProxySettings (& appleCert )
77
108
}
109
+
110
+ me .logger .Errorf ("Apple push notifications not configured. Missing ApplePushCertPrivate. for type=%v" , me .ApplePushSettings .Type )
111
+ return false
78
112
}
79
113
80
114
func (me * AppleNotificationServer ) SendNotification (msg * PushNotification ) PushResponse {
0 commit comments