添加TCP规则
(tcpConfig *ddosconfigs.TCPConfig)
| 166 | |
| 167 | // 添加TCP规则 |
| 168 | func (this *DDoSProtectionManager) addTCPRules(tcpConfig *ddosconfigs.TCPConfig) error { |
| 169 | var nftExe = nftables.NftExePath() |
| 170 | if len(nftExe) == 0 { |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | // 检查nft版本不能小于0.9 |
| 175 | if len(nftablesInstance.version) > 0 && stringutil.VersionCompare("0.9", nftablesInstance.version) > 0 { |
| 176 | return nil |
| 177 | } |
| 178 | |
| 179 | var ports = []int32{} |
| 180 | for _, portConfig := range tcpConfig.Ports { |
| 181 | if !lists.ContainsInt32(ports, portConfig.Port) { |
| 182 | ports = append(ports, portConfig.Port) |
| 183 | } |
| 184 | } |
| 185 | if len(ports) == 0 { |
| 186 | ports = []int32{80, 443} |
| 187 | } |
| 188 | |
| 189 | for _, filter := range nftablesFilters { |
| 190 | chain, oldRules, err := this.getRules(filter) |
| 191 | if err != nil { |
| 192 | return fmt.Errorf("get old rules failed: %w", err) |
| 193 | } |
| 194 | |
| 195 | var protocol = filter.protocol() |
| 196 | |
| 197 | // max connections |
| 198 | var maxConnections = tcpConfig.MaxConnections |
| 199 | if maxConnections <= 0 { |
| 200 | maxConnections = nodeconfigs.DefaultTCPMaxConnections |
| 201 | if maxConnections <= 0 { |
| 202 | maxConnections = 100000 |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // max connections per ip |
| 207 | var maxConnectionsPerIP = tcpConfig.MaxConnectionsPerIP |
| 208 | if maxConnectionsPerIP <= 0 { |
| 209 | maxConnectionsPerIP = nodeconfigs.DefaultTCPMaxConnectionsPerIP |
| 210 | if maxConnectionsPerIP <= 0 { |
| 211 | maxConnectionsPerIP = 100000 |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // new connections rate (minutely) |
| 216 | var newConnectionsMinutelyRate = tcpConfig.NewConnectionsMinutelyRate |
| 217 | if newConnectionsMinutelyRate <= 0 { |
| 218 | newConnectionsMinutelyRate = nodeconfigs.DefaultTCPNewConnectionsMinutelyRate |
| 219 | if newConnectionsMinutelyRate <= 0 { |
| 220 | newConnectionsMinutelyRate = 100000 |
| 221 | } |
| 222 | } |
| 223 | var newConnectionsMinutelyRateBlockTimeout = tcpConfig.NewConnectionsMinutelyRateBlockTimeout |
| 224 | if newConnectionsMinutelyRateBlockTimeout < 0 { |
| 225 | newConnectionsMinutelyRateBlockTimeout = 0 |
no test coverage detected