coreos / go-iptables

Go wrapper around iptables utility
Apache License 2.0
1.11k stars 257 forks source link

I should how to insert rules with go-iptables #85

Open cylonchau opened 3 years ago

cylonchau commented 3 years ago

hi , I tried insert rule,but I do not what format should be inserted? go-iptables only get but not insert rules?

version

system version:centos7 golang version:1.15.3

code

func main() {
    ips, err := iptables.New()
    if err != nil {
        panic(err)
    }

    stas, err := ips.StructuredStats("filter", "IN_public_allow")
    if err != nil {
        panic(err)
    }

    fmt.Printf("%#v\n\n ", stas)

    stasstr, err := ips.Stats("filter", "IN_public_allow")

    for _, v := range stasstr {
        if err := ips.Append("filter", "INPUT", v...); err != nil {
            panic(err)
        }
    }

    stasstr, err = ips.Stats("filter", "IN_public_allow")
    if err != nil {
        panic(err)
    }

    fmt.Printf("%#v\n\n ", stasstr)
}

phenomenon

I I tried insert rule,but I do not what format should be inserted?but I tried used Results obtained by go-iptables,but or not insert.

[root@node01 ~]# ./iptables-api 
[]iptables.Stat{iptables.Stat{Packets:0x2, Bytes:0xa8, Target:"ACCEPT", Protocol:"tcp", Opt:"--", Input:"*", Output:"*", Source:(*net.IPNet)(0xc000058870), Destination:(*net.IPNet)(0xc0000588a0), Options:"tcp dpt:22 ctstate NEW,UNTRACKED"}, iptables.Stat{Packets:0x0, Bytes:0x0, Target:"ACCEPT", Protocol:"tcp", Opt:"--", Input:"*", Output:"*", Source:(*net.IPNet)(0xc0000588d0), Destination:(*net.IPNet)(0xc000058900), Options:"tcp dpt:1088 ctstate NEW,UNTRACKED"}}

    panic: running [/usr/sbin/iptables -t filter -A INPUT 2 168 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW,UNTRACKED --wait]: exit status 2: Bad argument `2'
Try `iptables -h' or 'iptables --help' for more information.

goroutine 1 [running]:
main.main()
    /mnt/d/go_work/src/netlink/main.go:58 +0x488

go-iptables generated format cannot be inserted , it's not iptables cli format? I should how to insert rules with go-iptables?

JerryZhou343 commented 2 years ago

use root user to run your process.