| 280 | } |
| 281 | |
| 282 | func (this *IPSetAction) runActionSingleIP(action string, listType IPListType, item *pb.IPItem) error { |
| 283 | if item.Type == "all" { |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | var listName string |
| 288 | var isIPv6 = strings.Contains(item.IpFrom, ":") |
| 289 | |
| 290 | switch listType { |
| 291 | case IPListTypeWhite: |
| 292 | if isIPv6 { |
| 293 | listName = this.config.WhiteNameIPv6 |
| 294 | } else { |
| 295 | listName = this.config.WhiteName |
| 296 | } |
| 297 | case IPListTypeBlack: |
| 298 | if isIPv6 { |
| 299 | listName = this.config.BlackNameIPv6 |
| 300 | } else { |
| 301 | listName = this.config.BlackName |
| 302 | } |
| 303 | default: |
| 304 | // 不支持的类型 |
| 305 | return nil |
| 306 | } |
| 307 | if len(listName) == 0 { |
| 308 | return nil |
| 309 | } |
| 310 | |
| 311 | var path = this.config.Path |
| 312 | var err error |
| 313 | if len(path) == 0 { |
| 314 | path, err = executils.LookPath("ipset") |
| 315 | if err != nil { |
| 316 | // 找不到ipset命令错误只提示一次 |
| 317 | if this.ipsetNotfound { |
| 318 | return nil |
| 319 | } |
| 320 | this.ipsetNotfound = true |
| 321 | return err |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // ipset add edge_ip_list 192.168.2.32 timeout 30 |
| 326 | var args = []string{} |
| 327 | switch action { |
| 328 | case "addItem": |
| 329 | args = append(args, "add") |
| 330 | case "deleteItem": |
| 331 | args = append(args, "del") |
| 332 | } |
| 333 | |
| 334 | args = append(args, listName, item.IpFrom) |
| 335 | if action == "addItem" { |
| 336 | var timestamp = time.Now().Unix() |
| 337 | if item.ExpiredAt > timestamp { |
| 338 | args = append(args, "timeout", strconv.FormatInt(item.ExpiredAt-timestamp, 10)) |
| 339 | } |