regexp.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package http
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "regexp"
  7. "strconv"
  8. "go-common/app/service/main/antispam/conf"
  9. "go-common/app/service/main/antispam/dao"
  10. "go-common/app/service/main/antispam/model"
  11. "go-common/app/service/main/antispam/util"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. )
  16. // GetRegexp .
  17. func GetRegexp(c *bm.Context) {
  18. params := c.Request.Form
  19. _, err := getAdminID(params)
  20. if err != nil {
  21. errResp(c, ecode.RequestErr, err)
  22. return
  23. }
  24. id, err := strconv.ParseInt(params.Get(ProtocolRegexpID), 10, 64)
  25. if err != nil {
  26. errResp(c, ecode.RequestErr, err)
  27. return
  28. }
  29. regexp, err := Svr.GetRegexpByID(c, id)
  30. if err != nil {
  31. errResp(c, ecode.RequestErr, err)
  32. return
  33. }
  34. c.JSON(regexp, nil)
  35. }
  36. // GetRegexps .
  37. func GetRegexps(c *bm.Context) {
  38. params := c.Request.Form
  39. _, area, err := getAdminIDAndArea(params)
  40. if err != nil {
  41. errResp(c, ecode.RequestErr, err)
  42. return
  43. }
  44. cond := &Condition{
  45. Area: area,
  46. Pagination: &util.Pagination{},
  47. }
  48. if params.Get(ProtocolCurPage) != "" {
  49. if cond.CurPage, err = strconv.ParseInt(params.Get(ProtocolCurPage), 10, 64); err != nil {
  50. log.Error("%v", err)
  51. errResp(c, ecode.RequestErr, err)
  52. return
  53. }
  54. }
  55. if perPage := params.Get(ProtocolPerPage); perPage != "" {
  56. if cond.PerPage, err = strconv.ParseInt(perPage, 10, 64); err != nil {
  57. log.Error("%v", err)
  58. errResp(c, ecode.RequestErr, err)
  59. return
  60. }
  61. }
  62. if err = cond.Valid(); err != nil {
  63. log.Error("%v", err)
  64. errResp(c, ecode.RequestErr, err)
  65. return
  66. }
  67. regexps, total, err := Svr.GetRegexpsByCond(c, ToServiceCond(cond))
  68. if err != nil {
  69. errResp(c, ecode.RequestErr, err)
  70. return
  71. }
  72. res := map[string]interface{}{}
  73. res[ProtocolTotalCounts] = total
  74. res[ProtocolData] = regexps
  75. c.JSONMap(res, nil)
  76. }
  77. // EditRegexp .
  78. func EditRegexp(c *bm.Context) {
  79. params := c.Request.Form
  80. if _, err := getAdminID(params); err != nil {
  81. errResp(c, ecode.RequestErr, err)
  82. return
  83. }
  84. id, err := strconv.ParseInt(params.Get(ProtocolRegexpID), 10, 64)
  85. if err != nil {
  86. log.Error("%v", err)
  87. errResp(c, ecode.RequestErr, err)
  88. return
  89. }
  90. addOrEditRegexp(c, params, id)
  91. }
  92. // AddRegexp .
  93. func AddRegexp(c *bm.Context) {
  94. params := c.Request.Form
  95. if _, _, err := getAdminIDAndArea(params); err != nil {
  96. errResp(c, ecode.RequestErr, err)
  97. return
  98. }
  99. var id int64
  100. addOrEditRegexp(c, params, id)
  101. }
  102. // RecoverRegexp .
  103. func RecoverRegexp(c *bm.Context) {
  104. params := c.Request.Form
  105. adminID, err := getAdminID(params)
  106. if err != nil {
  107. errResp(c, ecode.RequestErr, err)
  108. return
  109. }
  110. id, err := strconv.ParseInt(params.Get(ProtocolRegexpID), 10, 64)
  111. if err != nil {
  112. log.Error("%v", err)
  113. errResp(c, ecode.RequestErr, err)
  114. return
  115. }
  116. r, err := Svr.GetRegexpByID(c, id)
  117. if err != nil {
  118. errResp(c, ecode.RequestErr, err)
  119. return
  120. }
  121. if r.State != model.StateDeleted {
  122. c.JSON(r, nil)
  123. return
  124. }
  125. _, total, err := Svr.GetRegexpsByCond(c, ToServiceCond(&Condition{State: model.StateDefault, Area: r.Area}))
  126. if err != nil {
  127. errResp(c, ecode.ServerErr, err)
  128. return
  129. }
  130. if total >= conf.Conf.ServiceOption.MaxRegexpCountsPerArea {
  131. err = fmt.Errorf("regexp counts cannot exceed %d", conf.Conf.ServiceOption.MaxRegexpCountsPerArea)
  132. log.Error("%v", err)
  133. errResp(c, ecode.RequestErr, err)
  134. return
  135. }
  136. r.State, r.AdminID = model.StateDefault, adminID
  137. result, err := Svr.UpsertRegexp(c, r)
  138. if err != nil {
  139. errResp(c, ecode.ServerErr, err)
  140. return
  141. }
  142. c.JSON(result, nil)
  143. }
  144. // DeleteRegexp .
  145. func DeleteRegexp(c *bm.Context) {
  146. params := c.Request.Form
  147. adminID, err := getAdminID(params)
  148. if err != nil {
  149. errResp(c, ecode.RequestErr, err)
  150. return
  151. }
  152. id, err := strconv.ParseInt(params.Get(ProtocolRegexpID), 10, 64)
  153. if err != nil {
  154. log.Error("%v", err)
  155. errResp(c, ecode.RequestErr, err)
  156. return
  157. }
  158. r, err := Svr.DeleteRegexp(c, id, adminID)
  159. if err != nil {
  160. errResp(c, ecode.ServerErr, err)
  161. return
  162. }
  163. c.JSON(r, nil)
  164. }
  165. func addOrEditRegexp(c *bm.Context, params url.Values, id int64) {
  166. adminID, err := getAdminID(params)
  167. if err != nil {
  168. errResp(c, ecode.RequestErr, err)
  169. return
  170. }
  171. name := params.Get(ProtocolRegexpName)
  172. if name == "" {
  173. err = errors.New("empty regexp name")
  174. log.Error("%v", err)
  175. errResp(c, ecode.RequestErr, err)
  176. return
  177. }
  178. content := params.Get(ProtocolRegexpContent)
  179. if content == "" {
  180. err = errors.New("empty regexp content")
  181. log.Error("%v", err)
  182. errResp(c, ecode.RequestErr, err)
  183. return
  184. }
  185. if _, err = regexp.Compile(content); err != nil {
  186. log.Error("%v", err)
  187. errResp(c, ecode.FilterIllegalRegexp, err)
  188. return
  189. }
  190. op := params.Get(ProtocolRegexpOperation)
  191. if op == "" {
  192. op = model.OperationLimit
  193. }
  194. area := params.Get(ProtocolArea)
  195. if id == 0 {
  196. _, total, err1 := Svr.GetRegexpsByCond(c, ToServiceCond(&Condition{State: model.StateDefault, Area: area}))
  197. if err1 != nil {
  198. errResp(c, ecode.ServerErr, err1)
  199. return
  200. }
  201. if total >= conf.Conf.ServiceOption.MaxRegexpCountsPerArea {
  202. err1 = fmt.Errorf("regexp counts cannot exceed %d", conf.Conf.ServiceOption.MaxRegexpCountsPerArea)
  203. log.Error("%v", err1)
  204. errResp(c, ecode.RequestErr, err1)
  205. return
  206. }
  207. oldR, err1 := Svr.GetRegexpByAreaAndContent(c, area, content)
  208. if err1 == nil {
  209. if oldR.State != model.StateDefault {
  210. oldR.State = model.StateDefault
  211. result, err2 := Svr.UpsertRegexp(c, oldR)
  212. if err2 != nil {
  213. errResp(c, ecode.ServerErr, err2)
  214. return
  215. }
  216. c.JSON(result, nil)
  217. return
  218. }
  219. err1 = fmt.Errorf("regexp(%s) already exists", content)
  220. log.Error("%v", err1)
  221. errResp(c, ecode.RequestErr, err1)
  222. return
  223. }
  224. if err != dao.ErrResourceNotExist {
  225. log.Error("%v", err1)
  226. errResp(c, ecode.ServerErr, err1)
  227. return
  228. }
  229. }
  230. r := &model.Regexp{
  231. ID: id,
  232. Area: area,
  233. AdminID: adminID,
  234. Name: name,
  235. Content: content,
  236. Operation: op,
  237. }
  238. result, err := Svr.UpsertRegexp(c, r)
  239. if err != nil {
  240. errResp(c, ecode.ServerErr, err)
  241. return
  242. }
  243. c.JSON(result, nil)
  244. }