5using System.Threading.Tasks;
39 private const string DefaultDataProtectionAgreementTemplate =
"25e2a2b5-4184-5cc8-a824-8b9e562c081b@legal.waher.se";
45 private readonly ManualResetEvent p2pInitiationComplete =
new ManualResetEvent(
false);
48 private bool disposeE2eEncryption =
false;
49 private bool started =
false;
50 private bool presenceSent =
false;
67 if (this.provider is
null)
72 Database.ObjectInserted += Database_ObjectInserted;
73 Database.ObjectUpdated += Database_ObjectUpdated;
74 Database.ObjectDeleted += Database_ObjectDeleted;
75 Database.CollectionCleared += this.Database_CollectionCleared;
76 Database.CollectionRepaired += Database_CollectionRepaired;
78 RosterConfiguration.OnGetGroupSuggestions += this.RosterConfiguration_OnGetGroupSuggestions;
80 Gateway.OnAfterBackup += this.AfterBackup;
82 Gateway.XmppClient.EntityHashFunction = Security.HashFunction.SHA256;
83 Gateway.XmppClient.EntityNode =
"http://waher.se/NL";
86 List<ISniffer> P2PSniffers =
new List<ISniffer>();
88 bool HasSniffers =
false;
104 Gateway.XmppClient.OnStateChanged += this.XmppClient_OnStateChanged;
106 this.p2pInitiationComplete.Reset();
110 this.serverlessMessaging.Network.OnStateChange += this.P2PNetwork_OnStateChange;
111 this.serverlessMessaging.OnNewXmppClient += this.ServerlessMessaging_OnNewXmppClient;
112 this.serverlessMessaging.OnResynch += this.ServerlessMessaging_OnResynch;
117 if (Networking.XMPP.P2P.EndpointSecurity.TryGetEndpointSecurity(
125 this.disposeE2eEncryption =
true;
129 this.e2eEncryption.PeerUnavailable += this.E2eEncryption_PeerUnavailable;
131 Gateway.AvatarClient.E2E = this.e2eEncryption;
136 Gateway.XmppClient.OnReceiveText += Counter.ShortenChunks;
137 Gateway.XmppClient.OnInformation += Counter.RemoveChunkMessage;
138 Gateway.XmppClient.OnDisposed += Counter.Client_OnDisposed;
141 Gateway.XmppClient.OnTransmitText += Counter.ShortenChunks;
142 Gateway.XmppClient.OnDisposed += Counter.Client_OnDisposed;
148 Gateway.HttpxProxy.ServerlessMessaging = this.serverlessMessaging;
151 Gateway.HttpxProxy.DefaultHttpxClient.E2e = this.e2eEncryption;
155 Gateway.HttpxServer.RequiresE2e =
true;
157 Gateway.ContractsClient.PetitionForIdentityReceived += this.ContractsClient_PetitionForIdentityReceived;
162 this.p2pInitiationComplete.WaitOne(60000);
169 if (!this.presenceSent)
170 await this.SetPresence();
172 await this.CheckContracts();
185 bool Accept = !(Id.State == IdentityState.Compromised || Id.State ==
IdentityState.Rejected);
188 List<KeyValuePair<string, object>> Tags =
new List<KeyValuePair<string, object>>()
195 Tags.Add(
new KeyValuePair<string, object>(P.
Name, P.
Value));
203 private Task RosterConfiguration_OnGetGroupSuggestions(
object Sender,
SuggestionEventArgs e)
211 return Task.CompletedTask;
220 this.started =
false;
222 if (!(this.client is
null))
227 if (this.disposeE2eEncryption)
230 this.e2eEncryption =
null;
232 if (!(this.serverlessMessaging is
null))
235 this.serverlessMessaging =
null;
238 Gateway.OnAfterBackup -= this.AfterBackup;
240 RosterConfiguration.OnGetGroupSuggestions -= this.RosterConfiguration_OnGetGroupSuggestions;
242 Database.ObjectInserted -= Database_ObjectInserted;
243 Database.ObjectUpdated -= Database_ObjectUpdated;
244 Database.ObjectDeleted -= Database_ObjectDeleted;
245 Database.CollectionCleared -= this.Database_CollectionCleared;
246 Database.CollectionRepaired -= Database_CollectionRepaired;
248 Gateway.XmppClient.OnStateChanged -= this.XmppClient_OnStateChanged;
250 Gateway.ContractsClient.PetitionForIdentityReceived -= this.ContractsClient_PetitionForIdentityReceived;
253 this.synchronizationResource =
null;
257 private Task CheckContracts()
259 return this.CheckContracts(
null);
262 private async Task CheckContracts(
object P)
280 string Xml = File.ReadAllText(Path.Combine(
Gateway.
AppDataFolder,
"Contracts",
"BlockSubscription.xml"));
286 string TemplateId = await
RuntimeSettings.
GetAsync(
"NL.DPA.TemplateId", DefaultDataProtectionAgreementTemplate);
287 if (
string.IsNullOrEmpty(TemplateId))
299 new StringParameter()
302 Value = Gateway.XmppClient.BareJID
310 Log.
Error(
"Unable to create Data Protection Smart Contract.",
311 new KeyValuePair<string, object>(
"Message", ex.Message));
315 LedgerConfiguration.Instance.DataProtectionAgreementId = Dpa.
ContractId;
319 bool GeneratorSigned =
false;
321 if (!(Dpa?.ClientSignatures is
null))
327 GeneratorSigned =
true;
333 if (!GeneratorSigned)
336 "designated parties before they can access blocks generated by the Neuro-Ledger on this machine.");
340 private static readonly Dictionary<Type, bool> processInLedger =
new Dictionary<Type, bool>();
342 private static async Task<bool> Process(
object Object)
344 Type T = Object.GetType();
347 lock (processInLedger)
349 if (processInLedger.TryGetValue(T, out Result))
361 lock (processInLedger)
363 processInLedger[T] = Result;
369 private static async
void Database_ObjectInserted(
object Sender,
ObjectEventArgs e)
373 if (!await Process(e.
Object))
385 private static async
void Database_ObjectUpdated(
object Sender,
ObjectEventArgs e)
389 if (!await Process(e.
Object))
401 private static async
void Database_ObjectDeleted(
object Sender,
ObjectEventArgs e)
405 if (!await Process(e.
Object))
423 if (Array.IndexOf(
this.provider.Collections, Collection) >= 0)
432 private async Task XmppClient_OnStateChanged(
object Sender,
XmppState NewState)
442 if (!(this.serverlessMessaging is
null))
453 await this.CheckContracts();
469 PostResource = Gateway.HttpxProxy.PostResource
472 bool Sniffers =
false;
474 HttpxServer.RequiresE2e =
true;
478 Client.
SetTag(
"ShowE2E",
false);
481 Client.OnValidateSender += this.PeerClient_OnValidateSender;
482 Client.OnDisposed += this.P2PClient_OnDisposed;
496 e.
RemoteJid + Path.DirectorySeparatorChar +
"XMPP Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml",
497 AppDataFolder +
"Transforms" + Path.DirectorySeparatorChar +
"SnifferXmlToHtml.xslt",
513 ChunkCounter Counter =
new ChunkCounter(
true, Client,
true);
514 Client.OnReceiveText += Counter.ShortenChunks;
515 Client.OnInformation += Counter.RemoveChunkMessage;
516 Client.OnDisposed += Counter.Client_OnDisposed;
518 Counter =
new ChunkCounter(
false, Client,
true);
519 Client.OnTransmitText += Counter.ShortenChunks;
520 Client.OnDisposed += Counter.Client_OnDisposed;
523 return Task.CompletedTask;
526 private Task ServerlessMessaging_OnResynch(
object Sender,
ResynchEventArgs e)
531 return Task.CompletedTask;
534 return Task.CompletedTask;
539 if (!e.
Client.
TryGetTag(
"ExpectedSender", out
object Obj) || !(Obj is
string s) ||
string.Compare(e.
From, s,
true) != 0)
542 return Task.CompletedTask;
545 private async Task P2PClient_OnDisposed(
object Sender, EventArgs e)
551 if (Client.TryGetTag(
"HttpxServer", out
object Obj))
554 await DisposableAsync.DisposeAsync();
555 else if (Obj is IDisposable Disposable)
556 Disposable.Dispose();
559 if (Client.TryGetTag(
"HttpxClient", out Obj))
562 await DisposableAsync.DisposeAsync();
563 else if (Obj is IDisposable Disposable)
564 Disposable.Dispose();
571 if (this.serverlessMessaging is
null)
590 await this.SetPresence();
592 this.p2pInitiationComplete.Set();
596 P2P.DesiredLocalPort = 0;
597 P2P.DesiredExternalPort = 0;
599 this.p2pInitiationComplete.Set();
612 return Task.CompletedTask;
620 get {
return instance?.serverlessMessaging; }
628 get {
return instance?.e2eEncryption; }
631 private async Task SetPresence()
636 this.presenceSent =
true;
640 private async Task AfterBackup(
object Sender, EventArgs e)
645 await this.SetPresence();
Helps with common XML-related tasks.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Static class managing the runtime environment of the IoT Gateway.
static HttpxProxy HttpxProxy
HTTPX Proxy resource
static HttpServer HttpServer
HTTP Server
static string AppDataFolder
Application data folder.
static async Task RequestContractSignature(Contract Contract, string Role, string Purpose)
Requests the operator to sign a smart contract.
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
static DateTime ScheduleEvent(Action< object > Callback, DateTime When, object State)
Schedules a one-time event.
static HttpxServer HttpxServer
HTTPX Server
static XmppClient XmppClient
XMPP Client connection of gateway.
Configures legal identity for the gateway.
static bool TryGetMyIdentity(CaseInsensitiveString LegalId, out LegalIdentity Identity)
Tries to get one of the legal identities belonging to the current instance.
Event arguments for suggestion event handlers.
void AddSuggestion(string Suggestion)
Adds a suggestion.
ISniffer[] Sniffers
Registered sniffers.
virtual void Add(ISniffer Sniffer)
ICommunicationLayer.Add
Base class for all HTTP resources.
Represets a response of an HTTP client request.
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Manages a peer-to-peer network that can receive connections from outside of a NAT-enabled firewall.
IPEndPoint ExternalEndpoint
External IP Endpoint.
ushort DesiredExternalPort
Desired external port number. If 0, a dynamic port number will be assigned.
IPEndPoint LocalEndpoint
Local IP Endpoint.
Outputs sniffed data to an XML file.
Represents a digital signature on a contract.
Contains the definition of a contract
static Task< ParsedContract > Parse(XmlDocument Xml)
Validates a contract XML Document, and returns the contract definition in it.
Parameter[] Parameters
Defined parameters for the smart contract.
Duration? ArchiveRequired
Requied time to archive a signed smart contract, after it becomes obsolete.
ClientSignature[] ClientSignatures
Client signatures of the contract.
HumanReadableText[] ForHumans
Human-readable contents of the contract.
Duration? ArchiveOptional
Optional time to archive a signed smart contract, after it becomes obsolete, and after its required a...
XmlElement ForMachines
Machine-readable contents of the contract.
Role[] Roles
Roles defined in the smart contract.
DateTime? SignAfter
Signatures will only be accepted after this point in time.
ContractParts PartsMode
How parts are defined in the smart contract.
string ContractId
Contract identity
Part[] Parts
Defined parts for the smart contract.
Duration? Duration
Duration of the contract. Is counted from the time it is signed by the required parties.
DateTime? SignBefore
Signatures will only be accepted until this point in time.
ContractVisibility Visibility
Contrat Visibility
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Task PetitionIdentityResponseAsync(string LegalId, string PetitionId, string RequestorFullJid, bool Response)
Sends a response to a petition for information about a legal identity. When a petition is received,...
Task< Contract > GetContractAsync(string ContractId)
Gets a contract
const string E2eKeySemaphoreName
Name of semaphore used to synchronize access to End-to-End Encryption keys used for legal identities ...
Task< Contract > CreateContractAsync(XmlElement ForMachines, HumanReadableText[] ForHumans, Role[] Roles, Part[] Parts, Parameter[] Parameters, ContractVisibility Visibility, ContractParts PartsMode, Duration? Duration, Duration? ArchiveRequired, Duration? ArchiveOptional, DateTime? SignAfter, DateTime? SignBefore, bool CanActAsTemplate)
Creates a new contract.
Event arguments for legal identity petitions
string RequestedIdentityId
Requested identity ID
LegalIdentity RequestorIdentity
Legal Identity of requesting entity.
string RequestorFullJid
Full JID of requestor.
string PetitionId
Petition ID
string Provider
Provider where the identity is maintained.
Property[] Properties
Properties detailing the legal identity.
Abstract base class for contractual parameters
Contains information about a parsed contract.
Contract Contract
Contract object
Class defining a part in a contract
string Name
Name of property
string Value
Property value
Abstract base class of signatures
string Id
ID attribute of message stanza.
Event arguments for presence events.
string From
From where the presence was received.
Event arguments for sender validation events.
string From
From where the stanza was received.
XmppClient Client
XMPP Client.
void Reject()
Called from an event handler to reject the sender.
IEndToEndEncryption E2e
Optional end-to-end encryption interface to use in requests.
XmppServerlessMessaging ServerlessMessaging
Serverless messaging manager.
HttpxClient DefaultHttpxClient
Default HTTPX client.
Task ProcessBlockRequest(HTTP.HeaderFields.HttpFieldAccept Accept, HTTP.HttpResponse Response, BlockReference Ref)
TODO
override void Dispose()
IDisposable.Dispose
Class managing end-to-end encryption.
virtual void RegisterHandlers(XmppClient Client)
Registers XMPP stanza handlers
virtual void UnregisterHandlers(XmppClient Client)
Unregisters XMPP stanza handlers
Task SynchronizeE2e(string FullJID, EventHandlerAsync< IqResultEventArgs > Callback)
Synchronizes End-to-End Encryption and Peer-to-Peer connectivity parameters with a remote entity.
void GenerateNewKey()
Generates new local keys.
virtual void Dispose()
IDisposable.Dispose
Peer connection event arguments.
XmppClient Client
XMPP client, if aquired, or null otherwise.
string RemoteJid
JID of the remote end-point.
Peer connection event arguments.
string RemoteFullJid
JID of the remote end-point.
Class managing peer-to-peer serveless XMPP communication.
PeerToPeerNetwork Network
Peer-to-peer network.
async Task RemovePeerAddresses(string FullJID)
Removes a JID from the recognized set of JIDs.
async Task DisposeAsync()
IDisposable.Dispose
Maintains information about an item in the roster.
string BareJid
Bare JID of the roster item.
PendingSubscription PendingSubscription
If there's a pending unanswered presence subscription or unsubscription request made to the contact.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
XmppState State
Current state of connection.
Task RequestPresenceSubscription(string BareJid)
Requests subscription of presence information from a contact.
void SetTag(string TagName, object Tag)
Sets a tag value.
Task SetPresence()
Sets the presence of the connection. Add a CustomPresenceXml event handler to add custom presence XML...
bool TryGetTag(string TagName, out object Tag)
Tries to get a tag from the client. Tags can be used to attached application specific objects to the ...
RosterItem[] Roster
Items in the roster.
Event arguments for collection events.
string Collection
Collection
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static async Task Update(object Object)
Updates an object in the database.
static Task< GenericObject > Generalize(object Object)
Creates a generalized representation of an object.
Static interface for ledger persistence. In order to work, a ledger provider has to be assigned to it...
static async Task DeletedEntry(object Object)
Deletes an entry in the ledger.
static ILedgerProvider Provider
Registered ledger provider.
static async Task NewEntry(object Object)
Adds an entry to the ledger.
static async Task ClearedCollection(string Collection)
Clears a collection in the ledger.
static async Task UpdatedEntry(object Object)
Updates an entry in the ledger.
string[] Collections
Array of collections archived in the ledger.
Task< ObjectSerializer > GetObjectSerializerEx(object Object)
Gets the object serializer corresponding to a specific object.
Contains a reference to a block in the ledger.
Event arguments for database object events.
Generic object. Contains a sequence of properties.
Serializes a class, taking into account attributes defined in Attributes.
bool ArchiveObjects
If objects of this type can be archived.
Static class that dynamically manages types and interfaces available in the runtime environment.
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Static class of application-wide semaphores that can be used to order access to editable objects.
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
static Task ProcessBlockRequest(Networking.HTTP.HeaderFields.HttpFieldAccept Accept, HttpResponse Response, BlockReference Ref)
TODO
string DataProtectionAgreementId
Data Protection Agreement to use before allowing access to the Neuro-Ledger.
static LedgerConfiguration Instance
Current instance of configuration.
Allows operators to manually start network synchronization.
Interface for asynchronously disposable objects.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Interface for late-bound modules loaded at runtime.
PeerToPeerNetworkState
State of Peer-to-peer network.
BinaryPresentationMethod
How binary data is to be presented.
IdentityState
Lists recognized legal identity states.
ContractParts
How the parts of the contract are defined.
ContractVisibility
Visibility types for contracts.
Availability
Resource availability.
XmppState
State of XMPP connection.
PendingSubscription
Pending subscription states.