chore(netlink): separate linux only and OS independent code
- Move `Addr` and its `String` method to `types.go` - Move `IsWireguardSupported` to `wireguard.go` to have `family.go` OS independant - Remove dependency on vishvananda/netlink in `ipv6.go` - Move `Link` to `types.go` - Move `Route` to `types.go` - Move `Rule` and its `String` method to `types.go`
This commit is contained in:
@@ -1,19 +1,9 @@
|
|||||||
package netlink
|
package netlink
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Addr struct {
|
|
||||||
Network netip.Prefix
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a Addr) String() string {
|
|
||||||
return a.Network.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *NetLink) AddrList(link Link, family int) (
|
func (n *NetLink) AddrList(link Link, family int) (
|
||||||
addresses []Addr, err error) {
|
addresses []Addr, err error) {
|
||||||
netlinkLink := linkToNetlinkLink(&link)
|
netlinkLink := linkToNetlinkLink(&link)
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package netlink
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -24,16 +22,3 @@ func FamilyToString(family int) string {
|
|||||||
return fmt.Sprint(family)
|
return fmt.Sprint(family)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NetLink) IsWireguardSupported() (ok bool, err error) {
|
|
||||||
families, err := netlink.GenlFamilyList()
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("listing gen 1 families: %w", err)
|
|
||||||
}
|
|
||||||
for _, family := range families {
|
|
||||||
if family.Name == "wireguard" {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package netlink
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
|
func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
|
||||||
@@ -15,7 +13,7 @@ func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
|
|||||||
var totalRoutes uint
|
var totalRoutes uint
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
link := link
|
link := link
|
||||||
routes, err := n.RouteList(&link, netlink.FAMILY_V6)
|
routes, err := n.RouteList(&link, FamilyV6)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("listing IPv6 routes for link %s: %w",
|
return false, fmt.Errorf("listing IPv6 routes for link %s: %w",
|
||||||
link.Name, err)
|
link.Name, err)
|
||||||
|
|||||||
@@ -2,14 +2,6 @@ package netlink
|
|||||||
|
|
||||||
import "github.com/vishvananda/netlink"
|
import "github.com/vishvananda/netlink"
|
||||||
|
|
||||||
type Link struct {
|
|
||||||
Type string
|
|
||||||
Name string
|
|
||||||
Index int
|
|
||||||
EncapType string
|
|
||||||
MTU uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *NetLink) LinkList() (links []Link, err error) {
|
func (n *NetLink) LinkList() (links []Link, err error) {
|
||||||
netlinkLinks, err := netlink.LinkList()
|
netlinkLinks, err := netlink.LinkList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,22 +1,9 @@
|
|||||||
package netlink
|
package netlink
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Route struct {
|
|
||||||
LinkIndex int
|
|
||||||
Dst netip.Prefix
|
|
||||||
Src netip.Addr
|
|
||||||
Gw netip.Addr
|
|
||||||
Priority int
|
|
||||||
Family int
|
|
||||||
Table int
|
|
||||||
Type int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *NetLink) RouteList(link *Link, family int) (
|
func (n *NetLink) RouteList(link *Link, family int) (
|
||||||
routes []Route, err error) {
|
routes []Route, err error) {
|
||||||
netlinkLink := linkToNetlinkLink(link)
|
netlinkLink := linkToNetlinkLink(link)
|
||||||
|
|||||||
@@ -1,37 +1,9 @@
|
|||||||
package netlink
|
package netlink
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Rule struct {
|
|
||||||
Priority int
|
|
||||||
Family int
|
|
||||||
Table int
|
|
||||||
Mark int
|
|
||||||
Src netip.Prefix
|
|
||||||
Dst netip.Prefix
|
|
||||||
Invert bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Rule) String() string {
|
|
||||||
from := "all"
|
|
||||||
if r.Src.IsValid() {
|
|
||||||
from = r.Src.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
to := "all"
|
|
||||||
if r.Dst.IsValid() {
|
|
||||||
to = r.Dst.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
|
|
||||||
r.Priority, from, to, r.Table)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRule() Rule {
|
func NewRule() Rule {
|
||||||
// defaults found from netlink.NewRule() for fields we use,
|
// defaults found from netlink.NewRule() for fields we use,
|
||||||
// the rest of the defaults is set when converting from a `Rule`
|
// the rest of the defaults is set when converting from a `Rule`
|
||||||
|
|||||||
58
internal/netlink/types.go
Normal file
58
internal/netlink/types.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package netlink
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Addr struct {
|
||||||
|
Network netip.Prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Addr) String() string {
|
||||||
|
return a.Network.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Link struct {
|
||||||
|
Type string
|
||||||
|
Name string
|
||||||
|
Index int
|
||||||
|
EncapType string
|
||||||
|
MTU uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
type Route struct {
|
||||||
|
LinkIndex int
|
||||||
|
Dst netip.Prefix
|
||||||
|
Src netip.Addr
|
||||||
|
Gw netip.Addr
|
||||||
|
Priority int
|
||||||
|
Family int
|
||||||
|
Table int
|
||||||
|
Type int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
Priority int
|
||||||
|
Family int
|
||||||
|
Table int
|
||||||
|
Mark int
|
||||||
|
Src netip.Prefix
|
||||||
|
Dst netip.Prefix
|
||||||
|
Invert bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Rule) String() string {
|
||||||
|
from := "all"
|
||||||
|
if r.Src.IsValid() {
|
||||||
|
from = r.Src.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
to := "all"
|
||||||
|
if r.Dst.IsValid() {
|
||||||
|
to = r.Dst.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
|
||||||
|
r.Priority, from, to, r.Table)
|
||||||
|
}
|
||||||
20
internal/netlink/wireguard.go
Normal file
20
internal/netlink/wireguard.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package netlink
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/vishvananda/netlink"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n *NetLink) IsWireguardSupported() (ok bool, err error) {
|
||||||
|
families, err := netlink.GenlFamilyList()
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("listing gen 1 families: %w", err)
|
||||||
|
}
|
||||||
|
for _, family := range families {
|
||||||
|
if family.Name == "wireguard" {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user