added login function

This commit is contained in:
Alexander Eckart committed 2013-05-04 20:28:32 +02:00
1 parent 7f71853672
commit 1147d612fe
13 files changed
+249 -23

No files matched your search

@@ -33,12 +33,6 @@ namespace ClientServerLib
get { return connected; } get { return connected; }
} }
public bool LoggedIn
{
get { return connection.LoggedIn; }
set { connection.LoggedIn = value; }
}
public CClient() public CClient()
{ {
doConnect = false; doConnect = false;
@@ -18,7 +18,6 @@ namespace ClientServerLib
private int connectionID; private int connectionID;
public TcpClient TcpClient; public TcpClient TcpClient;
public bool LoggedIn;
public bool KeySet public bool KeySet
{ {
@@ -34,7 +33,6 @@ namespace ClientServerLib
{ {
TcpClient = client; TcpClient = client;
keySet = false; keySet = false;
LoggedIn = false;
connectionID = ConnectionID; connectionID = ConnectionID;
encoder = new UTF8Encoding(); encoder = new UTF8Encoding();
} }
+24 -13
View File
@@ -73,28 +73,39 @@ namespace ClientServerLib
private void ListenForClients() private void ListenForClients()
{ {
tcpListener.Start(); tcpListener.Start();
TcpClient client;
while (running) while (running)
{ {
//wait for new client //wait for new client
TcpClient client = tcpListener.AcceptTcpClient(); try
int id = ids.Dequeue();
CConnection connection = new CConnection(client, id);
lock (clients)
{ {
clients.Add(id, connection); client = tcpListener.AcceptTcpClient();
}
catch
{
client = null;
} }
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom)); if (client != null)
{
int id = ids.Dequeue();
CConnection connection = new CConnection(client, id);
NetworkStream clientStream = client.GetStream(); lock (clients)
byte[] serverParams = connection.GetKeyParams(); {
clientStream.Write(serverParams, 0, serverParams.Length); clients.Add(id, connection);
clientStream.Flush(); }
clientThread.Start(connection); Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom));
NetworkStream clientStream = client.GetStream();
byte[] serverParams = connection.GetKeyParams();
clientStream.Write(serverParams, 0, serverParams.Length);
clientStream.Flush();
clientThread.Start(connection);
}
} }
} }
@@ -48,6 +48,8 @@
<Compile Include="DeffieHellman\StrongNumberProvider.cs" /> <Compile Include="DeffieHellman\StrongNumberProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Structs.cs" /> <Compile Include="Structs.cs" />
<Compile Include="Vocaluxe\CCommands.cs" />
<Compile Include="Vocaluxe\VocaluxeStructs.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Security.Cryptography;
namespace Vocaluxe.Base.Server
{
public static class CCommands
{
public static SHA256Managed SHA256 = new SHA256Managed();
public const int CommandLogin = 20;
public const int ResponseLoginWrongPassword = 21;
public const int ResponseLoginFailed = 22;
public const int ResponseLoginOK = 23;
public static byte[] CreateCommandWithoutParams(int Command)
{
return BitConverter.GetBytes(Command);
}
/*
public static byte[] CreateResponseSendUserNames(SUsers Users)
{
return Serialize<SUsers>(ResponseSendUserNames, Users);
}
public static SUsers DecodeResponseSendUserNames(byte[] response)
{
SUsers users;
TryDeserialize<SUsers>(response, out users);
return users;
}
*/
public static byte[] CreateCommandLogin(string Password)
{
SLoginData data = new SLoginData();
data.SHA256 = SHA256.ComputeHash(Encoding.UTF8.GetBytes(Password));
return Serialize<SLoginData>(CommandLogin, data);
}
public static bool ResponseCommandLogin(byte[] Message, out SLoginData LoginData)
{
return TryDeserialize<SLoginData>(Message, out LoginData);
}
private static byte[] Serialize<T>(int Command, T obj)
{
byte[] command = BitConverter.GetBytes(Command);
MemoryStream stream = new MemoryStream();
stream.Write(command, 0, command.Length);
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
data = ms.ToArray();
}
stream.Write(data, 0, data.Length);
return stream.ToArray();
}
private static bool TryDeserialize<T>(byte[] message, out T obj)
{
obj = default(T);
if (message == null)
return false;
if (message.Length < 5)
return false;
byte[] data = new byte[message.Length - 4];
Array.Copy(message, 4, data, 0, data.Length);
using (MemoryStream ms = new MemoryStream(data))
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
obj = (T)formatter.Deserialize(ms);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
}
@@ -0,0 +1,10 @@
using System;
namespace Vocaluxe.Base.Server
{
[Serializable]
public struct SLoginData
{
public byte[] SHA256;
}
}
+1 -1
View File
@@ -174,7 +174,7 @@
this.tbPassword.Name = "tbPassword"; this.tbPassword.Name = "tbPassword";
this.tbPassword.Size = new System.Drawing.Size(86, 20); this.tbPassword.Size = new System.Drawing.Size(86, 20);
this.tbPassword.TabIndex = 6; this.tbPassword.TabIndex = 6;
this.tbPassword.Text = "vocaluxe"; this.tbPassword.UseSystemPasswordChar = true;
// //
// lbPassword // lbPassword
// //
+39
View File
@@ -9,16 +9,19 @@ using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using ClientServerLib; using ClientServerLib;
using Vocaluxe.Base.Server;
namespace ClientTest namespace ClientTest
{ {
public partial class ClientTest : Form public partial class ClientTest : Form
{ {
CClient client; CClient client;
private bool loggedIn;
public ClientTest() public ClientTest()
{ {
client = new CClient(); client = new CClient();
loggedIn = false;
InitializeComponent(); InitializeComponent();
} }
@@ -39,7 +42,10 @@ namespace ClientTest
private void btLogin_Click(object sender, EventArgs e) private void btLogin_Click(object sender, EventArgs e)
{ {
if (!client.Connected)
return;
client.SendMessage(CCommands.CreateCommandLogin(tbPassword.Text), OnResponse);
} }
private void Connect() private void Connect()
@@ -67,6 +73,38 @@ namespace ClientTest
client.Disconnect(); client.Disconnect();
} }
private void OnResponse(byte[] Message)
{
if (Message == null)
return;
if (Message.Length < 4)
return;
int command = BitConverter.ToInt32(Message, 0);
switch (command)
{
case CCommands.ResponseLoginFailed:
MessageBox.Show("Unknown Error on login", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case CCommands.ResponseLoginWrongPassword:
MessageBox.Show("Wrong Password", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case CCommands.ResponseLoginOK:
loggedIn = true;
this.Invoke((MethodInvoker)delegate
{
lbConnectionStatusText.Text = "Logged in";
});
break;
default:
break;
}
}
private void OnConnectionChanged(bool Connected) private void OnConnectionChanged(bool Connected)
{ {
if (Connected) if (Connected)
@@ -87,6 +125,7 @@ namespace ClientTest
btConnect.Text = "Connect"; btConnect.Text = "Connect";
btLogin.Text = "Login"; btLogin.Text = "Login";
btLogin.Enabled = false; btLogin.Enabled = false;
loggedIn = false;
}); });
} }
} }
Binary file not shown.
+1
View File
@@ -115,6 +115,7 @@ namespace Vocaluxe.Base
// Server // Server
public static EOffOn Server = EOffOn.TR_CONFIG_ON; public static EOffOn Server = EOffOn.TR_CONFIG_ON;
public static int ServerPort = 3000; public static int ServerPort = 3000;
public static string ServerPassword = "vocaluxe";
//Lists to save parameters and values //Lists to save parameters and values
private static readonly List<string> _Params = new List<string>(); private static readonly List<string> _Params = new List<string>();
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Vocaluxe.Base.Server
{
internal class CClientHandler
{
public int ConnectionID;
public bool LoggedIn;
public CClientHandler(int ConnectionID)
{
this.ConnectionID = ConnectionID;
}
}
}
+54 -1
View File
@@ -11,9 +11,11 @@ namespace Vocaluxe.Base.Server
static class CVocaluxeServer static class CVocaluxeServer
{ {
private static CServer _Server; private static CServer _Server;
private static Dictionary<int, CClientHandler> _Clients;
public static void Init() public static void Init()
{ {
_Clients = new Dictionary<int, CClientHandler>();
_Server = new CServer(RequestHandler, CConfig.ServerPort); _Server = new CServer(RequestHandler, CConfig.ServerPort);
} }
@@ -26,11 +28,62 @@ namespace Vocaluxe.Base.Server
public static void Close() public static void Close()
{ {
_Server.Stop(); _Server.Stop();
_Clients = new Dictionary<int, CClientHandler>();
} }
private static byte[] RequestHandler(int ConnectionID, byte[] Message) private static byte[] RequestHandler(int ConnectionID, byte[] Message)
{ {
return null; if (Message == null)
return null;
if (Message.Length < 4)
return null;
lock (_Clients)
{
if (!_Clients.ContainsKey(ConnectionID))
{
CClientHandler client = new CClientHandler(ConnectionID);
_Clients.Add(ConnectionID, client);
}
}
int command = BitConverter.ToInt32(Message, 0);
byte[] answer = null;
switch (command)
{
case CCommands.CommandLogin:
SLoginData data;
if (!CCommands.ResponseCommandLogin(Message, out data))
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseLoginFailed);
else
{
byte[] serverPW = CCommands.SHA256.ComputeHash(Encoding.UTF8.GetBytes(CConfig.ServerPassword));
bool ok = true;
for (int i = 0; i < 32; i++)
{
if (serverPW[i] != data.SHA256[i])
{
ok = false;
break;
}
}
if (!ok)
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseLoginWrongPassword);
else
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseLoginOK);
}
break;
default:
break;
}
return answer;
} }
} }
} }
+1
View File
@@ -200,6 +200,7 @@
<Compile Include="Base\CWebcam.cs" /> <Compile Include="Base\CWebcam.cs" />
<Compile Include="Base\Font\CFonts.cs" /> <Compile Include="Base\Font\CFonts.cs" />
<Compile Include="Base\Font\CGlyph.cs" /> <Compile Include="Base\Font\CGlyph.cs" />
<Compile Include="Base\Server\CClientHandler.cs" />
<Compile Include="Base\Server\CVocaluxeServer.cs" /> <Compile Include="Base\Server\CVocaluxeServer.cs" />
<Compile Include="CSplashScreen.cs"> <Compile Include="CSplashScreen.cs">
<SubType>Form</SubType> <SubType>Form</SubType>