优化核心逻辑
This commit is contained in:
20
ipset.go
20
ipset.go
@@ -62,7 +62,7 @@ func NumIPSet(setName string) (int, error) {
|
||||
}
|
||||
|
||||
// IsIpset 检查名为 setName 的 ipset 是否存在,通过返回 0 表示存在,非零表示不存在或其他错误。
|
||||
func IsIpset(setName string) int {
|
||||
func Is_Name_Ipset(setName string) int {
|
||||
cmd := exec.Command("ipset", "list", setName)
|
||||
err := cmd.Run()
|
||||
|
||||
@@ -80,6 +80,24 @@ func IsIpset(setName string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func Is_Ip_Ipset(ip string) int {
|
||||
cmd := exec.Command("sh", "-c", fmt.Sprintf("ipset list | grep \"%s\" | cut -d ':' -f 2 | sed 's/ //g'", ip))
|
||||
err := cmd.Run()
|
||||
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
// The program has exited with an exit code != 0
|
||||
return exitError.ExitCode()
|
||||
} else {
|
||||
// Another error occurred (e.g., command not found)
|
||||
return -1 // 或者你可以选择其他方式来标识这种情况
|
||||
}
|
||||
}
|
||||
|
||||
// Command executed successfully, the set exists
|
||||
return 0
|
||||
}
|
||||
|
||||
// 添加 Iptables 规则
|
||||
func iptables_add(setName string) error {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user