From e784ae538a0183c1f071f7e2548053862dfbdfe6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 29 Jul 2016 08:48:24 +0800 Subject: [PATCH 1/3] Support set http proxy for gorush CLI. only working for GCM protocol. Signed-off-by: Bo-Yi Wu --- gorush.go | 14 ++++++++++++++ gorush/notification.go | 15 +++++++++++++++ gorush/notification_test.go | 13 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/gorush.go b/gorush.go index d4d31cb..1199958 100644 --- a/gorush.go +++ b/gorush.go @@ -30,6 +30,7 @@ Server Options: -c, --config Configuration file -m, --message Notification message -t, --token Notification token + --proxy Proxy URL (only for GCM) iOS Options: -i, --key certificate key file path -P, --password certificate key password @@ -58,6 +59,7 @@ func main() { var topic string var message string var token string + var proxy string flag.BoolVar(&showVersion, "version", false, "Print version information.") flag.BoolVar(&showVersion, "v", false, "Print version information.") @@ -79,6 +81,7 @@ func main() { flag.BoolVar(&opts.Ios.Enabled, "ios", false, "send ios notification") flag.BoolVar(&opts.Ios.Production, "production", false, "production mode in iOS") flag.StringVar(&topic, "topic", "", "apns topic in iOS") + flag.StringVar(&proxy, "proxy", "", "http proxy url") flag.Usage = usage flag.Parse() @@ -100,6 +103,17 @@ func main() { // set default parameters. gorush.PushConf = config.BuildDefaultPushConf() + // set http proxy for GCM + if proxy != "" { + err = gorush.SetProxy(proxy) + + if err != nil { + log.Printf("Set Proxy error: '%v'", err) + + return + } + } + // load user define config. if configFile != "" { gorush.PushConf, err = config.LoadConfYaml(configFile) diff --git a/gorush/notification.go b/gorush/notification.go index 96ea7ae..ec061de 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -7,6 +7,8 @@ import ( apns "github.com/sideshow/apns2" "github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/payload" + "net/http" + "net/url" "path/filepath" "time" ) @@ -116,6 +118,19 @@ func CheckMessage(req PushNotification) error { return nil } +func SetProxy(proxy string) error { + + proxyUrl, err := url.ParseRequestURI(proxy) + + if err != nil { + return err + } + + http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)} + + return nil +} + // CheckPushConf provide check your yml config. func CheckPushConf() error { if !PushConf.Ios.Enabled && !PushConf.Android.Enabled { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index a376975..71f23e8 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -542,3 +542,16 @@ func TestCheckAndroidMessage(t *testing.T) { success := PushToAndroid(req) assert.False(t, success) } + +func TestSetProxyURL(t *testing.T) { + + err := SetProxy("87.236.233.92:8080") + assert.Error(t, err) + assert.Equal(t, "parse 87.236.233.92:8080: invalid URI for request", err.Error()) + + err = SetProxy("a.html") + assert.Error(t, err) + + err = SetProxy("http://87.236.233.92:8080") + assert.NoError(t, err) +} From 4a431243df7603f559d468454556ed1d5dbf0d78 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 29 Jul 2016 09:38:06 +0800 Subject: [PATCH 2/3] Initial proxy setting for web server. Signed-off-by: Bo-Yi Wu --- config/config.go | 2 ++ config/config.yml | 1 + config/config_test.go | 2 ++ gorush.go | 26 +++++++++++++++----------- gorush/notification.go | 1 + 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 07b9b1a..fa1b728 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,7 @@ type SectionCore struct { SSL bool `yaml:"ssl"` CertPath string `yaml:"cert_path"` KeyPath string `yaml:"key_path"` + HTTPProxy string `yaml:"http_proxy"` } // SectionAPI is sub seciont of config. @@ -94,6 +95,7 @@ func BuildDefaultPushConf() ConfYaml { conf.Core.CertPath = "cert.pem" conf.Core.KeyPath = "key.pem" conf.Core.MaxNotification = 100 + conf.Core.HTTPProxy = "" // Api conf.API.PushURI = "/api/push" diff --git a/config/config.yml b/config/config.yml index eaa991e..da5e334 100644 --- a/config/config.yml +++ b/config/config.yml @@ -7,6 +7,7 @@ core: ssl: false cert_path: "cert.pem" key_path: "key.pem" + http_proxy: "" api: push_uri: "/api/push" diff --git a/config/config_test.go b/config/config_test.go index e6ace0b..4e10842 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -62,6 +62,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() { assert.Equal(suite.T(), "cert.pem", suite.ConfGorushDefault.Core.CertPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Core.KeyPath) assert.Equal(suite.T(), 100, suite.ConfGorushDefault.Core.MaxNotification) + assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.HTTPProxy) // Api assert.Equal(suite.T(), "/api/push", suite.ConfGorushDefault.API.PushURI) @@ -107,6 +108,7 @@ func (suite *ConfigTestSuite) TestValidateConf() { assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath) assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath) assert.Equal(suite.T(), 100, suite.ConfGorush.Core.MaxNotification) + assert.Equal(suite.T(), "", suite.ConfGorush.Core.HTTPProxy) // Api assert.Equal(suite.T(), "/api/push", suite.ConfGorush.API.PushURI) diff --git a/gorush.go b/gorush.go index 1199958..f593d7b 100644 --- a/gorush.go +++ b/gorush.go @@ -103,17 +103,6 @@ func main() { // set default parameters. gorush.PushConf = config.BuildDefaultPushConf() - // set http proxy for GCM - if proxy != "" { - err = gorush.SetProxy(proxy) - - if err != nil { - log.Printf("Set Proxy error: '%v'", err) - - return - } - } - // load user define config. if configFile != "" { gorush.PushConf, err = config.LoadConfYaml(configFile) @@ -148,6 +137,21 @@ func main() { return } + // set http proxy for GCM + if proxy != "" { + err = gorush.SetProxy(proxy) + + if err != nil { + gorush.LogError.Fatal("Set Proxy error: ", err) + } + } else if gorush.PushConf.Core.HTTPProxy != "" { + err = gorush.SetProxy(gorush.PushConf.Core.HTTPProxy) + + if err != nil { + gorush.LogError.Fatal("Set Proxy error: ", err) + } + } + // send android notification if opts.Android.Enabled { gorush.PushConf.Android.Enabled = opts.Android.Enabled diff --git a/gorush/notification.go b/gorush/notification.go index ec061de..ff07560 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -127,6 +127,7 @@ func SetProxy(proxy string) error { } http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)} + LogAccess.Debug("Set http proxy as " + proxy) return nil } From d4942853c41f30780cefb6df7ac1017230317fbb Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 29 Jul 2016 09:45:42 +0800 Subject: [PATCH 3/3] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 264e09a..2898a68 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ A push notification micro server using [Gin](https://github.com/gin-gonic/gin) f * 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. * Support `/sys/stats` show response time, status code count, etc. +* Support for HTTP proxy to Google server (GCM). See the [YAML config example](config/config.yml): @@ -61,6 +62,7 @@ core: ssl: false cert_path: "cert.pem" key_path: "key.pem" + http_proxy: "" # only working for GCM server api: push_uri: "/api/push" @@ -134,6 +136,7 @@ Server Options: -c, --config Configuration file -m, --message Notification message -t, --token Notification token + --proxy Proxy URL (only for GCM) iOS Options: -i, --key certificate key file path -P, --password certificate key password @@ -159,6 +162,7 @@ $ gorush -android -m="your message" -k="API Key" -t="Device token" * `-m`: Notification message. * `-k`: [Google cloud message](https://developers.google.com/cloud-messaging/) api key * `-t`: Device token. +* `--proxy`: Set http proxy url. (only working for GCM) ### Send iOS notification