From 190f2587d5620e86cc9d0868314d08f922217b9e Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Wed, 18 Mar 2020 21:58:45 +0100 Subject: WIP add Netplan to config struct --- pkg/tcpip/config/config.go | 34 +++++++++++++++++++++++----- pkg/tcpip/sample/wg_tunnel/main.go | 46 +++++++++++++++----------------------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/pkg/tcpip/config/config.go b/pkg/tcpip/config/config.go index 19e8711db..c3518ef35 100644 --- a/pkg/tcpip/config/config.go +++ b/pkg/tcpip/config/config.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/hex" "fmt" + "io/ioutil" "log" "net" "os" @@ -65,10 +66,6 @@ type Tuntap struct { type WireguardKey []byte -type Config struct { - routes []tcpip.Route -} - func (wgKey *WireguardKey) UnmarshalYAML(value *yaml.Node) error{ key, err := base64.StdEncoding.DecodeString(value.Value) fmt.Println("UnmarshalYAML", key, err) @@ -116,6 +113,32 @@ type Netplan struct { } `yaml:"network"` } +type Config struct { + np Netplan + routes []tcpip.Route +} + +func Load(yamlname string) (*Config, error) { + data, err := ioutil.ReadFile(yamlname) + if err != nil { + log.Fatalf("File reading error", err) + } + + var np Netplan + err = yaml.Unmarshal(data, &np) + fmt.Println("err", err) + fmt.Println("res", np) + + if err != nil { + return nil, err + } + + return &Config{ + np: np, + routes: []tcpip.Route{}, + }, nil +} + func CheckError(err error) { if err != nil { log.Fatal("Error: " , err) @@ -511,7 +534,8 @@ func (config *Config) SetRouteTable(s *stack.Stack) { s.SetRouteTable(config.routes) } -func (config *Config) Setup(s *stack.Stack, np *Netplan) { +func (config *Config) Setup(s *stack.Stack) { + var np *Netplan = &config.np s.SetForwarding(true) var nic tcpip.NICID = -1 diff --git a/pkg/tcpip/sample/wg_tunnel/main.go b/pkg/tcpip/sample/wg_tunnel/main.go index 47d0b40dc..618fa31f3 100644 --- a/pkg/tcpip/sample/wg_tunnel/main.go +++ b/pkg/tcpip/sample/wg_tunnel/main.go @@ -23,7 +23,6 @@ import ( "context" "flag" "fmt" - "io/ioutil" "log" "math/rand" "net" @@ -44,9 +43,6 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/transport/udp" "gvisor.dev/gvisor/pkg/waiter" - - "gopkg.in/yaml.v3" - "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/dhcpv6/nclient6" @@ -170,17 +166,18 @@ func dumpRoutes(s *stack.Stack) { } } -func KeepAliveTunnel(np *config.Netplan) { - KeepAliveTunnelEx(np, true) +func KeepAliveTunnel(cfg *config.Config) { + KeepAliveTunnelEx(cfg, true) } -func KeepAliveTunnelEx(np *config.Netplan, debug bool) { - for _, tun := range np.Network.Tunnels { - if debug { - fmt.Println("Tunnel ", tun.Mode, tun.Local, tun.Remote, tun.Conn, tun.Sd) - } - runtime.KeepAlive(tun.Conn) - } +func KeepAliveTunnelEx(cfg *config.Config, debug bool) { + // FIXME + // for _, tun := range np.Network.Tunnels { + // if debug { + // fmt.Println("Tunnel ", tun.Mode, tun.Local, tun.Remote, tun.Conn, tun.Sd) + // } + // runtime.KeepAlive(tun.Conn) + // } } func withIAPD(iaid [4]byte, prefixLength int) dhcpv6.Modifier { @@ -428,16 +425,11 @@ func main() { log.Fatal("Usage: ", os.Args[0], " ") } - data, err := ioutil.ReadFile("config.yaml") + cfg, err := config.Load("config.yaml") if err != nil { - log.Fatalf("File reading error", err) + log.Fatal("Unable to load config.yaml") } - var np config.Netplan - err = yaml.Unmarshal(data, &np) - fmt.Println("err", err) - fmt.Println("res", np) - portName := flag.Arg(0) rand.Seed(time.Now().UnixNano()) @@ -447,8 +439,6 @@ func main() { log.Fatalf("Unable to convert port %v: %v", portName, err) } - cfg := config.Config{} - // Create the stack with ip and tcp protocols, then add a tun-based // NIC and address. s := stack.New(stack.Options{ @@ -463,15 +453,15 @@ func main() { }) // FIXME enable - cfg.Setup(s, &np) + cfg.Setup(s) - KeepAliveTunnel(&np) + KeepAliveTunnel(cfg) // FIXME disabled for now, to test startSolicitingRouters if false { // FIXME var wg2Nic tcpip.NICID = -1 - doClient(s, &cfg, wg2Nic) + doClient(s, cfg, wg2Nic) } cfg.SetRouteTable(s) @@ -479,11 +469,11 @@ func main() { dumpAddresses(s) dumpRoutes(s) - KeepAliveTunnel(&np) + KeepAliveTunnel(cfg) runtime.GC() - KeepAliveTunnel(&np) + KeepAliveTunnel(cfg) // Create TCP endpoint, bind it, then start listening. if true { @@ -522,7 +512,7 @@ func main() { log.Fatal("Accept() failed:", err) } - KeepAliveTunnelEx(&np, false) + KeepAliveTunnelEx(cfg, false) go echo(wq, n) } } -- cgit v1.2.3