// Decompiled with JetBrains decompiler // Type: FritzBox.API.FritzWebAccess.FritzWebAccess // Assembly: FritzBox, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // MVID: DC9FD63C-0A96-43D7-A76E-0506FEB07200 // Assembly location: \\192.168.178.26\Freigabe\FritzBox.dll using System; using System.IO; using System.Net; using System.Security.Cryptography; using System.Text; using System.Xml.Linq; #nullable disable namespace FritzBox.API.FritzWebAccess { public class FritzWebAccess { public Uri Address { get; set; } = new Uri("http://fritz.box/"); public Uri SoapAddress => new Uri(this.Address.Scheme + "://" + this.Address.Host + ":49000/"); public string SessionId { get; private set; } public bool IsAuthenticated { get => !string.IsNullOrEmpty(this.SessionId) && this.SessionId != "0000000000000000"; } public FritzWebAccess() { } public FritzWebAccess(string username, string password) => this.LogIn(username, password); public void LogIn(string password) => this.LogIn(string.Empty, password); public void LogIn(string username, string password) { Uri uri = new Uri(this.Address, "login_sid.lua"); XDocument doc = XDocument.Load(uri.AbsoluteUri); this.SessionId = this.GetValue(doc, "SID"); if (this.IsAuthenticated) return; string challenge = this.GetValue(doc, "Challenge"); this.SessionId = this.GetValue(XDocument.Load(string.Format("{0}?username={1}&response={2}", (object)uri.AbsoluteUri, (object)username, (object)this.GetAuthResponse(challenge, password))), "SID"); } private string GetValue(XDocument doc, string elementName) { return (doc.FirstNode as XElement).Element((XName)elementName).Value; } private string GetAuthResponse(string challenge, string password) { return challenge + "-" + this.GetMD5Hash(challenge + "-" + password); } private string GetMD5Hash(string input) { byte[] hash = MD5.Create().ComputeHash(Encoding.Unicode.GetBytes(input)); StringBuilder stringBuilder = new StringBuilder(); for (int index = 0; index < hash.Length; ++index) stringBuilder.Append(hash[index].ToString("x2")); return stringBuilder.ToString(); } public string GetWebContent(string relativeUrl, string parameter = "") { return this.GetContent(this.Address, relativeUrl, parameter); } public string GetSoapContent(string relativeUrl, string parameter = "") { return this.GetContent(this.SoapAddress, relativeUrl, parameter); } public string GetContent(Uri baseAddress, string relativeUrl, string parameter = "") { using (HttpWebResponse response = (WebRequest.Create(new Uri(baseAddress, relativeUrl + "?sid=" + this.SessionId + parameter)) as HttpWebRequest).GetResponse() as HttpWebResponse) { using (StreamReader streamReader = new StreamReader(((WebResponse)response).GetResponseStream())) return ((TextReader)streamReader).ReadToEnd(); } } } }