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 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 d4d31cb..f593d7b 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() @@ -134,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 96ea7ae..ff07560 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,20 @@ 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)} + LogAccess.Debug("Set http proxy as " + proxy) + + 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) +}