diff --git a/config/config.go b/config/config.go index 6f2270a..ac823bf 100644 --- a/config/config.go +++ b/config/config.go @@ -120,15 +120,16 @@ stat: // ConfYaml is config structure. type ConfYaml struct { - Core SectionCore `yaml:"core"` - API SectionAPI `yaml:"api"` - Android SectionAndroid `yaml:"android"` - Huawei SectionHuawei `yaml:"huawei"` - Ios SectionIos `yaml:"ios"` - Queue SectionQueue `yaml:"queue"` - Log SectionLog `yaml:"log"` - Stat SectionStat `yaml:"stat"` - GRPC SectionGRPC `yaml:"grpc"` + Core SectionCore `yaml:"core"` + API SectionAPI `yaml:"api"` + Android SectionAndroid `yaml:"android"` + Huawei SectionHuawei `yaml:"huawei"` + Ios SectionIos `yaml:"ios"` + SMSFactor SectionSMSFactor `yaml:"smsfactor"` + Queue SectionQueue `yaml:"queue"` + Log SectionLog `yaml:"log"` + Stat SectionStat `yaml:"stat"` + GRPC SectionGRPC `yaml:"grpc"` } // SectionCore is sub section of config. @@ -201,6 +202,13 @@ type SectionIos struct { TeamID string `yaml:"team_id"` } +// SectionIos is sub section of config. +type SectionSMSFactor struct { + Enabled bool `yaml:"enabled"` + APIKey string `yaml:"apikey"` + MaxRetry int `yaml:"max_retry"` +} + // SectionLog is sub section of config. type SectionLog struct { Format string `yaml:"format"` @@ -392,6 +400,11 @@ func LoadConf(confPath ...string) (*ConfYaml, error) { conf.Ios.KeyID = viper.GetString("ios.key_id") conf.Ios.TeamID = viper.GetString("ios.team_id") + // SMSFactor + conf.SMSFactor.Enabled = viper.GetBool("smsfactor.enabled") + conf.SMSFactor.APIKey = viper.GetString("smsfactor.apikey") + conf.SMSFactor.MaxRetry = viper.GetInt("smsfactor.max_retry") + // log conf.Log.Format = viper.GetString("log.format") conf.Log.AccessLog = viper.GetString("log.access_log") diff --git a/core/core.go b/core/core.go index 4791bb0..e764981 100644 --- a/core/core.go +++ b/core/core.go @@ -7,6 +7,10 @@ const ( PlatFormAndroid // PlatFormHuawei constant is 3 for Huawei PlatFormHuawei + // PlatformUnifiedPush constant is 4 for UnifiedPush + PlatformUnifiedPush + // PlatformSMSFactor constant is 5 for SMSFactor + PlatformSMSFactor ) const ( diff --git a/core/storage.go b/core/storage.go index 12ff824..33fde77 100644 --- a/core/storage.go +++ b/core/storage.go @@ -21,6 +21,18 @@ const ( // HuaweiErrorKey is key name for huawei error count of storage HuaweiErrorKey = "gorush-huawei-error-count" + + // UnifiedPushSuccessKey is key name for unifiedpush success count of storage + UnifiedPushSuccessKey = "gorush-unifiedpush-success-count" + + // UnifiedPushErrorKey is key name for unifiedpush error count of storage + UnifiedPushErrorKey = "gorush-unifiedpush-error-count" + + // SMSFactirSuccessKey is key name for smsfactor success count of storage + SMSFactorSuccessKey = "gorush-smsfactor-success-count" + + // SMSfactorErrorKey is key name for smsfactor error count of storage + SMSFactorErrorKey = "gorush-smsfactor-error-count" ) // Storage interface diff --git a/notify/notification.go b/notify/notification.go index ad40a98..2987997 100644 --- a/notify/notification.go +++ b/notify/notification.go @@ -232,6 +232,12 @@ func CheckPushConf(cfg *config.ConfYaml) error { } } + if cfg.SMSFactor.Enabled { + if cfg.SMSFactor.APIKey == "" { + return errors.New("missing smsfactor api key") + } + } + return nil } @@ -251,6 +257,8 @@ func SendNotification(req qcore.QueuedMessage, cfg *config.ConfYaml) (resp *Resp resp, err = PushToAndroid(v, cfg) case core.PlatFormHuawei: resp, err = PushToHuawei(v, cfg) + case core.PlatformSMSFactor: + resp, err = PushToSMSFactor(v, cfg) } if cfg.Core.FeedbackURL != "" { diff --git a/notify/notification_smsfactor.go b/notify/notification_smsfactor.go new file mode 100644 index 0000000..28d11ea --- /dev/null +++ b/notify/notification_smsfactor.go @@ -0,0 +1,58 @@ +package notify + +import ( + "net/http" + + "github.com/appleboy/gorush/config" + "github.com/appleboy/gorush/logx" +) + +// InitSMSFactorClient use for initialize SMSFactor Request. +func InitSMSFactorRequest(cfg *config.ConfYaml, token string, notification *PushNotification, recipient string) (*http.Request, error) { + + req, _ := http.NewRequest("GET", "https://api.smsfactor.com/send", nil) + req.Header.Set("Content-Type", "application/json") + + q := req.URL.Query() + q.Add("text", notification.Message) + q.Add("to", recipient) + q.Add("token", token) + req.URL.RawQuery = q.Encode() + + return req, nil +} + +func PushToSMSFactor(notification *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) { + logx.LogAccess.Debug("Start push notification for SMSFactor") + + // check message + err = CheckMessage(notification) + if err != nil { + logx.LogError.Error("request error: " + err.Error()) + return + } + + resp = &ResponsePush{} + + for _, recipient := range notification.Tokens { + var request *http.Request + + if notification.APIKey != "" { + request, err = InitSMSFactorRequest(cfg, notification.APIKey, notification, recipient) + } else { + request, err = InitSMSFactorRequest(cfg, cfg.SMSFactor.APIKey, notification, recipient) + } + + if err != nil { + logx.LogError.Error(err.Error()) + } + + client := &http.Client{} + _, err := client.Do(request) + if err != nil { + logx.LogError.Error(err.Error()) + } + } + + return resp, nil +}