diff --git a/gorush/notification.go b/gorush/notification.go index ef9a463..eb7d12e 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -7,6 +7,7 @@ import ( apns "github.com/sideshow/apns2" "github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/payload" + "path/filepath" "time" ) @@ -140,8 +141,20 @@ func CheckPushConf() error { func InitAPNSClient() error { if PushConf.Ios.Enabled { var err error + ext := filepath.Ext(PushConf.Ios.PemPath) - CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) + LogAccess.Debug("certificate ext is ", ext) + + switch ext { + case ".p12": + CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.PemPath, PushConf.Ios.Password) + case ".pem": + CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) + default: + err = errors.New("Wrong Certificate key extension.") + } + + // CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) if err != nil { LogError.Error("Cert Error:", err.Error()) diff --git a/gorush/notification_test.go b/gorush/notification_test.go index e6cb466..176dd6d 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -427,7 +427,7 @@ func TestDisabledIosNotifications(t *testing.T) { assert.Equal(t, 2, count) } -func TestMissingIosCertificate(t *testing.T) { +func TestWrongIosCertificateExt(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true @@ -435,13 +435,14 @@ func TestMissingIosCertificate(t *testing.T) { err := InitAPNSClient() assert.Error(t, err) + assert.Equal(t, "Wrong Certificate key extension.", err.Error()) } func TestAPNSClientDevHost(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.PemPath = "../certificate/certificate-valid.p12" InitAPNSClient() assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)