From d144398e0d7a07a61cd4808d579796ad0a2fdb79 Mon Sep 17 00:00:00 2001 From: Ferda Mravenec Date: Fri, 3 Dec 2021 15:48:38 +0100 Subject: [PATCH] Add project files. --- App.config | 6 ++ Program.cs | 52 ++++++++++++++ Properties/AssemblyInfo.cs | 36 ++++++++++ WebApiClient.cs | 136 +++++++++++++++++++++++++++++++++++++ Zradelna.csproj | 62 +++++++++++++++++ Zradelna.sln | 25 +++++++ packages.config | 5 ++ 7 files changed, 322 insertions(+) create mode 100644 App.config create mode 100644 Program.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 WebApiClient.cs create mode 100644 Zradelna.csproj create mode 100644 Zradelna.sln create mode 100644 packages.config diff --git a/App.config b/App.config new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..7a31f69 --- /dev/null +++ b/Program.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Zradelna +{ + class Program + { + static void Main(string[] args) + { + //Create new instance of client + WebApiClient wa = new WebApiClient(); + + //Open homepage first to get sessin ID and cookie + wa.OpenHomepage(); + + //Login as "FFFFFFFF" + wa.Login(CardToId("FFFFFFFF")); + + //Get current account balance + wa.GetAccountBalance(); + } + + /// + /// HEX (Little-endian) string to UInt32 as string + /// + /// + /// + static string CardToId(string hexString) + { + StringBuilder sb = new StringBuilder(); + + uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier); + + byte[] floatVals = BitConverter.GetBytes(num); + byte[] newArray = new byte[4]; + int position = 3; + + foreach (byte b in floatVals) + { + newArray[position] = b; + position--; + } + + return BitConverter.ToUInt32(newArray, 0).ToString(); + } + } + + +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ec50840 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Zradelna")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("HP Inc.")] +[assembly: AssemblyProduct("Zradelna")] +[assembly: AssemblyCopyright("Copyright © HP Inc. 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("9d0dfb31-ab4a-4a14-8b90-260300cdf667")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/WebApiClient.cs b/WebApiClient.cs new file mode 100644 index 0000000..4ed5a93 --- /dev/null +++ b/WebApiClient.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.IO; +using System.Data; +using System.Web; +using Newtonsoft.Json; +using System.Text.RegularExpressions; + +namespace Zradelna +{ + public class WebApiClient + { + string cookie = string.Empty; + + private string login_id; + private string boarder; + private string workstation; + private string acc_id; + + private double accountBalance; + + public string Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; + public string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 OPR/81.0.4196.60"; + + public string Login_id { get => login_id; set => login_id = value; } + public string Boarder { get => boarder; set => boarder = value; } + public string Workstation { get => workstation; set => workstation = value; } + public string Acc_id { get => acc_id; set => acc_id = value; } + public double AccountBalance { get => accountBalance; } + + /// + /// Open homepage to set session ID and get in the cookie + /// + public void OpenHomepage() + { + var request = (HttpWebRequest)WebRequest.Create("http://172.15.27.236:8080/isis/objednavkytouch/index"); + + request.Accept = Accept; + request.Method = "GET"; + request.ContentType = "text/html; charset=utf-8"; + request.UserAgent = UserAgent; + + var response = (HttpWebResponse)request.GetResponse(); + var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + + cookie = response.Headers[HttpResponseHeader.SetCookie]; //Get cookie + } + + /// + /// Call login function using cookie + /// + /// Card ID - personal indetifier + /// + public void Login(string cardreader) + { + var request = (HttpWebRequest)WebRequest.Create("http://172.15.27.236:8080/isis/objednavkytouch/loginon"); + + var postData = "{\"cardreader\": \"" + cardreader + "\"}"; + var data = Encoding.ASCII.GetBytes(postData); + + request.Accept = Accept; + request.Method = "POST"; + request.ContentLength = data.Length; + request.ContentType = "application/json"; + request.UserAgent = UserAgent; + request.Referer = "http://172.15.27.236:8080/isis/objednavkytouch/index"; + + request.Headers[HttpRequestHeader.Cookie] = cookie; //Set cookie + + using (var stream = request.GetRequestStream()) + stream.Write(data, 0, data.Length); + + var response = (HttpWebResponse)request.GetResponse(); + var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + + GetLoginInfo( responseString); + } + + /// + /// Get login info from response string while logged in + /// + /// + public void GetLoginInfo(string response) + { + //Get login info + string json = new Regex("a_data=(.*)").Match(response).Groups[1].Value; //Extract desired JSON data + DataTable dtLoginInfo = (DataTable)Newtonsoft.Json.JsonConvert.DeserializeObject("[" + json + "]", (typeof(DataTable))); //Convert to DataTable + + login_id = dtLoginInfo.Rows[0]["login_id"].ToString(); + boarder = dtLoginInfo.Rows[0]["boarder"].ToString(); + workstation = dtLoginInfo.Rows[0]["workstation"].ToString(); + acc_id = dtLoginInfo.Rows[0]["acc_id"].ToString(); + } + + /// + /// Get current account balance for active login + /// + public void GetAccountBalance() + { + var request = (HttpWebRequest)WebRequest.Create("http://172.15.27.236:8080/isis/objednavky/AccountList"); + + var postData = "{\"login_id\": \"" + login_id + "\","; + postData += "\"boarder\": \"" + boarder + "\","; + postData += "\"accounts\": \"" + "" + "\","; + postData += "\"workstation\": \"" + workstation + "\","; + postData += "\"acc_id\": \"" + acc_id + "\","; + postData += "\"dest_id\": \"" + acc_id + "\"}"; + var data = Encoding.ASCII.GetBytes(postData); + + request.Accept = Accept; + request.Method = "POST"; + request.ContentLength = data.Length; + request.ContentType = "application/json"; + request.UserAgent = UserAgent; + request.Referer = "http://172.15.27.236:8080/isis/objednavkytouch/index"; + + request.Headers[HttpRequestHeader.Cookie] = cookie; //Set cookie + + using (var stream = request.GetRequestStream()) + stream.Write(data, 0, data.Length); + + var response = (HttpWebResponse)request.GetResponse(); + var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + + string stringBalance = new Regex("(.*) Kč").Match(responseString).Groups[1].Value; //Extract desired data + + Double.TryParse(stringBalance, out accountBalance); + } + + + } +} diff --git a/Zradelna.csproj b/Zradelna.csproj new file mode 100644 index 0000000..18606ba --- /dev/null +++ b/Zradelna.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {9D0DFB31-AB4A-4A14-8B90-260300CDF667} + Exe + Zradelna + Zradelna + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + packages\HtmlAgilityPack.1.11.39\lib\Net45\HtmlAgilityPack.dll + + + packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Zradelna.sln b/Zradelna.sln new file mode 100644 index 0000000..77e7ff7 --- /dev/null +++ b/Zradelna.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29728.190 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zradelna", "Zradelna.csproj", "{9D0DFB31-AB4A-4A14-8B90-260300CDF667}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9D0DFB31-AB4A-4A14-8B90-260300CDF667}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9D0DFB31-AB4A-4A14-8B90-260300CDF667}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9D0DFB31-AB4A-4A14-8B90-260300CDF667}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9D0DFB31-AB4A-4A14-8B90-260300CDF667}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {32E404A9-25D5-43C3-8020-D181EA891837} + EndGlobalSection +EndGlobal diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..228b153 --- /dev/null +++ b/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file