aliyun.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package smsdriver
  15. import (
  16. "encoding/json"
  17. "regexp"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  19. sdkerrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/errors"
  24. api "yunion.io/x/onecloud/pkg/apis/notify"
  25. "yunion.io/x/onecloud/pkg/notify/models"
  26. )
  27. type SAliyunSMSDriver struct{}
  28. var parser = regexp.MustCompile(`\+(\d*) (.*)`)
  29. func (d *SAliyunSMSDriver) Name() string {
  30. return DriverAliyun
  31. }
  32. func (d *SAliyunSMSDriver) Verify(config *api.NotifyConfig) error {
  33. return d.Send(api.SSMSSendParams{
  34. RemoteTemplate: config.VerifiyCode,
  35. To: config.PhoneNumber,
  36. RemoteTemplateParam: api.SRemoteTemplateParam{Code: "0000"},
  37. }, true, config)
  38. }
  39. func (d *SAliyunSMSDriver) Send(args api.SSMSSendParams, isVerify bool, config *api.NotifyConfig) error {
  40. if isVerify {
  41. args.AppKey = config.AccessKeyId
  42. args.AppSecret = config.AccessKeySecret
  43. args.Signature = config.Signature
  44. } else {
  45. args.AppKey = models.ConfigMap[api.MOBILE].Content.AccessKeyId
  46. args.AppSecret = models.ConfigMap[api.MOBILE].Content.AccessKeySecret
  47. args.Signature = models.ConfigMap[api.MOBILE].Content.Signature
  48. }
  49. args.TemplateId = args.RemoteTemplate
  50. return d.sendSms(args)
  51. }
  52. func (d *SAliyunSMSDriver) sendSms(args api.SSMSSendParams) error {
  53. // lock and update
  54. client, err := sdk.NewClientWithAccessKey("default", args.AppKey, args.AppSecret)
  55. if err != nil {
  56. return errors.Wrap(err, "NewClientWithAccessKey")
  57. }
  58. m := parser.FindStringSubmatch(args.To)
  59. if len(m) > 0 {
  60. if m[1] == "86" {
  61. args.To = m[2]
  62. } else {
  63. args.To = m[1] + m[2]
  64. }
  65. }
  66. request := requests.NewCommonRequest()
  67. request.Method = "POST"
  68. request.Scheme = "https" // https | http
  69. request.Domain = "dysmsapi.aliyuncs.com"
  70. request.Version = "2017-05-25"
  71. request.ApiName = "SendSms"
  72. request.QueryParams["RegionId"] = "default"
  73. request.QueryParams["PhoneNumbers"] = args.To
  74. request.QueryParams["SignName"] = args.Signature
  75. request.QueryParams["TemplateCode"] = args.TemplateId
  76. request.QueryParams["TemplateParam"] = jsonutils.Marshal(args.RemoteTemplateParam).String()
  77. return d.checkResponseAndError(client.ProcessCommonRequest(request))
  78. }
  79. func (d *SAliyunSMSDriver) checkResponseAndError(rep *responses.CommonResponse, err error) error {
  80. if err != nil {
  81. serr, ok := err.(*sdkerrors.ServerError)
  82. if !ok {
  83. return err
  84. }
  85. if serr.ErrorCode() == ACCESSKEYID_NOTFOUND {
  86. return ErrAccessKeyIdNotFound
  87. }
  88. if serr.ErrorCode() == SIGN_DOESNOTMATCH {
  89. return ErrSignatureDoesNotMatch
  90. }
  91. return err
  92. }
  93. type RepContent struct {
  94. Message string
  95. Code string
  96. }
  97. respContent := rep.GetHttpContentBytes()
  98. rc := RepContent{}
  99. err = json.Unmarshal(respContent, &rc)
  100. if err != nil {
  101. return errors.Wrap(err, "json.Unmarshal")
  102. }
  103. if rc.Code == "OK" {
  104. return nil
  105. }
  106. if rc.Code == SIGHNTURE_ILLEGAL {
  107. return ErrSignnameInvalid
  108. } else if rc.Code == TEMPLATE_ILLGAL {
  109. return ErrSignnameInvalid
  110. }
  111. return errors.Error(rc.Message)
  112. }
  113. func init() {
  114. models.SMSRegister(&SAliyunSMSDriver{})
  115. }