Encode/decode password.

Added Length options to protocol.
This commit is contained in:
2015-10-13 22:03:08 +02:00
parent a348917810
commit 7b806ac847
3 changed files with 93 additions and 28 deletions
+12
View File
@@ -16,5 +16,17 @@ namespace ErgometerLibrary
string timestr = time.AddMilliseconds(millis) + ""; string timestr = time.AddMilliseconds(millis) + "";
return timestr; return timestr;
} }
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
} }
} }
+79 -26
View File
@@ -8,10 +8,11 @@ namespace ErgometerLibrary
{ {
public class NetCommand public class NetCommand
{ {
public enum CommandType { LOGIN, DATA, CHAT, LOGOUT, SESSION, VALUESET, USER, RESPONSE, REQUEST } public enum CommandType { LOGIN, DATA, CHAT, LOGOUT, SESSION, VALUESET, USER, RESPONSE, REQUEST, LENGTH, SESSIONDATA }
public enum RequestType { USERS, ALLSESSIONS, CURRENTSESSIONS, OLDDATA, SESSIONDATA } public enum RequestType { USERS, ALLSESSIONS, CURRENTSESSIONS, OLDDATA, SESSIONDATA }
public enum ResponseType { LOGINOK, LOGINWRONG, ERROR, NOTLOGGEDIN } public enum ResponseType { LOGINOK, LOGINWRONG, ERROR, NOTLOGGEDIN }
public enum ValueType { TIME, POWER, ENERGY, DISTANCE } public enum ValueType { TIME, POWER, ENERGY, DISTANCE }
public enum LengthType { USERS, SESSIONS, SESSIONDATA, DATA, CURRENTSESSIONS}
public double Timestamp { get; set; } public double Timestamp { get; set; }
public int Session { get; set; } public int Session { get; set; }
@@ -19,13 +20,14 @@ namespace ErgometerLibrary
public ResponseType Response { get; set; } public ResponseType Response { get; set; }
public ValueType Value { get; set; } public ValueType Value { get; set; }
public RequestType Request { get; set; } public RequestType Request { get; set; }
public LengthType Length { get; set; }
public double SetValue { get; set; } public double SetValue { get; set; }
public string DisplayName { get; set; } public string DisplayName { get; set; }
public bool IsDoctor { get; set; } public bool IsDoctor { get; set; }
public string Password { get; set; } public string Password { get; set; }
public string ChatMessage { get; set; } public string ChatMessage { get; set; }
public Meting Meting { get; set; } public Meting Meting { get; set; }
public Dictionary<string, string> users; public int LengthValue { get; set; }
//SESSION //SESSION
public NetCommand(int session) public NetCommand(int session)
@@ -35,6 +37,15 @@ namespace ErgometerLibrary
Timestamp = Helper.Now; Timestamp = Helper.Now;
} }
//SESSIONDATA
public NetCommand(string name, bool foo, int session)
{
Type = CommandType.SESSIONDATA;
Session = session;
Timestamp = Helper.Now;
DisplayName = name;
}
//RESPONSE //RESPONSE
public NetCommand(ResponseType response, int session) public NetCommand(ResponseType response, int session)
{ {
@@ -44,6 +55,16 @@ namespace ErgometerLibrary
Response = response; Response = response;
} }
//Length
public NetCommand(LengthType lengthtype, int length, int session)
{
Type = CommandType.LENGTH;
Length = lengthtype;
Session = session;
Timestamp = Helper.Now;
LengthValue = length;
}
//REQUEST //REQUEST
public NetCommand(RequestType request, int session) public NetCommand(RequestType request, int session)
{ {
@@ -90,12 +111,12 @@ namespace ErgometerLibrary
SetValue = val; SetValue = val;
} }
//USERS //USER
public NetCommand(Dictionary<string, string> users, int session) public NetCommand(string username, string password, int session)
{ {
Type = CommandType.USER; Type = CommandType.USER;
Session = session; Session = session;
this.users = users; DisplayName = username;
} }
//LOGIN //LOGIN
@@ -146,14 +167,50 @@ namespace ErgometerLibrary
case 7: case 7:
return ParseValue(session, args); return ParseValue(session, args);
case 8: case 8:
return ParseUsers(session, args); return ParseUser(session, args);
case 9: case 9:
return ParseRequest(session, args); return ParseRequest(session, args);
case 10:
return ParseLength(session, args);
case 11:
return ParseSessionData(session, args);
default: default:
throw new FormatException("Error in NetCommand: " + comType + " is not a valid command type."); throw new FormatException("Error in NetCommand: " + comType + " is not a valid command type.");
} }
} }
private static NetCommand ParseSessionData(int session, string[] args)
{
if (args.Length != 1)
throw new MissingFieldException("Error in NetCommand: Session Data is missing arguments");
NetCommand temp = new NetCommand(args[0], false, session);
return temp;
}
private static NetCommand ParseLength(int session, string[] args)
{
if (args.Length != 2)
throw new MissingFieldException("Error in NetCommand: Length is missing arguments");
switch (args[0])
{
case "users":
return new NetCommand(LengthType.USERS, int.Parse(args[1]), session);
case "sessiondata":
return new NetCommand(LengthType.SESSIONDATA, int.Parse(args[1]), session);
case "sessions":
return new NetCommand(LengthType.SESSIONS, int.Parse(args[1]), session);
case "data":
return new NetCommand(LengthType.DATA, int.Parse(args[1]), session);
case "currentsessions":
return new NetCommand(LengthType.CURRENTSESSIONS, int.Parse(args[1]), session);
default:
throw new FormatException("Error in NetCommand: Length type not recognised");
}
}
private static NetCommand ParseRequest(int session, string[] args) private static NetCommand ParseRequest(int session, string[] args)
{ {
if (args.Length != 1) if (args.Length != 1)
@@ -176,22 +233,14 @@ namespace ErgometerLibrary
} }
} }
private static NetCommand ParseUsers(int session, string[] args) private static NetCommand ParseUser(int session, string[] args)
{ {
if (args.Length < 2) if (args.Length != 2)
throw new MissingFieldException("Error in NetCommand: Users is missing arguments"); throw new MissingFieldException("Error in NetCommand: User is missing arguments");
Dictionary<string, string> users = new Dictionary<string, string>(); NetCommand temp = new NetCommand(args[0], Helper.Base64Decode(args[1]), session);
foreach(string str in args) return temp;
{
string[] temp = str.Split('|');
string name = temp[0];
string password = temp[1];
users.Add(name, password);
}
return new NetCommand(users, session);
} }
private static NetCommand ParseValue(int session, string[] args) private static NetCommand ParseValue(int session, string[] args)
@@ -227,6 +276,8 @@ namespace ErgometerLibrary
return new NetCommand(ResponseType.LOGINWRONG, session); return new NetCommand(ResponseType.LOGINWRONG, session);
case "notloggedin": case "notloggedin":
return new NetCommand(ResponseType.NOTLOGGEDIN, session); return new NetCommand(ResponseType.NOTLOGGEDIN, session);
case "error":
return new NetCommand(ResponseType.ERROR, session);
default: default:
throw new FormatException("Error in NetCommand: Response type not recognised"); throw new FormatException("Error in NetCommand: Response type not recognised");
} }
@@ -281,7 +332,7 @@ namespace ErgometerLibrary
NetCommand temp = new NetCommand(CommandType.LOGIN, session); NetCommand temp = new NetCommand(CommandType.LOGIN, session);
temp.IsDoctor = doctor; temp.IsDoctor = doctor;
temp.DisplayName = args[0]; temp.DisplayName = args[0];
temp.Password = args[1]; temp.Password = Helper.Base64Decode(args[1]);
return temp; return temp;
} }
@@ -293,7 +344,7 @@ namespace ErgometerLibrary
switch (Type) switch (Type)
{ {
case CommandType.LOGIN: case CommandType.LOGIN:
command += "1»ses" + Session + "»" + DisplayName + "»" + Password + "»" + IsDoctor; command += "1»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password) + "»" + IsDoctor;
break; break;
case CommandType.DATA: case CommandType.DATA:
command += "2»ses" + Session + "»" + Meting.ToCommand(); command += "2»ses" + Session + "»" + Meting.ToCommand();
@@ -314,15 +365,17 @@ namespace ErgometerLibrary
command += "7»ses" + Session + "»" + Value.ToString().ToLower() + "»" + SetValue; command += "7»ses" + Session + "»" + Value.ToString().ToLower() + "»" + SetValue;
break; break;
case CommandType.USER: case CommandType.USER:
command += "8»ses" + Session; command += "8»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password);
foreach(KeyValuePair<string, string> keys in users)
{
command += "»" + keys.Key + "|" + keys.Value;
}
break; break;
case CommandType.REQUEST: case CommandType.REQUEST:
command += "9»ses" + Session + "»" + Request.ToString().ToLower(); command += "9»ses" + Session + "»" + Request.ToString().ToLower();
break; break;
case CommandType.LENGTH:
command += "10»ses" + Session + "»" + Length.ToString().ToLower() + "»" + LengthValue;
break;
case CommandType.SESSIONDATA:
command += "11»ses" + Session + "»" + DisplayName;
break;
default: default:
throw new FormatException("Error in NetCommand: Cannot find type of command"); throw new FormatException("Error in NetCommand: Cannot find type of command");
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.8")] [assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.2.8")] [assembly: AssemblyFileVersion("1.0.3.0")]