附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

217 lines
6.2 KiB

  1. package utils
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils/logx"
  5. "fmt"
  6. "github.com/forgoer/openssl"
  7. "github.com/gin-gonic/gin"
  8. "github.com/syyongx/php2go"
  9. "strings"
  10. "time"
  11. )
  12. var publicKey = []byte(`-----BEGIN PUBLIC KEY-----
  13. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCFQD7RL2tDNuwdg0jTfV0zjAzh
  14. WoCWfGrcNiucy2XUHZZU2oGhHv1N10qu3XayTDD4pu4sJ73biKwqR6ZN7IS4Sfon
  15. vrzaXGvrTG4kmdo3XrbrkzmyBHDLTsJvv6pyS2HPl9QPSvKDN0iJ66+KN8QjBpw1
  16. FNIGe7xbDaJPY733/QIDAQAB
  17. -----END PUBLIC KEY-----`)
  18. var privateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
  19. MIICXAIBAAKBgQCFQD7RL2tDNuwdg0jTfV0zjAzhWoCWfGrcNiucy2XUHZZU2oGh
  20. Hv1N10qu3XayTDD4pu4sJ73biKwqR6ZN7IS4SfonvrzaXGvrTG4kmdo3Xrbrkzmy
  21. BHDLTsJvv6pyS2HPl9QPSvKDN0iJ66+KN8QjBpw1FNIGe7xbDaJPY733/QIDAQAB
  22. AoGADi14wY8XDY7Bbp5yWDZFfV+QW0Xi2qAgSo/k8gjeK8R+I0cgdcEzWF3oz1Q2
  23. 9d+PclVokAAmfj47e0AmXLImqMCSEzi1jDBUFIRoJk9WE1YstE94mrCgV0FW+N/u
  24. +L6OgZcjmF+9dHKprnpaUGQuUV5fF8j0qp8S2Jfs3Sw+dOECQQCQnHALzFjmXXIR
  25. Ez3VSK4ZoYgDIrrpzNst5Hh6AMDNZcG3CrCxlQrgqjgTzBSr3ZSavvkfYRj42STk
  26. TqyX1tQFAkEA6+O6UENoUTk2lG7iO/ta7cdIULnkTGwQqvkgLIUjk6w8E3sBTIfw
  27. rerTEmquw5F42HHE+FMrRat06ZN57lENmQJAYgUHlZevcoZIePZ35Qfcqpbo4Gc8
  28. Fpm6vwKr/tZf2Vlt0qo2VkhWFS6L0C92m4AX6EQmDHT+Pj7BWNdS+aCuGQJBAOkq
  29. NKPZvWdr8jNOV3mKvxqB/U0uMigIOYGGtvLKt5vkh42J7ILFbHW8w95UbWMKjDUG
  30. X/hF3WQEUo//Imsa2yECQHSZIpJxiTRueoDiyRt0LH+jdbYFUu/6D0UIYXhFvP/p
  31. EZX+hfCfUnNYX59UVpRjSZ66g0CbCjuBPOhmOD+hDeQ=
  32. -----END RSA PRIVATE KEY-----`)
  33. func GetApiVersion(c *gin.Context) int {
  34. var apiVersion = c.GetHeader("apiVersion")
  35. if StrToInt(apiVersion) == 0 { //没有版本号先不校验
  36. apiVersion = c.GetHeader("Apiversion")
  37. }
  38. if StrToInt(apiVersion) == 0 { //没有版本号先不校验
  39. apiVersion = c.GetHeader("api_version")
  40. }
  41. if StrToInt(apiVersion) == 0 { //没有版本号先不校验
  42. apiVersion = c.GetString("apiVersion")
  43. }
  44. if StrToInt(apiVersion) == 0 {
  45. platform := c.GetHeader("platform")
  46. if InArr(platform, []string{"ios", "android"}) == false && c.GetString("h5_applet_must_sign") == "1" {
  47. apiVersion = "1"
  48. }
  49. if InArr(platform, []string{"android"}) && c.GetString("android_must_sign") == "1" {
  50. apiVersion = "1"
  51. }
  52. if InArr(platform, []string{"ios"}) && c.GetString("ios_must_sign") == "1" {
  53. apiVersion = "1"
  54. }
  55. }
  56. if c.GetString("api_version") == "1" && cfg.Prd {
  57. apiVersion = "1"
  58. }
  59. if (strings.Contains(c.Request.Host, "zhios-app") || strings.Contains(c.Request.Host, "api.zhios.cn")) && apiVersion == "1" {
  60. apiVersion = "0"
  61. c.Set("api_version", "0")
  62. }
  63. //if InArr(c.GetHeader("platform"), []string{"ios", "android"}) {
  64. // apiVersion = "0"
  65. //}
  66. var uri = c.Request.RequestURI
  67. if InArr(c.GetHeader("platform"), []string{"ios", "android", "pc"}) { //不用签名的接口
  68. var filterList = []string{
  69. "/api/v1/appcheck",
  70. "/api/v1/app/guide",
  71. "/api/v1/new/config.json",
  72. "pub.flutter.web_download_page",
  73. }
  74. for _, v := range filterList {
  75. if strings.Contains(uri, v) {
  76. apiVersion = "0"
  77. }
  78. }
  79. }
  80. return StrToInt(apiVersion)
  81. }
  82. func CheckUri(c *gin.Context) int {
  83. apiVersion := "1"
  84. //var uri = c.Request.RequestURI
  85. if InArr(c.GetHeader("platform"), []string{"ios", "android"}) { //不用签名的接口
  86. //var filterList = []string{
  87. // "/api/v1/appcheck",
  88. // "/api/v1/app/guide",
  89. // "/api/v1/new/config.json",
  90. // "api/v1/rec",
  91. // "api/v1/custom/mod/",
  92. // "api/v1/mod/",
  93. // "api/v1/s/",
  94. //}
  95. //for _, v := range filterList {
  96. // if strings.Contains(uri, v) {
  97. // apiVersion = "0"
  98. // }
  99. //}
  100. apiVersion = "0"
  101. }
  102. return StrToInt(apiVersion)
  103. }
  104. // 签名校验
  105. func SignCheck(c *gin.Context) bool {
  106. var apiVersion = GetApiVersion(c)
  107. if apiVersion == 0 { //没有版本号先不校验
  108. return true
  109. }
  110. //1.通过rsa 解析出 aes
  111. var key = c.GetHeader("key")
  112. //拼接对应参数
  113. var uri = c.Request.RequestURI
  114. var query = GetQueryParam(uri)
  115. fmt.Println(query)
  116. query["timestamp"] = c.GetHeader("timestamp")
  117. query["nonce"] = c.GetHeader("nonce")
  118. query["key"] = key
  119. token := c.GetHeader("Authorization")
  120. if token != "" {
  121. // 按空格分割
  122. parts := strings.SplitN(token, " ", 2)
  123. if len(parts) == 2 && parts[0] == "Bearer" {
  124. token = parts[1]
  125. }
  126. }
  127. query["token"] = token
  128. //2.query参数按照 ASCII 码从小到大排序
  129. str := JoinStringsInASCII(query, "&", false, false, "")
  130. //3.拼上密钥
  131. secret := ""
  132. if InArr(c.GetHeader("platform"), []string{"android", "ios"}) {
  133. secret = c.GetString("app_api_secret_key")
  134. } else if c.GetHeader("platform") == "wap" {
  135. secret = c.GetString("h5_api_secret_key")
  136. } else {
  137. secret = c.GetString("applet_api_secret_key")
  138. }
  139. str = fmt.Sprintf("%s&secret=%s", str, secret)
  140. fmt.Println(str)
  141. //4.md5加密 转小写
  142. sign := strings.ToLower(Md5(str))
  143. //5.判断跟前端传来的sign是否一致
  144. if sign != c.GetHeader("sign") {
  145. return false
  146. }
  147. if StrToInt64(query["timestamp"])/1000 < time.Now().Unix()-300 {
  148. fmt.Println("============" + query["timestamp"])
  149. return false
  150. }
  151. //if query["nonce"] != "" {
  152. // //TODO s
  153. // getString, err := cache.GetString(query["nonce"])
  154. // if err != nil {
  155. // fmt.Println("nonce", err)
  156. // }
  157. // if getString != "" {
  158. // fmt.Println("nonce", "============"+getString)
  159. // return false
  160. // } else {
  161. // cache.SetEx(query["nonce"], "1", 300)
  162. // }
  163. //}
  164. return true
  165. }
  166. func ResultAes(c *gin.Context, raw []byte) string {
  167. var key = c.GetHeader("key")
  168. base, _ := php2go.Base64Decode(key)
  169. aes, err := RsaDecrypt([]byte(base), privateKey)
  170. if err != nil {
  171. logx.Info(err)
  172. return ""
  173. }
  174. fmt.Println("============aes============")
  175. fmt.Println(string(aes))
  176. fmt.Println(string(raw))
  177. str, _ := openssl.AesECBEncrypt(raw, aes, openssl.PKCS7_PADDING)
  178. value := php2go.Base64Encode(string(str))
  179. fmt.Println(value)
  180. return value
  181. }
  182. func ResultAesDecrypt(c *gin.Context, raw string) string {
  183. var key = c.GetHeader("key")
  184. if key == "" {
  185. key = c.GetHeader("Key")
  186. }
  187. fmt.Println("验签", key)
  188. base, _ := php2go.Base64Decode(key)
  189. aes, err := RsaDecrypt([]byte(base), privateKey)
  190. if err != nil {
  191. logx.Info(err)
  192. return ""
  193. }
  194. raw = strings.ReplaceAll(raw, "\"", "")
  195. fmt.Println(raw)
  196. value1, _ := php2go.Base64Decode(raw)
  197. if value1 == "" {
  198. return ""
  199. }
  200. str1, _ := openssl.AesECBDecrypt([]byte(value1), aes, openssl.PKCS7_PADDING)
  201. fmt.Println("==========解码=========")
  202. fmt.Println(string(str1))
  203. return string(str1)
  204. }