使用 go-conntrack 包来实现 conntrack 会话的创建和删除
package main import ( "fmt" "time" "github.com/florianl/go-conntrack" ) func main() { // 创建 conntrack 客户端 client, err := conntrack.Dial(nil) if err != nil { panic(err) } defer client.Close() // 创建 conntrack 对象 ct, err := client.New() if err != nil { panic(err) } defer ct.Close() // 创建 conntrack 元组 tuple := conntrack.Tuple{ IPVersion: 4, Protocol: conntrack.ProtocolUDP, OriginalSource: conntrack.Addr{ IP: [4]byte{127, 0, 0, 1}, Port: 12345, }, OriginalDestination: conntrack.Addr{ IP: [4]byte{127, 0, 0, 1}, Port: 12346, }, } // 创建 conntrack 属性 attr := conntrack.Attribute{ Type: conntrack.AttrTimeout, Value: uint32(time.Second * 60 / time.Millisecond), } // 创建 conntrack 对象 obj := conntrack.NewObject(tuple, attr) // 创建 conntrack 会话 err = ct.Create(obj) if err != nil { panic(err) } fmt.Println("Conntrack session created") // 等待 5 秒钟 time.Sleep(5 * time.Second) // 删除 conntrack 会话 err = ct.Delete(obj) if err != nil { panic(err) } fmt.Println("Conntrack session deleted") }