|
@@ -0,0 +1,146 @@
|
|
|
|
+package main
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "bytes"
|
|
|
|
+ "crypto/md5"
|
|
|
|
+ "crypto/sha256"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "fmt"
|
|
|
|
+ "io/ioutil"
|
|
|
|
+ "log"
|
|
|
|
+ "math/rand"
|
|
|
|
+ "net"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/rpc"
|
|
|
|
+ "net/url"
|
|
|
|
+ "qfw/util"
|
|
|
|
+ "strings"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
|
|
+ "github.com/donnie4w/go-logger/logger"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type Config struct {
|
|
|
|
+ Port string
|
|
|
|
+ Api string
|
|
|
|
+ AccountId string
|
|
|
|
+ Password string
|
|
|
|
+ ProductId int
|
|
|
|
+ FixEncryptKey string
|
|
|
|
+ TempCode string
|
|
|
|
+ PoolSize int
|
|
|
|
+ ReTry int
|
|
|
|
+ IdToCode map[string]int
|
|
|
|
+ Warn string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+type Sms struct {
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+type ReqData struct {
|
|
|
|
+ Id string
|
|
|
|
+ Phones string
|
|
|
|
+ Params []string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var config *Config
|
|
|
|
+var pool chan bool
|
|
|
|
+
|
|
|
|
+func init() {
|
|
|
|
+ util.ReadConfig(&config)
|
|
|
|
+ pool = make(chan bool, config.PoolSize)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+func main() {
|
|
|
|
+ logger.SetConsole(false)
|
|
|
|
+ logger.SetRollingDaily("./logs", "sms.log")
|
|
|
|
+ frpc := new(Sms)
|
|
|
|
+ rpc.Register(frpc)
|
|
|
|
+ rpc.HandleHTTP()
|
|
|
|
+ //处理链接
|
|
|
|
+ listen, err := net.Listen("tcp", ":"+config.Port)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println(err)
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("rpc server is listening", config.Port)
|
|
|
|
+ }
|
|
|
|
+ http.Serve(listen, nil)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+func (s *Sms) Execute(param *[]byte, ret *string) error {
|
|
|
|
+ go func() {
|
|
|
|
+ defer util.Catch()
|
|
|
|
+ pool <- true
|
|
|
|
+ defer func() {
|
|
|
|
+ <-pool
|
|
|
|
+ }()
|
|
|
|
+ var reqData *ReqData
|
|
|
|
+ if err := json.Unmarshal(*param, &reqData); err != nil {
|
|
|
|
+ logger.Error(err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ logger.Info("req data:", fmt.Sprintf("%+v", reqData))
|
|
|
|
+ TempCode := config.IdToCode[reqData.Id]
|
|
|
|
+ if TempCode == 0 {
|
|
|
|
+ logger.Error("无效的参数id,没有找到TempCode")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
+ randInt := r.Intn(999999) + 5
|
|
|
|
+ Timestamp := time.Now().Unix()
|
|
|
|
+ m5 := strings.ToUpper(Mmd5(config.Password + config.FixEncryptKey))
|
|
|
|
+ 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))
|
|
|
|
+ pb, _ := json.Marshal(map[string]interface{}{
|
|
|
|
+ "AccountId": config.AccountId,
|
|
|
|
+ "AccessKey": m256,
|
|
|
|
+ "Timestamp": Timestamp,
|
|
|
|
+ "Random": randInt,
|
|
|
|
+ "ProductId": config.ProductId,
|
|
|
|
+ "PhoneNos": reqData.Phones,
|
|
|
|
+ "TempCode": TempCode,
|
|
|
|
+ "TempParams": reqData.Params,
|
|
|
|
+ })
|
|
|
|
+ for i := 1; i <= config.ReTry; i++ {
|
|
|
|
+ resp, err := http.Post(config.Api, "application/json", bytes.NewReader(pb))
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Error("第", i, "次请求出错", err)
|
|
|
|
+ if i == config.ReTry {
|
|
|
|
+ go http.Get(fmt.Sprintf(config.Warn, reqData.Phones+",调用接口超时"))
|
|
|
|
+ }
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ b, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
+ logger.Info("返回值:", string(b))
|
|
|
|
+ var result struct {
|
|
|
|
+ Result string
|
|
|
|
+ Reason string
|
|
|
|
+ MsgId string
|
|
|
|
+ SplitCount int
|
|
|
|
+ }
|
|
|
|
+ json.Unmarshal(b, &result)
|
|
|
|
+ if result.Result != "succ" {
|
|
|
|
+ go http.Get(fmt.Sprintf(config.Warn, url.QueryEscape(result.Reason)))
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+ *ret = "y"
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func Msha256(val string) string {
|
|
|
|
+ sh := sha256.New()
|
|
|
|
+ sh.Write([]byte(val))
|
|
|
|
+ return fmt.Sprintf("%x", sh.Sum(nil))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func Mmd5(val string) string {
|
|
|
|
+ m5 := md5.New()
|
|
|
|
+ m5.Write([]byte(val))
|
|
|
|
+ return fmt.Sprintf("%x", m5.Sum(nil))
|
|
|
|
+}
|