tools.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package public
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "time"
  11. "unicode/utf8"
  12. )
  13. // 将内容写入到文件,如果文件名带路径,则会判断路径是否存在,不存在则创建
  14. func WriteToFile(path string, data []byte) error {
  15. tmp := strings.Split(path, "/")
  16. if len(tmp) > 0 {
  17. tmp = tmp[:len(tmp)-1]
  18. }
  19. err := os.MkdirAll(strings.Join(tmp, "/"), os.ModePerm)
  20. if err != nil {
  21. return err
  22. }
  23. err = ioutil.WriteFile(path, data, 0755)
  24. if err != nil {
  25. return err
  26. }
  27. return nil
  28. }
  29. // JudgeGroup 判断群ID是否在白名单
  30. func JudgeGroup(s string) bool {
  31. if len(Config.AllowGroups) == 0 {
  32. return true
  33. }
  34. for _, v := range Config.AllowGroups {
  35. if v == s {
  36. return true
  37. }
  38. }
  39. return false
  40. }
  41. // JudgeOutgoingGroup 判断群ID是否在为outgoing白名单
  42. func JudgeOutgoingGroup(s string) bool {
  43. if len(Config.AllowOutgoingGroups) == 0 {
  44. return true
  45. }
  46. for _, v := range Config.AllowOutgoingGroups {
  47. if v == s {
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. // JudgeUsers 判断用户是否在白名单
  54. func JudgeUsers(s string) bool {
  55. // 优先判断黑名单,黑名单用户返回:不在白名单
  56. if len(Config.DenyUsers) != 0 {
  57. for _, v := range Config.DenyUsers {
  58. if v == s {
  59. return false
  60. }
  61. }
  62. }
  63. // 白名单配置逻辑处理
  64. if len(Config.AllowUsers) == 0 {
  65. return true
  66. }
  67. for _, v := range Config.AllowUsers {
  68. if v == s {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. // JudgeAdminUsers 判断用户是否为系统管理员
  75. func JudgeAdminUsers(s string) bool {
  76. // 如果secret或者用户的userid都为空的话,那么默认没有管理员
  77. if len(Config.AppSecrets) == 0 || s == "" {
  78. return false
  79. }
  80. // 如果没有指定,则没有人是管理员
  81. if len(Config.AdminUsers) == 0 {
  82. return false
  83. }
  84. for _, v := range Config.AdminUsers {
  85. if v == s {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. // JudgeVipUsers 判断用户是否为VIP用户
  92. func JudgeVipUsers(s string) bool {
  93. // 如果secret或者用户的userid都为空的话,那么默认不是VIP用户
  94. if len(Config.AppSecrets) == 0 || s == "" {
  95. return false
  96. }
  97. // 管理员默认是VIP用户
  98. for _, v := range Config.AdminUsers {
  99. if v == s {
  100. return true
  101. }
  102. }
  103. // 如果没有指定,则没有人是VIP用户
  104. if len(Config.VipUsers) == 0 {
  105. return false
  106. }
  107. for _, v := range Config.VipUsers {
  108. if v == s {
  109. return true
  110. }
  111. }
  112. return false
  113. }
  114. func GetReadTime(t time.Time) string {
  115. return t.Format("2006-01-02 15:04:05")
  116. }
  117. func CheckRequestWithCredentials(ts, sg string) (clientId string, pass bool) {
  118. clientId, pass = "", false
  119. credentials := Config.Credentials
  120. if credentials == nil || len(credentials) == 0 {
  121. return "", true
  122. }
  123. for _, credential := range Config.Credentials {
  124. stringToSign := fmt.Sprintf("%s\n%s", ts, credential.ClientSecret)
  125. mac := hmac.New(sha256.New, []byte(credential.ClientSecret))
  126. _, _ = mac.Write([]byte(stringToSign))
  127. if base64.StdEncoding.EncodeToString(mac.Sum(nil)) == sg {
  128. return credential.ClientID, true
  129. }
  130. }
  131. return
  132. }
  133. func CheckRequest(ts, sg string) bool {
  134. appSecrets := Config.AppSecrets
  135. // 如果没有指定或者outgoing类型机器人下使用,则默认不做校验
  136. if len(appSecrets) == 0 || sg == "" {
  137. return true
  138. }
  139. // 校验appSecret
  140. for _, secret := range appSecrets {
  141. stringToSign := fmt.Sprintf("%s\n%s", ts, secret)
  142. mac := hmac.New(sha256.New, []byte(secret))
  143. _, _ = mac.Write([]byte(stringToSign))
  144. if base64.StdEncoding.EncodeToString(mac.Sum(nil)) == sg {
  145. return true
  146. }
  147. }
  148. return false
  149. }
  150. // JudgeSensitiveWord 判断内容是否包含敏感词
  151. func JudgeSensitiveWord(s string) bool {
  152. if len(Config.SensitiveWords) == 0 {
  153. return false
  154. }
  155. for _, v := range Config.SensitiveWords {
  156. if strings.Contains(s, v) {
  157. return true
  158. }
  159. }
  160. return false
  161. }
  162. // SolveSensitiveWord 将敏感词用 🚫 占位
  163. func SolveSensitiveWord(s string) string {
  164. for _, v := range Config.SensitiveWords {
  165. if strings.Contains(s, v) {
  166. return strings.Replace(s, v, printStars(utf8.RuneCountInString(v)), -1)
  167. }
  168. }
  169. return s
  170. }
  171. // 将对应敏感词替换为 🚫
  172. func printStars(num int) string {
  173. s := ""
  174. for i := 0; i < num; i++ {
  175. s += "🚫"
  176. }
  177. return s
  178. }