sockscape/server/Socks/MasterIntraClient.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2017-06-16 21:00:01 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
2017-06-16 21:00:01 +00:00
using System.Text;
2017-08-29 20:14:44 +00:00
using System.Threading;
2017-06-16 21:00:01 +00:00
using System.Threading.Tasks;
2017-08-30 21:02:51 +00:00
using Glove;
using SockScape.Encryption;
2017-06-16 21:00:01 +00:00
2017-08-29 20:14:44 +00:00
namespace SockScape {
static class MasterIntraClient {
2017-08-30 21:02:51 +00:00
private static Key Key;
private static UdpClient Sock;
2017-08-29 20:14:44 +00:00
private static Thread ListeningThread;
private static bool IsOpen;
2017-09-06 20:59:38 +00:00
private static DateTime LastMessageOut = new DateTime(0);
private static TimeSpan DeltaLastOut
=> DateTime.Now - LastMessageOut;
private static DateTime LastMessageIn = new DateTime(0);
private static TimeSpan DeltaLastIn
=> DateTime.Now - LastMessageIn;
2017-06-16 21:00:01 +00:00
public static void Initialize() {
2017-08-30 21:02:51 +00:00
if(IsOpen || ListeningThread != null)
return;
ushort port = (ushort)Configuration.General["Master Port"];
2017-08-29 20:14:44 +00:00
Sock = new UdpClient(Configuration.General["Master Addr"], port);
2017-08-30 21:02:51 +00:00
Key = new Key();
Encryptor = null;
2017-08-30 21:02:51 +00:00
IsOpen = true;
2017-08-30 21:02:51 +00:00
ListeningThread = new Thread(Listener);
ListeningThread.Start();
}
2017-08-29 20:14:44 +00:00
public static void Listener() {
2017-08-30 21:02:51 +00:00
while(IsOpen) {
var endPoint = new IPEndPoint(0, 0);
2017-08-30 21:02:51 +00:00
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 StreamCipher(Key.PrivateKey);
if(responsePacket != null)
Send(responsePacket);
else
LastMessageIn = new DateTime(0);
break;
2017-09-05 21:02:14 +00:00
case kIntraMasterId.PositiveAck:
2017-09-06 20:59:38 +00:00
Console.WriteLine($"Packet type {packet[0]} accepted by master");
2017-09-05 21:02:14 +00:00
break;
case kIntraMasterId.NegativeAck:
2017-09-06 20:59:38 +00:00
Console.WriteLine($"Packet type {packet[0]} declined by master for reason {packet[1]}");
2017-09-05 21:02:14 +00:00
break;
case kIntraMasterId.EncryptionError:
NextSendId = NextRecvId = 0;
Buffer.Clear();
2017-09-05 21:02:14 +00:00
Key = new Key();
Encryptor = null;
LastMessageIn = new DateTime(0);
break;
}
2017-08-30 21:02:51 +00:00
}
if(LastMessageIn.Ticks != 0) {
2017-09-07 04:26:58 +00:00
if(DeltaLastOut.TotalSeconds > 2)
Send(Encryptor.Parse(ServerContext.StatusUpdatePacket.GetBytes()));
2017-09-07 04:26:58 +00:00
} else
if(DeltaLastOut.TotalSeconds > 10)
Send(new Packet(kIntraSlaveId.InitiationAttempt, Configuration.General["Master Secret"]));
2017-09-06 20:59:38 +00:00
2017-08-30 21:02:51 +00:00
Thread.Sleep(1);
}
2017-08-29 20:14:44 +00:00
}
public static void Send(Packet packet) {
Send(packet.GetBytes());
}
public static void Send(byte[] bytes) {
Sock.Send(bytes, bytes.Length);
LastMessageOut = DateTime.Now;
Buffer.Add(NextSendId, bytes);
++NextSendId;
}
2017-08-29 20:14:44 +00:00
public static void Close() {
IsOpen = false;
ListeningThread.Join();
ListeningThread = null;
}
2017-06-16 21:00:01 +00:00
}
}