| 1 | # go-nft |
| 2 | |
| 3 | [](https://www.apache.org/licenses/LICENSE-2.0) |
| 4 | [](https://github.com/networkplumbing/go-nft/actions/workflows/main.yml) |
| 5 | [](https://goreportcard.com/report/github.com/networkplumbing/go-nft) |
| 6 | |
| 7 | Go bindings for nft utility. |
| 8 | |
| 9 | go-nft wraps invocation of the nft utility with functions to append and delete |
| 10 | rules; create, clear and delete tables and chains. |
| 11 | |
| 12 | ## To start using go-nft |
| 13 | |
| 14 | go-nft is a library that provides a structured API to nftables. |
| 15 | |
| 16 | go-nft uses the [libnftables-json specification](https://www.mankier.com/5/libnftables-json) |
| 17 | and exposes a subset of its structures. |
| 18 | |
| 19 | - Apply the configuration: |
| 20 | ```golang |
| 21 | config := nft.NewConfig() |
| 22 | config.AddTable(nft.NewTable("mytable", nft.FamilyIP)) |
| 23 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 24 | defer cancel() |
| 25 | err := nft.ApplyConfigContext(ctx, config) |
| 26 | ``` |
| 27 | |
| 28 | - Read the configuration: |
| 29 | ```golang |
| 30 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 31 | defer cancel() |
| 32 | config, err := nft.ReadConfigContext(ctx) |
| 33 | nftVersion := config.Nftables[0].Metainfo.Version |
| 34 | ``` |
| 35 | |
| 36 | For full setup example, see the integration test [examples](tests/example). |
| 37 | |
| 38 | ## Contribution |
| 39 | |
| 40 | We welcome contribution of any kind! |
| 41 | Read [CONTRIBUTING](CONTRIBUTING.md) to learn how to contribute to the project. |
| 42 | |
| 43 | ## Changelog |
| 44 | |
| 45 | Please refer to [CHANGELOG](CHANGELOG) |