sockscape/server/Socks/PendingConnection.cs

55 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
2017-05-12 21:05:18 +00:00
using Square;
using Kneesocks;
2017-05-25 21:08:21 +00:00
using CircleScape.Encryption;
2017-05-25 21:08:21 +00:00
namespace CircleScape {
2017-05-12 21:05:18 +00:00
class PendingConnection : Connection {
private DateTime ConnectionOpened;
2017-05-25 21:08:21 +00:00
private KeyExchange Key;
private Cipher Encryptor;
2017-05-12 21:05:18 +00:00
2017-05-17 21:06:16 +00:00
2017-05-12 21:05:18 +00:00
protected override void OnOpen() {
ConnectionOpened = DateTime.UtcNow;
2017-05-25 21:08:21 +00:00
Key = new KeyExchange();
Send(Key.GenerateRequestPacket().GetBytes());
2017-05-12 21:05:18 +00:00
}
protected override void OnParse() {
if((DateTime.UtcNow - ConnectionOpened).Seconds > 60) {
Disconnect(Frame.kClosingReason.ProtocolError, "Logon request timed out.");
}
}
protected override void OnReceive(byte[] data) {
2017-05-25 21:08:21 +00:00
var packet = Packet.FromBytes(data);
if(!packet.IsLegal) {
Disconnect(Frame.kClosingReason.ProtocolError, "Packet received was not legal.");
return;
}
switch(packet.Id) {
case Packet.kId.KeyExchange:
Key.ParseResponsePacket(packet);
if(!Key.Succeeded) {
Disconnect(Frame.kClosingReason.ProtocolError, "Could not exchange keys.");
return;
}
Encryptor = new Cipher(Key.PrivateKey);
break;
}
2017-05-12 21:05:18 +00:00
Console.WriteLine(Id + " says " + data.GetString());
}
}
}