2016-03-27 02:58:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2016-05-31 08:09:08 +00:00
|
|
|
"fmt"
|
2016-03-27 02:58:57 +00:00
|
|
|
"log"
|
2016-05-31 06:52:18 +00:00
|
|
|
"os"
|
2017-01-10 09:13:12 +00:00
|
|
|
"path/filepath"
|
2016-08-19 13:16:10 +00:00
|
|
|
"strconv"
|
2016-10-03 05:42:29 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
|
|
|
"github.com/appleboy/gorush/gorush"
|
2016-03-27 02:58:57 +00:00
|
|
|
)
|
|
|
|
|
2016-04-16 09:11:31 +00:00
|
|
|
func checkInput(token, message string) {
|
|
|
|
if len(token) == 0 {
|
|
|
|
gorush.LogError.Fatal("Missing token flag (-t)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(message) == 0 {
|
|
|
|
gorush.LogError.Fatal("Missing message flag (-m)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 13:40:38 +00:00
|
|
|
// Version control for gorush.
|
2016-05-09 06:48:07 +00:00
|
|
|
var Version = "No Version Provided"
|
|
|
|
|
2016-05-31 08:09:08 +00:00
|
|
|
var usageStr = `
|
2016-11-06 00:34:42 +00:00
|
|
|
________ .__
|
|
|
|
/ _____/ ____ _______ __ __ ______| |__
|
|
|
|
/ \ ___ / _ \\_ __ \| | \/ ___/| | \
|
|
|
|
\ \_\ \( <_> )| | \/| | /\___ \ | Y \
|
|
|
|
\______ / \____/ |__| |____//____ >|___| /
|
|
|
|
\/ \/ \/
|
|
|
|
|
2016-05-31 08:09:08 +00:00
|
|
|
Usage: gorush [options]
|
|
|
|
|
|
|
|
Server Options:
|
|
|
|
-p, --port <port> Use port for clients (default: 8088)
|
2017-01-07 13:16:27 +00:00
|
|
|
-c, --config <file> Configuration file path
|
2016-05-31 08:09:08 +00:00
|
|
|
-m, --message <message> Notification message
|
|
|
|
-t, --token <token> Notification token
|
2017-06-07 07:59:14 +00:00
|
|
|
-e, --engine <engine> Storage engine (memory, redis ...)
|
2017-01-21 07:46:44 +00:00
|
|
|
--title <title> Notification title
|
2016-07-29 00:48:24 +00:00
|
|
|
--proxy <proxy> Proxy URL (only for GCM)
|
2017-01-07 13:16:27 +00:00
|
|
|
--pid <pid path> Process identifier path
|
2016-05-31 08:09:08 +00:00
|
|
|
iOS Options:
|
2016-06-12 11:25:06 +00:00
|
|
|
-i, --key <file> certificate key file path
|
2016-05-31 08:09:08 +00:00
|
|
|
-P, --password <password> certificate key password
|
|
|
|
--topic <topic> iOS topic
|
|
|
|
--ios enabled iOS (default: false)
|
|
|
|
--production iOS production mode (default: false)
|
|
|
|
Android Options:
|
2016-06-12 11:25:06 +00:00
|
|
|
-k, --apikey <api_key> Android API Key
|
2016-05-31 08:09:08 +00:00
|
|
|
--android enabled android (default: false)
|
|
|
|
Common Options:
|
|
|
|
-h, --help Show this message
|
|
|
|
-v, --version Show version
|
|
|
|
`
|
|
|
|
|
|
|
|
// usage will print out the flag options for the server.
|
|
|
|
func usage() {
|
|
|
|
fmt.Printf("%s\n", usageStr)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:16:10 +00:00
|
|
|
func createPIDFile() error {
|
|
|
|
if !gorush.PushConf.Core.PID.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-10 09:13:12 +00:00
|
|
|
|
|
|
|
pidPath := gorush.PushConf.Core.PID.Path
|
|
|
|
_, err := os.Stat(pidPath)
|
2016-08-19 13:16:10 +00:00
|
|
|
if os.IsNotExist(err) || gorush.PushConf.Core.PID.Override {
|
|
|
|
currentPid := os.Getpid()
|
2017-01-10 09:13:12 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("Can't create PID folder on %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(pidPath)
|
2016-08-19 13:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't create PID file: %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2017-01-07 10:45:34 +00:00
|
|
|
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
|
2017-01-10 09:13:12 +00:00
|
|
|
return fmt.Errorf("Can'write PID information on %s: %v", pidPath, err)
|
2016-08-19 13:16:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-10 09:13:12 +00:00
|
|
|
return fmt.Errorf("%s already exists", pidPath)
|
2016-08-19 13:16:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-27 02:58:57 +00:00
|
|
|
func main() {
|
2016-05-31 06:52:18 +00:00
|
|
|
opts := config.ConfYaml{}
|
|
|
|
|
|
|
|
var showVersion bool
|
|
|
|
var configFile string
|
|
|
|
var topic string
|
|
|
|
var message string
|
|
|
|
var token string
|
2016-07-29 00:48:24 +00:00
|
|
|
var proxy string
|
2017-01-21 07:46:44 +00:00
|
|
|
var title string
|
2016-05-31 06:52:18 +00:00
|
|
|
|
|
|
|
flag.BoolVar(&showVersion, "version", false, "Print version information.")
|
|
|
|
flag.BoolVar(&showVersion, "v", false, "Print version information.")
|
2017-01-07 13:16:27 +00:00
|
|
|
flag.StringVar(&configFile, "c", "", "Configuration file path.")
|
|
|
|
flag.StringVar(&configFile, "config", "", "Configuration file path.")
|
|
|
|
flag.StringVar(&opts.Core.PID.Path, "pid", "", "PID file path.")
|
2016-06-12 11:09:41 +00:00
|
|
|
flag.StringVar(&opts.Ios.KeyPath, "i", "", "iOS certificate key file path")
|
2016-06-12 11:25:06 +00:00
|
|
|
flag.StringVar(&opts.Ios.KeyPath, "key", "", "iOS certificate key file path")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
|
|
|
|
flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush")
|
2016-06-12 11:25:06 +00:00
|
|
|
flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&opts.Core.Port, "port", "", "port number for gorush")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&token, "t", "", "token string")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&token, "token", "", "token string")
|
2017-06-07 07:59:14 +00:00
|
|
|
flag.StringVar(&opts.Stat.Engine, "e", "", "store engine")
|
|
|
|
flag.StringVar(&opts.Stat.Engine, "engine", "", "store engine")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&message, "m", "", "notification message")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&message, "message", "", "notification message")
|
2017-01-21 07:46:44 +00:00
|
|
|
flag.StringVar(&title, "title", "", "notification title")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.BoolVar(&opts.Android.Enabled, "android", false, "send android notification")
|
|
|
|
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")
|
2016-07-29 00:48:24 +00:00
|
|
|
flag.StringVar(&proxy, "proxy", "", "http proxy url")
|
2016-03-27 02:58:57 +00:00
|
|
|
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.Usage = usage
|
2016-03-27 02:58:57 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2016-05-09 06:48:07 +00:00
|
|
|
gorush.SetVersion(Version)
|
|
|
|
|
2016-05-31 08:09:08 +00:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
// Show version and exit
|
|
|
|
if showVersion {
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PrintGoRushVersion()
|
2016-05-31 06:52:18 +00:00
|
|
|
os.Exit(0)
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// set default parameters.
|
2016-05-02 13:13:25 +00:00
|
|
|
gorush.PushConf = config.BuildDefaultPushConf()
|
2016-03-27 02:58:57 +00:00
|
|
|
|
|
|
|
// load user define config.
|
2016-05-31 06:52:18 +00:00
|
|
|
if configFile != "" {
|
|
|
|
gorush.PushConf, err = config.LoadConfYaml(configFile)
|
2016-03-27 02:58:57 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-04-11 06:54:37 +00:00
|
|
|
log.Printf("Load yaml config file error: '%v'", err)
|
2016-03-27 02:58:57 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:09:41 +00:00
|
|
|
if opts.Ios.KeyPath != "" {
|
|
|
|
gorush.PushConf.Ios.KeyPath = opts.Ios.KeyPath
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Ios.Password != "" {
|
|
|
|
gorush.PushConf.Ios.Password = opts.Ios.Password
|
2016-05-30 07:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Android.APIKey != "" {
|
|
|
|
gorush.PushConf.Android.APIKey = opts.Android.APIKey
|
2016-03-27 04:55:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 07:59:14 +00:00
|
|
|
if opts.Stat.Engine != "" {
|
|
|
|
gorush.PushConf.Stat.Engine = opts.Stat.Engine
|
|
|
|
}
|
|
|
|
|
2016-03-27 02:58:57 +00:00
|
|
|
// overwrite server port
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Core.Port != "" {
|
|
|
|
gorush.PushConf.Core.Port = opts.Core.Port
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
if err = gorush.InitLog(); err != nil {
|
2016-04-05 06:50:08 +00:00
|
|
|
log.Println(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-04-05 05:15:47 +00:00
|
|
|
|
2016-07-29 01:38:06 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 08:00:17 +00:00
|
|
|
// send android notification
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Android.Enabled {
|
|
|
|
gorush.PushConf.Android.Enabled = opts.Android.Enabled
|
2016-04-13 07:22:04 +00:00
|
|
|
req := gorush.PushNotification{
|
2016-05-31 06:52:18 +00:00
|
|
|
Tokens: []string{token},
|
2016-04-13 07:22:04 +00:00
|
|
|
Platform: gorush.PlatFormAndroid,
|
2016-05-31 06:52:18 +00:00
|
|
|
Message: message,
|
2017-01-21 07:46:44 +00:00
|
|
|
Title: title,
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 07:54:41 +00:00
|
|
|
err := gorush.CheckMessage(req)
|
2016-04-24 06:30:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
gorush.LogError.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err := gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PushToAndroid(req)
|
2016-04-02 15:51:42 +00:00
|
|
|
|
2016-04-02 23:27:10 +00:00
|
|
|
return
|
2016-04-02 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 08:00:17 +00:00
|
|
|
// send android notification
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Ios.Enabled {
|
|
|
|
if opts.Ios.Production {
|
|
|
|
gorush.PushConf.Ios.Production = opts.Ios.Production
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
gorush.PushConf.Ios.Enabled = opts.Ios.Enabled
|
2016-04-13 07:22:04 +00:00
|
|
|
req := gorush.PushNotification{
|
2016-05-31 06:52:18 +00:00
|
|
|
Tokens: []string{token},
|
2016-04-13 07:22:04 +00:00
|
|
|
Platform: gorush.PlatFormIos,
|
2016-05-31 06:52:18 +00:00
|
|
|
Message: message,
|
2017-01-21 07:46:44 +00:00
|
|
|
Title: title,
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
if topic != "" {
|
|
|
|
req.Topic = topic
|
2016-04-28 06:39:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 07:54:41 +00:00
|
|
|
err := gorush.CheckMessage(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
gorush.LogError.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err := gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gorush.InitAPNSClient(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PushToIOS(req)
|
2016-04-10 08:00:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
if err = gorush.CheckPushConf(); err != nil {
|
|
|
|
gorush.LogError.Fatal(err)
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 13:16:27 +00:00
|
|
|
if opts.Core.PID.Path != "" {
|
|
|
|
gorush.PushConf.Core.PID.Path = opts.Core.PID.Path
|
|
|
|
gorush.PushConf.Core.PID.Enabled = true
|
|
|
|
gorush.PushConf.Core.PID.Override = true
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:16:10 +00:00
|
|
|
if err = createPIDFile(); err != nil {
|
|
|
|
gorush.LogError.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err = gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = gorush.InitAPNSClient(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:19:39 +00:00
|
|
|
gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
|
2017-05-12 03:17:55 +00:00
|
|
|
|
|
|
|
if err = gorush.RunHTTPServer(); err != nil {
|
|
|
|
gorush.LogError.Fatal(err)
|
|
|
|
}
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|