Merge pull request #111 from appleboy/proxy

Support HTTP Proxy for GCM
This commit is contained in:
Bo-Yi Wu 2016-07-29 09:50:43 +08:00 committed by GitHub
commit b0bb1fa483
7 changed files with 56 additions and 0 deletions

View File

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

View File

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

View File

@ -7,6 +7,7 @@ core:
ssl: false
cert_path: "cert.pem"
key_path: "key.pem"
http_proxy: ""
api:
push_uri: "/api/push"

View File

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

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

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

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