Support set http proxy for gorush CLI.

only working for GCM protocol.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-07-29 08:48:24 +08:00
parent 9112f34b2f
commit e784ae538a
3 changed files with 42 additions and 0 deletions

View File

@ -30,6 +30,7 @@ Server Options:
-c, --config <file> Configuration file
-m, --message <message> Notification message
-t, --token <token> Notification token
--proxy <proxy> Proxy URL (only for GCM)
iOS Options:
-i, --key <file> certificate key file path
-P, --password <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)

View File

@ -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 {

View File

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