wangchuanjin 3 rokov pred
commit
c5768cac14
7 zmenil súbory, kde vykonal 288 pridanie a 0 odobranie
  1. 0 0
      README.md
  2. 15 0
      src/config.json
  3. 23 0
      src/logs/sms.log
  4. 146 0
      src/main.go
  5. 33 0
      src/main_test.go
  6. BIN
      src/test/ddd.exe
  7. 71 0
      src/test/main.go

+ 0 - 0
README.md


+ 15 - 0
src/config.json

@@ -0,0 +1,15 @@
+{
+	"port":"932",
+	"api":"https://api.51welink.com/EncryptionSubmit/SendTemplateSms.ashx",
+	"accountId":"dljianyu0",
+	"password":"Jianyubx20220315",
+	"productId":1012888,
+	"fixEncryptKey":"SMmsEncryptKey",
+	"tempCode":"134220",
+	"poolSize":20,
+	"reTry":3,
+	"idToCode":{
+		"01":134220
+	},
+	"warn":"http://172.17.4.195:19281/_send/_mail?program=Sms_Service&to=wangchuanjin@topnet.net.cn,renzheng@topnet.net.cn,zhaoyujian@topnet.net.cn&title=短信服务程序报警&body=%s"
+}

+ 23 - 0
src/logs/sms.log

@@ -0,0 +1,23 @@
+2022/03/16 10:46:38 main.go:112: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 10:46:38 main.go:120: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 10:49:12 main.go:84: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 10:56:34 main.go:84: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 10:57:09 main.go:84: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 10:57:10 main.go:114: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 10:57:10 main.go:122: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 11:08:06 main.go:125: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 11:08:07 main.go:155: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 11:08:07 main.go:163: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 11:09:15 main.go:125: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 11:09:15 main.go:155: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 11:09:15 main.go:163: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 11:23:22 main.go:123: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 11:23:22 main.go:153: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 11:23:22 main.go:161: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 13:17:24 main.go:84: info  req data: &{Id:01 Phones:150378707650 Params:[123456]}
+2022/03/16 13:18:57 main.go:84: info  req data: &{Id:01 Phones:150378707650 Params:[2]}
+2022/03/16 13:18:58 main.go:114: info  返回值: {"Result":"1052","Reason":"无效计费条数,号码不规则过滤[1:150378707650,]","MsgId":0,"SplitCount":0}
+2022/03/16 13:18:58 main.go:122: info  1052 无效计费条数,号码不规则过滤[1:150378707650,]  0
+2022/03/16 13:20:21 main.go:84: info  req data: &{Id:01 Phones:15037870765 Params:[5]}
+2022/03/16 13:20:21 main.go:114: info  返回值: {"Result":"succ","Reason":"提交成功","MsgId":2203161320217202597,"SplitCount":1}
+2022/03/16 13:20:21 main.go:122: info  succ 提交成功  1

+ 146 - 0
src/main.go

@@ -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))
+}

+ 33 - 0
src/main_test.go

@@ -0,0 +1,33 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"log"
+	"math/rand"
+	"net/rpc"
+	"testing"
+	"time"
+)
+
+func Test_Main(t *testing.T) {
+	var repl string
+	client, err := rpc.DialHTTP("tcp", "127.0.0.1:932")
+	if err != nil {
+		log.Println(err.Error())
+		return
+	}
+	defer client.Close()
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	b, _ := json.Marshal(map[string]interface{}{
+		"id":     "01",
+		"phones": "15037870765",
+		"params": []string{fmt.Sprint(r.Intn(6))},
+	})
+	err = client.Call("Sms.Execute", b, &repl)
+	if err != nil {
+		log.Println(err.Error())
+		return
+	}
+	log.Println(repl)
+}

BIN
src/test/ddd.exe


+ 71 - 0
src/test/main.go

@@ -0,0 +1,71 @@
+package main
+
+import (
+	"bytes"
+	"context"
+	"crypto/md5"
+	"crypto/sha256"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"math/rand"
+	"net/http"
+	"strings"
+	"time"
+)
+
+var (
+	url       = `https://api.51welink.com/EncryptionSubmit/SendTemplateSms.ashx`
+	AccountId = "dljianyu0"
+	pwd       = "Jianyubx20220315"
+	ProductId = 1012888
+	ctx       = context.Background()
+	fix       = "SMmsEncryptKey"
+)
+
+func main() {
+	content := "您的验证码:2598。请勿泄露。【数字本草】"
+	Send("15037870765", content)
+
+}
+
+func Send(phone, content string) {
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	randInt := r.Intn(999999) + 5
+	Timestamp := time.Now().Unix()
+	m5 := strings.ToUpper(Mmd5(pwd + fix))
+	TempCode := "134220"
+	m256 := Msha256(fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%s&Timestamp=%d`, AccountId, phone, m5, randInt, TempCode, Timestamp))
+	log.Println(m5, randInt, Timestamp, m256, fmt.Sprintf(`AccountId=%s&PhoneNos=%s&Password=%s&Random=%d&TempCode=%s&Timestamp=%d`, AccountId, phone, m5, randInt, TempCode, Timestamp))
+	pb, _ := json.Marshal(map[string]interface{}{
+		"AccountId":  AccountId,
+		"AccessKey":  m256,
+		"Timestamp":  Timestamp,
+		"Random":     randInt,
+		"ProductId":  ProductId,
+		"PhoneNos":   phone,
+		"TempCode":   134220,
+		"TempParams": []string{fmt.Sprint(r.Intn(6))},
+	})
+	resp, err := http.Post(url, "application/json", bytes.NewReader(pb))
+	if err != nil {
+		log.Println(err)
+		return
+	}
+	defer resp.Body.Close()
+	b, _ := ioutil.ReadAll(resp.Body)
+	log.Println("返回值:", string(b))
+}
+
+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))
+}