template.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package RouterManchong
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "errors"
  6. "manchong"
  7. "time"
  8. )
  9. type head struct {
  10. VERSION string `json:"VERSION"`
  11. TIMESTAMP string `json:"TIMESTAMP"`
  12. SEQNO string `json:"SEQNO"`
  13. APPID string `json:"APPID"`
  14. SECERTKEY string `json:"SECERTKEY"`
  15. }
  16. func (h *head) check() (string, error) {
  17. n := len(h.TIMESTAMP)
  18. if n < 15 {
  19. return "14", errors.New("TIMESTAMP超过时限")
  20. }
  21. s := h.TIMESTAMP[:14] + "." + h.TIMESTAMP[14:]
  22. t, err := time.ParseInLocation("20060102150405.999999999", s, time.Local)
  23. if err != nil {
  24. return "99", err
  25. }
  26. bigt := time.Now().Add(5 * time.Minute)
  27. lowt := time.Now().Add(-5 * time.Minute)
  28. if t.Before(lowt) || t.After(bigt) {
  29. return "14", errors.New("TIMESTAMP超过时限")
  30. }
  31. appc, ok := manchong.CFG.Users[h.APPID]
  32. if !ok {
  33. return "09", errors.New("APPID已经失效")
  34. }
  35. m5 := md5.New()
  36. m5.Write([]byte(h.TIMESTAMP + h.SEQNO + h.APPID + appc))
  37. s = hex.EncodeToString(m5.Sum(nil))
  38. if s != h.SECERTKEY {
  39. return "08", errors.New("签名验证失败")
  40. }
  41. return "00", nil
  42. }
  43. type template struct {
  44. HEADER *head `json:"HEADER"`
  45. MSGBODY interface{} `json:"MSGBODY"`
  46. }