Apply 应用配置
(config *ddosconfigs.ProtectionConfig)
| 73 | |
| 74 | // Apply 应用配置 |
| 75 | func (this *DDoSProtectionManager) Apply(config *ddosconfigs.ProtectionConfig) error { |
| 76 | // 加锁防止并发更改 |
| 77 | if !this.locker.TryLock() { |
| 78 | return nil |
| 79 | } |
| 80 | defer this.locker.Unlock() |
| 81 | |
| 82 | // 同集群节点IP白名单 |
| 83 | var allowIPListChanged = false |
| 84 | nodeConfig, _ := nodeconfigs.SharedNodeConfig() |
| 85 | if nodeConfig != nil { |
| 86 | var allowIPList = nodeConfig.AllowedIPs |
| 87 | if !utils.EqualStrings(allowIPList, this.lastAllowIPList) { |
| 88 | allowIPListChanged = true |
| 89 | this.lastAllowIPList = allowIPList |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // 对比配置 |
| 94 | configJSON, err := json.Marshal(config) |
| 95 | if err != nil { |
| 96 | return fmt.Errorf("encode config to json failed: %w", err) |
| 97 | } |
| 98 | if !allowIPListChanged && bytes.Equal(this.lastConfig, configJSON) { |
| 99 | return nil |
| 100 | } |
| 101 | remotelogs.Println("FIREWALL", "change DDoS protection config") |
| 102 | |
| 103 | if len(nftables.NftExePath()) == 0 { |
| 104 | return errors.New("can not find nft command") |
| 105 | } |
| 106 | |
| 107 | if nftablesInstance == nil { |
| 108 | if config == nil || !config.IsOn() { |
| 109 | return nil |
| 110 | } |
| 111 | return errors.New("nftables instance should not be nil") |
| 112 | } |
| 113 | |
| 114 | if config == nil { |
| 115 | // TCP |
| 116 | err := this.removeTCPRules() |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | // TODO other protocols |
| 122 | |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // TCP |
| 127 | if config.TCP == nil { |
| 128 | err := this.removeTCPRules() |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | } else { |
no test coverage detected