Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InternetGatewayConfiguration.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Events;
13
15{
20 {
21 private static InternetGatewayConfiguration instance = null;
22
23 private HttpResource setRegistration = null;
24 private InternetGatewayRegistrator registrator = null;
25
26 private bool registerInGateway = false;
27
31 public static InternetGatewayConfiguration Instance => instance;
32
36 public override string Resource => "/Settings/InternetGateway.md";
37
41 public override int Priority => 190;
42
46 [DefaultValue(false)]
48 {
49 get => this.registerInGateway;
50 set => this.registerInGateway = value;
51 }
52
58 public override Task<string> Title(Language Language)
59 {
60 return Language.GetStringAsync(typeof(XmppServerModule), 30, "Internet Gateway Registration");
61 }
62
66 public override async Task ConfigureSystem()
67 {
68 if (this.registerInGateway && (this.registrator is null))
69 {
70 List<InternetGatewayRegistration> Ports = new List<InternetGatewayRegistration>();
71
72 foreach (string Protocol in Gateway.GetProtocols())
73 {
74 foreach (int Port in Gateway.GetConfigPorts(Protocol))
75 {
76 if (Port >= 0 && Port <= ushort.MaxValue)
77 {
78 Ports.Add(new InternetGatewayRegistration()
79 {
80 ApplicationName = "TAG Neuron " + Protocol + "-" + Port.ToString(),
81 ExternalPort = (ushort)Port,
82 LocalPort = (ushort)Port,
83 Tcp = true,
84 Udp = false
85 });
86 }
87 }
88 }
89
90 if (Ports.Count > 0)
91 {
92 //ISniffer Sniffer = new XmlFileSniffer(Gateway.AppDataFolder + "UPnP" + Path.DirectorySeparatorChar +
93 // "XMPP Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml",
94 // Gateway.AppDataFolder + "Transforms" + Path.DirectorySeparatorChar + "SnifferXmlToHtml.xslt",
95 // 7, BinaryPresentationMethod.ByteCount);
96
97 this.registrator = new InternetGatewayRegistrator(Ports.ToArray()); //, Sniffer);
98 await this.registrator.Start();
99 }
100 }
101 }
102
107 public override void SetStaticInstance(ISystemConfiguration Configuration)
108 {
109 instance = Configuration as InternetGatewayConfiguration;
110 }
111
116 public override Task InitSetup(HttpServer WebServer)
117 {
118 this.setRegistration = WebServer.Register("/Settings/SetRegistration", null, this.SetRegistration, true, false, true);
119
120 return base.InitSetup(WebServer);
121 }
122
126 protected override string ConfigPrivilege => "Admin.Communication.InternetGateway";
127
128 private async Task SetRegistration(HttpRequest Request, HttpResponse Response)
129 {
130 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
131
132 if (!Request.HasData)
133 {
134 await Response.SendResponse(new BadRequestException());
135 return;
136 }
137
138 ContentResponse Content = await Request.DecodeDataAsync();
139 if (Content.HasError || !(Content.Decoded is Dictionary<string, object> Parameters))
140 {
141 await Response.SendResponse(new BadRequestException());
142 return;
143 }
144
145 if (!Parameters.TryGetValue("clicked", out object Obj) || !(Obj is bool Clicked))
146 {
147 await Response.SendResponse(new BadRequestException());
148 return;
149 }
150
151 this.registerInGateway = Clicked;
152
153 Response.StatusCode = 200;
154 Response.StatusMessage = "OK";
155
156 await Database.Update(this);
157 }
158
163 public override Task UnregisterSetup(HttpServer WebServer)
164 {
165 WebServer.Unregister(this.setRegistration);
166
167 return base.UnregisterSetup(WebServer);
168 }
169
174 public override Task<bool> SimplifiedConfiguration()
175 {
176 this.registerInGateway = false;
177 return Task.FromResult(true);
178 }
179
183 [Obsolete("Use DisposeAsync() instead.")]
184 public void Dispose()
185 {
186 try
187 {
188 this.DisposeAsync().Wait();
189 }
190 catch (Exception ex)
191 {
192 Log.Exception(ex);
193 }
194 }
195
199 public async Task DisposeAsync()
200 {
201 if (!(this.registrator is null))
202 {
203 await this.registrator.DisposeAsync();
204 this.registrator = null;
205 }
206 }
207
213
218 public override Task<bool> EnvironmentConfiguration()
219 {
220 if(!this.TryGetEnvironmentVariable(BROKER_INET_GATEWAY_REG, false, out this.registerInGateway))
221 return Task.FromResult(false);
222
223 return Task.FromResult(true);
224 }
225
226 }
227}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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.
Definition: Log.cs:1657
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
static string[] GetProtocols()
Gets the protocol names defined in the configuration file.
Definition: Gateway.cs:3215
static int[] GetConfigPorts(string Protocol)
Gets the port numbers defined for a given protocol in the configuration file.
Definition: Gateway.cs:3198
Abstract base class for system configurations.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Implements an HTTP server.
Definition: HttpServer.cs:41
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1568
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1743
Represents a registraing in an UPnP-compatible Internet Gateway.
Manages registration of TCP and UDP ports in an Internet Gateway
virtual async Task Start()
Starts the registration.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Provides the user with options to register the broker in UPnP-compatible Internet Gateways.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
static InternetGatewayConfiguration Instance
Current instance of configuration.
override Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override async Task ConfigureSystem()
Is called during startup to configure the system.
const string BROKER_INET_GATEWAY_REG
true or 1 if the broker should register itself in any Internet Gateway it finds in the Local Area Net...
override Task< string > Title(Language Language)
Gets a title for the system configuration.
override Task InitSetup(HttpServer WebServer)
Initializes the setup object.
bool RegisterInGateway
If a connection to the support account should be added.
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
Service Module hosting the XMPP broker and its components.
Interface for asynchronously disposable objects.
Interface for system configurations. The gateway will scan all module for system configuration classe...