Esempio n. 1
0
        /// <summary>
        /// Compares the card by speed.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        private static int CompareProfileByCardSpeed(NetworkProfile x, NetworkProfile y)
        {
            // check first of all adatper type
            if (x.NetworkCardInfo.CardType != y.NetworkCardInfo.CardType)
            {
                return(-x.NetworkCardInfo.CardType.CompareTo(y.NetworkCardInfo.CardType));
            }

            // otherwise max speed
            return(-x.NetworkCardInfo.MaxSpeed.CompareTo(y.NetworkCardInfo.MaxSpeed));
        }
Esempio n. 2
0
        /// <summary>
        /// Copies the specified origin.
        /// </summary>
        /// <param name="origin">The origin.</param>
        /// <returns></returns>
        public static NetworkProfile Copy(NetworkProfile origin)
        {
            NetworkProfile profile = new NetworkProfile();

            profile.Id        = 0;
            profile.Name      = origin.Name;
            profile.ImageName = origin.ImageName;

            profile.NetworkCardInfo = WindowsNetworkCard.Copy(origin.NetworkCardInfo);
            profile.ProxyConfig     = ProxyConfiguration.Copy(origin.ProxyConfig);

            profile.ServiceList = new List <IWindowsServiceInfo>();
            foreach (IWindowsServiceInfo item in profile.ServiceList)
            {
                WindowsServiceInfoImpl temp = (WindowsServiceInfoImpl)item;
                profile.ServiceList.Add(WindowsServiceInfoImpl.Copy(temp));
            }

            profile.ExecList = new List <WindowsExecutable>();
            foreach (WindowsExecutable item in origin.ExecList)
            {
                profile.ExecList.Add(WindowsExecutable.Copy(item));
            }

            profile.DriveMapList = new List <DriveMap>();
            foreach (DriveMap item in origin.DriveMapList)
            {
                profile.DriveMapList.Add(DriveMap.Copy(item));
            }

            profile.DefaultPrinter = origin.DefaultPrinter;

            foreach (WindowsNetworkCard item in origin.DisabledNetworkCards)
            {
                profile.DisabledNetworkCards.Add(WindowsNetworkCard.Copy(item));
            }

            return(profile);
        }
Esempio n. 3
0
 /// <summary>
 /// Runs the printer setup.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunPrinterSetup(NetworkProfile profile)
 {
     PrinterManager.SetDefaultPrinter(profile.DefaultPrinter);
 }
Esempio n. 4
0
 /// <summary>
 /// Runs the programs setup.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunProgramsSetup(NetworkProfile profile)
 {
     WindowsExecutableManager.Apply(profile.ExecList);
 }
Esempio n. 5
0
 /// <summary>
 /// Runs the services setup.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunServicesSetup(NetworkProfile profile)
 {
     WindowsServiceManager.Apply(profile.ServiceList);
 }
Esempio n. 6
0
 /// <summary>
 /// Runs the drive mapping.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunDriveMapping(NetworkProfile profile)
 {
     DriveMapManager.Apply(profile.DriveMapList);
 }
Esempio n. 7
0
 /// <summary>
 /// Runs the setup proxy.
 /// </summary>
 /// <param name="profile">The profile.</param>
 /// <returns>true if proxy config applied, false if configuration </returns>
 public static bool RunProxySetup(NetworkProfile profile)
 {
     return(ProxyConfigurationManager.Apply(profile.ProxyConfig));
 }
Esempio n. 8
0
 /// <summary>
 /// Runs the network card setup.
 /// </summary>
 /// <param name="profile">The profile.</param>
 public static void RunNetworkCardSetup(NetworkProfile profile)
 {
     WindowsNetworkCardManager.Apply(profile.NetworkCardInfo);
 }
Esempio n. 9
0
        /// <summary>
        /// Loads the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="imageName">default name of the image.</param>
        /// <returns></returns>
        public static List <NetworkProfile> Load(string fileName, string imageName = NetworkProfile.DEFAULT_PROFILE_IMAGE_NAME)
        {
            List <NetworkProfile> profiles = new List <NetworkProfile>();

            if (!File.Exists(fileName))
            {
                return(null);
            }

            profiles.Clear();
            XmlTextReader reader = null;

            try
            {
                reader = new XmlTextReader(fileName);
                NetworkProfile currentProfile = null;

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:     // The node is an element.
                        switch (reader.Name)
                        {
                        case "profile":
                            currentProfile           = new NetworkProfile();
                            currentProfile.Id        = Int32.Parse(reader.GetAttribute("id"));
                            currentProfile.Name      = reader.GetAttribute("name");
                            currentProfile.ImageName = XmlUtility.ReadAttributeIfPresent(reader, "imageName", imageName);
                            profiles.Add(currentProfile);
                            break;

                        case "networkcard":
                            currentProfile.NetworkCardInfo    = new WindowsNetworkCard();
                            currentProfile.NetworkCardInfo.Id = XmlUtility.ReadAttributeIfPresent(reader, "id", "");

                            currentProfile.NetworkCardInfo.ViewId = XmlUtility.ReadAttributeIfPresent(reader, "viewId", "");
                            currentProfile.NetworkCardInfo.Name   = XmlUtility.ReadAttributeIfPresent(reader, "name", "");

                            currentProfile.NetworkCardInfo.Dhcp           = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "dhcp", Boolean.FalseString));
                            currentProfile.NetworkCardInfo.IpAddress      = XmlUtility.ReadAttributeIfPresent(reader, "ipAddress", "");
                            currentProfile.NetworkCardInfo.SubnetMask     = XmlUtility.ReadAttributeIfPresent(reader, "subnetMask", "");
                            currentProfile.NetworkCardInfo.GatewayAddress = XmlUtility.ReadAttributeIfPresent(reader, "defaultGateway", "");

                            currentProfile.NetworkCardInfo.MacAddress = XmlUtility.ReadAttributeIfPresent(reader, "macAddress", "");
                            currentProfile.NetworkCardInfo.DynamicDNS = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "dynamicDns", Boolean.FalseString));
                            currentProfile.NetworkCardInfo.Dns        = XmlUtility.ReadAttributeIfPresent(reader, "dns", "");
                            currentProfile.NetworkCardInfo.Dns2       = XmlUtility.ReadAttributeIfPresent(reader, "dns2", "");

                            currentProfile.NetworkCardInfo.HardwareName = XmlUtility.ReadAttributeIfPresent(reader, "hardwareName", "");

                            currentProfile.NetworkCardInfo.WinsPrimaryServer   = XmlUtility.ReadAttributeIfPresent(reader, "winsPrimaryServer", "");
                            currentProfile.NetworkCardInfo.WinsSecondaryServer = XmlUtility.ReadAttributeIfPresent(reader, "winsSecondaryServer", "");

                            break;

                        case "proxy":
                            currentProfile.ProxyConfig                      = new ProxyConfiguration();
                            currentProfile.ProxyConfig.Enabled              = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "enabled", Boolean.FalseString));
                            currentProfile.ProxyConfig.ServerAddress        = XmlUtility.ReadAttributeIfPresent(reader, "serverAddress", "");
                            currentProfile.ProxyConfig.Port                 = Int32.Parse(XmlUtility.ReadAttributeIfPresent(reader, "port", "80"));
                            currentProfile.ProxyConfig.ProxyOverrideEnabled = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "overrideEnabled", Boolean.FalseString));
                            currentProfile.ProxyConfig.ProxyOverride        = XmlUtility.ReadAttributeIfPresent(reader, "proxyOverride", "");
                            break;

                        case "application":
                            WindowsExecutable application = new WindowsExecutable();
                            application.Name        = XmlUtility.ReadAttributeIfPresent(reader, "name", "");
                            application.Arguments   = XmlUtility.ReadAttributeIfPresent(reader, "arguments", "");
                            application.Description = XmlUtility.ReadAttributeIfPresent(reader, "description", "");
                            application.Directory   = XmlUtility.ReadAttributeIfPresent(reader, "directory", "");
                            application.File        = XmlUtility.ReadAttributeIfPresent(reader, "fileName", "");
                            application.WaitForExit = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "wait", Boolean.TrueString));
                            application.Kill        = Boolean.Parse(XmlUtility.ReadAttributeIfPresent(reader, "kill", Boolean.TrueString));

                            currentProfile.ExecList.Add(application);
                            break;

                        case "service":
                            WindowsServiceInfoImpl service = new WindowsServiceInfoImpl();

                            service.ServiceName = XmlUtility.ReadAttributeIfPresent(reader, "name", "");

                            string temp = XmlUtility.ReadAttributeIfPresent(reader, "status", ServiceForcedStatus.STOPPED.ToString());

                            if (temp.Equals(ServiceForcedStatus.RUNNING.ToString()))
                            {
                                service.ForcedStatus = ServiceForcedStatus.RUNNING;
                            }
                            else if (temp.Equals(ServiceForcedStatus.STOPPED.ToString()))
                            {
                                service.ForcedStatus = ServiceForcedStatus.STOPPED;
                            }
                            else
                            {
                                service.ForcedStatus = ServiceForcedStatus.NONE;
                            }
                            currentProfile.ServiceList.Add(service);

                            break;

                        case "driveMap":
                            DriveMap drive = new DriveMap();
                            drive.Name        = XmlUtility.ReadAttributeIfPresent(reader, "name", "");
                            drive.Drive       = XmlUtility.ReadAttributeIfPresent(reader, "drive", "");
                            drive.Description = XmlUtility.ReadAttributeIfPresent(reader, "description", "");
                            drive.Username    = XmlUtility.ReadAttributeIfPresent(reader, "username", "");
                            drive.Password    = XmlUtility.ReadAttributeIfPresent(reader, "password", "");
                            drive.RealPath    = XmlUtility.ReadAttributeIfPresent(reader, "realPath", "");
                            string temp1 = XmlUtility.ReadAttributeIfPresent(reader, "type", DriveMapType.MOUNT.ToString());

                            if (temp1.Equals(DriveMapType.MOUNT.ToString()))
                            {
                                drive.Type = DriveMapType.MOUNT;
                            }
                            else
                            {
                                drive.Type = DriveMapType.UNMOUNT;
                            }

                            currentProfile.DriveMapList.Add(drive);
                            break;

                        case "printer":
                            currentProfile.DefaultPrinter = XmlUtility.ReadAttributeIfPresent(reader, "defaultPrinter", "");
                            break;

                        case "forcedNic":
                            // disabled nic
                            WindowsNetworkCard disabledNIC;

                            disabledNIC              = new WindowsNetworkCard();
                            disabledNIC.Id           = XmlUtility.ReadAttributeIfPresent(reader, "id", "");
                            disabledNIC.HardwareName = XmlUtility.ReadAttributeIfPresent(reader, "hardwareName", "");

                            currentProfile.DisabledNetworkCards.Add(disabledNIC);
                            break;

                        case "wifi":
                            // wifi
                            string associatedSSID = XmlUtility.ReadAttributeIfPresent(reader, "associatedSSID", "");

                            currentProfile.AssociatedWifiSSID = associatedSSID;
                            break;
                        }
                        break;

                    case XmlNodeType.Text:     //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;

                    case XmlNodeType.EndElement:     //Display the end of the element.
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return(profiles);
        }
Esempio n. 10
0
        /// <summary>
        /// Autodetects the network profile. For the moment it works only for wifi connections!
        /// If no connections found, it return null.
        /// Use
        ///     WindowsNetworkCardManager.EnabledWindowsNetworkCardList
        ///     WifiProfileManager.ActiveWifiProfile;
        /// </summary>
        ///
        /// <param name="profiles">profiles.</param>
        /// <returns></returns>
        public static NetworkProfile AutodetectNetworkProfile(List <NetworkProfile> profiles)
        {
            NetworkProfileHelper.FireNotifyEvent("Start autodetect");
            if (profiles == null || profiles.Count == 0)
            {
                NetworkProfileHelper.FireNotifyEvent("There's no profiles");
                NetworkProfileHelper.FireNotifyEvent("End autodetect");
                return(null);
            }
            // initial setup for cards
            SetupNetworkCardForAutodetect(profiles);

            NetworkProfile selectedProfile = null;

            // one or more card (not only wifi)
            // wifi connected,
            WifiProfile currentWifiProfile = WifiProfileManager.ActiveWifiProfile;

            // enable card
            List <WindowsNetworkCard> enabledCardList = WindowsNetworkCardManager.EnabledWindowsNetworkCardList;

            if (enabledCardList.Count == 1 && enabledCardList[0].CardType == WindowsNetworkCardType.WIRELESS && currentWifiProfile.SSID != null)
            {
                WindowsNetworkCard card = enabledCardList[0];
                // only a wifi connection avaible
                foreach (NetworkProfile item in profiles)
                {
                    // select if card is right and (ssid is right or for that profile there's no ssid)
                    if (item.NetworkCardInfo.Id.Equals(card.Id) && (currentWifiProfile.SSID.Equals(item.AssociatedWifiSSID) || string.IsNullOrWhiteSpace(item.AssociatedWifiSSID)))
                    {
                        // ok, we found it!!!
                        selectedProfile = item;

                        if (!string.IsNullOrWhiteSpace(item.AssociatedWifiSSID))
                        {
                            NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are connected with right SSID (" + currentWifiProfile.SSID + ")");
                        }
                        NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + " without do anything else!!");
                        break;
                    }
                }
            }
            else
            {
                // order list by maxSpeed
                //enabledCardList.Sort(CompareCardBySpeed);

                // if there's no enabled card, it's a problem!
                if (enabledCardList.Count == 0)
                {
                    NetworkProfileHelper.FireNotifyEvent("No card enabled, found"); return(null);
                }

                List <NetworkProfile> enabledProfileList = FindValidOrderedNetworkProfiles(profiles, enabledCardList, currentWifiProfile);

                // assert: enabledProfile contains the right profiles. Now we have to test it.
                // the first with ping ok it's ok!
                bool pingOk;
                foreach (NetworkProfile item in enabledProfileList)
                {
                    NetworkProfileHelper.FireNotifyEvent("Start analizing profile " + item.Name + " with card " + item.NetworkCardInfo.Name);
                    //WindowsNetworkCardHelper.SetDeviceStatus(item.NetworkCardInfo, false);
                    NetworkProfileHelper.RunDisableNetworkCardsSetup(item);
                    NetworkProfileHelper.RunNetworkCardSetup(item);

                    // wait for a while
                    //NetworkProfileHelper.FireNotifyEvent("Wait " + (WAIT_BEFORE_PING) + " ms.");
                    //System.Threading.Thread.Sleep(WAIT_BEFORE_PING);
                    WindowsNetworkCardEventHandler eventHandler = new WindowsNetworkCardEventHandler(item.NetworkCardInfo);

                    eventHandler.WaitUntilNetworkCardIsUp();
                    //WaitUntilNetworkCardIsUp(item.NetworkCardInfo);

                    NetworkProfileHelper.FireNotifyEvent("Wait " + (WAIT_BEFORE_PING) + " ms.");
                    System.Threading.Thread.Sleep(WAIT_BEFORE_PING);

                    WindowsNetworkCard card = WindowsNetworkCardManager.RefreshStatus(item.NetworkCardInfo.Id);

                    if (card.CardType == WindowsNetworkCardType.WIRELESS)
                    {
                        // get current wifi profile only for wifi card
                        currentWifiProfile = WifiProfileManager.GetActiveWifiProfileForCard(card);

                        if (currentWifiProfile != null && currentWifiProfile.SSID.Equals(item.AssociatedWifiSSID) && card.NetConnectionStatus == 2)
                        {
                            // ok, we found it!!!
                            selectedProfile = item;
                            NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are connected with right SSID (" + currentWifiProfile.SSID + ")");
                            NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + " without do anything else!!");
                            break;
                        }
                    }
                    else
                    {
                        pingOk = false;

                        NetworkProfileHelper.FireNotifyEvent("The card " + card.Name + " are in status " + card.NetConnectionStatus);
                        // test both static config or dynamic config
                        pingOk = PingHelper.RunPing(card.GatewayAddress);
                        pingOk = pingOk || PingHelper.RunPing(card.CurrentGatewayAddress);

                        NetworkProfileHelper.FireNotifyEvent("For card " + card.Name + " and profile " + item.Name + ", ping to gateway are " + pingOk);

                        if (pingOk)
                        {
                            selectedProfile = item;
                            NetworkProfileHelper.FireNotifyEvent("Selected profile " + item.Name + "!!");
                            break;
                        }
                    }

                    NetworkProfileHelper.FireNotifyEvent("Stop analizing profile " + item.Name + ", go to next profile");
                }

                // restore initial enabled card
                if (selectedProfile == null)
                {
                    NetworkProfileHelper.FireNotifyEvent("No profile found, so restore status card");
                    foreach (WindowsNetworkCard item in enabledCardList)
                    {
                        NetworkProfileHelper.FireNotifyEvent("Enable card " + item.Name);
                        WindowsNetworkCardHelper.SetDeviceStatus(item, true);
                    }
                }
            }

            NetworkProfileHelper.FireNotifyEvent("End autodetect");
            return(selectedProfile);
        }