优化核心逻辑

This commit is contained in:
2025-01-08 14:52:14 +08:00
parent 172ccca162
commit b51d64644f
3 changed files with 35 additions and 12 deletions

View File

@@ -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 {