anonymousUser.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package filter
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  5. "app.yhyue.com/moapp/jypkg/public"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/base64"
  10. "encoding/json"
  11. "encoding/pem"
  12. "errors"
  13. "fmt"
  14. "io/ioutil"
  15. "log"
  16. "net/http"
  17. "time"
  18. )
  19. func initPrivatePublicKey() {
  20. // 读取私钥文件
  21. privateKeyBytes, err := ioutil.ReadFile("rsa_private_key.pem")
  22. if err != nil {
  23. log.Println("无法读取私钥文件:", err)
  24. return
  25. }
  26. // 解码私钥
  27. privateKeyBlock, _ := pem.Decode(privateKeyBytes)
  28. if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
  29. log.Println("私钥文件格式错误")
  30. return
  31. }
  32. // 解析私钥
  33. privateKeys, err := x509.ParsePKCS8PrivateKey(privateKeyBlock.Bytes)
  34. if err != nil {
  35. log.Println("私钥解析失败:", err)
  36. return
  37. }
  38. // 转换为RSA类型的私钥
  39. rsaPrivateKey, ok := privateKeys.(*rsa.PrivateKey)
  40. if !ok {
  41. fmt.Println("无法转换为RSA类型的私钥")
  42. return
  43. }
  44. PrivateKey = rsaPrivateKey
  45. // 读取公钥文件
  46. publicKeyBytes, err := ioutil.ReadFile("rsa_public_key.pem")
  47. if err != nil {
  48. log.Println("无法读取公钥文件:", err)
  49. return
  50. }
  51. // 解码公钥
  52. publicKeyBlock, _ := pem.Decode(publicKeyBytes)
  53. if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
  54. log.Println("公钥文件格式错误", publicKeyBlock.Type)
  55. return
  56. }
  57. // 解析公钥
  58. publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
  59. if err != nil {
  60. log.Println("公钥解析失败:", err.Error())
  61. return
  62. }
  63. // 转换为公钥类型
  64. rsaPublicKey, ok := publicKeyInterface.(*rsa.PublicKey)
  65. if !ok {
  66. log.Println("无法转换为公钥类型")
  67. return
  68. }
  69. PublicKey = rsaPublicKey
  70. }
  71. func AnonymousAuthentication(W http.ResponseWriter, R *http.Request, Session *httpsession.Session) {
  72. anonymousEid := Session.Get("anonymousEid")
  73. if anonymousEid != nil && anonymousEid != "" {
  74. return
  75. }
  76. fid, err := R.Cookie("fid")
  77. if err != nil {
  78. log.Println("匿名用户获取fid失败", err.Error())
  79. return
  80. }
  81. eid, err := R.Cookie("eid")
  82. if err != nil {
  83. log.Println("匿名用户获取eid失败", err.Error())
  84. return
  85. }
  86. if fid.Value != "" && eid.Value != "" {
  87. eData, err1 := Decryption(eid.Value)
  88. if err1 != nil {
  89. log.Printf("匿名用户eid:%s,fid:%s 解密失败err:%s", eid.Value, fid.Value, err1.Error())
  90. return
  91. }
  92. if fid.Value != eData {
  93. log.Printf("匿名用户身份验证不通过,eid:%s,fid:%s,eData:%s", eid.Value, fid.Value, eData)
  94. return
  95. }
  96. Session.Set("anonymousEid", eid.Value)
  97. //未登录用户增加匿名身份信息
  98. if Session.Get("mgoUserId") == nil || Session.Get("mgoUserId") == "" {
  99. log.Println("未登录用户增加匿名身份信息")
  100. agent := R.Header.Get("user-agent")
  101. md, _ := json.Marshal(R.Form)
  102. str := string(md)
  103. data := map[string]interface{}{
  104. "ip": util.GetIp(R),
  105. "client": agent,
  106. "os": util.GetOS(agent),
  107. "browse": util.GetBrowse(agent),
  108. "fid": fid,
  109. "url": R.RequestURI,
  110. "mdescribe": str,
  111. "refer": R.Referer(),
  112. "method": R.Method,
  113. "creation_time": time.Now().Unix(),
  114. }
  115. public.BaseMysql.Insert("anonymous_identity", data)
  116. }
  117. } else {
  118. log.Println("无身份验证信息")
  119. }
  120. }
  121. // Encryption 加密
  122. func Encryption(content string) (string, error) {
  123. if content == "" {
  124. return "", errors.New("加密内容为空")
  125. }
  126. plaintext := []byte(content)
  127. // 使用公钥加密数据
  128. ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
  129. if err != nil {
  130. log.Println("加密失败:", err)
  131. return "", err
  132. }
  133. return string(ciphertext), nil
  134. }
  135. func Decryption(content string) (string, error) {
  136. if content == "" {
  137. return "", errors.New("解密内容为空")
  138. }
  139. if PrivateKey == nil {
  140. return "", errors.New("无效私钥")
  141. }
  142. ciphertext, err := base64.StdEncoding.DecodeString(content)
  143. if err != nil {
  144. log.Println("密文解码失败:", err)
  145. return "", errors.New("密文解码失败")
  146. }
  147. // 使用私钥解密数据
  148. decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
  149. if err != nil {
  150. log.Println("解密失败:", err)
  151. return "", err
  152. }
  153. return string(decryptedText), nil
  154. }