wafformatter.go 13 KB

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