-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFritzSoapAccess.cs
338 lines (294 loc) · 12.7 KB
/
FritzSoapAccess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Collections.Generic;
using FritzWebAccess.SoapTypes;
// Sample Fritz!Box SOAP reqiuest and response
// POST https://fritz.box/tr064/upnp/control/hosts HTTP/1.1
// User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)
// VsDebuggerCausalityData: uIDPo9xYFSZefCpIrRzN+SNIpikAAAAAmsBQEjXHW0mckwLsN9RYZqCTwkC7p69CrZEBm00SYoAACQAA
// Content-Type: text/xml; charset=utf-8
// SOAPAction: "urn:dslforum-org:service:Hosts:1#GetHostNumberOfEntries"
// Authorization: Digest username="admin", ...
// Host: fritz.box
// Content-Length: 313
// Expect: 100-continue
//
// <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetHostNumberOfEntries
// xmlns="urn:dslforum-org:service:Hosts:1" /></soap:Body></soap:Envelope>
// HTTP/1.1 200 OK
// CONNECTION: close
// Connection: Keep-Alive
// Content-Length: 345
// CONTENT-TYPE: text/xml; charset="utf-8"
// DATE: Sun, 01 Jan 2017 20:45:23 GMT
// SERVER: FRITZ!Box 7490 (UI) UPnP/1.0 AVM FRITZ!Box 7490 (UI) 113.06.60
// EXT:
// Keep-Alive: timeout=60, max=300
//
// <?xml version="1.0"?>
// <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
// <s:Body>
// <u:GetHostNumberOfEntriesResponse xmlns:u="urn:dslforum-org:service:Hosts:1">
// <NewHostNumberOfEntries>13</NewHostNumberOfEntries>
// </u:GetHostNumberOfEntriesResponse>
// </s:Body>
// </s:Envelope>
namespace FritzWebAccess
{
/// <summary>
/// Access to Fritz!Box via UPnP (Soap).
/// </summary>
/// <remarks>
/// UPnP must be anabled in the device settings.
/// </remarks>
public class FritzSoapAccess
{
public Uri BaseAddress { get; set; } = new Uri("https://fritz.box/");
// default "admin" is used, if Fritz!Box security is set to "password only"
public string Username { get; set; } = "admin";
public string Password { get; set; } = string.Empty;
public FritzSoapAccess()
{
}
public int GetHostNumberOfEntries()
{
var response = SendSoapRequest(
"tr064/upnp/control/hosts",
"urn:dslforum-org:service:Hosts:1#GetHostNumberOfEntries"
);
// var resultString = new StreamReader(response.GetResponseStream()).ReadToEnd();
// Sample result:
// <?xml version="1.0"?>
// <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" >
// <s:Body>
// <u:GetHostNumberOfEntriesResponse xmlns:u="urn:dslforum-org:service:Hosts:1">
// <NewHostNumberOfEntries>13</NewHostNumberOfEntries>
// </u:GetHostNumberOfEntriesResponse>
// </s:Body>
// </s:Envelope>
var result = GetElementValue(response.GetResponseStream(), "NewHostNumberOfEntries");
if (result != null)
{
return Convert.ToInt32(result);
}
return -1;
}
public HostInfo GetGenericHostEntryExt(int index)
{
var response = SendSoapRequest(
"tr064/upnp/control/hosts",
"urn:dslforum-org:service:Hosts:1#X_AVM-DE_GetGenericHostEntryExt",
new Dictionary<string, string>
{
{"NewIndex" , index.ToString()}
});
var values = GetElementValues(
response.GetResponseStream(),
new string[]
{
"NewIPAddress",
"NewMACAddress",
"NewActive",
"NewHostName",
"NewInterfaceType",
"NewX_AVM-DE_Port",
"NewX_AVM-DE_Speed",
// "NewX_AVM-DE_UpdateAvailable",
// "NewX_AVM-DE_UpdateSuccessful",
// "NewX_AVM-DE_InfoURL",
// "NewX_AVM-DE_Model",
// "NewX_AVM-DE_URL",
});
return new HostInfo
{
IPAddress = values["NewIPAddress"],
MACAddress = values["NewMACAddress"],
IsActive = values["NewActive"] == "1",
HostName = values["NewHostName"],
InterfaceType = values["NewInterfaceType"],
Port = values["NewX_AVM-DE_Port"],
Speed = values["NewX_AVM-DE_Speed"],
};
}
public int GetTotalBytesSent()
{
var response = SendSoapRequest(
"tr064/upnp/control/wancommonifconfig1",
"urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetTotalBytesSent");
var result = GetElementValue(response.GetResponseStream(), "NewTotalBytesSent");
return (result != null) ? Convert.ToInt32(result) : -1;
}
public int GetTotalBytesReceived()
{
var response = SendSoapRequest(
"tr064/upnp/control/wancommonifconfig1",
"urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetTotalBytesReceived");
var result = GetElementValue(response.GetResponseStream(), "NewTotalBytesReceived");
return (result != null) ? Convert.ToInt32(result) : -1;
}
public CommonLinkProperties GetCommonLinkProperties()
{
var response = SendSoapRequest(
"tr064/upnp/control/wancommonifconfig1",
"urn:dslforum-org:service:WANCommonInterfaceConfig:1#GetCommonLinkProperties");
var values = GetElementValues(
response.GetResponseStream(),
new string[]
{
"NewWANAccessType",
"NewLayer1UpstreamMaxBitRate",
"NewLayer1DownstreamMaxBitRate",
"NewPhysicalLinkStatus"
});
return new CommonLinkProperties
{
AccessType = values["NewWANAccessType"],
MaxUpstreamBitRate = values["NewLayer1UpstreamMaxBitRate"],
MaxDownstreamBitRate = values["NewLayer1DownstreamMaxBitRate"],
Status = values["NewPhysicalLinkStatus"]
};
}
public DslInterfaceInfo GetDslInterfaceInfo()
{
var response = SendSoapRequest(
"tr064/upnp/control/wandslifconfig1",
"urn:dslforum-org:service:WANDSLInterfaceConfig:1#GetInfo");
// return new StreamReader(response.GetResponseStream()).ReadToEnd();
var values = GetElementValues(
response.GetResponseStream(),
new string[]
{
"NewStatus",
"NewUpstreamCurrRate", // Datenraten in kbit/s
"NewDownstreamCurrRate",
"NewUpstreamMaxRate",
"NewDownstreamMaxRate",
"NewUpstreamNoiseMargin", // Störabstandsmarge in dB*10
"NewDownstreamNoiseMargin",
"NewUpstreamAttenuation", // Leistungsdämpfung in dB*10
"NewDownstreamAttenuation"
});
return new DslInterfaceInfo
{
Status = values["NewStatus"],
CurrentUpstreamRate = values["NewUpstreamCurrRate"],
CurrentDownstreamRate = values["NewDownstreamCurrRate"],
MaxUpstreamRate = values["NewUpstreamMaxRate"],
MaxDownstreamRate = values["NewDownstreamMaxRate"],
UpstreamNoiseMargin = values["NewUpstreamNoiseMargin"],
DownstreamNoiseMargin = values["NewDownstreamNoiseMargin"],
UpstreamAttenuation = values["NewUpstreamAttenuation"],
DownstreamAttenuation = values["NewDownstreamAttenuation"]
};
}
public string GetExternalIPAddress()
{
var response = SendSoapRequest(
"tr064/upnp/control/wanpppconn1",
"urn:dslforum-org:service:WANPPPConnection:1#GetExternalIPAddress"
);
return GetElementValue(response.GetResponseStream(), "NewExternalIPAddress");
}
public WirelessLanInfo GetWirelessLanInfo()
{
var response = SendSoapRequest(
"tr064/upnp/control/wlanconfig1",
"urn:dslforum-org:service:WLANConfiguration:1#GetInfo"
);
var values = GetElementValues(
response.GetResponseStream(),
new string[]
{
"NewEnable",
"NewStatus",
"NewChannel",
"NewSSID"
});
return new WirelessLanInfo
{
IsEnabled = values["NewEnable"] == "1",
Status = values["NewStatus"],
Channel = values["NewChannel"],
SSID = values["NewSSID"]
};
}
public void SetWirelessLan(bool enable)
{
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)
{
// build absolute addr
var requestAddress = new Uri(BaseAddress, relativeUrl);
// create request
var request = WebRequest.Create(requestAddress) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", soapAction);
request.Credentials = new NetworkCredential(Username, Password);
// set request content
var SoapActionParts = soapAction.Split('#');
Stream requestStream = request.GetRequestStream();
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
streamWriter.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
streamWriter.WriteLine(@"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">");
streamWriter.WriteLine(@"<s:Body>");
if (soapActionParameters == null)
{
streamWriter.WriteLine(@"<{1} xmlns=""{0}""/>", SoapActionParts[0], SoapActionParts[1]);
}
else
{
streamWriter.WriteLine(@"<{1} xmlns=""{0}"">", SoapActionParts[0], SoapActionParts[1]);
foreach (string parameterName in soapActionParameters.Keys)
{
streamWriter.WriteLine(@"<{0}>{1}</{0}>", parameterName, soapActionParameters[parameterName]);
}
streamWriter.WriteLine(@"</{0}>", SoapActionParts[1]);
}
streamWriter.WriteLine(@"</s:Body>");
streamWriter.WriteLine(@"</s:Envelope>");
streamWriter.Close();
//send request and get response as string
return request.GetResponse() as HttpWebResponse;
}
private string GetElementValue(Stream xmlStream, string elementName)
{
var responseDocument = new XPathDocument(xmlStream);
var responseNavigator = responseDocument.CreateNavigator();
var resultElement = responseNavigator.SelectSingleNode("//" + elementName);
if (resultElement != null)
{
return resultElement.Value;
}
// not found
return null;
}
private Dictionary<string, string> GetElementValues(Stream xmlStream, IEnumerable<string> elementNames)
{
var result = new Dictionary<string, string>();
var responseDocument = new XPathDocument(xmlStream);
var responseNavigator = responseDocument.CreateNavigator();
foreach (string elementName in elementNames)
{
var resultElement = responseNavigator.SelectSingleNode("//" + elementName);
if (resultElement != null)
{
result[elementName] = resultElement.Value;
}
}
return result;
}
}
}