Example #1
0
func ExampleIPv4List() {
	ctrl := NewController(ports.NewMapper())
	ctrl.AddHost(&Host{IPv4: net.IPv4(172, 18, 0, 3)})
	ctrl.AddHost(&Host{IPv4: net.IPv4(172, 18, 0, 5)})
	ctrl.AddHost(&Host{IPv4: net.IPv4(172, 18, 0, 4)})
	ctrl.AddHost(&Host{})
	ctrl.AddHost(&Host{IPv4: net.IPv4(172, 18, 0, 2)})

	tab := ctrl.GetTable()
	for _, host := range tab.ipv4 {
		fmt.Printf("%s\n", host.IPv4)
	}

	fmt.Println("---")

	if host := tab.LookupByIPv4(net.IPv4(172, 18, 0, 3)); host != nil {
		fmt.Printf("lookup: %s ok\n", host.IPv4)
	} else {
		fmt.Printf("lookup: %s failed\n", net.IPv4(172, 18, 0, 3))
	}

	if host := tab.LookupByIPv4(net.IPv4(172, 18, 0, 5)); host != nil {
		fmt.Printf("lookup: %s ok\n", host.IPv4)
	} else {
		fmt.Printf("lookup: %s failed\n", net.IPv4(172, 18, 0, 5))
	}

	if host := tab.LookupByIPv4(net.IPv4(172, 18, 0, 4)); host != nil {
		fmt.Printf("lookup: %s ok\n", host.IPv4)
	} else {
		fmt.Printf("lookup: %s failed\n", net.IPv4(172, 18, 0, 4))
	}

	if host := tab.LookupByIPv4(net.IPv4(172, 18, 0, 2)); host != nil {
		fmt.Printf("lookup: %s ok\n", host.IPv4)
	} else {
		fmt.Printf("lookup: %s failed\n", net.IPv4(172, 18, 0, 2))
	}

	// Output:
	// <nil>
	// 172.18.0.2
	// 172.18.0.3
	// 172.18.0.4
	// 172.18.0.5
	// ---
	// lookup: 172.18.0.3 ok
	// lookup: 172.18.0.5 ok
	// lookup: 172.18.0.4 ok
	// lookup: 172.18.0.2 ok
}
Example #2
0
func Example() {
	var (
		pm   = ports.NewMapper()
		ctrl = NewController(pm)
	)

	ctrl.AddRoute(&Route{
		Protocol: protocols.TCP,
		HostID:   "host-b",
		Inbound: Stream{
			SrcIP:   net.IPv4(127, 0, 1, 1),
			SrcPort: 22001,
			DstIP:   net.IPv4(127, 0, 1, 2),
			DstPort: 1024,
		},
		Outbound: Stream{
			SrcIP:   net.IPv4(127, 0, 1, 2),
			SrcPort: 22001,
			DstIP:   net.IPv4(127, 0, 1, 3),
			DstPort: 1024,
		},
	})

	ctrl.AddRoute(&Route{
		Protocol: protocols.TCP,
		HostID:   "host-a",
		Inbound: Stream{
			SrcIP:   net.IPv4(127, 0, 0, 1),
			SrcPort: 22001,
			DstIP:   net.IPv4(127, 0, 0, 2),
			DstPort: 1024,
		},
		Outbound: Stream{
			SrcIP:   net.IPv4(127, 0, 0, 2),
			SrcPort: 22001,
			DstIP:   net.IPv4(127, 0, 0, 3),
			DstPort: 1024,
		},
	})

	for _, route := range ctrl.GetTable().routes {
		fmt.Printf("%s\n", route)
	}

	fmt.Println("---")

	tab := ctrl.GetTable()
	fmt.Printf("%s\n", tab.Lookup(protocols.TCP, net.IPv4(127, 0, 1, 1), net.IPv4(127, 0, 1, 2), 22001, 1024))
	fmt.Printf("%s\n", tab.Lookup(protocols.TCP, net.IPv4(127, 0, 1, 3), net.IPv4(127, 0, 1, 2), 1024, 22001))
	fmt.Printf("%s\n", tab.Lookup(protocols.TCP, net.IPv4(127, 0, 0, 1), net.IPv4(127, 0, 0, 2), 22001, 1024))
	fmt.Printf("%s\n", tab.Lookup(protocols.TCP, net.IPv4(127, 0, 0, 3), net.IPv4(127, 0, 0, 2), 1024, 22001))

	fmt.Println("---")

	fmt.Printf("%s\n", tab.Lookup(protocols.TCP, net.IPv4(127, 0, 1, 1), net.IPv4(127, 0, 1, 2), 22001, 1025))

	// Output:
	// Route{host-a, TCP, (127.0.0.1:22001 -> 127.0.0.2:1024) => (127.0.0.2:22001 -> 127.0.0.3:1024)}
	// Route{host-a, TCP, (127.0.0.3:1024 -> 127.0.0.2:22001) => (127.0.0.2:1024 -> 127.0.0.1:22001)}
	// Route{host-b, TCP, (127.0.1.1:22001 -> 127.0.1.2:1024) => (127.0.1.2:22001 -> 127.0.1.3:1024)}
	// Route{host-b, TCP, (127.0.1.3:1024 -> 127.0.1.2:22001) => (127.0.1.2:1024 -> 127.0.1.1:22001)}
	// ---
	// Route{host-b, TCP, (127.0.1.1:22001 -> 127.0.1.2:1024) => (127.0.1.2:22001 -> 127.0.1.3:1024)}
	// Route{host-b, TCP, (127.0.1.3:1024 -> 127.0.1.2:22001) => (127.0.1.2:1024 -> 127.0.1.1:22001)}
	// Route{host-a, TCP, (127.0.0.1:22001 -> 127.0.0.2:1024) => (127.0.0.2:22001 -> 127.0.0.3:1024)}
	// Route{host-a, TCP, (127.0.0.3:1024 -> 127.0.0.2:22001) => (127.0.0.2:1024 -> 127.0.0.1:22001)}
	// ---
	// <nil>
}
Example #3
0
func Run(ctx context.Context) (*VNET, error) {
	rand.Seed(time.Now().Unix())

	p := ports.NewMapper()
	r := routes.NewController(p)

	vnet := &VNET{
		ports:  p,
		routes: r,
		hosts:  hosts.NewController(p),
		rules:  rules.NewController(p),
		peers:  peers.NewController(),
		proxy:  proxy.NewProxy(r),
		system: &System{},
	}

	{ // insert controller
		host, err := vnet.hosts.AddHost(&hosts.Host{
			ID:    "7ce86376-34f0-4951-bead-6152c8291f1c",
			Name:  "controller",
			Local: true,

			IPv6Addrs: []net.IP{net.ParseIP("fd4c:bd56:5cee:8000::2")},

			Up: true,
		})
		if err != nil {
			return nil, err
		}
		log.Printf("insert %s: %v", host.Name, host)
	}

	host, err := vnet.hosts.AddHost(&hosts.Host{
		IPv4Addrs: []net.IP{net.IPv4(172, 18, 0, 2)},
		Local:     true,
		Up:        true,
	})
	if err != nil {
		panic(err)
	}
	log.Printf("insert %s: %v", host.Name, host)

	rule, err := vnet.rules.AddRule(rules.Rule{
		Protocol:  protocols.TCP,
		SrcHostID: host.ID,
		// SrcPort:   80,
		// DstPort:   20559,
		SrcPort: 80,
		DstIP:   net.IPv4(192, 168, 99, 100),
		DstPort: 32768,
		// SrcPort: 80,
		// DstIP:   net.IPv4(64, 15, 124, 217),
	})
	if err != nil {
		panic(err)
	}
	log.Printf("insert: %v", rule)

	iface, err := vmnet.Open("31fbf731-e896-4d03-9bc8-7a6221b91860")
	if err != nil {
		return nil, err
	}
	vnet.iface = iface

	vnet.chanEth = vnet.dispatchEthernet(ctx)
	vnet.chanArp = vnet.dispatchARP(ctx)
	vnet.chanIpv4 = vnet.dispatchIPv4(ctx)
	vnet.chanICMP = vnet.dispatchICMP(ctx)
	vnet.chanUDP = vnet.dispatchUDP(ctx)
	vnet.chanTCP = vnet.dispatchTCP(ctx)
	vnet.chanDHCP = vnet.dispatchDHCP(ctx)

	vnet.wg.Add(6)
	go vnet.runReader(ctx)
	go vnet.vmnetCloser(ctx)
	go vnet.gc(ctx)
	go vnet.addGatewayHost(ctx)
	go vnet.addIPv6AddressToVMNET(ctx)
	go vnet.routeIPv4SubnetToController(ctx)

	err = vnet.proxy.Run(ctx)
	if err != nil {
		return nil, err
	}

	log.Printf("UUID: %s", vnet.iface.ID())
	log.Printf("MAC:  %s", vnet.iface.HardwareAddr())

	vnet.system.SetControllerMAC(vnet.iface.HardwareAddr())
	return vnet, nil
}