Entwicklung/Sensormat/FritzBox/API/FritzWebAccess/FritzSoapAccess.cs

207 lines
9.4 KiB
C#
Raw Normal View History

2025-09-23 10:21:23 +02:00
// Decompiled with JetBrains decompiler
// Type: FritzBox.API.FritzWebAccess.FritzSoapAccess
// 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 FritzBox.API.FritzWebAccess.SoapTypes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.XPath;
#nullable disable
namespace FritzBox.API.FritzWebAccess
{
public class FritzSoapAccess
{
public Uri BaseAddress { get; set; } = new Uri("https://fritz.box/");
public string Username { get; set; } = "admin";
public string Password { get; set; } = string.Empty;
public int GetHostNumberOfEntries()
{
string elementValue = this.GetElementValue(((WebResponse)this.SendSoapRequest("tr064/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1#GetHostNumberOfEntries")).GetResponseStream(), "NewHostNumberOfEntries");
return elementValue != null ? Convert.ToInt32(elementValue) : -1;
}
public HostInfo GetGenericHostEntryExt(int index)
{
Dictionary<string, string> elementValues = this.GetElementValues(((WebResponse)this.SendSoapRequest("tr064/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1#X_AVM-DE_GetGenericHostEntryExt", new Dictionary<string, string>()
{
{
"NewIndex",
index.ToString()
}
})).GetResponseStream(), (IEnumerable<string>)new string[7]
{
"NewIPAddress",
"NewMACAddress",
"NewActive",
"NewHostName",
"NewInterfaceType",
"NewX_AVM-DE_Port",
"NewX_AVM-DE_Speed"
});
return new HostInfo()
{
IPAddress = elementValues["NewIPAddress"],
MACAddress = elementValues["NewMACAddress"],
IsActive = elementValues["NewActive"] == "1",
HostName = elementValues["NewHostName"],
InterfaceType = elementValues["NewInterfaceType"],
Port = elementValues["NewX_AVM-DE_Port"],
Speed = elementValues["NewX_AVM-DE_Speed"]
};
}
public int GetTotalBytesSent()
{
string elementValue = this.GetElementValue(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wancommonifconfig1", "urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetTotalBytesSent")).GetResponseStream(), "NewTotalBytesSent");
return elementValue == null ? -1 : Convert.ToInt32(elementValue);
}
public int GetTotalBytesReceived()
{
string elementValue = this.GetElementValue(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wancommonifconfig1", "urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetTotalBytesReceived")).GetResponseStream(), "NewTotalBytesReceived");
return elementValue == null ? -1 : Convert.ToInt32(elementValue);
}
public CommonLinkProperties GetCommonLinkProperties()
{
Dictionary<string, string> elementValues = this.GetElementValues(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wancommonifconfig1", "urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetCommonLinkProperties")).GetResponseStream(), (IEnumerable<string>)new string[4]
{
"NewWANAccessType",
"NewLayer1UpstreamMaxBitRate",
"NewLayer1DownstreamMaxBitRate",
"NewPhysicalLinkStatus"
});
return new CommonLinkProperties()
{
AccessType = elementValues["NewWANAccessType"],
MaxUpstreamBitRate = elementValues["NewLayer1UpstreamMaxBitRate"],
MaxDownstreamBitRate = elementValues["NewLayer1DownstreamMaxBitRate"],
Status = elementValues["NewPhysicalLinkStatus"]
};
}
public DslInterfaceInfo GetDslInterfaceInfo()
{
Dictionary<string, string> elementValues = this.GetElementValues(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wandslifconfig1", "urn:dslforum-org:service:WANDSLInterfaceConfig:1#GetInfo")).GetResponseStream(), (IEnumerable<string>)new string[9]
{
"NewStatus",
"NewUpstreamCurrRate",
"NewDownstreamCurrRate",
"NewUpstreamMaxRate",
"NewDownstreamMaxRate",
"NewUpstreamNoiseMargin",
"NewDownstreamNoiseMargin",
"NewUpstreamAttenuation",
"NewDownstreamAttenuation"
});
return new DslInterfaceInfo()
{
Status = elementValues["NewStatus"],
CurrentUpstreamRate = elementValues["NewUpstreamCurrRate"],
CurrentDownstreamRate = elementValues["NewDownstreamCurrRate"],
MaxUpstreamRate = elementValues["NewUpstreamMaxRate"],
MaxDownstreamRate = elementValues["NewDownstreamMaxRate"],
UpstreamNoiseMargin = elementValues["NewUpstreamNoiseMargin"],
DownstreamNoiseMargin = elementValues["NewDownstreamNoiseMargin"],
UpstreamAttenuation = elementValues["NewUpstreamAttenuation"],
DownstreamAttenuation = elementValues["NewDownstreamAttenuation"]
};
}
public string GetExternalIPAddress()
{
return this.GetElementValue(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wanpppconn1", "urn:dslforum-org:service:WANPPPConnection:1#GetExternalIPAddress")).GetResponseStream(), "NewExternalIPAddress");
}
public WirelessLanInfo GetWirelessLanInfo()
{
Dictionary<string, string> elementValues = this.GetElementValues(((WebResponse)this.SendSoapRequest("tr064/upnp/control/wlanconfig1", "urn:dslforum-org:service:WLANConfiguration:1#GetInfo")).GetResponseStream(), (IEnumerable<string>)new string[4]
{
"NewEnable",
"NewStatus",
"NewChannel",
"NewSSID"
});
return new WirelessLanInfo()
{
IsEnabled = elementValues["NewEnable"] == "1",
Status = elementValues["NewStatus"],
Channel = elementValues["NewChannel"],
SSID = elementValues["NewSSID"]
};
}
public void SetWirelessLan(bool enable)
{
this.SendSoapRequest("tr064/upnp/control/wlanconfig1", "urn:dslforum-org:service:WLANConfiguration:1#SetEnable", new Dictionary<string, string>()
{
{
"NewEnable",
enable ? "1" : "0"
}
});
}
private HttpWebResponse SendSoapRequest(
string relativeUrl,
string soapAction,
Dictionary<string, string> soapActionParameters = null)
{
HttpWebRequest httpWebRequest = WebRequest.Create(new Uri(this.BaseAddress, relativeUrl)) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "text/xml; charset=utf-8";
httpWebRequest.Headers.Add("SOAPAction", soapAction);
httpWebRequest.Credentials = (ICredentials)new NetworkCredential(this.Username, this.Password);
string[] strArray = soapAction.Split('#', StringSplitOptions.None);
StreamWriter streamWriter = new StreamWriter(((WebRequest)httpWebRequest).GetRequestStream(), Encoding.ASCII);
((TextWriter)streamWriter).WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
((TextWriter)streamWriter).WriteLine("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
((TextWriter)streamWriter).WriteLine("<s:Body>");
if (soapActionParameters == null)
{
((TextWriter)streamWriter).WriteLine("<{1} xmlns=\"{0}\"/>", (object)strArray[0], (object)strArray[1]);
}
else
{
((TextWriter)streamWriter).WriteLine("<{1} xmlns=\"{0}\">", (object)strArray[0], (object)strArray[1]);
foreach (string key in soapActionParameters.Keys)
((TextWriter)streamWriter).WriteLine("<{0}>{1}</{0}>", (object)key, (object)soapActionParameters[key]);
((TextWriter)streamWriter).WriteLine("</{0}>", (object)strArray[1]);
}
((TextWriter)streamWriter).WriteLine("</s:Body>");
((TextWriter)streamWriter).WriteLine("</s:Envelope>");
((TextWriter)streamWriter).Close();
return httpWebRequest.GetResponse() as HttpWebResponse;
}
private string GetElementValue(Stream xmlStream, string elementName)
{
return ((XPathItem)new XPathDocument(xmlStream).CreateNavigator().SelectSingleNode("//" + elementName))?.Value;
}
private Dictionary<string, string> GetElementValues(
Stream xmlStream,
IEnumerable<string> elementNames)
{
Dictionary<string, string> elementValues = new Dictionary<string, string>();
XPathNavigator navigator = new XPathDocument(xmlStream).CreateNavigator();
foreach (string elementName in elementNames)
{
XPathNavigator xpathNavigator = navigator.SelectSingleNode("//" + elementName);
if (xpathNavigator != null)
elementValues[elementName] = ((XPathItem)xpathNavigator).Value;
}
return elementValues;
}
}
}