feat: use netlink

This commit is contained in:
fengcaiwen
2023-03-24 14:59:02 +08:00
parent 7435d2c75b
commit 4d01468e1d
3 changed files with 9 additions and 13 deletions

1
go.mod
View File

@@ -12,7 +12,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/miekg/dns v1.1.50 github.com/miekg/dns v1.1.50
github.com/milosgajdos/tenus v0.0.3
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 github.com/moby/term v0.0.0-20221205130635-1aeaba878587
github.com/opencontainers/image-spec v1.0.3-0.20220303224323-02efb9a75ee1 github.com/opencontainers/image-spec v1.0.3-0.20220303224323-02efb9a75ee1
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1

2
go.sum
View File

@@ -628,8 +628,6 @@ github.com/miekg/pkcs11 v1.0.2/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WT
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU= github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/milosgajdos/tenus v0.0.3 h1:jmaJzwaY1DUyYVD0lM4U+uvP2kkEg1VahDqRFxIkVBE=
github.com/milosgajdos/tenus v0.0.3/go.mod h1:eIjx29vNeDOYWJuCnaHY2r4fq5egetV26ry3on7p8qY=
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4= github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=

View File

@@ -11,7 +11,6 @@ import (
"github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types"
"github.com/docker/libcontainer/netlink" "github.com/docker/libcontainer/netlink"
"github.com/milosgajdos/tenus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
@@ -29,40 +28,40 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) {
mtu = config.DefaultMTU mtu = config.DefaultMTU
} }
var ifce tun.Device var device tun.Device
ifce, err = tun.CreateTUN("utun", mtu) device, err = tun.CreateTUN("utun", mtu)
if err != nil { if err != nil {
return return
} }
var name string var name string
name, err = ifce.Name() name, err = device.Name()
if err != nil { if err != nil {
return return
} }
ifc, err := net.InterfaceByName(name)
link, err := tenus.NewLinkFrom(name)
if err != nil { if err != nil {
err = fmt.Errorf("could not find interface name: %s", err)
return return
} }
cmd := fmt.Sprintf("ip link set dev %s mtu %d", name, mtu) cmd := fmt.Sprintf("ip link set dev %s mtu %d", name, mtu)
log.Debugf("[tun] %s", cmd) log.Debugf("[tun] %s", cmd)
if er := link.SetLinkMTU(mtu); er != nil { if er := netlink.NetworkSetMTU(ifc, mtu); er != nil {
err = fmt.Errorf("%s: %v", cmd, er) err = fmt.Errorf("%s: %v", cmd, er)
return return
} }
cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, name) cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, name)
log.Debugf("[tun] %s", cmd) log.Debugf("[tun] %s", cmd)
if er := link.SetLinkIp(ip, ipNet); er != nil { if er := netlink.NetworkLinkAddIp(ifc, ip, ipNet); er != nil {
err = fmt.Errorf("%s: %v", cmd, er) err = fmt.Errorf("%s: %v", cmd, er)
return return
} }
cmd = fmt.Sprintf("ip link set dev %s up", name) cmd = fmt.Sprintf("ip link set dev %s up", name)
log.Debugf("[tun] %s", cmd) log.Debugf("[tun] %s", cmd)
if er := link.SetLinkUp(); er != nil { if er := netlink.NetworkLinkUp(ifc); er != nil {
err = fmt.Errorf("%s: %v", cmd, er) err = fmt.Errorf("%s: %v", cmd, er)
return return
} }
@@ -81,7 +80,7 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) {
} }
conn = &tunConn{ conn = &tunConn{
ifce: ifce, ifce: device,
addr: &net.IPAddr{IP: ip}, addr: &net.IPAddr{IP: ip},
} }
return return