main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "math/rand"
  11. "net"
  12. "net/http"
  13. "net/rpc"
  14. "net/url"
  15. "qfw/util"
  16. "strings"
  17. "time"
  18. "github.com/donnie4w/go-logger/logger"
  19. )
  20. type Config struct {
  21. Port string
  22. Api string
  23. AccountId string
  24. Password string
  25. ProductId int
  26. FixEncryptKey string
  27. TempCode string
  28. PoolSize int
  29. ReTry int
  30. IdToCode map[string]int
  31. Warn string
  32. }
  33. //
  34. type Sms struct {
  35. }
  36. //
  37. type ReqData struct {
  38. Id string
  39. Phones string
  40. Params []string
  41. }
  42. var config *Config
  43. var pool chan bool
  44. func init() {
  45. util.ReadConfig(&config)
  46. pool = make(chan bool, config.PoolSize)
  47. }
  48. //
  49. func main() {
  50. logger.SetConsole(false)
  51. logger.SetRollingDaily("./logs", "sms.log")
  52. frpc := new(Sms)
  53. rpc.Register(frpc)
  54. rpc.HandleHTTP()
  55. //处理链接
  56. listen, err := net.Listen("tcp", ":"+config.Port)
  57. if err != nil {
  58. log.Println(err)
  59. } else {
  60. log.Println("rpc server is listening", config.Port)
  61. }
  62. http.Serve(listen, nil)
  63. }
  64. //
  65. func (s *Sms) Execute(param *[]byte, ret *string) error {
  66. go func() {
  67. defer util.Catch()
  68. pool <- true
  69. defer func() {
  70. <-pool
  71. }()
  72. var reqData *ReqData
  73. if err := json.Unmarshal(*param, &reqData); err != nil {
  74. logger.Error(err)
  75. return
  76. }
  77. logger.Info("req data:", fmt.Sprintf("%+v", reqData))
  78. TempCode := config.IdToCode[reqData.Id]
  79. if TempCode == 0 {
  80. logger.Error("无效的参数id,没有找到TempCode")
  81. return
  82. }
  83. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  84. randInt := r.Intn(999999) + 5
  85. Timestamp := time.Now().Unix()
  86. m5 := strings.ToUpper(Mmd5(config.Password + config.FixEncryptKey))
  87. m256 := Msha256(fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%d&Timestamp=%d`, config.AccountId, strings.Split(reqData.Phones, ",")[0], m5, randInt, TempCode, Timestamp))
  88. pb, _ := json.Marshal(map[string]interface{}{
  89. "AccountId": config.AccountId,
  90. "AccessKey": m256,
  91. "Timestamp": Timestamp,
  92. "Random": randInt,
  93. "ProductId": config.ProductId,
  94. "PhoneNos": reqData.Phones,
  95. "TempCode": TempCode,
  96. "TempParams": reqData.Params,
  97. })
  98. for i := 1; i <= config.ReTry; i++ {
  99. resp, err := http.Post(config.Api, "application/json", bytes.NewReader(pb))
  100. if err != nil {
  101. logger.Error("第", i, "次请求出错", err)
  102. if i == config.ReTry {
  103. go http.Get(fmt.Sprintf(config.Warn, reqData.Phones+",调用接口超时"))
  104. }
  105. continue
  106. }
  107. defer resp.Body.Close()
  108. b, _ := ioutil.ReadAll(resp.Body)
  109. logger.Info("返回值:", string(b))
  110. var result struct {
  111. Result string
  112. Reason string
  113. MsgId string
  114. SplitCount int
  115. }
  116. json.Unmarshal(b, &result)
  117. if result.Result != "succ" {
  118. go http.Get(fmt.Sprintf(config.Warn, url.QueryEscape(result.Reason)))
  119. }
  120. return
  121. }
  122. }()
  123. *ret = "y"
  124. return nil
  125. }
  126. func Msha256(val string) string {
  127. sh := sha256.New()
  128. sh.Write([]byte(val))
  129. return fmt.Sprintf("%x", sh.Sum(nil))
  130. }
  131. func Mmd5(val string) string {
  132. m5 := md5.New()
  133. m5.Write([]byte(val))
  134. return fmt.Sprintf("%x", m5.Sum(nil))
  135. }