2017-06-05 12:20:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-09-08 21:06:55 +00:00
|
|
|
|
using System.Net;
|
2017-06-05 12:20:39 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-06-16 21:00:01 +00:00
|
|
|
|
using Kneesocks;
|
2017-07-22 19:27:41 +00:00
|
|
|
|
using Glove;
|
2017-09-08 21:06:55 +00:00
|
|
|
|
using SockScape.DAL;
|
2017-08-18 21:01:11 +00:00
|
|
|
|
using SockScape.Encryption;
|
2017-06-05 12:20:39 +00:00
|
|
|
|
|
2017-08-18 21:01:11 +00:00
|
|
|
|
namespace SockScape {
|
2017-06-06 21:13:25 +00:00
|
|
|
|
class MasterConnection : Connection {
|
2017-08-18 21:01:11 +00:00
|
|
|
|
private Key Key;
|
2017-09-07 21:00:03 +00:00
|
|
|
|
public StreamCipher Encryptor { get; private set; }
|
2017-08-18 21:01:11 +00:00
|
|
|
|
|
|
|
|
|
protected override void OnOpen() {
|
|
|
|
|
Key = new Key();
|
|
|
|
|
Send(Key.GenerateRequestPacket().GetBytes());
|
|
|
|
|
}
|
2017-06-05 12:20:39 +00:00
|
|
|
|
|
2017-08-18 21:01:11 +00:00
|
|
|
|
protected override void OnParse() {
|
2017-09-08 21:06:55 +00:00
|
|
|
|
|
2017-08-18 21:01:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnReceive(byte[] data) {
|
|
|
|
|
Packet packet =
|
|
|
|
|
Encryptor == null ? Packet.FromBytes(data)
|
|
|
|
|
: Packet.FromBytes(Encryptor.Parse(data));
|
|
|
|
|
|
|
|
|
|
if(packet == null) {
|
|
|
|
|
Disconnect(Frame.kClosingReason.ProtocolError, "Packet received was not legal.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-08 21:06:55 +00:00
|
|
|
|
if(packet.Id != (int)kInterMasterId.KeyExchange && Encryptor == null) {
|
|
|
|
|
Disconnect(Frame.kClosingReason.ProtocolError, "You must exchange keys before performing any other operations.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 21:01:11 +00:00
|
|
|
|
switch((kInterMasterId)packet.Id) {
|
|
|
|
|
case kInterMasterId.KeyExchange:
|
|
|
|
|
Key.ParseResponsePacket(packet);
|
|
|
|
|
if(!Key.Succeeded) {
|
|
|
|
|
Disconnect(Frame.kClosingReason.ProtocolError, "Could not exchange keys.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 21:00:03 +00:00
|
|
|
|
Encryptor = new StreamCipher(Key.PrivateKey);
|
2017-08-18 21:01:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case kInterMasterId.LoginAttempt:
|
2017-09-08 21:06:55 +00:00
|
|
|
|
if(packet.RegionCount != 2)
|
|
|
|
|
break;
|
2017-08-18 21:01:11 +00:00
|
|
|
|
|
2017-09-08 21:06:55 +00:00
|
|
|
|
using(var db = new ScapeDb()) {
|
|
|
|
|
if(db.Users.)
|
|
|
|
|
}
|
2017-08-18 21:01:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case kInterMasterId.RegistrationAttempt:
|
2017-09-08 21:06:55 +00:00
|
|
|
|
using(var db = new ScapeDb()) {
|
|
|
|
|
|
|
|
|
|
}
|
2017-08-18 21:01:11 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Disconnect(Frame.kClosingReason.ProtocolError, "Packet ID could not be understood at this time.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 20:59:38 +00:00
|
|
|
|
Console.WriteLine($"{Id} says {data.GetString()}");
|
2017-08-18 21:01:11 +00:00
|
|
|
|
}
|
2017-06-05 12:20:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|