Пример #1
0
        public static bool Start(IEnumerable <NetworkInterface> requiredNics)
        {
            Global.WriteLog("Load Balancer is starting.", true);
            Status.Update(State.Starting);
            if (requiredNics.Count() == 0)
            {
                Global.WriteLog("Load Balancer can't start without any physical interface.", true);
                Global.ShowTrayTip("Load Balancer", "Can't start whitout any physical interface", System.Windows.Forms.ToolTipIcon.Error);
                Status.Update(State.Failed);
                return(false);
            }
            if (!Jobs.Extensions.Dependencies.Check())
            {
                Status.Update(State.Failed);
                return(false);
            }
            if (!TapInterface.PutUp())
            {
                Global.WriteLog("Load Balancer failed to connect to " + TapInterface.FriendlyName, true);
                Global.ShowTrayTip("Load Balancer", "Failed to connect to " + TapInterface.FriendlyName, System.Windows.Forms.ToolTipIcon.Error);
                Status.Update(State.Failed);
                return(false);
            }
            NetworkInterface tapInterface = new NetworkInterface();

            tapInterface.Guid = TapInterface.Guid;
            tapInterface.Name = TapInterface.FriendlyName;
            if (!Dependencies.RunWinPcapService(requiredNics.Concat(new NetworkInterface[] { tapInterface }), true))
            {
                Global.WriteLog("Load Balancer failed to start because some interfaces were not captured by WinPcap.", true);
                Global.ShowTrayTip("Load Balancer", "Failed to start", System.Windows.Forms.ToolTipIcon.Error);
                TapInterface.PutDown();
                Status.Update(State.Failed);
                return(false);
            }
            Interfaces = requiredNics;
            // start LB threads
            LoadingForm splash = LoadingForm.Create("Initializing ...");

            foreach (NetworkInterface nic in Global.NetworkInterfaces.Values)
            {
                if (nic.Guid != TapInterface.Guid &&
                    (nic.IPv4Gateway.Count > 0 || nic.IPv6Gateway.Count > 0))
                {
                    splash.UpdateStatus("Configuring " + nic.Name + " ...");
                    nic.SetInterfaceMetric("4000");
                    foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway)
                    {
                        nic.EditIPv4Gateway(ip.Address, "4000");
                    }
                    foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway)
                    {
                        nic.EditIPv6Gateway(ip.Address, "4000");
                    }
                }
            }
            splash.UpdateStatus("Initializing " + TapInterface.FriendlyName + " ...");
            physicalWorkers.Clear();
            tapWorker = new TapWorker(TapInterface.Guid, TapInterface.FriendlyName, TapInterface.Mac,
                                      Global.Config.LoadBalancer.IPv4LocalAddresses.First().Address, Global.Config.LoadBalancer.IPv4GatewayAddresses.First().Address);
            new Thread(new ThreadStart(tapWorker.ReceivePackets)).Start();
            tapWorker.Initialized.Wait(1000);
            foreach (NetworkInterface nic in requiredNics)
            {
                splash.UpdateStatus("Initializing " + nic.Name + " ...");
                physicalWorkers.Add(new PhysicalWorker(nic.Guid, nic.Name, nic.Mac, nic.IPv4Address.First().Address,
                                                       nic.IPv4Address.First().Subnet, nic.DefaultIPv4GatewayMac, nic.DefaultIPv4Gateway));
                new Thread(new ThreadStart(physicalWorkers.Last().ReceivePackets)).Start();
                physicalWorkers.Last().Initialized.Wait(10000);
            }
            MTU = (int)requiredNics.Min((i) => i.IPv4Mtu > 0 ? i.IPv4Mtu : 1500);
            Global.WriteLog("Load Balancer: Negociated MTU = " + MTU);
            MSS = (ushort)(MTU - 40);
            splash.Stop();
            Dependencies.WinPcapInUse.Reset(Dependencies.WinPcapInUse.CurrentCount + 1);
            Global.WriteLog("Load Balancer: started");
            Global.ShowTrayTip("Load Balancer", "Started", System.Windows.Forms.ToolTipIcon.Info);
            Status.Update(State.Running);
            new Thread(new ThreadStart(CheckUp)).Start();
            return(true);
        }