public static RdpData ParseRDPFile(string path) { path = FileUtilities.ResolvePath(path); if (!FileUtilities.FileExists(path)) { throw new System.IO.FileNotFoundException("The supplied file path does not exist.", path); } RdpData data = new RdpData(); data.Path = path; string fileContents = FileUtilities.ReadFile(path); foreach (string line in fileContents.Split(Environment.NewLine.ToCharArray()).Where(s => !string.IsNullOrWhiteSpace(s))) { string[] parts = line.Split(RDP_PART_SPLIT); if (parts.Length < 3) { continue; } data.Properties.Add(parts[0], parts.Skip(2).Aggregate((t, s) => t + RDP_PART_SPLIT + s)); } return(data); }
public static async Task <OpenRdpResult> OpenRdp(string query, string fileSearchPath) { if (string.IsNullOrWhiteSpace(query)) { return(OpenRdpResult.EmptyAddress); } IEnumerable <RdpData> rdpDatas = RdpUtilities.ParseRDPFiles(fileSearchPath); //Find RDPData by file name RdpData rdpData = RdpUtilities.GetRDPDataByFileName(rdpDatas, query); //Find RDPData by domain if (rdpData == null) { rdpData = RdpUtilities.GetRDPDataByKeyValue(rdpDatas, new KeyValuePair <string, string>("full address", query)); } //Find RDPData by IP if (rdpData == null) { IPAddress ipAddress; string[] addressParts = query.Split(':'); if (addressParts.Length >= 1) { IPAddress.TryParse(addressParts[0], out ipAddress); if (ipAddress == null) { try { ipAddress = (await Dns.GetHostAddressesAsync(query)).First(); } catch { } } if (ipAddress != null) { string address = ipAddress.ToString(); if (addressParts.Length >= 2) { address += $":{addressParts[1]}"; } rdpData = RdpUtilities.GetRDPDataByKeyValue(rdpDatas, new KeyValuePair <string, string>("full address", address)); } } } //Start the RDP session if (rdpData != null) { RdpUtilities.StartExistingRDP(rdpData.Path); return(OpenRdpResult.Success); } else { return(OpenRdpResult.NotFound); } }
internal static string DebugRdpPattern(RdpPattern p, IDictionary <object, object> visitedPattern) { if (p is RdpText) { return("<text/>\n"); } if (p is RdpEmpty) { return("<empty/>\n"); } if (p is RdpNotAllowed) { return("<notAllowed/>\n"); } if (visitedPattern.ContainsKey(p)) { return("<" + p.PatternType + " ref='" + p.GetHashCode() + "'/>"); } visitedPattern.Add(p, p); string intl = "(id=" + p.GetHashCode() + ") "; RdpAbstractSingleContent s = p as RdpAbstractSingleContent; if (s != null) { intl = DebugRdpPattern(s.Child, visitedPattern); } RdpAbstractBinary b = p as RdpAbstractBinary; if (b != null) { intl = DebugRdpPattern(b.LValue, visitedPattern) + DebugRdpPattern(b.RValue, visitedPattern); } RdpData data = p as RdpData; if (data != null) { intl = String.Format("name={0},ns={1},type={2} {3}", data.Datatype.LocalName, data.Datatype.NamespaceURI, data.Datatype.GetType(), data is RdpDataExcept ? DebugRdpPattern(((RdpDataExcept)data).Except, visitedPattern) : String.Empty); } RdpValue value = p as RdpValue; if (value != null) { intl = String.Format("name={0},ns={1},value={2} type={3}", value.Datatype.LocalName, value.Datatype.NamespaceURI, value.Value, value.Datatype.GetType()); } RdpElement el = p as RdpElement; if (el != null) { intl = DebugNameClass(el.NameClass) + DebugRdpPattern(el.Children, visitedPattern); } RdpAttribute at = p as RdpAttribute; if (at != null) { intl = DebugNameClass(at.NameClass) + DebugRdpPattern(at.Children, visitedPattern); } string str = String.Format("<{0} id='id{1}'>\n{2}\n</{0}>", p.PatternType.ToString(), p.GetHashCode(), intl); return(str); }