Ejemplo n.º 1
1
func Run(ctx context.Context, vnet *dispatcher.VNET) <-chan error {
	out := make(chan error, 1)
	go func() {
		defer close(out)

		system := vnet.System()
		system.WaitForGatewayIPv4()

		l, err := net.ListenUDP("udp", &net.UDPAddr{IP: system.GatewayIPv4()})
		if err != nil {
			log.Printf("error=%s", err)
			return
		}

		_, err = vnet.Rules().AddRule(rules.Rule{
			DstPort:   uint16(l.LocalAddr().(*net.UDPAddr).Port),
			Protocol:  protocols.UDP,
			SrcHostID: vnet.Hosts().GetTable().LookupByName("controller").ID,
			SrcPort:   53,
		})
		if err != nil {
			log.Printf("error=%s", err)
			return
		}

		server := &dns.Server{
			Net:        "udp",
			Handler:    &handler{vnet: vnet},
			PacketConn: l,
		}

		err = server.ActivateAndServe()
		if err != nil {
			log.Printf("error=%s", err)
		}
	}()
	return out
}
Ejemplo n.º 2
0
func Run(ctx context.Context, vnet *dispatcher.VNET) error {
	var (
		port int
	)

	vnet.System().WaitForControllerIPv4()
	vnet.System().WaitForGatewayIPv4()

	l, err := net.Listen("tcp", vnet.System().GatewayIPv4().String()+":0")
	if err != nil {
		return err
	}

	controller := vnet.Hosts().GetTable().LookupByName("controller")

	port = l.Addr().(*net.TCPAddr).Port
	_, err = vnet.Rules().AddRule(rules.Rule{
		Protocol:  protocols.TCP,
		SrcHostID: controller.ID,
		SrcPort:   8080,
		DstPort:   uint16(port),
	})
	if err != nil {
		return err
	}

	grpcServer := grpc.NewServer()
	protocol.RegisterHostsServer(grpcServer, &hostsServer{hosts: vnet.Hosts()})
	protocol.RegisterRulesServer(grpcServer, &rulesServer{rules: vnet.Rules()})

	for _, ip := range controller.IPv4Addrs {
		log.Printf("API: %s:%d (external)", ip.String(), 8080)
	}
	log.Printf("API: %s:%d (internal)", vnet.System().GatewayIPv4(), port)

	go func() {
		<-ctx.Done()
		l.Close()
	}()

	go func() {
		err := grpcServer.Serve(l)
		if err != nil {
			log.Printf("API/error: %s", err)
		}
	}()

	return nil
}