using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO; //StreamReader
using System.Net; //Http
using System.Security.Cryptography; //Hash Md5
using System.Xml.Linq; //XDocument
using HtmlAgilityPack;
namespace Fritzbox_Tools
{
/// <summary>
/// Interaktionslogik für
MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
//tbxLog.Text = tbxLog.Text =
fl_Get_MD5Hash_of_String("17e17c0e-DemoPass");
Test();
}
public void Test()
{
//-------------------< Test()
>-------------------
//*test login and get first page
//--<
login >--
string benutzername = "xxxxxxxx";
string kennwort =tbxPassword.Text ;
//
SessionID ermitteln
string sid = GetSessionId(benutzername, kennwort);
tbxSID.Text = sid;
//--</ login >--
//----< tests >----
//< get home page >
string sHtml_of_website = SeiteEinlesen(@"http://fritz.box/home/home.lua", sid);
log(sHtml_of_website);
//</ get home page >
string sTest = "<html>hallo this is a
html-String</html>";
//--< loadHTML String to Browser
>--
clsWebbrowser_Errors.SuppressscriptErrors(ctlBrowser,
true);
ctlBrowser.NavigateToString(sTest);
//--</ loadHTML String to Browser
>--
//ctlBrowser.Navigate("http://fritz.box/home/home.lua");
//mshtml.HTMLDocument doc =
ctlBrowser.Document as mshtml.HTMLDocument;
//doc = ctlBrowser.Document as
HtmlDocument;
//< as htmldoc from webreqest >
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sTest);
//</ as htmldoc from webreqest >
//----</ tests >----
//-------------------</ Test()
>-------------------
}
public string SeiteEinlesen(string url, string sid)
{
//-------------------<
SeiteEinlesen() >-------------------
//*read page with sid access, by
webrequest
Uri uri = new Uri(url + "?sid=" + sid);
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
HttpWebResponse response =
request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
return str; //*return string-result
//-------------------</
SeiteEinlesen() >-------------------
}
public string GetSessionId(string benutzername, string kennwort)
{
//-------------------< GetSessionId()
>-------------------
//*get the current sessionID for
fritz.box
XDocument doc = XDocument.Load(@"http://fritz.box/login_sid.lua");
string sid =
fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "SID");
if (sid == "0000000000000000")
{
string challenge =
fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "Challenge");
tbxCallenge.Text = challenge;
log("Challenge: " + challenge );
string sResponse =
fl_GetResponse_by_TempUser_Passwort(challenge, kennwort);
log("Response of TempUser_Passwort:
" +
sResponse);
string uri = @"http://fritz.box/login_sid.lua?username=" + benutzername + @"&response=" + sResponse;
doc = XDocument.Load(uri);
sid =
fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "SID");
tbxSID.Text = sid;
log("SID:" + sid);
}
return sid;
//-------------------</
GetSessionId() >-------------------
}
public string fl_GetResponse_by_TempUser_Passwort(string challenge, string kennwort)
{
//-------------------< get
fritzbox-challenge+hashcode () >-------------------
return challenge + "-" + fl_Get_MD5Hash_of_String(challenge + "-" + kennwort);
//-------------------</ get
fritzbox-challenge+hashcode () >-------------------
}
public string fl_Get_MD5Hash_of_String(string input)
{
//-------------------<
fl_Get_MD5Hash_of_String() >-------------------
//*create hashcode from string
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(input));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2"));
}
return sb.ToString();
//-------------------</
fl_Get_MD5Hash_of_String() >-------------------
}
#region ----< helper-methods
>------
public string
fl_Get_Value_of_Node_in_XDocument_by_NodeName(XDocument doc, string name)
{
XElement info = doc.FirstNode as XElement;
return info.Element(name).Value;
}
private void log(string sText)
{
tbxLog.Text = DateTime.Now.ToLongTimeString() + ": " + sText + Environment.NewLine + tbxLog.Text;
}
#endregion ----</ helper-methods >------
}
}
|