view ServerMonitor/Helpers.cs @ 5:b6fe203af9d5

Private key passwords and validation
author Brad Greco <brad@bgreco.net>
date Thu, 28 Feb 2019 21:19:32 -0500
parents 3142e52cbe69
children 052aa62cb42a
line wrap: on
line source

´╗┐using Renci.SshNet;
using Renci.SshNet.Common;
using ServerMonitorApp.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    static class Helpers
    {
        public static void FormatImageButton(Button button)
        {
            button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
        }

        public static string GetAllMessages(this Exception ex)
        {
            return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
        }

        public static string GetDisplayName(Type type)
        {
            return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
        }

        public static bool IsNullOrEmpty(this string aString)
        {
            return aString == null || aString.Trim() == string.Empty;
        }

        public static string ConvertNewlines(this string aString)
        {
            return aString.Replace("\r\n", "\n").Replace('\r', '\n');
        }

        public static T GetAttribute<T>(this Type type) where T : Attribute
        {
            return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
        }

        public static Image GetIcon(this CheckStatus checkStatus)
        {
            switch (checkStatus)
            {
                case CheckStatus.Error: return Resources.error;
                case CheckStatus.Warning: return Resources.warning;
                case CheckStatus.Information: return Resources.info;
                case CheckStatus.Success: return Resources.pass;
                case CheckStatus.Running: return Resources.run;
                case CheckStatus.Disabled: return Resources.disable;
                default: return null;
            }
        }

        public static bool In(this Enum value, params Enum[] values) {
            return values.Contains(value);
        }

        // https://stackoverflow.com/a/8345173
        /// <summary>
        /// Indicates whether any network connection is available.
        /// Filter connections below a specified speed, as well as virtual network cards.
        /// </summary>
        /// <returns>
        ///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNetworkAvailable()
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
                return false;

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                // discard because of standard reasons
                if ((ni.OperationalStatus != OperationalStatus.Up) ||
                    (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
                    (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
                    continue;

                // discard virtual cards (virtual box, virtual pc, etc.)
                if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
                    continue;

                // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
                if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
                    continue;

                return true;
            }
            return false;
        }
    }
}