Skip to content

Commit

Permalink
Example: update externalapi
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Feb 6, 2023
1 parent 5d4502b commit 8c3028b
Showing 1 changed file with 142 additions and 18 deletions.
160 changes: 142 additions & 18 deletions Plugins/example18-externalapi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using Org.BouncyCastle.Tls;
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Org.BouncyCastle.Utilities.Encoders;

//loadassembly: BouncyCastle

namespace MissionPlanner.plugins
Expand All @@ -25,8 +29,8 @@ public override bool Exit()
return true;
}

public string address = Settings.Instance.GetString("ex_api_address");
public int port = Settings.Instance.GetInt32("ex_api_port");
public string address = Settings.Instance.GetString("ex_api_address", "droneshare.cubepilot.org");
public int port = Settings.Instance.GetInt32("ex_api_port", 8042);

public override bool Init()
{
Expand Down Expand Up @@ -59,20 +63,28 @@ private void but_Click(object sender2, EventArgs e)
Settings.Instance["ex_api_username"] = username;
Settings.Instance["ex_api_psk"] = token;

var psk = new BasicTlsPskIdentity(username, token.MakeBytes());
var pskclient = new DTLSPsk(psk);

Task.Run(() =>
{
try
{
var psk = new BasicTlsPskIdentity(username, token.MakeBytes());
var pskclient = new DTLSPsk(psk);


DtlsClientProtocol client = new DtlsClientProtocol();
DatagramTransport transport = new UDPTransport(address, port);
var dtlstx = client.Connect(pskclient, transport);

MainV2.comPort.OnPacketReceived += (sender, message) =>
{
dtlstx.Send(message.buffer, 0, message.buffer.Length);
try
{
dtlstx.Send(message.buffer, 0, message.buffer.Length);
}
catch (Exception ex)
{
}
};

var buf = new byte[dtlstx.GetReceiveLimit()];
Expand All @@ -88,9 +100,13 @@ private void but_Click(object sender2, EventArgs e)
MainV2.comPort.BaseStream.Write(buf, 0, read);
}
}
catch (Exception ex) { }
catch (Exception ex)
{
}
}
} catch (Exception ex) {
}
catch (Exception ex)
{
CustomMessageBox.Show(Strings.ERROR, ex.ToString());
}
});
Expand All @@ -104,29 +120,137 @@ public override bool Loaded()

internal class DTLSPsk : PskTlsClient
{
public DTLSPsk(TlsPskIdentity pskIdentity) : base(new BcTlsCrypto(new Org.BouncyCastle.Security.SecureRandom()), pskIdentity)
public DTLSPsk(TlsPskIdentity pskIdentity) : base(new BcTlsCrypto(new Org.BouncyCastle.Security.SecureRandom()),
pskIdentity)
{
}

public override int[] GetCipherSuites()
{
return new int[]
{
CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CCM
//Cipher Suite: TLS_PSK_WITH_AES_128_GCM_SHA256 (0x00a8)
CipherSuite.TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_DHE_PSK_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_PSK_WITH_AES_128_GCM_SHA256
};
}

//public override ProtocolVersion MinimumVersion { get { return ProtocolVersion.DTLSv10; } }
public override void NotifyAlertRaised(short alertLevel, short alertDescription, string message,
Exception cause)
{
TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;
output.WriteLine("DTLS client raised alert: " + AlertLevel.GetText(alertLevel)
+ ", " + AlertDescription.GetText(alertDescription));
if (message != null)
{
output.WriteLine("> " + message);
}

if (cause != null)
{
output.WriteLine(cause);
}
}

public override void NotifyAlertReceived(short alertLevel, short alertDescription)
{
TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;
output.WriteLine("DTLS client received alert: " + AlertLevel.GetText(alertLevel)
+ ", " + AlertDescription.GetText(alertDescription));
}

public override void NotifyServerVersion(ProtocolVersion serverVersion)
{
base.NotifyServerVersion(serverVersion);

Console.WriteLine("DTLS client negotiated " + serverVersion);
}

public override TlsAuthentication GetAuthentication()
{
return base.GetAuthentication();
}

public override void NotifySecureRenegotiation(bool secureRenegotiation)
{
// this is psk, not needed
//base.NotifySecureRenegotiation(secureRenegotiation);
}

public override void NotifyHandshakeComplete()
{
base.NotifyHandshakeComplete();

ProtocolName protocolName = m_context.SecurityParameters.ApplicationProtocol;
if (protocolName != null)
{
Console.WriteLine("Client ALPN: " + protocolName.GetUtf8Decoding());
}

TlsSession newSession = m_context.Session;
if (newSession != null)
{
if (newSession.IsResumable)
{
byte[] newSessionID = newSession.SessionID;
string hex = ToHexString(newSessionID);
/*
if (base.m_session != null && Arrays.AreEqual(base.m_session.SessionID, newSessionID))
{
Console.WriteLine("Client resumed session: " + hex);
}
else
{
Console.WriteLine("Client established session: " + hex);
}
this.m_session = newSession;
*/

Console.WriteLine("Client established session: " + hex);
}

byte[] tlsServerEndPoint = m_context.ExportChannelBinding(ChannelBinding.tls_server_end_point);
if (null != tlsServerEndPoint)
{
Console.WriteLine("Client 'tls-server-end-point': " + ToHexString(tlsServerEndPoint));
}

byte[] tlsUnique = m_context.ExportChannelBinding(ChannelBinding.tls_unique);
Console.WriteLine("Client 'tls-unique': " + ToHexString(tlsUnique));
}
}

public override IDictionary<int, byte[]> GetClientExtensions()
{
if (m_context.SecurityParameters.ClientRandom == null)
throw new TlsFatalAlert(AlertDescription.internal_error);

return base.GetClientExtensions();
}

//public override ProtocolVersion ClientVersion => ProtocolVersion.DTLSv12;
public override void ProcessServerExtensions(IDictionary<int, byte[]> serverExtensions)
{
if (m_context.SecurityParameters.ServerRandom == null)
throw new TlsFatalAlert(AlertDescription.internal_error);

base.ProcessServerExtensions(serverExtensions);
}

protected virtual string ToHexString(byte[] data)
{
return data == null ? "(null)" : Hex.ToHexString(data);
}

protected override ProtocolVersion[] GetSupportedVersions()
{
return ProtocolVersion.DTLSv12.Only();
}
}

public class UDPTransport : DatagramTransport
Expand All @@ -142,12 +266,12 @@ public UDPTransport(string address, int port)
this.address = address;
this.port = port;

_udpclient = new UdpClient(address, port);
_udpclient = new UdpClient(address, port);
}

public void Close()
{
_udpclient.Close();
_udpclient.Close();
}

public int GetReceiveLimit()
Expand All @@ -163,7 +287,7 @@ public int GetSendLimit()
public int Receive(byte[] buf, int off, int len, int waitMillis)
{
var endtime = DateTime.Now.AddMilliseconds(waitMillis);
while(mRecordQueue.Available < len && endtime > DateTime.Now)
while (mRecordQueue.Available < len && endtime > DateTime.Now)
{
if (_udpclient.Available > 0)
{
Expand All @@ -190,4 +314,4 @@ public void Send(byte[] buf, int off, int len)
_udpclient.Send(span.ToArray(), span.Length);
}
}
}
}

0 comments on commit 8c3028b

Please sign in to comment.