Ejemplo n.º 1
0
 private static string QueryCurrentDevices(bool wait = false)
 {
     try
     {
         var status = WlanManager.QueryStatus();
         if (status.HostedNetworkState != WlanHostedNetworkState.Active)
         {
             return("查询客户端失败,托管网络尚未启用。");
         }
         var result = new StringBuilder();
         var lookup = Lookup;
         var i      = 0;
         foreach (var peer in status.PeerList)
         {
             i++;
             result.AppendFormat("设备 #{0} {1}", i, GetDeviceDetails(peer, wait, lookup,
                                                                    string.Empty.PadLeft(8 + (int)Math.Floor(Math.Log10(i)))));
         }
         return(result.ToString());
     }
     catch (Exception exc)
     {
         return("查询客户端失败,详细信息:" + exc.GetMessage());
     }
 }
Ejemplo n.º 2
0
 private static void Boot()
 {
     Close();
     Try(() =>
     {
         WlanHostedNetworkReason reason;
         Console.Write("启用托管网络中……");
         WriteReason(reason = WlanManager.SetEnabled(true));
         if (reason != WlanHostedNetworkReason.Success)
         {
             Close();
             return;
         }
         Console.Write("正在应用设置……");
         reason = WlanManager.SetConnectionSettings(ssid, peersCount);
         if (reason != WlanHostedNetworkReason.Success)
         {
             WriteReason(reason);
             Close();
             return;
         }
         var keyString      = key as string;
         WriteReason(reason = keyString != null ? WlanManager.SetSecondaryKey(keyString)
                                                : WlanManager.SetSecondaryKey((byte[])key));
         if (reason != WlanHostedNetworkReason.Success)
         {
             Close();
             return;
         }
         Console.Write("启动共享中……");
         WriteReason(WlanManager.ForceStart());
     }, Close);
 }
Ejemplo n.º 3
0
        private static void DeepRestart()
        {
            ServiceController sa = new ServiceController("SharedAccess"), w = new ServiceController("Wlansvc"),
                              ra = new ServiceController("RemoteAccess");

            Try(() =>
            {
                Console.WriteLine("正在停止服务 {0}……", sa.DisplayName);
                if (sa.Status != ServiceControllerStatus.StopPending && sa.Status != ServiceControllerStatus.Stopped)
                {
                    sa.Stop();
                }
                sa.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
                Console.WriteLine("正在停止服务 {0}……", w.DisplayName);
                if (w.Status != ServiceControllerStatus.StopPending && w.Status != ServiceControllerStatus.Stopped)
                {
                    w.Stop();
                }
                w.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
                Console.WriteLine("正在停止服务 {0}……", ra.DisplayName);
                if (ra.Status != ServiceControllerStatus.StopPending && w.Status != ServiceControllerStatus.Stopped)
                {
                    ra.Stop();
                }
                w.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
                Console.WriteLine("正在重置相关配置……");
                using (var rk = Registry.LocalMachine.OpenSubKey
                                    (@"SYSTEM\CurrentControlSet\services\Wlansvc\Parameters\HostedNetworkSettings", true))
                    if (rk != null)
                    {
                        rk.DeleteValue("EncryptedSettings");
                        rk.DeleteValue("HostedNetworkSettings");
                    }
            }, prefix: DeepRestartPrefix);
            Try(() =>
            {
                Console.WriteLine("正在启动服务 {0}……", ra.DisplayName);
                ra.Start();
                ra.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
            }, prefix: DeepRestartPrefix);
            Try(() =>
            {
                Console.WriteLine("正在启动服务 {0}……", w.DisplayName);
                w.Start();
                w.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
            }, prefix: DeepRestartPrefix);
            Try(() =>
            {
                Console.WriteLine("正在启动服务 {0}……", sa.DisplayName);
                sa.Start();
                sa.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
            }, prefix: DeepRestartPrefix);
            Thread.Sleep(1000);
            WlanManager.Restart();
        }
Ejemplo n.º 4
0
        public static string GetDeviceDetails(WlanHostedNetworkPeerState peer, bool wait = false,
                                              ILookup <string, Arp.MibIpNetRow> lookup   = null, string padding = "")
        {
            var result = $"物理地址:{peer.PeerMacAddress} ({WlanManager.ToString(peer.PeerAuthState)})\n";
            var ips    = (lookup ?? Lookup)[peer.PeerMacAddress.ToString()].ToArray();

            if (ips.Length > 0)
            {
                result += padding + "IP  地址:" + string.Join("\n          " + padding,
                                                            ips.Select(ip =>
                {
                    var domains = DnsCache.GetDomains(ip.IPAddress, wait || Ttl == 0);
                    return(ip.ToString() + (domains == null ? string.Empty : (" [" + domains + ']')));
                })) + '\n';
            }
            else
            {
                lookupTime = DateTime.MinValue;
            }
            return(result);
        }
Ejemplo n.º 5
0
 private static void ShowStatus()
 {
     Try(() =>
     {
         WlanOpcodeValueType opCode;
         try
         {
             WlanHostedNetworkConnectionSettings cs;
             opCode = WlanManager.QueryConnectionSettings(out cs);
             Console.WriteLine("托管网络连接设置来源:\t{0}\nSSID:\t\t\t{1}\n最大客户端数:\t\t{2}",
                               WlanManager.ToString(opCode), cs.HostedNetworkSSID.Content, cs.MaxNumberOfPeers);
         }
         catch (BadConfigurationException)
         {
         }
         WlanHostedNetworkSecuritySettings ss;
         opCode = WlanManager.QuerySecuritySettings(out ss);
         Console.WriteLine("托管网络安全设置来源:\t{0}\n验证算法:\t\t{1}\n加密算法:\t\t{2}",
                           WlanManager.ToString(opCode),
                           WlanManager.ToString(ss.Dot11AuthAlgo), WlanManager.ToString(ss.Dot11CipherAlgo));
         try
         {
             string profile;
             opCode = WlanManager.QueryStationProfile(out profile);
             Console.Write("托管网络配置文件来源:\t{0}\n配置文件:\n{1}",
                           WlanManager.ToString(opCode), profile.Trim('\0'));
         }
         catch (BadConfigurationException)
         {
         }
         bool enabled;
         opCode = WlanManager.QueryEnabled(out enabled);
         Console.WriteLine("托管网络启用设置来源:\t{0}\n托管网络启用:\t\t{1}",
                           WlanManager.ToString(opCode), enabled ? '是' : '否');
         string passKey;
         bool isPassPhrase, isPersistent;
         var reason = WlanManager.QuerySecondaryKey(out passKey, out isPassPhrase, out isPersistent);
         if (reason == WlanHostedNetworkReason.Success)
         {
             Console.WriteLine("密码:\t\t\t{0}\n使用密码短语:\t\t{1}\n永久密码:\t\t{2}",
                               passKey, isPassPhrase ? '是' : '否', isPersistent ? '是' : '否');
         }
         else
         {
             Console.WriteLine("查询密码失败,原因:" + WlanManager.ToString(reason));
         }
         var status = WlanManager.QueryStatus();
         Console.WriteLine("状态:\t\t\t" + WlanManager.ToString(status.HostedNetworkState));
         if (status.HostedNetworkState == WlanHostedNetworkState.Unavailable)
         {
             return;
         }
         Console.WriteLine("实际网络 ID:\t\t{0}\nBSSID:\t\t\t{1}",
                           status.IPDeviceID, status.wlanHostedNetworkBSSID);
         if (status.HostedNetworkState == WlanHostedNetworkState.Active)
         {
             Console.WriteLine("802.11 物理层类型:\t{0}\n网络接口信道频率:\t{1}\n已认证客户端数量:\t{2}",
                               WlanManager.ToString(status.dot11PhyType), status.ulChannelFrequency,
                               status.dwNumberOfPeers);
         }
     });
 }
Ejemplo n.º 6
0
 private static void RefreshKey()
 {
     Console.Write("正在刷新安全设置……");
     Try(() => WriteReason(WlanManager.RefreshSecuritySettings()));
 }
Ejemplo n.º 7
0
 private static void Close()
 {
     Console.Write("正在关闭共享……");
     WriteReason(WlanManager.ForceStop());
 }
Ejemplo n.º 8
0
 private static void WriteReason(WlanHostedNetworkReason reason)
 {
     Console.WriteLine(WlanManager.ToString(reason));
 }