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) +}