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>
|
||||
<th colspan="100" class="center">
|
||||
ID 2: Positive ACK<br />
|
||||
Responder
|
||||
[Encrypted] Responder
|
||||
</th>
|
||||
</thead>
|
||||
<thead>
|
||||
|
@ -110,7 +110,7 @@ Communication between the master server and clients will be done over a WebSocke
|
|||
<thead>
|
||||
<th colspan="100" class="center">
|
||||
ID 3: Negative ACK<br />
|
||||
Responder
|
||||
[Encrypted] Responder
|
||||
</th>
|
||||
</thead>
|
||||
<thead>
|
||||
|
@ -130,6 +130,25 @@ Communication between the master server and clients will be done over a WebSocke
|
|||
</tr>
|
||||
</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
|
||||
|
||||
<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>
|
||||
<th colspan="100" class="center">
|
||||
ID 2: Status Update<br />
|
||||
Blind Requester
|
||||
[Encrypted] Blind Requester
|
||||
</th>
|
||||
</thead>
|
||||
<thead>
|
||||
|
|
|
@ -45,10 +45,7 @@ namespace SockScape.Encryption {
|
|||
|
||||
var clientKey = BigInteger.ModPow(generator, Secret, modulus);
|
||||
PrivateKey = BigInteger.ModPow(serverKey, Secret, modulus);
|
||||
return new Packet(
|
||||
1,
|
||||
clientKey.ToHexString()
|
||||
);
|
||||
return new Packet(1, clientKey.ToHexString());
|
||||
}
|
||||
|
||||
public BigInteger ParseResponsePacket(Packet packet) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
@ -11,12 +12,19 @@ using SockScape.Encryption;
|
|||
namespace SockScape {
|
||||
static class MasterUdpClient {
|
||||
private static Key Key;
|
||||
public static Cipher Encryptor { get; private set; }
|
||||
private static Cipher Encryptor;
|
||||
|
||||
private static UdpClient Sock;
|
||||
private static Thread ListeningThread;
|
||||
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() {
|
||||
if(IsOpen || ListeningThread != null)
|
||||
|
@ -26,6 +34,7 @@ namespace SockScape {
|
|||
Sock = new UdpClient(Configuration.General["Master Addr"], port);
|
||||
|
||||
Key = new Key();
|
||||
Encryptor = null;
|
||||
|
||||
IsOpen = true;
|
||||
ListeningThread = new Thread(Listener);
|
||||
|
@ -34,17 +43,39 @@ namespace SockScape {
|
|||
|
||||
public static void Listener() {
|
||||
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) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Send(byte[] message) {
|
||||
public static void Send(Packet packet) {
|
||||
var message = packet.GetBytes();
|
||||
Sock.Send(message, message.Length);
|
||||
LastMessage = DateTime.Now;
|
||||
LastMessageOut = DateTime.Now;
|
||||
}
|
||||
|
||||
public static void Close() {
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace SockScape {
|
|||
|
||||
public static void Listener() {
|
||||
while(IsOpen) {
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
IPEndPoint endPoint = new IPEndPoint(0, 0);
|
||||
while(Sock.Available > 0) {
|
||||
var data = Sock.Receive(ref endPoint);
|
||||
var client = endPoint.ToString();
|
||||
|
@ -51,11 +51,10 @@ namespace SockScape {
|
|||
|
||||
if(packet == null)
|
||||
break;
|
||||
|
||||
byte[] sendBuffer;
|
||||
switch((kIntraMasterId)packet.Id) {
|
||||
case kIntraMasterId.InitiationAttempt:
|
||||
if(packet.RegionCount != 1)
|
||||
|
||||
switch((kIntraSlaveId)packet.Id) {
|
||||
case kIntraSlaveId.InitiationAttempt:
|
||||
if(packet.RegionCount != 1 || IsProspectConnected(client))
|
||||
break;
|
||||
|
||||
if(packet[0] == Configuration.General["Master Secret"]) {
|
||||
|
@ -66,12 +65,11 @@ namespace SockScape {
|
|||
Key = key
|
||||
};
|
||||
|
||||
sendBuffer = key.GenerateRequestPacket().GetBytes();
|
||||
Sock.Send(sendBuffer, sendBuffer.Length, endPoint);
|
||||
Send(key.GenerateRequestPacket(), endPoint);
|
||||
}
|
||||
break;
|
||||
|
||||
case kIntraMasterId.KeyExchange:
|
||||
case kIntraSlaveId.KeyExchange:
|
||||
if(!IsProspectConnected(client))
|
||||
break;
|
||||
|
||||
|
@ -84,11 +82,11 @@ namespace SockScape {
|
|||
Prospects.Remove(client);
|
||||
break;
|
||||
|
||||
case kIntraMasterId.StatusUpdate:
|
||||
case kIntraSlaveId.StatusUpdate:
|
||||
if(!IsClientConnected(client) || packet.RegionCount < 1)
|
||||
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() {
|
||||
IsOpen = false;
|
||||
ListeningThread.Join();
|
||||
|
@ -115,11 +118,15 @@ namespace SockScape {
|
|||
}
|
||||
|
||||
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) {
|
||||
return new Packet((int)kIntraMasterAckId.NegativeAck, new { id, message });
|
||||
private static Packet NegativeAck(byte id, string 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 {
|
||||
|
|
|
@ -5,14 +5,16 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace SockScape {
|
||||
public enum kIntraMasterId {
|
||||
public enum kIntraSlaveId {
|
||||
InitiationAttempt = 0,
|
||||
KeyExchange,
|
||||
StatusUpdate
|
||||
}
|
||||
|
||||
public enum kIntraMasterAckId {
|
||||
PositiveAck = 1,
|
||||
NegativeAck
|
||||
public enum kIntraMasterId {
|
||||
KeyExchange = 1,
|
||||
PositiveAck,
|
||||
NegativeAck,
|
||||
EncryptionError
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,15 @@ namespace SockScape {
|
|||
|
||||
protected Packet() { }
|
||||
|
||||
public Packet(Enum id, params object[] regions) {
|
||||
Initialize((int)Convert.ChangeType(id, id.GetTypeCode()), regions);
|
||||
}
|
||||
|
||||
public Packet(int id, params object[] regions) {
|
||||
Initialize(id, regions);
|
||||
}
|
||||
|
||||
private void Initialize(int id, object[] regions) {
|
||||
Id = id;
|
||||
|
||||
foreach(var region in regions)
|
||||
|
|
Loading…
Reference in a new issue