WlanTool.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using Azylee.Core.ModelUtils.ResultModels;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Azylee.Core.NetUtils.WifiManUtils
  7. {
  8. /// <summary>
  9. /// Wlan连接工具
  10. /// </summary>
  11. public class WlanTool
  12. {
  13. /// <summary>
  14. /// 连接到指定Wifi网络
  15. /// </summary>
  16. /// <param name="name">wifi名称</param>
  17. /// <param name="key">wifi密码</param>
  18. /// <returns></returns>
  19. public bool Connect(string name, string key)
  20. {
  21. WlanClient client = new WlanClient();
  22. foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
  23. {
  24. // Lists all networks with WEP security
  25. Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
  26. foreach (Wlan.WlanAvailableNetwork network in networks)
  27. {
  28. if (network.profileName == name)
  29. {
  30. Connect(wlanIface, network, key);
  31. }
  32. }
  33. if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected &&
  34. wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected &&
  35. wlanIface.CurrentConnection.profileName == name)
  36. {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. private void Connect(WlanClient.WlanInterface wlanIface, Wlan.WlanAvailableNetwork ssid, string key)
  43. {
  44. bool result = false;
  45. string description = "初始化错误";
  46. try
  47. {
  48. String auth = string.Empty;
  49. String cipher = string.Empty;
  50. bool isNoKey = false;
  51. String keytype = string.Empty;
  52. switch (ssid.dot11DefaultAuthAlgorithm)
  53. {
  54. case Wlan.Dot11AuthAlgorithm.IEEE80211_Open:
  55. auth = "open"; break;
  56. //case Wlan.Dot11AuthAlgorithm.IEEE80211_SharedKey:
  57. // 'not implemented yet;
  58. //break;
  59. case Wlan.Dot11AuthAlgorithm.RSNA:
  60. auth = "WPA2PSK"; break;
  61. case Wlan.Dot11AuthAlgorithm.RSNA_PSK:
  62. auth = "WPA2PSK"; break;
  63. case Wlan.Dot11AuthAlgorithm.WPA:
  64. auth = "WPAPSK"; break;
  65. case Wlan.Dot11AuthAlgorithm.WPA_None:
  66. auth = "WPAPSK"; break;
  67. case Wlan.Dot11AuthAlgorithm.WPA_PSK:
  68. auth = "WPAPSK"; break;
  69. }
  70. switch (ssid.dot11DefaultCipherAlgorithm)
  71. {
  72. case Wlan.Dot11CipherAlgorithm.CCMP:
  73. cipher = "AES";
  74. keytype = "passPhrase";
  75. break;
  76. case Wlan.Dot11CipherAlgorithm.TKIP:
  77. cipher = "TKIP";
  78. keytype = "passPhrase";
  79. break;
  80. case Wlan.Dot11CipherAlgorithm.None:
  81. cipher = "none"; keytype = "";
  82. isNoKey = true;
  83. break;
  84. case Wlan.Dot11CipherAlgorithm.WEP:
  85. cipher = "WEP";
  86. keytype = "networkKey";
  87. break;
  88. case Wlan.Dot11CipherAlgorithm.WEP40:
  89. cipher = "WEP";
  90. keytype = "networkKey";
  91. break;
  92. case Wlan.Dot11CipherAlgorithm.WEP104:
  93. cipher = "WEP";
  94. keytype = "networkKey";
  95. break;
  96. }
  97. if (isNoKey && !string.IsNullOrEmpty(key))
  98. {
  99. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  100. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  101. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString();
  102. return;
  103. }
  104. else if (!isNoKey && string.IsNullOrEmpty(key))
  105. {
  106. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  107. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  108. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString();
  109. return;
  110. }
  111. else
  112. {
  113. string profileName = ssid.profileName; // this is also the SSID
  114. string mac = StringToHex(profileName);
  115. string profileXml = string.Empty;
  116. if (!string.IsNullOrEmpty(key))
  117. {
  118. profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>{4}</keyType><protected>false</protected><keyMaterial>{5}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>",
  119. profileName, mac, auth, cipher, keytype, key);
  120. }
  121. else
  122. {
  123. profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>",
  124. profileName, mac, auth, cipher, keytype);
  125. }
  126. wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
  127. //ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, ssid.profileNames);
  128. wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
  129. }
  130. }
  131. catch (Exception e)
  132. {
  133. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  134. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  135. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString() + "\r\n"
  136. + e.Message;
  137. return;
  138. }
  139. }
  140. private Tuple<bool, string> ConnectSynchronously(WlanClient.WlanInterface wlanIface, Wlan.WlanAvailableNetwork ssid, string key)
  141. {
  142. bool result = false;
  143. string description = "初始化错误";
  144. try
  145. {
  146. String auth = string.Empty;
  147. String cipher = string.Empty;
  148. bool isNoKey = false;
  149. String keytype = string.Empty;
  150. switch (ssid.dot11DefaultAuthAlgorithm)
  151. {
  152. case Wlan.Dot11AuthAlgorithm.IEEE80211_Open:
  153. auth = "open"; break;
  154. //case Wlan.Dot11AuthAlgorithm.IEEE80211_SharedKey:
  155. // 'not implemented yet;
  156. //break;
  157. case Wlan.Dot11AuthAlgorithm.RSNA:
  158. auth = "WPA2PSK"; break;
  159. case Wlan.Dot11AuthAlgorithm.RSNA_PSK:
  160. auth = "WPA2PSK"; break;
  161. case Wlan.Dot11AuthAlgorithm.WPA:
  162. auth = "WPAPSK"; break;
  163. case Wlan.Dot11AuthAlgorithm.WPA_None:
  164. auth = "WPAPSK"; break;
  165. case Wlan.Dot11AuthAlgorithm.WPA_PSK:
  166. auth = "WPAPSK"; break;
  167. }
  168. switch (ssid.dot11DefaultCipherAlgorithm)
  169. {
  170. case Wlan.Dot11CipherAlgorithm.CCMP:
  171. cipher = "AES";
  172. keytype = "passPhrase";
  173. break;
  174. case Wlan.Dot11CipherAlgorithm.TKIP:
  175. cipher = "TKIP";
  176. keytype = "passPhrase";
  177. break;
  178. case Wlan.Dot11CipherAlgorithm.None:
  179. cipher = "none"; keytype = "";
  180. isNoKey = true;
  181. break;
  182. case Wlan.Dot11CipherAlgorithm.WEP:
  183. cipher = "WEP";
  184. keytype = "networkKey";
  185. break;
  186. case Wlan.Dot11CipherAlgorithm.WEP40:
  187. cipher = "WEP";
  188. keytype = "networkKey";
  189. break;
  190. case Wlan.Dot11CipherAlgorithm.WEP104:
  191. cipher = "WEP";
  192. keytype = "networkKey";
  193. break;
  194. }
  195. if (isNoKey && !string.IsNullOrEmpty(key))
  196. {
  197. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  198. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  199. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString();
  200. return new Tuple<bool, string>(result, description);
  201. }
  202. else if (!isNoKey && string.IsNullOrEmpty(key))
  203. {
  204. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  205. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  206. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString();
  207. return new Tuple<bool, string>(result, description);
  208. }
  209. else
  210. {
  211. string profileName = ssid.profileName; // this is also the SSID
  212. string mac = StringToHex(profileName);
  213. string profileXml = string.Empty;
  214. if (!string.IsNullOrEmpty(key))
  215. {
  216. profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>{4}</keyType><protected>false</protected><keyMaterial>{5}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>",
  217. profileName, mac, auth, cipher, keytype, key);
  218. }
  219. else
  220. {
  221. profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>",
  222. profileName, mac, auth, cipher, keytype);
  223. }
  224. wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
  225. //ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, ssid.profileNames);
  226. bool success = wlanIface.ConnectSynchronously(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName, 15000);
  227. if (!success)
  228. {
  229. description = "连接网络失败 SSID:" + ssid.profileName + "\r\n"
  230. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  231. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString();
  232. return new Tuple<bool, string>(result, description);
  233. }
  234. else
  235. {
  236. result = true;
  237. description = "连接网络成功";
  238. return new Tuple<bool, string>(result, description);
  239. }
  240. }
  241. }
  242. catch (Exception e)
  243. {
  244. description = "无法连接网络 SSID:" + ssid.profileName + "\r\n"
  245. + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n"
  246. + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString() + "\r\n"
  247. + e.Message;
  248. return new Tuple<bool, string>(result, description);
  249. }
  250. }
  251. /// <summary>
  252. /// 是否连接到某wifi
  253. /// </summary>
  254. /// <param name="name">wifi名称</param>
  255. /// <returns></returns>
  256. public bool IsConnect(string name)
  257. {
  258. WlanClient client = new WlanClient();
  259. foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
  260. {
  261. if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected &&
  262. wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected &&
  263. wlanIface.CurrentConnection.profileName == name)
  264. {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /// <summary>
  271. /// 字符串转Hex
  272. /// </summary>
  273. /// <param name="s"></param>
  274. /// <returns></returns>
  275. public static string StringToHex(string s)
  276. {
  277. StringBuilder sb = new StringBuilder();
  278. //默认是System.Text.Encoding.Default.GetBytes(str)
  279. byte[] byStr = Encoding.Default.GetBytes(s);
  280. for (int i = 0; i < byStr.Length; i++)
  281. {
  282. sb.Append(Convert.ToString(byStr[i], 16));
  283. }
  284. return (sb.ToString().ToUpper());
  285. }
  286. /// <summary>
  287. /// Converts a 802.11 SSID to a string.
  288. /// </summary>
  289. public static string GetStringForSSID(Wlan.Dot11Ssid ssid)
  290. {
  291. return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
  292. }
  293. }
  294. }