wafformatter.go 13 KB

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