Go wrapper for firewalld D-Bus interface.
package main
import (
"log"
"github.com/digilolnet/go-firewalld"
)
func main() {
fw, err := firewalld.NewFirewalldClient()
if err != nil {
log.Println(err)
}
// Add new chain using permanent direct rule
if err := fw.DirectAddChainPermanent("ipv4", "filter", "MYCHAIN"); err != nil {
log.Println(err)
}
// Handle packets related to processes with owner UID 1000 at MYCHAIN
if err := fw.DirectAddRulePermanent("ipv4", "filter", "OUTPUT", 0, "-m owner --uid-owner 1000 -j MYCHAIN"); err != nil {
log.Println(err)
}
if err := fw.DirectAddRulePermanent("ipv4", "filter", "MYCHAIN", 0, "-m state --state ESTABLISHED,RELATED -j ACCEPT"); err != nil {
log.Println(err)
}
if err := fw.DirectAddRulePermanent("ipv4", "filter", "MYCHAIN", 0, "-p udp --dport 53 -j ACCEPT"); err != nil {
log.Println(err)
}
if err := fw.DirectAddRulePermanent("ipv4", "filter", "MYCHAIN", 0, "-j REJECT"); err != nil {
log.Println(err)
}
// Reload for changes to take effect immediately
if err := fw.Reload(); err != nil {
log.Println(err)
}
}