diff --git a/LicenseServer/App.config b/LicenseServer/App.config index 8e15646..85cc877 100644 --- a/LicenseServer/App.config +++ b/LicenseServer/App.config @@ -1,4 +1,4 @@ - + diff --git a/LicenseServer/Helpers.cs b/LicenseServer/Helpers.cs new file mode 100644 index 0000000..62272e4 --- /dev/null +++ b/LicenseServer/Helpers.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using System.Data.SQLite; +using System.Data.Linq; + +namespace LicenseServer +{ + public class Helpers + { + public static void InitDB() + { + using (var conn = new SQLiteConnection($"URI=file:{Environment.CurrentDirectory}\\data.sqlite")) + { + conn.Open(); + + using (var transaction = conn.BeginTransaction()) + { + var cmd = new SQLiteCommand(conn); + cmd.CommandText = @"CREATE TABLE IF NOT EXISTS LicenseUser(productid INT, licensekey TEXT, machinecode TEXT, username TEXT, lastaccess DATETIME, PRIMARY KEY (productid, licensekey,machinecode));"; + cmd.ExecuteNonQuery(); + cmd.CommandText = @"CREATE TABLE IF NOT EXISTS UserLog(id INT, productid INT, licensekey TEXT, machinecode TEXT, username TEXT, time DATETIME, PRIMARY KEY (id));"; + cmd.ExecuteNonQuery(); + transaction.Commit(); + } + + conn.Close(); + } + } + + public static void UpdateUser(int productId, string licenseKey, string machineCode, string username) + { + using (var conn = new SQLiteConnection($"URI=file:{Environment.CurrentDirectory}\\data.sqlite")) + { + conn.Open(); + + using (var transaction = conn.BeginTransaction()) + { + var cmd = new SQLiteCommand(conn); + + cmd.CommandText = @"INSERT OR REPLACE INTO licenseuser (productid, licensekey,machinecode,username,lastaccess) VALUES (@product, @license,@machine,@user,@time)"; + cmd.Parameters.AddWithValue("@product", productId); + cmd.Parameters.AddWithValue("@license", licenseKey); + cmd.Parameters.AddWithValue("@machine", machineCode); + cmd.Parameters.AddWithValue("@user", username); + cmd.Parameters.AddWithValue("@time", DateTime.UtcNow); + cmd.ExecuteNonQuery(); + transaction.Commit(); + } + + conn.Close(); + } + } + + //public GetParameters + + } +} diff --git a/LicenseServer/LicenseServer.csproj b/LicenseServer/LicenseServer.csproj index fff76b8..8b8e642 100644 --- a/LicenseServer/LicenseServer.csproj +++ b/LicenseServer/LicenseServer.csproj @@ -11,6 +11,8 @@ v4.5 512 true + + AnyCPU @@ -37,6 +39,11 @@ + + + ..\packages\System.Data.SQLite.Core.1.0.112.0\lib\net45\System.Data.SQLite.dll + + @@ -45,11 +52,20 @@ + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + \ No newline at end of file diff --git a/LicenseServer/Program.cs b/LicenseServer/Program.cs index 721d0cf..1065198 100644 --- a/LicenseServer/Program.cs +++ b/LicenseServer/Program.cs @@ -9,6 +9,7 @@ using System.IO; using System.Web; using System.Net.Sockets; +using System.Collections.Specialized; namespace LicenseServer { @@ -17,12 +18,18 @@ class Program static HttpListener httpListener = new HttpListener(); public static int port = 8080; + public static bool logging = false; static void Main(string[] args) { Console.WriteLine("Cryptolens License Server v1.0\n"); - if (args.Length == 1) + if(args.Length == 2) + { + port = Convert.ToInt32(args[0]); + logging = Convert.ToBoolean(args[1]); + } + else if (args.Length == 1) { port = Convert.ToInt32(args[0]); } @@ -45,6 +52,19 @@ static void Main(string[] args) Console.ReadLine(); return; } + + Console.WriteLine("\nWould you like to enable local logging [y/N]? This will create a local sqlite database in the same folder as the executable."); + + if(Console.ReadLine() == "y") + { + logging = true; + WriteMessage("Logging enabled."); + Helpers.InitDB(); + } + else + { + WriteMessage("Logging disabled."); + } } // inspired by https://www.codeproject.com/Tips/485182/%2FTips%2F485182%2FCreate-a-local-server-in-Csharp. @@ -97,7 +117,6 @@ static void ResponseThread() byte[] originalStream = ReadToByteArray(original.InputStream, 1024); - if (original.HttpMethod == "GET") { throw new ArgumentException("GET requests are not supported."); @@ -115,7 +134,21 @@ static void ResponseThread() context.Response.OutputStream.Write(output, 0, output.Length); context.Response.KeepAlive = false; - context.Response.Close(); + context.Response.Close(); + + if (logging) + { + NameValueCollection coll = HttpUtility.ParseQueryString(System.Text.UTF8Encoding.UTF8.GetString(originalStream)); + + try + { + Helpers.UpdateUser(Convert.ToInt32(coll["ProductId"]), coll["Key"], coll["MachineCode"], "no user"); + } + catch(Exception ex) + { + + } + } } } else diff --git a/LicenseServer/packages.config b/LicenseServer/packages.config new file mode 100644 index 0000000..e5f02dd --- /dev/null +++ b/LicenseServer/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file