123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using Vinno.FIS.Sonopost.Wireless.Win32.Interop;
- namespace Vinno.FIS.Sonopost.Wireless
- {
- internal static class ProfileFactory
- {
- /// <summary>
- /// Generates the profile XML for the access point and password
- /// </summary>
- internal static string Generate(WlanAvailableNetwork network, string password)
- {
- string profile = string.Empty;
- string template = string.Empty;
- string name = Encoding.UTF8.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
- string hex = GetHexString(network.dot11Ssid.SSID);
- var authAlgo = network.dot11DefaultAuthAlgorithm;
- switch (network.dot11DefaultCipherAlgorithm)
- {
- case Dot11CipherAlgorithm.None:
- template = GetTemplate("OPEN");
- profile = string.Format(template, name, hex);
- break;
- case Dot11CipherAlgorithm.WEP:
- template = GetTemplate("WEP");
- profile = string.Format(template, name, hex, password);
- break;
- case Dot11CipherAlgorithm.CCMP:
- if (authAlgo == Dot11AuthAlgorithm.RSNA)
- {
- template = GetTemplate("WPA2-Enterprise-PEAP-MSCHAPv2");
- profile = string.Format(template, name);
- }
- else // PSK
- {
- template = GetTemplate("WPA2-PSK");
- profile = string.Format(template, name, password);
- }
- break;
- case Dot11CipherAlgorithm.TKIP:
- #warning Robin: Not sure WPA uses RSNA
- if (authAlgo == Dot11AuthAlgorithm.RSNA)
- {
- template = GetTemplate("WPA-Enterprise-PEAP-MSCHAPv2");
- profile = string.Format(template, name);
- }
- else // PSK
- {
- template = GetTemplate("WPA-PSK");
- profile = string.Format(template, name, password);
- }
- break;
- default:
- throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
- }
- return profile;
- }
- /// <summary>
- /// Fetches the template for an wireless connection profile.
- /// </summary>
- private static string GetTemplate(string name)
- {
- var projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
- string resourceName = $"{projectName}.ProfileXML.{name}.xml";
- using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
- {
- return reader.ReadToEnd();
- }
- }
- /// <summary>
- /// Converts an byte array into the hex representation, ex: [255, 255] -> "FFFF"
- /// </summary>
- private static string GetHexString(byte[] ba)
- {
- StringBuilder sb = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- {
- if (b == 0)
- break;
- sb.AppendFormat("{0:x2}", b);
- }
- return sb.ToString();
- }
- }
- }
|