i get to go home early today so not much
womm
This commit is contained in:
parent
8230796a1b
commit
81609f8391
6 changed files with 94 additions and 30 deletions
25
protocol.md
25
protocol.md
|
@ -91,7 +91,7 @@ Communication between the master server and clients will be done over a WebSocke
|
||||||
<thead>
|
<thead>
|
||||||
<th colspan="100" class="center">
|
<th colspan="100" class="center">
|
||||||
ID 2: Positive ACK<br />
|
ID 2: Positive ACK<br />
|
||||||
Responder
|
[Encrypted] Responder
|
||||||
</th>
|
</th>
|
||||||
</thead>
|
</thead>
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -110,7 +110,7 @@ Communication between the master server and clients will be done over a WebSocke
|
||||||
<thead>
|
<thead>
|
||||||
<th colspan="100" class="center">
|
<th colspan="100" class="center">
|
||||||
ID 3: Negative ACK<br />
|
ID 3: Negative ACK<br />
|
||||||
Responder
|
[Encrypted] Responder
|
||||||
</th>
|
</th>
|
||||||
</thead>
|
</thead>
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -130,6 +130,25 @@ Communication between the master server and clients will be done over a WebSocke
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
<table style="margin-right: 8px; margin-bottom: 8px;">
|
||||||
|
<thead>
|
||||||
|
<th colspan="100" class="center">
|
||||||
|
ID 4: Encryption Error<br />
|
||||||
|
Responder
|
||||||
|
</th>
|
||||||
|
</thead>
|
||||||
|
<thead>
|
||||||
|
<th>#</th>
|
||||||
|
<th>Region</th>
|
||||||
|
<th>Type</th>
|
||||||
|
</thead>
|
||||||
|
<tr>
|
||||||
|
<td>1</td>
|
||||||
|
<td>Error Message</td>
|
||||||
|
<td>String</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
#### Slave to Master
|
#### Slave to Master
|
||||||
|
|
||||||
<table style="margin-right: 8px; margin-bottom: 8px;">
|
<table style="margin-right: 8px; margin-bottom: 8px;">
|
||||||
|
@ -174,7 +193,7 @@ Communication between the master server and clients will be done over a WebSocke
|
||||||
<thead>
|
<thead>
|
||||||
<th colspan="100" class="center">
|
<th colspan="100" class="center">
|
||||||
ID 2: Status Update<br />
|
ID 2: Status Update<br />
|
||||||
Blind Requester
|
[Encrypted] Blind Requester
|
||||||
</th>
|
</th>
|
||||||
</thead>
|
</thead>
|
||||||
<thead>
|
<thead>
|
||||||
|
|
|
@ -45,10 +45,7 @@ namespace SockScape.Encryption {
|
||||||
|
|
||||||
var clientKey = BigInteger.ModPow(generator, Secret, modulus);
|
var clientKey = BigInteger.ModPow(generator, Secret, modulus);
|
||||||
PrivateKey = BigInteger.ModPow(serverKey, Secret, modulus);
|
PrivateKey = BigInteger.ModPow(serverKey, Secret, modulus);
|
||||||
return new Packet(
|
return new Packet(1, clientKey.ToHexString());
|
||||||
1,
|
|
||||||
clientKey.ToHexString()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BigInteger ParseResponsePacket(Packet packet) {
|
public BigInteger ParseResponsePacket(Packet packet) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
@ -11,12 +12,19 @@ using SockScape.Encryption;
|
||||||
namespace SockScape {
|
namespace SockScape {
|
||||||
static class MasterUdpClient {
|
static class MasterUdpClient {
|
||||||
private static Key Key;
|
private static Key Key;
|
||||||
public static Cipher Encryptor { get; private set; }
|
private static Cipher Encryptor;
|
||||||
|
|
||||||
private static UdpClient Sock;
|
private static UdpClient Sock;
|
||||||
private static Thread ListeningThread;
|
private static Thread ListeningThread;
|
||||||
private static bool IsOpen;
|
private static bool IsOpen;
|
||||||
private static DateTime LastMessage;
|
|
||||||
|
private static DateTime LastMessageOut;
|
||||||
|
private static TimeSpan DeltaLastOut
|
||||||
|
=> DateTime.Now - LastMessageOut;
|
||||||
|
|
||||||
|
private static DateTime LastMessageIn = new DateTime(0);
|
||||||
|
private static TimeSpan DeltaLastIn
|
||||||
|
=> DateTime.Now - LastMessageIn;
|
||||||
|
|
||||||
public static void Initialize() {
|
public static void Initialize() {
|
||||||
if(IsOpen || ListeningThread != null)
|
if(IsOpen || ListeningThread != null)
|
||||||
|
@ -26,6 +34,7 @@ namespace SockScape {
|
||||||
Sock = new UdpClient(Configuration.General["Master Addr"], port);
|
Sock = new UdpClient(Configuration.General["Master Addr"], port);
|
||||||
|
|
||||||
Key = new Key();
|
Key = new Key();
|
||||||
|
Encryptor = null;
|
||||||
|
|
||||||
IsOpen = true;
|
IsOpen = true;
|
||||||
ListeningThread = new Thread(Listener);
|
ListeningThread = new Thread(Listener);
|
||||||
|
@ -34,17 +43,39 @@ namespace SockScape {
|
||||||
|
|
||||||
public static void Listener() {
|
public static void Listener() {
|
||||||
while(IsOpen) {
|
while(IsOpen) {
|
||||||
|
if(LastMessageIn.Ticks == 0 && DeltaLastOut.TotalSeconds > 10)
|
||||||
|
Send(new Packet(kIntraSlaveId.InitiationAttempt, Configuration.General["Master Secret"]));
|
||||||
|
|
||||||
|
var endPoint = new IPEndPoint(0, 0);
|
||||||
while(Sock.Available > 0) {
|
while(Sock.Available > 0) {
|
||||||
|
var data = Sock.Receive(ref endPoint);
|
||||||
|
LastMessageIn = DateTime.Now;
|
||||||
|
|
||||||
|
Packet packet =
|
||||||
|
Encryptor == null ? Packet.FromBytes(data)
|
||||||
|
: Packet.FromBytes(Encryptor.Parse(data));
|
||||||
|
|
||||||
|
switch((kIntraMasterId)packet.Id) {
|
||||||
|
case kIntraMasterId.KeyExchange:
|
||||||
|
var responsePacket = Key.ParseRequestPacket(packet);
|
||||||
|
Encryptor = new Cipher(Key.PrivateKey);
|
||||||
|
if(responsePacket != null)
|
||||||
|
Send(responsePacket);
|
||||||
|
|
||||||
|
else
|
||||||
|
LastMessageIn = new DateTime(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread.Sleep(1);
|
Thread.Sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Send(byte[] message) {
|
public static void Send(Packet packet) {
|
||||||
|
var message = packet.GetBytes();
|
||||||
Sock.Send(message, message.Length);
|
Sock.Send(message, message.Length);
|
||||||
LastMessage = DateTime.Now;
|
LastMessageOut = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Close() {
|
public static void Close() {
|
||||||
|
|
|
@ -37,7 +37,7 @@ namespace SockScape {
|
||||||
|
|
||||||
public static void Listener() {
|
public static void Listener() {
|
||||||
while(IsOpen) {
|
while(IsOpen) {
|
||||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
IPEndPoint endPoint = new IPEndPoint(0, 0);
|
||||||
while(Sock.Available > 0) {
|
while(Sock.Available > 0) {
|
||||||
var data = Sock.Receive(ref endPoint);
|
var data = Sock.Receive(ref endPoint);
|
||||||
var client = endPoint.ToString();
|
var client = endPoint.ToString();
|
||||||
|
@ -51,11 +51,10 @@ namespace SockScape {
|
||||||
|
|
||||||
if(packet == null)
|
if(packet == null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
byte[] sendBuffer;
|
switch((kIntraSlaveId)packet.Id) {
|
||||||
switch((kIntraMasterId)packet.Id) {
|
case kIntraSlaveId.InitiationAttempt:
|
||||||
case kIntraMasterId.InitiationAttempt:
|
if(packet.RegionCount != 1 || IsProspectConnected(client))
|
||||||
if(packet.RegionCount != 1)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if(packet[0] == Configuration.General["Master Secret"]) {
|
if(packet[0] == Configuration.General["Master Secret"]) {
|
||||||
|
@ -66,12 +65,11 @@ namespace SockScape {
|
||||||
Key = key
|
Key = key
|
||||||
};
|
};
|
||||||
|
|
||||||
sendBuffer = key.GenerateRequestPacket().GetBytes();
|
Send(key.GenerateRequestPacket(), endPoint);
|
||||||
Sock.Send(sendBuffer, sendBuffer.Length, endPoint);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kIntraMasterId.KeyExchange:
|
case kIntraSlaveId.KeyExchange:
|
||||||
if(!IsProspectConnected(client))
|
if(!IsProspectConnected(client))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -84,11 +82,11 @@ namespace SockScape {
|
||||||
Prospects.Remove(client);
|
Prospects.Remove(client);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kIntraMasterId.StatusUpdate:
|
case kIntraSlaveId.StatusUpdate:
|
||||||
if(!IsClientConnected(client) || packet.RegionCount < 1)
|
if(!IsClientConnected(client) || packet.RegionCount < 1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,6 +97,11 @@ namespace SockScape {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void Send(Packet packet, IPEndPoint client) {
|
||||||
|
var message = packet.GetBytes();
|
||||||
|
Sock.Send(message, message.Length, client);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Close() {
|
public static void Close() {
|
||||||
IsOpen = false;
|
IsOpen = false;
|
||||||
ListeningThread.Join();
|
ListeningThread.Join();
|
||||||
|
@ -115,11 +118,15 @@ namespace SockScape {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Packet PositiveAck(byte id) {
|
private static Packet PositiveAck(byte id) {
|
||||||
return new Packet((int)kIntraMasterAckId.PositiveAck, new { id });
|
return new Packet(kIntraMasterId.PositiveAck, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Packet NegativeAck(byte id, string message) {
|
private static Packet NegativeAck(byte id, string message = "") {
|
||||||
return new Packet((int)kIntraMasterAckId.NegativeAck, new { id, message });
|
return new Packet(kIntraMasterId.NegativeAck, id, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Packet EncryptionError(string message = "A general encryption error has occurred.") {
|
||||||
|
return new Packet(kIntraMasterId.EncryptionError, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
|
|
|
@ -5,14 +5,16 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SockScape {
|
namespace SockScape {
|
||||||
public enum kIntraMasterId {
|
public enum kIntraSlaveId {
|
||||||
InitiationAttempt = 0,
|
InitiationAttempt = 0,
|
||||||
KeyExchange,
|
KeyExchange,
|
||||||
StatusUpdate
|
StatusUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum kIntraMasterAckId {
|
public enum kIntraMasterId {
|
||||||
PositiveAck = 1,
|
KeyExchange = 1,
|
||||||
NegativeAck
|
PositiveAck,
|
||||||
|
NegativeAck,
|
||||||
|
EncryptionError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,15 @@ namespace SockScape {
|
||||||
|
|
||||||
protected Packet() { }
|
protected Packet() { }
|
||||||
|
|
||||||
|
public Packet(Enum id, params object[] regions) {
|
||||||
|
Initialize((int)Convert.ChangeType(id, id.GetTypeCode()), regions);
|
||||||
|
}
|
||||||
|
|
||||||
public Packet(int id, params object[] regions) {
|
public Packet(int id, params object[] regions) {
|
||||||
|
Initialize(id, regions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Initialize(int id, object[] regions) {
|
||||||
Id = id;
|
Id = id;
|
||||||
|
|
||||||
foreach(var region in regions)
|
foreach(var region in regions)
|
||||||
|
|
Loading…
Reference in a new issue