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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
12
14{
19 {
20 private static InternetGatewayConfiguration instance = null;
21
22 private HttpResource setRegistration = null;
23 private InternetGatewayRegistrator registrator = null;
24
25 private bool registerInGateway = false;
26
30 public static InternetGatewayConfiguration Instance => instance;
31
35 public override string Resource => "/Settings/InternetGateway.md";
36
40 public override int Priority => 190;
41
45 [DefaultValue(false)]
47 {
48 get => this.registerInGateway;
49 set => this.registerInGateway = value;
50 }
51
57 public override Task<string> Title(Language Language)
58 {
59 return Language.GetStringAsync(typeof(XmppServerModule), 30, "Internet Gateway Registration");
60 }
61
65 public override async Task ConfigureSystem()
66 {
67 if (this.registerInGateway && (this.registrator is null))
68 {
69 List<InternetGatewayRegistration> Ports = new List<InternetGatewayRegistration>();
70
71 foreach (string Protocol in Gateway.GetProtocols())
72 {
73 foreach (int Port in Gateway.GetConfigPorts(Protocol))
74 {
75 if (Port >= 0 && Port <= ushort.MaxValue)
76 {
77 Ports.Add(new InternetGatewayRegistration()
78 {
79 ApplicationName = "TAG Neuron " + Protocol + "-" + Port.ToString(),
80 ExternalPort = (ushort)Port,
81 LocalPort = (ushort)Port,
82 Tcp = true,
83 Udp = false
84 });
85 }
86 }
87 }
88
89 if (Ports.Count > 0)
90 {
91 //ISniffer Sniffer = new XmlFileSniffer(Gateway.AppDataFolder + "UPnP" + Path.DirectorySeparatorChar +
92 // "XMPP Log %YEAR%-%MONTH%-%DAY%T%HOUR%.xml",
93 // Gateway.AppDataFolder + "Transforms" + Path.DirectorySeparatorChar + "SnifferXmlToHtml.xslt",
94 // 7, BinaryPresentationMethod.ByteCount);
95
96 this.registrator = new InternetGatewayRegistrator(Ports.ToArray()); //, Sniffer);
97 await this.registrator.Start();
98 }
99 }
100 }
101
106 public override void SetStaticInstance(ISystemConfiguration Configuration)
107 {
108 instance = Configuration as InternetGatewayConfiguration;
109 }
110
115 public override Task InitSetup(HttpServer WebServer)
116 {
117 this.setRegistration = WebServer.Register("/Settings/SetRegistration", null, this.SetRegistration, true, false, true);
118
119 return base.InitSetup(WebServer);
120 }
121
125 protected override string ConfigPrivilege => "Admin.Communication.InternetGateway";
126
127 private async Task SetRegistration(HttpRequest Request, HttpResponse Response)
128 {
129 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
130
131 if (!Request.HasData)
132 throw new BadRequestException();
133
134 object Obj = await Request.DecodeDataAsync();
135 if (!(Obj is Dictionary<string, object> Parameters))
136 throw new BadRequestException();
137
138 if (!Parameters.TryGetValue("clicked", out Obj) || !(Obj is bool Clicked))
139 throw new BadRequestException();
140
141 this.registerInGateway = Clicked;
142
143 Response.StatusCode = 200;
144
145 await Database.Update(this);
146 }
147
152 public override Task UnregisterSetup(HttpServer WebServer)
153 {
154 WebServer.Unregister(this.setRegistration);
155
156 return base.UnregisterSetup(WebServer);
157 }
158
163 public override Task<bool> SimplifiedConfiguration()
164 {
165 this.registerInGateway = false;
166 return Task.FromResult(true);
167 }
168
172 [Obsolete("Use DisposeAsync() instead.")]
173 public void Dispose()
174 {
175 try
176 {
177 this.DisposeAsync().Wait();
178 }
179 catch (Exception ex)
180 {
181 Log.Exception(ex);
182 }
183 }
184
188 public async Task DisposeAsync()
189 {
190 if (!(this.registrator is null))
191 {
192 await this.registrator.DisposeAsync();
193 this.registrator = null;
194 }
195 }
196
202
207 public override Task<bool> EnvironmentConfiguration()
208 {
209 if(!this.TryGetEnvironmentVariable(BROKER_INET_GATEWAY_REG, false, out this.registerInGateway))
210 return Task.FromResult(false);
211
212 return Task.FromResult(true);
213 }
214
215 }
216}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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:3041
static string[] GetProtocols()
Gets the protocol names defined in the configuration file.
Definition: Gateway.cs:2437
static int[] GetConfigPorts(string Protocol)
Gets the port numbers defined for a given protocol in the configuration file.
Definition: Gateway.cs:2420
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:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
Implements an HTTP server.
Definition: HttpServer.cs:36
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1287
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1438
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:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
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 string Resource
Resource to be redirected to, to perform the 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 system configurations. The gateway will scan all module for system configuration classe...