(ip string, timeoutSeconds int, async bool)
| 166 | } |
| 167 | |
| 168 | func (this *Firewalld) DropSourceIP(ip string, timeoutSeconds int, async bool) error { |
| 169 | if !this.isReady { |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // 避免短时间内重复添加 |
| 174 | if async && this.checkLatestIP(ip) { |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | var family = "ipv4" |
| 179 | if strings.Contains(ip, ":") { |
| 180 | family = "ipv6" |
| 181 | } |
| 182 | var args = []string{"--add-rich-rule=rule family='" + family + "' source address='" + ip + "' drop"} |
| 183 | if timeoutSeconds > 0 { |
| 184 | args = append(args, "--timeout="+types.String(timeoutSeconds)+"s") |
| 185 | } |
| 186 | var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, args...) |
| 187 | if async { |
| 188 | this.pushCmd(cmd, ip) |
| 189 | return nil |
| 190 | } |
| 191 | |
| 192 | // 关闭连接 |
| 193 | defer conns.SharedMap.CloseIPConns(ip) |
| 194 | |
| 195 | err := cmd.Run() |
| 196 | if err != nil { |
| 197 | return fmt.Errorf("run command failed '%s': %w", cmd.String(), err) |
| 198 | } |
| 199 | return nil |
| 200 | } |
| 201 | |
| 202 | func (this *Firewalld) RemoveSourceIP(ip string) error { |
| 203 | if !this.isReady { |
nothing calls this directly
no test coverage detected