Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for logging #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LicenseServer/App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
Expand Down
61 changes: 61 additions & 0 deletions LicenseServer/Helpers.cs
Original file line number Diff line number Diff line change
@@ -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

}
}
16 changes: 16 additions & 0 deletions LicenseServer/LicenseServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -37,6 +39,11 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Data.SQLite, Version=1.0.112.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.112.0\lib\net45\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -45,11 +52,20 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Data.SQLite.Core.1.0.112.0\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.112.0\build\net45\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.112.0\build\net45\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.112.0\build\net45\System.Data.SQLite.Core.targets'))" />
</Target>
</Project>
39 changes: 36 additions & 3 deletions LicenseServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Web;
using System.Net.Sockets;
using System.Collections.Specialized;

namespace LicenseServer
{
Expand All @@ -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]);
}
Expand All @@ -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.
Expand Down Expand Up @@ -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.");
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions LicenseServer/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Data.SQLite.Core" version="1.0.112.0" targetFramework="net45" />
</packages>