wafformatter.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "github.com/go-nunu/nunu-layout-advanced/pkg/rabbitmq"
  9. amqp "github.com/rabbitmq/amqp091-go"
  10. "github.com/spf13/cast"
  11. "go.uber.org/zap"
  12. "golang.org/x/net/publicsuffix"
  13. "net"
  14. "slices"
  15. "strconv"
  16. )
  17. type WafFormatterService interface {
  18. require(ctx context.Context, req v1.GlobalRequire, category string) (v1.GlobalRequire, error)
  19. sendFormData(ctx context.Context,addTokenUrl string,addSendUrl string,formData map[string]interface{}) (int, error)
  20. validateWafPortCount(ctx context.Context, hostId int) error
  21. validateWafDomainCount(ctx context.Context, req v1.GlobalRequire) error
  22. ConvertToWildcardDomain(ctx context.Context,domain string) (string, error)
  23. AppendWafIp(ctx context.Context, req []string) ([]v1.IpInfo, error)
  24. WashIps(ctx context.Context, req []string) ([]string, error)
  25. PublishIpWhitelistTask(ips []string, action string)
  26. PublishDomainWhitelistTask(domain, ip, action string)
  27. findIpDifferences(oldIps, newIps []string) ([]string, []string)
  28. }
  29. func NewWafFormatterService(
  30. service *Service,
  31. globalRep repository.GlobalLimitRepository,
  32. hostRep repository.HostRepository,
  33. required RequiredService,
  34. parser ParserService,
  35. tcpforwardingRep repository.TcpforwardingRepository,
  36. udpForWardingRep repository.UdpForWardingRepository,
  37. webForwardingRep repository.WebForwardingRepository,
  38. mq *rabbitmq.RabbitMQ,
  39. host HostService,
  40. ) WafFormatterService {
  41. return &wafFormatterService{
  42. Service: service,
  43. globalRep: globalRep,
  44. hostRep: hostRep,
  45. required: required,
  46. parser: parser,
  47. tcpforwardingRep: tcpforwardingRep,
  48. udpForWardingRep: udpForWardingRep,
  49. webForwardingRep: webForwardingRep,
  50. host : host,
  51. mq: mq,
  52. }
  53. }
  54. type wafFormatterService struct {
  55. *Service
  56. globalRep repository.GlobalLimitRepository
  57. hostRep repository.HostRepository
  58. required RequiredService
  59. parser ParserService
  60. tcpforwardingRep repository.TcpforwardingRepository
  61. udpForWardingRep repository.UdpForWardingRepository
  62. webForwardingRep repository.WebForwardingRepository
  63. host HostService
  64. mq *rabbitmq.RabbitMQ
  65. }
  66. func (s *wafFormatterService) require(ctx context.Context,req v1.GlobalRequire,category string) (v1.GlobalRequire, error) {
  67. RuleIds, err := s.globalRep.GetGlobalLimitByHostId(ctx, int64(req.HostId))
  68. if err != nil {
  69. return v1.GlobalRequire{}, err
  70. }
  71. req.WafGatewayGroupId = RuleIds.GatewayGroupId
  72. switch category {
  73. case "tcp":
  74. req.LimitRuleId = RuleIds.TcpLimitRuleId
  75. case "udp":
  76. req.LimitRuleId = RuleIds.UdpLimitRuleId
  77. case "web":
  78. req.LimitRuleId = RuleIds.WebLimitRuleId
  79. }
  80. domain, err := s.hostRep.GetDomainById(ctx, req.HostId)
  81. if err != nil {
  82. return v1.GlobalRequire{}, err
  83. }
  84. req.Tag = strconv.Itoa(req.Uid) + "_" + strconv.Itoa(req.HostId) + "_" + domain + "_" + req.Comment
  85. return req, nil
  86. }
  87. func (s *wafFormatterService) sendFormData(ctx context.Context,addTokenUrl string,addSendUrl string,formData map[string]interface{}) (int, error) {
  88. respBody, err := s.required.SendForm(ctx, addTokenUrl, addSendUrl, formData)
  89. if err != nil {
  90. return 0, err
  91. }
  92. // 解析响应内容中的 alert 消息
  93. res, err := s.parser.ParseAlert(string(respBody))
  94. if err != nil {
  95. return 0,err
  96. }
  97. if res != "" {
  98. return 0,fmt.Errorf(res)
  99. }
  100. ruleIdStr, err := s.parser.GetRuleIdByColumnName(ctx, respBody,formData["tag"].(string))
  101. if err != nil {
  102. return 0, err
  103. }
  104. ruleId, err := cast.ToIntE(ruleIdStr)
  105. if err != nil {
  106. return 0,err
  107. }
  108. return ruleId, nil
  109. }
  110. func (s *wafFormatterService) validateWafPortCount(ctx context.Context, hostId int) error {
  111. congfig, err := s.host.GetGlobalLimitConfig(ctx, hostId)
  112. if err != nil {
  113. return err
  114. }
  115. tcpCount, err := s.tcpforwardingRep.GetTcpForwardingPortCountByHostId(ctx, hostId)
  116. if err != nil {
  117. return err
  118. }
  119. udpCount, err := s.udpForWardingRep.GetUdpForwardingPortCountByHostId(ctx, hostId)
  120. if err != nil {
  121. return err
  122. }
  123. webCount, err := s.webForwardingRep.GetWebForwardingPortCountByHostId(ctx, hostId)
  124. if err != nil {
  125. return err
  126. }
  127. if int64(congfig.PortCount) > tcpCount + udpCount + webCount {
  128. return nil
  129. }
  130. return fmt.Errorf("端口数量超出套餐限制,已配置%d个端口,套餐限制为%d个端口", tcpCount+udpCount+webCount, congfig.PortCount)
  131. }
  132. func (s *wafFormatterService) validateWafDomainCount(ctx context.Context, req v1.GlobalRequire) error {
  133. congfig, err := s.host.GetGlobalLimitConfig(ctx, req.HostId)
  134. if err != nil {
  135. return err
  136. }
  137. domainCount, domainSlice, err := s.webForwardingRep.GetWebForwardingDomainCountByHostId(ctx, req.HostId)
  138. if err != nil {
  139. return err
  140. }
  141. if req.Domain != "" {
  142. if !slices.Contains(domainSlice, req.Domain) {
  143. domainCount += 1
  144. if domainCount > int64(congfig.DomainCount) {
  145. return fmt.Errorf("域名数量已达到上限,已配置%d个域名,套餐限制为%d个域名", domainCount, congfig.DomainCount)
  146. }
  147. }
  148. }
  149. return nil
  150. }
  151. func (s *wafFormatterService) ConvertToWildcardDomain(ctx context.Context, domain string) (string, error) {
  152. // 1. 使用 EffectiveTLDPlusOne 获取可注册域名部分。
  153. // 例如,对于 "www.google.com",这将返回 "google.com"。
  154. // 对于 "a.b.c.tokyo.jp",这将返回 "c.tokyo.jp"。
  155. registrableDomain, err := publicsuffix.EffectiveTLDPlusOne(domain)
  156. if err != nil {
  157. // 如果域名无效(如 IP 地址、localhost),则返回错误。
  158. return "", fmt.Errorf("无法处理 '%s': %w", domain, err)
  159. }
  160. // 2. 比较原始域名和可注册域名。
  161. // 如果它们不相等,说明原始域名包含子域名。
  162. if domain != registrableDomain {
  163. // 3. 如果存在子域名,则用 "*." 加上可注册域名来构造通配符域名。
  164. return registrableDomain, nil
  165. }
  166. // 4. 如果原始域名和可注册域名相同(例如,输入就是 "google.com"),
  167. // 则说明没有子域名可替换,直接返回原始域名。
  168. return domain, nil
  169. }
  170. func (s *wafFormatterService) AppendWafIp(ctx context.Context, req []string) ([]v1.IpInfo, error) {
  171. var ips []v1.IpInfo
  172. for _, v := range req {
  173. ips = append(ips, v1.IpInfo{
  174. FType: "0",
  175. FStartIp: v,
  176. FEndIp: v,
  177. FRemark: "宁波高防IP过白",
  178. FServerIp: "",
  179. })
  180. }
  181. return ips, nil
  182. }
  183. func (s *wafFormatterService) AppendWafIpByRemovePort(ctx context.Context, req []string) ([]v1.IpInfo, error) {
  184. var ips []v1.IpInfo
  185. for _, v := range req {
  186. ip, _, err := net.SplitHostPort(v)
  187. if err != nil {
  188. return nil, err
  189. }
  190. ips = append(ips, v1.IpInfo{
  191. FType: "0",
  192. FStartIp: ip,
  193. FEndIp: ip,
  194. FRemark: "宁波高防IP过白",
  195. FServerIp: "",
  196. })
  197. }
  198. return ips, nil
  199. }
  200. func (s *wafFormatterService) WashIps(ctx context.Context, req []string) ([]string, error) {
  201. var res []string
  202. for _, v := range req {
  203. res = append(res,v)
  204. }
  205. return res, nil
  206. }
  207. // publishDomainWhitelistTask is a helper function to publish domain whitelist tasks to RabbitMQ.
  208. // It can handle different actions like "add" or "del".
  209. func (s *wafFormatterService) PublishDomainWhitelistTask(domain, ip, action string) {
  210. // Define message payload, including the action
  211. type domainTaskPayload struct {
  212. Domain string `json:"domain"`
  213. Ip string `json:"ip"`
  214. Action string `json:"action"`
  215. }
  216. payload := domainTaskPayload{
  217. Domain: domain,
  218. Ip: ip,
  219. Action: action,
  220. }
  221. // Serialize the message
  222. msgBody, err := json.Marshal(payload)
  223. if err != nil {
  224. s.logger.Error("Failed to serialize domain whitelist task message", zap.Error(err), zap.String("domain", domain), zap.String("ip", ip), zap.String("action", action))
  225. return
  226. }
  227. // Get task configuration
  228. taskCfg, ok := s.mq.GetTaskConfig("domain_whitelist")
  229. if !ok {
  230. s.logger.Error("Failed to get 'domain_whitelist' task configuration")
  231. return
  232. }
  233. // Construct the routing key dynamically based on the action
  234. routingKey := fmt.Sprintf("whitelist.domain.%s", action)
  235. // Construct the amqp.Publishing message
  236. publishingMsg := amqp.Publishing{
  237. ContentType: "application/json",
  238. Body: msgBody,
  239. DeliveryMode: amqp.Persistent, // Persistent message
  240. }
  241. // Publish the message
  242. err = s.mq.PublishWithCh(taskCfg.Exchange, routingKey, publishingMsg)
  243. if err != nil {
  244. s.logger.Error("发布 域名 白名单任务到 MQ 失败", zap.Error(err), zap.String("domain", domain), zap.String("action", action))
  245. } else {
  246. s.logger.Info("成功将 域名 白名单任务发布到 MQ", zap.String("domain", domain), zap.String("action", action))
  247. }
  248. }
  249. func (s *wafFormatterService) PublishIpWhitelistTask(ips []string, action string) {
  250. // Define message payload, including the action
  251. type ipTaskPayload struct {
  252. Ips []string `json:"ips"`
  253. Action string `json:"action"`
  254. }
  255. payload := ipTaskPayload{
  256. Ips: ips,
  257. Action: action,
  258. }
  259. // Serialize the message
  260. msgBody, err := json.Marshal(payload)
  261. if err != nil {
  262. s.logger.Error("序列化 IP 白名单任务消息失败", zap.Error(err), zap.Any("IPs", ips), zap.String("action", action))
  263. return
  264. }
  265. // Get task configuration
  266. taskCfg, ok := s.mq.GetTaskConfig("ip_white")
  267. if !ok {
  268. s.logger.Error("无法获取“ip_white”任务配置")
  269. return
  270. }
  271. // Construct the routing key dynamically based on the action
  272. routingKey := fmt.Sprintf("task.ip_white.%s", action)
  273. // Construct the amqp.Publishing message
  274. publishingMsg := amqp.Publishing{
  275. ContentType: "application/json",
  276. Body: msgBody,
  277. DeliveryMode: amqp.Persistent, // Persistent message
  278. }
  279. // Publish the message
  280. err = s.mq.PublishWithCh(taskCfg.Exchange, routingKey, publishingMsg)
  281. if err != nil {
  282. s.logger.Error("发布 IP 白名单任务到 MQ 失败", zap.Error(err), zap.String("action", action))
  283. } else {
  284. s.logger.Info("成功将 IP 白名单任务发布到 MQ", zap.String("action", action))
  285. }
  286. }
  287. func (s *wafFormatterService) findIpDifferences(oldIps, newIps []string) ([]string, []string) {
  288. // 使用 map 实现 set,用于快速查找
  289. oldIpsSet := make(map[string]struct{}, len(oldIps))
  290. for _, ip := range oldIps {
  291. oldIpsSet[ip] = struct{}{}
  292. }
  293. newIpsSet := make(map[string]struct{}, len(newIps))
  294. for _, ip := range newIps {
  295. newIpsSet[ip] = struct{}{}
  296. }
  297. var addedIps []string
  298. // 查找新增的 IP:存在于 newIpsSet 但不存在于 oldIpsSet
  299. for ip := range newIpsSet {
  300. if _, found := oldIpsSet[ip]; !found {
  301. addedIps = append(addedIps, ip)
  302. }
  303. }
  304. var removedIps []string
  305. // 查找移除的 IP:存在于 oldIpsSet 但不存在于 newIpsSet
  306. for ip := range oldIpsSet {
  307. if _, found := newIpsSet[ip]; !found {
  308. removedIps = append(removedIps, ip)
  309. }
  310. }
  311. return addedIps, removedIps
  312. }