|
@@ -16,15 +16,14 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
private static readonly IPEndPoint AnyEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
|
- private readonly IReadOnlyDictionary<Guid, RSACryptoServiceProvider> _peerKeyRegister;
|
|
|
+ private readonly IReadOnlyDictionary<Guid, RSACng> _peerPublicKeyRegistry;
|
|
|
|
|
|
private IPEndPoint _serverEndPoint;
|
|
|
- private RSACryptoServiceProvider _serverKey;
|
|
|
+ private RSACng _serverPublicKey;
|
|
|
private SocketAsyncEventArgs _saeReceive;
|
|
|
|
|
|
private Guid _localId;
|
|
|
- private RSACryptoServiceProvider _localKey;
|
|
|
-
|
|
|
+ private RSACng _localPrivateKey;
|
|
|
private Socket _localSocket;
|
|
|
private IPEndPoint _localPublicEndPoint;
|
|
|
|
|
@@ -37,15 +36,10 @@ namespace UdPunching.ExampleW
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
|
|
|
- _peerKeyRegister = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PeerPublicKey"))
|
|
|
+ _peerPublicKeyRegistry = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PeerPublicKey"))
|
|
|
.ToDictionary(
|
|
|
s => new Guid(Path.GetFileNameWithoutExtension(s)),
|
|
|
- p =>
|
|
|
- {
|
|
|
- var rsa = new RSACryptoServiceProvider();
|
|
|
- rsa.FromXmlString(File.ReadAllText(p));
|
|
|
- return rsa;
|
|
|
- }
|
|
|
+ TransferCodec.LoadKey
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -53,8 +47,8 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
private void ExampleForm_Shown(object sender, EventArgs e)
|
|
|
{
|
|
|
- _serverKey = new RSACryptoServiceProvider();
|
|
|
- _serverKey.FromXmlString(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerPublicKey.txt")));
|
|
|
+ _serverPublicKey = new RSACng();
|
|
|
+ _serverPublicKey.FromXmlString(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerPublicKey.txt")));
|
|
|
|
|
|
var privateKeys = Directory
|
|
|
.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrivateKey"))
|
|
@@ -63,7 +57,7 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
PeerKetyDropDown.DataSource = privateKeys;
|
|
|
|
|
|
- PeerToKnockDropDown.DataSource = _peerKeyRegister.Keys.ToArray();
|
|
|
+ PeerToKnockDropDown.DataSource = _peerPublicKeyRegistry.Keys.ToArray();
|
|
|
}
|
|
|
|
|
|
private void StartButton_Click(object sender, EventArgs e)
|
|
@@ -73,13 +67,8 @@ namespace UdPunching.ExampleW
|
|
|
_serverEndPoint = ServerIEndPointTextBox.Text.ParseToIpEndPointV4();
|
|
|
|
|
|
_localId = new Guid(PeerKetyDropDown.Text);
|
|
|
- _localKey = new RSACryptoServiceProvider();
|
|
|
var peerPrivateKeyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrivateKey", PeerKetyDropDown.Text + ".txt");
|
|
|
- _localKey.FromXmlString(
|
|
|
- File.ReadAllText(
|
|
|
- peerPrivateKeyPath
|
|
|
- )
|
|
|
- );
|
|
|
+ _localPrivateKey = TransferCodec.LoadKey(peerPrivateKeyPath);
|
|
|
|
|
|
_localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
_localSocket.Bind(AnyEndPoint);
|
|
@@ -108,11 +97,11 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
_localSocket?.Dispose();
|
|
|
_saeReceive?.Dispose();
|
|
|
- _localKey?.Dispose();
|
|
|
+ _localPrivateKey?.Dispose();
|
|
|
|
|
|
_localSocket = null;
|
|
|
_saeReceive = null;
|
|
|
- _localKey = null;
|
|
|
+ _localPrivateKey = null;
|
|
|
|
|
|
PeerKetyDropDown.Enabled = true;
|
|
|
StartButton.Enabled = true;
|
|
@@ -127,7 +116,7 @@ namespace UdPunching.ExampleW
|
|
|
{
|
|
|
_keepAliveMsg.TimeStamp = DateTime.Now;
|
|
|
_keepAliveMsg.WriteToBuffer(_keepAliveBuf);
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, _keepAliveMsg, _serverEndPoint, _serverKey);
|
|
|
+ _localSocket.SendExchangeMessageTo(_serverEndPoint, _localPrivateKey, _serverPublicKey, _localId, _keepAliveMsg);
|
|
|
}
|
|
|
|
|
|
private void KnockButton_Click(object sender, EventArgs e)
|
|
@@ -144,7 +133,7 @@ namespace UdPunching.ExampleW
|
|
|
PeerId = idToKnock
|
|
|
};
|
|
|
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, msg, _serverEndPoint, _serverKey);
|
|
|
+ _localSocket.SendExchangeMessageTo(_serverEndPoint, _localPrivateKey, _serverPublicKey, _localId, msg);
|
|
|
}
|
|
|
|
|
|
private void SendButton_Click(object sender, EventArgs e)
|
|
@@ -152,7 +141,7 @@ namespace UdPunching.ExampleW
|
|
|
var to = SendToEndPointTextBox.Text.ParseToIpEndPointV4();
|
|
|
var sendMsg = new ExchangeMessage(ExchangeMessageId.DataTransfer);
|
|
|
sendMsg.PayloadBytes = Encoding.UTF8.GetBytes(SendContentTextBox.Text);
|
|
|
- var sendBytes = TransferCodec.Encode(_peerKeyRegister[new Guid(PeerToKnockDropDown.Text)], _localId, sendMsg.ToBytes());
|
|
|
+ var sendBytes = TransferCodec.Encode(_localPrivateKey, _peerPublicKeyRegistry[new Guid(PeerToKnockDropDown.Text)], _localId, sendMsg.ToBytes());
|
|
|
var sent = _localSocket.SendTo(sendBytes, to);
|
|
|
}
|
|
|
|
|
@@ -166,7 +155,7 @@ namespace UdPunching.ExampleW
|
|
|
if (BuildInPeerId.Invalid == peerId) throw new InvalidDataException("SERVER ERROR: FAILURE");
|
|
|
if (Guid.Empty != peerId) throw new InvalidDataException("SERVER ERROR: INVALID SERVER PEER ID");
|
|
|
|
|
|
- var msgData = TransferCodec.DecodeData(_localKey, _saeReceive.Buffer);
|
|
|
+ var msgData = TransferCodec.DecodeData(_localPrivateKey, _serverPublicKey, _saeReceive.Buffer);
|
|
|
var msg = new ExchangeMessage(msgData);
|
|
|
|
|
|
switch (msg.Id)
|
|
@@ -192,7 +181,7 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
ExchangeMessage msgReply;
|
|
|
|
|
|
- if (false == _peerKeyRegister.TryGetValue(msg.PeerId.Value, out var peerKey))
|
|
|
+ if (false == _peerPublicKeyRegistry.TryGetValue(msg.PeerId.Value, out var peerKey))
|
|
|
{
|
|
|
Log($"DENIED {msg.Id}: peer id {msg.PeerId.Value}");
|
|
|
msgReply = new ExchangeMessage(ExchangeMessageId.PeerKnockDenied);
|
|
@@ -204,10 +193,10 @@ namespace UdPunching.ExampleW
|
|
|
|
|
|
Log($"SENDING CONNECTION REQ to {msg.PeerId} @ {msg.PeerEndPoint}");
|
|
|
var connMsg = new ExchangeMessage(ExchangeMessageId.PeerKnockConnectionReq);
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, connMsg, msg.PeerEndPoint, peerKey);
|
|
|
+ _localSocket.SendExchangeMessageTo(msg.PeerEndPoint, _localPrivateKey, peerKey, _localId, connMsg);
|
|
|
}
|
|
|
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, msgReply, _serverEndPoint, _serverKey);
|
|
|
+ _localSocket.SendExchangeMessageTo(_serverEndPoint, _localPrivateKey, _serverPublicKey, _localId, msgReply);
|
|
|
break;
|
|
|
|
|
|
case ExchangeMessageId.PeerKnockAckRelay:
|
|
@@ -227,7 +216,7 @@ namespace UdPunching.ExampleW
|
|
|
{
|
|
|
Log($"SENDING CONNECTION REQ to {msg.PeerId} @ {msg.PeerEndPoint}");
|
|
|
var connMsg = new ExchangeMessage(ExchangeMessageId.PeerKnockConnectionReq);
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, connMsg, msg.PeerEndPoint, _peerKeyRegister[msg.PeerId.Value]);
|
|
|
+ _localSocket.SendExchangeMessageTo(msg.PeerEndPoint, _localPrivateKey, _peerPublicKeyRegistry[msg.PeerId.Value], _localId, connMsg);
|
|
|
}
|
|
|
break;
|
|
|
|
|
@@ -253,12 +242,12 @@ namespace UdPunching.ExampleW
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (BuildInPeerId.Invalid == peerId || BuildInPeerId.Server == peerId || false == _peerKeyRegister.TryGetValue(peerId, out var peerKey))
|
|
|
+ if (BuildInPeerId.Invalid == peerId || BuildInPeerId.Server == peerId || false == _peerPublicKeyRegistry.TryGetValue(peerId, out var peerPublicKey))
|
|
|
{
|
|
|
throw new InvalidDataException("PEER ERROR: INVALID PEER ID");
|
|
|
}
|
|
|
|
|
|
- var msgData = TransferCodec.DecodeData(_localKey, _saeReceive.Buffer);
|
|
|
+ var msgData = TransferCodec.DecodeData(_localPrivateKey, peerPublicKey, _saeReceive.Buffer);
|
|
|
var msg = new ExchangeMessage(msgData);
|
|
|
|
|
|
var reply = new ExchangeMessage { TimeStamp = DateTime.Now };
|
|
@@ -294,7 +283,7 @@ namespace UdPunching.ExampleW
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- _localSocket.SendExchangeMessageTo(_localId, reply, _saeReceive.RemoteEndPoint, peerKey);
|
|
|
+ _localSocket.SendExchangeMessageTo(_saeReceive.RemoteEndPoint, _localPrivateKey, peerPublicKey, _localId, reply);
|
|
|
}
|
|
|
}
|
|
|
|