From 146854e381601c53325241ff11fb8b2af017be0b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 12 Jun 2016 15:54:17 +0800 Subject: [PATCH 1/4] fixed #90 Support load p12 format of certificate file. Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 15 ++++++++++++++- gorush/notification_test.go | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) 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) From 377fca0bae5276877a63325c5725186614545faa Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 12 Jun 2016 15:56:13 +0800 Subject: [PATCH 2/4] [ci skip] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 1 + gorush/notification.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d31f28..47b818a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ A push notification server using [Gin](https://github.com/gin-gonic/gin) framewo * Support `/api/stat/app` show notification success and failure counts. * Support `/api/config` show your [YAML](https://en.wikipedia.org/wiki/YAML) config. * Support store app stat to memory, [Redis](http://redis.io/) or [BoltDB](https://github.com/boltdb/bolt). +* Support `p12` or `pem` formtat of iOS certificate file. See the [YAML config example](config/config.yml): diff --git a/gorush/notification.go b/gorush/notification.go index eb7d12e..ba62c99 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -154,8 +154,6 @@ func InitAPNSClient() error { 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()) From 6be5e24833ec070bc428b73117eff4225404e22c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 12 Jun 2016 19:09:41 +0800 Subject: [PATCH 3/4] rename pem_path to key_path. Signed-off-by: Bo-Yi Wu --- README.md | 2 +- config/config.go | 4 ++-- config/config.yml | 2 +- gorush.go | 8 ++++---- gorush/notification.go | 8 ++++---- gorush/notification_test.go | 18 +++++++++--------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 47b818a..b4f4c56 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ android: ios: enabled: false - pem_path: "key.pem" + key_path: "key.pem" password: "" # certificate password, default as empty string. production: false diff --git a/config/config.go b/config/config.go index c3d2052..f733ae7 100644 --- a/config/config.go +++ b/config/config.go @@ -45,7 +45,7 @@ type SectionAndroid struct { // SectionIos is sub seciont of config. type SectionIos struct { Enabled bool `yaml:"enabled"` - PemPath string `yaml:"pem_path"` + KeyPath string `yaml:"key_path"` Password string `yaml:"password"` Production bool `yaml:"production"` } @@ -106,7 +106,7 @@ func BuildDefaultPushConf() ConfYaml { // iOS conf.Ios.Enabled = false - conf.Ios.PemPath = "key.pem" + conf.Ios.KeyPath = "key.pem" conf.Ios.Password = "" conf.Ios.Production = false diff --git a/config/config.yml b/config/config.yml index a6387d1..2fd36a7 100644 --- a/config/config.yml +++ b/config/config.yml @@ -20,7 +20,7 @@ android: ios: enabled: false - pem_path: "key.pem" + key_path: "key.pem" password: "" production: false diff --git a/gorush.go b/gorush.go index 1aa0425..1af4b83 100644 --- a/gorush.go +++ b/gorush.go @@ -62,8 +62,8 @@ func main() { flag.BoolVar(&showVersion, "v", false, "Print version information.") flag.StringVar(&configFile, "c", "", "Configuration file.") flag.StringVar(&configFile, "config", "", "Configuration file.") - flag.StringVar(&opts.Ios.PemPath, "i", "", "iOS certificate key file path") - flag.StringVar(&opts.Ios.PemPath, "pem", "", "iOS certificate key file path") + flag.StringVar(&opts.Ios.KeyPath, "i", "", "iOS certificate key file path") + flag.StringVar(&opts.Ios.KeyPath, "pem", "", "iOS certificate key file path") flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush") flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush") flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush") @@ -110,8 +110,8 @@ func main() { } } - if opts.Ios.PemPath != "" { - gorush.PushConf.Ios.PemPath = opts.Ios.PemPath + if opts.Ios.KeyPath != "" { + gorush.PushConf.Ios.KeyPath = opts.Ios.KeyPath } if opts.Ios.Password != "" { diff --git a/gorush/notification.go b/gorush/notification.go index ba62c99..7ea6407 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -123,7 +123,7 @@ func CheckPushConf() error { } if PushConf.Ios.Enabled { - if PushConf.Ios.PemPath == "" { + if PushConf.Ios.KeyPath == "" { return errors.New("Missing iOS certificate path") } } @@ -141,15 +141,15 @@ func CheckPushConf() error { func InitAPNSClient() error { if PushConf.Ios.Enabled { var err error - ext := filepath.Ext(PushConf.Ios.PemPath) + ext := filepath.Ext(PushConf.Ios.KeyPath) LogAccess.Debug("certificate ext is ", ext) switch ext { case ".p12": - CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.PemPath, PushConf.Ios.Password) + CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.KeyPath, PushConf.Ios.Password) case ".pem": - CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemPath, PushConf.Ios.Password) + CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.KeyPath, PushConf.Ios.Password) default: err = errors.New("Wrong Certificate key extension.") } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 176dd6d..a376975 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -26,7 +26,7 @@ func TestMissingIOSCertificate(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "" + PushConf.Ios.KeyPath = "" err := CheckPushConf() @@ -53,7 +53,7 @@ func TestCorrectConf(t *testing.T) { PushConf.Android.APIKey = "xxxxx" PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "xxxxx" + PushConf.Ios.KeyPath = "xxxxx" err := CheckPushConf() @@ -222,7 +222,7 @@ func TestPushToIOS(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() InitAppStatus() @@ -332,7 +332,7 @@ func TestSenMultipleNotifications(t *testing.T) { InitWorkers(2, 2) PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() PushConf.Android.Enabled = true @@ -365,7 +365,7 @@ func TestDisabledAndroidNotifications(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() PushConf.Android.Enabled = false @@ -398,7 +398,7 @@ func TestDisabledIosNotifications(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = false - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() PushConf.Android.Enabled = true @@ -431,7 +431,7 @@ func TestWrongIosCertificateExt(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "test" + PushConf.Ios.KeyPath = "test" err := InitAPNSClient() assert.Error(t, err) @@ -442,7 +442,7 @@ func TestAPNSClientDevHost(t *testing.T) { PushConf = config.BuildDefaultPushConf() PushConf.Ios.Enabled = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.p12" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.p12" InitAPNSClient() assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host) @@ -453,7 +453,7 @@ func TestAPNSClientProdHost(t *testing.T) { PushConf.Ios.Enabled = true PushConf.Ios.Production = true - PushConf.Ios.PemPath = "../certificate/certificate-valid.pem" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" InitAPNSClient() assert.Equal(t, apns2.HostProduction, ApnsClient.Host) From 17a089e89ef65159d5ed5d5d2ff4e3ec43a67b75 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 12 Jun 2016 19:25:06 +0800 Subject: [PATCH 4/4] update gorush cli. Signed-off-by: Bo-Yi Wu --- README.md | 6 +++--- gorush.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b4f4c56..7e9e530 100644 --- a/README.md +++ b/README.md @@ -114,13 +114,13 @@ Server Options: -m, --message Notification message -t, --token Notification token iOS Options: - -i, --pem certificate key file path + -i, --key certificate key file path -P, --password certificate key password --topic iOS topic --ios enabled iOS (default: false) --production iOS production mode (default: false) Android Options: - -k, --key Android API Key + -k, --apikey Android API Key --android enabled android (default: false) Common Options: -h, --help Show this message @@ -148,7 +148,7 @@ $ gorush -ios -m="your message" -i="your certificate path" -t="device token" -to ``` * `-m`: Notification message. -* `-i`: Apple Push Notification Certificate path (`pem` file). +* `-i`: Apple Push Notification Certificate path (`pem` or `p12` file). * `-t`: Device token. * `-topic`: The topic of the remote notification. * `-password`: The certificate password. diff --git a/gorush.go b/gorush.go index 1af4b83..0245142 100644 --- a/gorush.go +++ b/gorush.go @@ -30,13 +30,13 @@ Server Options: -m, --message Notification message -t, --token Notification token iOS Options: - -i, --pem certificate key file path + -i, --key certificate key file path -P, --password certificate key password --topic iOS topic --ios enabled iOS (default: false) --production iOS production mode (default: false) Android Options: - -k, --key Android API Key + -k, --apikey Android API Key --android enabled android (default: false) Common Options: -h, --help Show this message @@ -63,11 +63,11 @@ func main() { flag.StringVar(&configFile, "c", "", "Configuration file.") flag.StringVar(&configFile, "config", "", "Configuration file.") flag.StringVar(&opts.Ios.KeyPath, "i", "", "iOS certificate key file path") - flag.StringVar(&opts.Ios.KeyPath, "pem", "", "iOS certificate key file path") + flag.StringVar(&opts.Ios.KeyPath, "key", "", "iOS certificate key file path") flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush") flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush") flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush") - flag.StringVar(&opts.Android.APIKey, "key", "", "Android api key configuration for gorush") + flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush") flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush") flag.StringVar(&opts.Core.Port, "port", "", "port number for gorush") flag.StringVar(&token, "t", "", "token string")