more...
authorStricted <info@stricted.de>
Fri, 30 Oct 2015 10:30:57 +0000 (11:30 +0100)
committerStricted <info@stricted.de>
Fri, 30 Oct 2015 10:30:57 +0000 (11:30 +0100)
SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs [new file with mode: 0644]
SpeedportHybridControl.Implementations/SpeedportHybridControl.Implementations.csproj
SpeedportHybridControl/Model/LoginPageModel.cs
SpeedportHybridControl/page/LoginPage.xaml

diff --git a/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs b/SpeedportHybridControl.Implementations/SpeedportHybridAPI.cs
new file mode 100644 (file)
index 0000000..c330b2f
--- /dev/null
@@ -0,0 +1,867 @@
+using Newtonsoft.Json.Linq;
+using System;
+using System.Linq;
+using System.IO;
+using System.Net;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Windows;
+using System.Threading;
+using System.Collections.Generic;
+
+namespace SpeedportHybridControl.Implementations {
+       public class SpeedportHybridAPI : SingletonFactory<SpeedportHybridAPI> {
+               public string ip = "speedport.ip";
+               private DateTime _lastReboot = DateTime.MinValue;
+               private bool _checkIsActive = false;
+               public string _password;
+               public string _challenge;
+               public string _hash;
+               public string _derivedk;
+               public CookieContainer _cookie = new CookieContainer();
+               
+               /**
+                * Requests the password-challenge from the router.
+                *
+                * @return      string
+                */
+               public string getChallenge () {
+                       string response = sendRequest("data/Login.json", "csrf_token=nulltoken&showpw=0&challengev=null");
+                       if (response.IsNullOrEmpty())
+                               return string.Empty;
+
+                       string challenge = string.Empty;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               challenge = jArray.getVar("challengev");
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return challenge;
+               }
+               
+               /**
+                * calculate the derivedk
+                *
+                * @param       string  $password
+                * @return      string
+                */
+               public string getDerviedk () {
+                       return _password.sha256().pbkdf2(_challenge.Substring(0, 16));
+               }
+
+               /**
+                * login into the router with the given password
+                * 
+                * @param       string  $password
+                * @return      bool
+                */
+               public bool login (string passwort) {
+                       if (passwort.IsNullOrEmpty()) {
+                               return false;
+                       }
+
+                       _cookie = new CookieContainer();
+
+                       _password = passwort;
+                       _challenge = getChallenge();
+                       _hash = string.Concat(_challenge, ":", _password).sha256();
+                       
+                       string response = sendRequest("data/Login.json", string.Concat("csrf_token=nulltoken&showpw=0&password=", _hash));
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       _cookie.Add(new Cookie("challengev", _challenge) { Domain = "speedport.ip" });
+
+                       bool login = false;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+                               if (jArray.getVar("login").Equals("success")) {
+                                       if (isLoggedin().Equals(false)) {
+                                               login = false;
+                                       }
+                                       else {
+                                               login = true;
+                                               _derivedk = getDerviedk();
+                                               _lastReboot = getLastReboot();
+                    }
+                               }
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+                       
+                       return login;
+               }
+
+               /**
+                * logout
+                * 
+                * @return      bool
+                */
+               public bool logout () {
+                       string response = sendRequest("data/Login.json", string.Concat("csrf_token=", getToken(), "&logout=byby"));
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       bool logout = false;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       if (isLoggedin().Equals(true)) {
+                                               logout = false;
+                                       }
+                                       else {
+                                               logout = true;
+                                               _password = "";
+                                               _challenge = "";
+                                               _cookie = new CookieContainer();
+                                               _lastReboot = DateTime.MinValue;
+                                               _hash = "";
+                                               _derivedk = "";
+                                       }
+                               }
+
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return logout;
+               }
+
+               /**
+                * check if we are logged in
+                *
+                * @return      bool
+                */
+               public bool checkLogin () {
+                       // TODO:
+
+                       /*
+                       if (_checkIsActive.Equals(false)) {
+                               _checkIsActive = true;
+                               if (isLoggedin().Equals(false)) {
+                                       Console.WriteLine("Session expired, try to relogin");
+
+                                       Thread.Sleep(400);
+
+                                       if (login(_password).Equals(false)) {
+                                               // should we try to relogin? login(_password);...
+                                               new Thread(() => { LogManager.WriteToLog("Session expired."); }).Start();
+                                               _password = "";
+                                               _challenge = "";
+                                               _cookie = new CookieContainer();
+                                               _lastReboot = DateTime.MinValue;
+                                               _hash = "";
+                                               _derivedk = "";
+
+                                               util.logout();
+                                               new Thread(() => { MessageBox.Show("Session expired.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                                               _checkIsActive = false;
+                                               return false;
+                                       }
+                               }
+
+                               _checkIsActive = false;
+                       }
+                       else {
+                               Console.WriteLine("check allready in progress");
+                       }
+                       */
+
+                       return true;
+               }
+
+               /**
+                * check if we are logged in
+                *
+                * @param       bool    ischeck
+                * @return      bool
+                */
+               public bool isLoggedin () {
+                       string response = sendRequest("data/SecureStatus.json");
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       bool login = false;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               if (jArray.getVar("loginstate").Equals("1")/* && jArray.getVar("login").Equals("true")*/) {
+                                       login = true;
+                               }
+
+                               jArray = null;
+                       }
+                       
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return login;
+               }
+
+               /**
+                * reboot the router
+                */
+               public void reboot () {
+                       // TODO:
+                       /*
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       string response = sendRequest("data/Reboot.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&reboot_device=true"));
+                       if (response.IsNullOrEmpty())
+                               return;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       new Thread(() => { MessageBox.Show("Router Reboot.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
+                                       LogManager.WriteToLog("Router Reboot.");
+                                       _password = "";
+                                       _challenge = "";
+                                       _cookie = new CookieContainer();
+                                       _hash = "";
+                                       _derivedk = "";
+
+                                       util.logout();
+                               }
+                               
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+                       */
+               }
+
+               /**
+                * reconnect LTE
+                *
+                * @return      bool
+                */
+               public bool reconnectLte () {
+                       if (checkLogin().Equals(false))
+                               return false;
+
+                       Thread.Sleep(400);
+
+                       string response = sendEnryptedRequest("data/modules.json", string.Concat("lte_reconn=1&csrf_token=", Uri.EscapeUriString(getToken())));
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               response = null;
+
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       jArray = null;
+                                       return true;
+                               }
+
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return false;
+               }
+
+               /**
+                * reconnect DSL
+                *
+                * @return      bool
+                */
+               public bool reconnectDSL () {
+                       if (checkLogin().Equals(false))
+                               return false;
+
+                       Thread.Sleep(400);
+
+                       string response = sendEnryptedRequest("data/Connect.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&showpw=0&password=", _hash, "&req_connect=offline"));
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       bool offline = false;
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               response = null;
+
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       offline = true;
+                               }
+
+                               jArray = null;
+
+                               if (offline.Equals(true)) {
+                                       response = sendEnryptedRequest("data/Connect.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&showpw=0&password=", _hash, "&req_connect=online"));
+                                       jArray = JToken.Parse(response);
+                                       if (jArray.getVar("status").Equals("ok")) {
+                                               jArray = null;
+                                               return true;
+                                       }
+                               }
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return false;
+               }
+
+               /**
+                * change dsl connection status
+                *
+                * @param       string  status
+                * @return      bool
+                */
+               public bool changeDSLStatus (string status) {
+                       if (checkLogin().Equals(false))
+                               return false;
+
+                       if (status.Equals("online") || status.Equals("offline")) {
+
+                               string response = sendEnryptedRequest("data/Connect.json", string.Concat("req_connect=", status, "&csrf_token=", Uri.EscapeUriString(getToken())));
+                               if (response.IsNullOrEmpty())
+                                       return false;
+                               try {
+                                       JToken jArray = JToken.Parse(response);
+
+                                       response = null;
+
+                                       if (jArray.getVar("status").Equals("ok")) {
+                                               jArray = null;
+                                               return true;
+                                       }
+                               }
+                               catch (Exception ex) {
+                                       LogManager.WriteToLog(ex.Message);
+                               }
+
+                               response = null;
+                       }
+
+                       return false;
+               }
+
+               /**
+                * change lte connection status
+                *
+                * @param       string  status
+                * @return      bool
+                */
+               public bool changeLTEStatus (string status) {
+                       if (checkLogin().Equals(false))
+                               return false;
+                       
+                       if (status.Equals("online") || status.Equals("offline")) {
+                               if (status.Equals("online"))
+                                       status = "1";
+
+                               if (status.Equals("offline"))
+                                       status = "0";
+
+                               string response = sendEnryptedRequest("data/Modules.json", string.Concat("use_lte=", status, "&csrf_token=", Uri.EscapeUriString(getToken())));
+                               if (response.IsNullOrEmpty())
+                                       return false;
+                               try {
+                                       JToken jArray = JToken.Parse(response);
+
+                                       response = null;
+
+                                       if (jArray.getVar("status").Equals("ok")) {
+                                               jArray = null;
+                                               return true;
+                                       }
+                               }
+                               catch (Exception ex) {
+                                       LogManager.WriteToLog(ex.Message);
+                               }
+
+                               response = null;
+                       }
+
+                       return false;
+               }
+
+               /**
+                * reset the router to Factory Default
+                * not tested
+                *
+                * @return      bool
+                */
+               public bool resetToFactoryDefault () {
+                       if (checkLogin().Equals(false))
+                               return false;
+
+                       Thread.Sleep(400);
+
+                       string response = sendEnryptedRequest("data/resetAllSetting.json", string.Concat("csrf_token=nulltoken&showpw=0&password=", _hash, "&reset_all=true"));
+                       if (response.IsNullOrEmpty())
+                               return false;
+
+                       try {
+                               JToken jArray = JToken.Parse(response);
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       return true;
+                               }
+
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+
+                       return false;
+               }
+
+               /**
+                * check for firmware update
+                */
+               public void checkFirmware () {
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       Thread.Sleep(400);
+
+                       string response = sendRequest("data/checkfirm.json");
+                       if (response.IsNullOrEmpty())
+                               return;
+
+                       try {
+                               bool fw_isActual = false;
+                               JToken jArray = JToken.Parse(response);
+
+                               if (jArray.getVar("fw_isActual").Equals("1")) {
+                                       fw_isActual = true;
+                               }
+
+                               if (fw_isActual.Equals(true)) {
+                                       // Die Firmware ist aktuell.
+                                       MessageBox.Show("Die Firmware ist aktuell.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
+                               }
+                               else {
+                                       // Es liegt eine neuere Firmware-Version vor. Möchten Sie diese Version jetzt installieren?
+                                       MessageBox.Show("Es liegt eine neuere Firmware-Version vor.\nMöchten Sie diese Version jetzt installieren?", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning);
+                               }
+
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+               }
+
+               /**
+                * flush dns cache
+                */
+               public void flushDNS () {
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       Thread.Sleep(400);
+
+                       string response = sendEnryptedRequest("data/dns.json", "op_type=flush_dns_cache");
+                       if (response.IsNullOrEmpty())
+                               return;
+
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               if (jArray["DCI"].Count().Equals(0)) {
+                                       new Thread(() => { MessageBox.Show("DNS cache geleert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
+                               }
+                               else {
+                                       new Thread(() => { MessageBox.Show("unable to flush dns cache", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                               }
+
+                               
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+               }
+
+               /**
+                * clear the Syslog
+                */
+               public void clearSyslog () {
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       Thread.Sleep(400);
+
+                       string response = sendEnryptedRequest("data/SystemMessages.json", string.Concat("action_clearlist=true&clear_type=0&", "csrf_token=", getToken()));
+                       if (response.IsNullOrEmpty())
+                               return;
+
+                       try {
+                               JToken jArray = JToken.Parse(response);
+
+                               if (jArray.getVar("status").Equals("ok")) {
+                                       // ok
+                                       new Thread(() => { MessageBox.Show("Syslog geleert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
+                               }
+                               else {
+                                       // fail
+                                       new Thread(() => { MessageBox.Show("Konnte Syslog nicht leeren.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                               }
+
+                               jArray = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+               }
+
+               /**
+                * set QueueSkbTimeOut
+                *
+                * @param       string  value
+                */
+               public void setQueueSkbTimeOut (string value) {
+                       // TODO:
+                       /*
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       string response = sendEnryptedRequest("data/bonding_tr181.json", string.Concat("bonding_QueueSkbTimeOut=", value));
+                       if (response.IsNullOrEmpty())
+                               return;
+                       try {
+                               TR181 obj = JsonConvert.DeserializeObject<TR181>(response);
+
+                               if (obj.QueueSkbTimeOut.Equals(value)) {
+                                       new Thread(() => { MessageBox.Show("QueueSkbTimeOut geändert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
+                               }
+                               else {
+                                       new Thread(() => { MessageBox.Show("unable to change QueueSkbTimeOut", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                               }
+
+                               obj = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+                       */
+               }
+
+               /**
+                * set Antenna Mode
+                *
+                * @param       string  value
+                */
+               public void setAntennaMode (string value) {
+                       // TODO:
+                       /*
+                       if (checkLogin().Equals(false))
+                               return;
+
+                       string response = sendEnryptedRequest("data/lteinfo.json", string.Concat("mode_select=", value));
+                       if (response.IsNullOrEmpty())
+                               return;
+                       try {
+                               LTE obj = JsonConvert.DeserializeObject<LTE>(response);
+
+                               string antenna_mode;
+                               if (obj.antenna_mode.Equals("Antennal set to internal")) {
+                                       antenna_mode = "Inner";
+                               }
+                               else if (obj.antenna_mode.Equals("Antennal set to external")) {
+                                       antenna_mode = "Outer";
+                               }
+                               else {
+                                       antenna_mode = "Auto";
+                               }
+
+                               if (antenna_mode.Equals(value)) {
+                                       new Thread(() => { MessageBox.Show("Antennen Modus geändert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
+                               }
+                               else {
+                                       new Thread(() => { MessageBox.Show("Antennen Modus ändern Fehlgeschlagen", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                               }
+
+                               antenna_mode = null;
+                               
+                               obj = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       response = null;
+                       */
+               }
+
+               /**
+                * get Last Reboot time
+                * 
+                * @return      DateTime
+                */
+               public DateTime getLastReboot () {
+                       if (_lastReboot.Equals(DateTime.MinValue).Equals(false)) {
+                               return _lastReboot;
+            }
+
+                       string response = sendRequest("data/Reboot.json");
+
+                       if (response.IsNullOrEmpty())
+                               return DateTime.Now;
+
+                       JToken jArray = JToken.Parse(response);
+
+                       DateTime lastReboot = DateTime.Parse(string.Concat(jArray.getVar("reboot_date"), " ", jArray.getVar("reboot_time")));
+
+                       jArray = null;
+
+                       return lastReboot;
+               }
+
+               /**
+                * get the csrf token from router
+                *
+                * @return      string
+                */
+               public string getToken () {
+                       string response = sendRequest("html/content/overview/index.html");
+                       if (response.IsNullOrEmpty())
+                               return string.Empty;
+
+                       string a = "csrf_token = \"";
+                       string b = "\";";
+                       string token = response.Substring((response.IndexOf(a) + a.Length), (response.IndexOf(b) - response.IndexOf(a) - a.Length));
+
+                       response = null;
+                       a = null;
+                       b = null;
+
+                       Console.WriteLine("csrf_token: " + token);
+                       return token;
+               }
+
+               /**
+                * send encrypted request to the router
+                *
+                * @param       string  path
+                * @param       string  post
+                * @param       bool    cookie
+                * @return      string
+                */
+               public string sendEnryptedRequest (string path, string post = "", bool cookie = true) {
+                       string response = string.Empty;
+
+                       try {
+                               sjcl sjcl = new sjcl();
+
+                               string iv = _challenge.Substring(16, 16);
+                               string adata = _challenge.Substring(32, 16);
+                               string dKey = _derivedk;
+
+
+                               // TODO: check if we need this really?
+                               if (post.IsNullOrEmpty().Equals(false)) {
+                                       post = sjcl.encrypt(dKey, post, iv, adata);
+                               }
+
+
+                               response = sendRequest(path, post, cookie);
+                               // check if the return value is hex (hex = enrypted)
+                               if (Regex.IsMatch(response, @"\A\b[0-9a-fA-F]+\b\Z").Equals(true)) {
+                                       response = sjcl.decrypt(dKey, response, iv, adata);
+                               }
+                               
+                               post = null;
+                               iv = null;
+                               adata = null;
+                               dKey = null;
+                               sjcl = null;
+                               
+                       }
+                       catch (ArgumentOutOfRangeException ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       return response;
+               }
+               
+               /**
+                * send request to the router
+                *
+                * @param       string  path
+                * @param       string  post
+                * @param       bool    cookie
+                * @return      string
+                */
+               public string sendRequest (string path, string post = "", bool cookie = true) {
+                       string response = string.Empty;
+                       try {
+                               string url = string.Concat("http://", ip, "/", path, "?lang=de");
+                               
+                               HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
+                               /* set timeout to 10 seconds */
+                               webRequest.Timeout = 10000;
+                               
+                               if (cookie.Equals(true)) {
+                                       webRequest.CookieContainer = _cookie;
+                               }
+
+                               if (post.IsNullOrEmpty().Equals(false)) {
+                                       webRequest.Method = "POST";
+                                       byte[] dataStream = Encoding.UTF8.GetBytes(post);
+                                       webRequest.ContentLength = dataStream.Length;
+                                       Stream newStream = webRequest.GetRequestStream();
+                                       newStream.Write(dataStream, 0, dataStream.Length);
+                                       newStream.Close();
+                                       newStream.Dispose();
+                                       newStream = null;
+                                       dataStream = null;
+                               }
+
+                               WebResponse webResponse = webRequest.GetResponse();
+                               StreamReader reader = new StreamReader(webResponse.GetResponseStream());
+                               response = reader.ReadToEnd().ToString();
+
+                               webResponse.Dispose();
+                               reader.Dispose();
+                               reader = null;
+                               webRequest = null;
+                               webResponse = null;
+                               post = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       return response;
+               }
+
+               public string sendRequest2 (string path, Dictionary<string, object> files, string post = "", bool cookie = true) {
+                       string response = string.Empty;
+
+                       try {
+                               string url = string.Concat("http://", ip, "/", path, "?lang=de");
+
+                               string boundary = string.Concat("---------------------------", DateTime.Now.Ticks.ToString("x"));
+                               byte[] boundaryBytes = Encoding.ASCII.GetBytes(string.Concat("\r\n--", boundary, "\r\n"));
+
+                               HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
+                               request.ContentType = string.Concat("multipart/form-data; boundary=", boundary);
+                               request.Method = "POST";
+                               request.KeepAlive = true;
+
+                               if (cookie.Equals(true)) {
+                                       request.CookieContainer = _cookie;
+                               }
+
+                               Stream requestStream = request.GetRequestStream();
+
+                               if (string.IsNullOrEmpty(post).Equals(false)) {
+                                       byte[] dataStream = Encoding.UTF8.GetBytes(post);
+                                       requestStream.Write(dataStream, 0, dataStream.Length);
+                                       dataStream = null;
+                               }
+
+                               if (files != null && files.Count > 0) {
+                                       foreach (KeyValuePair<string, object> pair in files) {
+                                               requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
+                                               if (pair.Value is FormFile) {
+                                                       FormFile file = pair.Value as FormFile;
+                                                       string header = string.Concat("Content-Disposition: form-data; name=\"", pair.Key, "\"; filename=\"", file.Name, "\"\r\nContent-Type: ", file.ContentType, "\r\n\r\n");
+                                                       byte[] bytes = Encoding.UTF8.GetBytes(header);
+                                                       requestStream.Write(bytes, 0, bytes.Length);
+                                                       byte[] buffer = new byte[32768];
+                                                       int bytesRead;
+                                                       if (file.Stream == null) {
+                                                               // upload from file
+                                                               FileStream fileStream = File.OpenRead(file.FilePath);
+                                                               while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
+                                                                       requestStream.Write(buffer, 0, bytesRead);
+                                                               }
+                                                               fileStream.Close();
+                                                       }
+                                                       else {
+                                                               // upload from given stream
+                                                               while ((bytesRead = file.Stream.Read(buffer, 0, buffer.Length)) != 0)
+                                                                       requestStream.Write(buffer, 0, bytesRead);
+                                                       }
+                                               }
+                                               else {
+                                                       string data = string.Concat("Content-Disposition: form-data; name=\"", pair.Key, "\"\r\n\r\n", pair.Value);
+                                                       byte[] bytes = Encoding.UTF8.GetBytes(data);
+                                                       requestStream.Write(bytes, 0, bytes.Length);
+                                               }
+                                       }
+
+                                       byte[] trailer = Encoding.ASCII.GetBytes(string.Concat("\r\n--", boundary, "--\r\n"));
+                                       requestStream.Write(trailer, 0, trailer.Length);
+                                       requestStream.Close();
+
+                               }
+
+                               WebResponse webResponse = request.GetResponse();
+                               Stream responseStream = webResponse.GetResponseStream();
+                               StreamReader reader = new StreamReader(responseStream);
+                               response = reader.ReadToEnd();
+
+                               webResponse.Dispose();
+                               reader.Dispose();
+                               reader = null;
+                               request = null;
+                               webResponse = null;
+                       }
+                       catch (Exception ex) {
+                               LogManager.WriteToLog(ex.Message);
+                       }
+
+                       return response;
+               }
+       }
+
+       public class FormFile {
+               public string Name { get; set; }
+
+               public string ContentType { get; set; }
+
+               public string FilePath { get; set; }
+
+               public Stream Stream { get; set; }
+       }
+}
\ No newline at end of file
index ecba430c1ede443869682e62966b8836b49e7de8..d6969c6678e6f3b554188c1b12345509615d1077 100644 (file)
@@ -61,6 +61,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
+    <Compile Include="SpeedportHybridAPI.cs" />
     <Compile Include="util.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
index a1589c3a209234ce760bdd195c2790626c3b81e5..29cddcb06d1aa8a53aec8d84247632579c1ad39a 100644 (file)
@@ -1,16 +1,17 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using SpeedportHybridControl.Implementations;
+using System.Windows;
+using System.Threading;
 
 namespace SpeedportHybridControl.Model {
        class LoginPageModel : SuperViewModel {
                private string _ip = "speedport.ip";
                private string _password;
+               private string _loginButtonText = "Login";
                private bool _showPassword;
                private bool _savePassword;
+               private Visibility _passwordBoxVisibility = Visibility.Visible;
+               private Visibility _passwordTextBoxVisibility = Visibility.Hidden;
 
                private DelegateCommand _showPasswordCommand;
                private DelegateCommand _savePasswordCommand;
@@ -26,6 +27,11 @@ namespace SpeedportHybridControl.Model {
                        set { SetProperty(ref _password, value); }
                }
 
+               public string LoginButtonText {
+                       get { return _loginButtonText; }
+                       set { SetProperty(ref _loginButtonText, value); }
+               }
+
                public bool ShowPassword {
                        get { return _showPassword; }
                        set { SetProperty(ref _showPassword, value); }
@@ -36,6 +42,16 @@ namespace SpeedportHybridControl.Model {
                        set { SetProperty(ref _savePassword, value); }
                }
 
+               public Visibility PasswordBoxVisibility {
+                       get { return _passwordBoxVisibility; }
+                       set { SetProperty(ref _passwordBoxVisibility, value); }
+               }
+
+               public Visibility PasswordTextBoxVisibility {
+                       get { return _passwordTextBoxVisibility; }
+                       set { SetProperty(ref _passwordTextBoxVisibility, value); }
+               }
+
                public DelegateCommand ShowPasswordCommand {
                        get { return _showPasswordCommand; }
                        set { SetProperty(ref _showPasswordCommand, value); }
@@ -52,7 +68,14 @@ namespace SpeedportHybridControl.Model {
                }
 
                private void OnShowPasswordCommandExecute () {
-                       Console.WriteLine(ShowPassword);
+                       if (ShowPassword.Equals(true)) {
+                               PasswordBoxVisibility = Visibility.Hidden;
+                               PasswordTextBoxVisibility = Visibility.Visible;
+            }
+                       else {
+                               PasswordBoxVisibility = Visibility.Visible;
+                               PasswordTextBoxVisibility = Visibility.Hidden;
+                       }
                }
 
                private void OnSavePasswordCommandExecute () {
@@ -60,10 +83,56 @@ namespace SpeedportHybridControl.Model {
                }
 
                private void OnLoginCommandExecute () {
-                       Console.WriteLine(password);
+                       MainWindowModel mwm = Application.Current.FindResource("MainWindowModel") as MainWindowModel;
+
+                       if (LoginButtonText.Equals("Login")) {
+                               if (SpeedportHybridAPI.getInstance().ip.Equals(ip).Equals(false)) {
+                                       SpeedportHybridAPI.getInstance().ip = ip;
+                               }
+
+                               bool login = SpeedportHybridAPI.getInstance().login(password);
+                               if (login.Equals(true)) {
+                                       if (SavePassword.Equals(true)) {
+                                               /*
+                                               SettingsModel SettingsModel = new SettingsModel {
+                                                       password = password,
+                                                       ip = SpeedportHybridAPI.getInstance().ip
+                                               };
+
+                                               Settings.save(SettingsModel);
+                                               */
+                                       }
+                                       else {
+                                               /*
+                                               SettingsModel SettingsModel = new SettingsModel {
+                                                       password = string.Empty,
+                                                       ip = SpeedportHybridAPI.getInstance().ip
+                                               };
+
+                                               Settings.save(SettingsModel);
+                                               */
+                                       }
+
+                                       LoginButtonText = "Logout";
+                                       mwm.LoginButtonContent = "Logout";
+                               }
+                               else {
+                                       new Thread(() => { MessageBox.Show("Login fehlgeschlagen. Sie haben ein falsches Gerätepasswort eingegeben. Bitte versuchen Sie es erneut und achten Sie auf die korrekte Schreibweise.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
+                                       LogManager.WriteToLog("Login Failed, wrong password");
+                               }
+                       }
+                       else {
+                               if (SpeedportHybridAPI.getInstance().logout().Equals(true)) {
+                                       // TODO: util.logout();
+                                       LoginButtonText = "Login";
+                                       mwm.LoginButtonContent = "Login";
+                               }
+                       }
                }
 
                public LoginPageModel () {
+                       // TODO: ip = Settings....
+                       // TODO: SpeedportHybridAPI.getInstance().ip = ip;
                        ShowPasswordCommand = new DelegateCommand(new Action(OnShowPasswordCommandExecute));
                        SavePasswordCommand = new DelegateCommand(new Action(OnSavePasswordCommandExecute));
                        LoginCommand = new DelegateCommand(new Action(OnLoginCommandExecute));
index 0dbb90403f08f511fee43642685d3c40d78d988b..7fa58df4f5d959c15c049f7e5dcfd670b86e7cab 100644 (file)
         <TextBox Text="{Binding Path=ip, Mode=TwoWay}" x:Name="tbip" HorizontalAlignment="Left" Height="23" Margin="115,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
 
         <TextBlock x:Name="tbpw" Text="Passwort:" HorizontalAlignment="Left" Margin="55,49,0,0" VerticalAlignment="Top" />
-        <PasswordBox Implementations:PasswordHelper.Attach="True" Implementations:PasswordHelper.Password="{Binding Path=password, Mode=TwoWay}"  x:Name="PasswordBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" ForceCursor="True" />
-        <TextBox Text="{Binding Path=password, Mode=TwoWay}" x:Name="PasswordTextBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" Visibility="Hidden"/>
+        <PasswordBox Implementations:PasswordHelper.Attach="True" Implementations:PasswordHelper.Password="{Binding Path=password, Mode=TwoWay}"  Visibility="{Binding Path=PasswordBoxVisibility}" x:Name="PasswordBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" ForceCursor="True" />
+        <TextBox Text="{Binding Path=password, Mode=TwoWay}" x:Name="PasswordTextBox" HorizontalAlignment="Left" Height="23" Margin="115,42,0,0" VerticalAlignment="Top" Width="120" Visibility="{Binding Path=PasswordTextBoxVisibility}"/>
         <CheckBox Command="{Binding Path=ShowPasswordCommand}" IsChecked="{Binding Path=ShowPassword, Mode=TwoWay}" x:Name="PasswordCheckBox" Content="Passwort anzeigen" Margin="240,50,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
         <CheckBox Command="{Binding Path=SavePasswordCommand}" IsChecked="{Binding Path=SavePassword, Mode=TwoWay}"  x:Name="cbSave" Content="Passwort speichern" Margin="240,65,0,0" Checked="CheckBox" Unchecked="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" />
-        <Button Command="{Binding Path=LoginCommand}" x:Name="button1" Content="Login" HorizontalAlignment="Left" Margin="115,70,0,0" VerticalAlignment="Top" Width="75" IsDefault="True"/>
+        <Button Command="{Binding Path=LoginCommand}" x:Name="button1" Content="{Binding Path=LoginButtonText}" HorizontalAlignment="Left" Margin="115,70,0,0" VerticalAlignment="Top" Width="75" IsDefault="True"/>
     </Grid>
 </Page>