diff --git a/.gitignore b/.gitignore index 569e43a..87343fe 100644 --- a/.gitignore +++ b/.gitignore @@ -235,6 +235,7 @@ FakesAssemblies/ # Node.js Tools for Visual Studio .ntvs_analysis.dat node_modules/ +/*/**/package-lock.json # Typescript v1 declaration files typings/ diff --git a/MQTTnet.sln b/MQTTnet.sln index 309b11f..033fe99 100644 --- a/MQTTnet.sln +++ b/MQTTnet.sln @@ -47,6 +47,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Extensions.ManagedC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore.Tests", "Tests\MQTTnet.AspNetCore.Tests\MQTTnet.AspNetCore.Tests.csproj", "{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnetServer", "Source\MQTTnetServer\MQTTnetServer.csproj", "{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -226,6 +228,22 @@ Global {61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x64.Build.0 = Release|Any CPU {61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x86.ActiveCfg = Release|Any CPU {61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x86.Build.0 = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|ARM.ActiveCfg = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|ARM.Build.0 = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x64.ActiveCfg = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x64.Build.0 = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x86.ActiveCfg = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x86.Build.0 = Debug|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|Any CPU.Build.0 = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|ARM.ActiveCfg = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|ARM.Build.0 = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x64.ActiveCfg = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x64.Build.0 = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x86.ActiveCfg = Release|Any CPU + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -241,6 +259,7 @@ Global {998D04DD-7CB0-45F5-A393-E2495C16399E} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} {C400533A-8EBA-4F0B-BF4D-295C3708604B} = {12816BCC-AF9E-44A9-9AE5-C246AF2A0587} {61B62223-F5D0-48E4-BBD6-2CBA9353CB5E} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} + {5699FB8C-838C-4AB0-80A5-9CA809F9B65B} = {32A630A7-2598-41D7-B625-204CD906F5FB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {07536672-5CBC-4BE3-ACE0-708A431A7894} diff --git a/Source/MQTTnetServer/Controllers/ValuesController.cs b/Source/MQTTnetServer/Controllers/ValuesController.cs new file mode 100644 index 0000000..173fc34 --- /dev/null +++ b/Source/MQTTnetServer/Controllers/ValuesController.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace MQTTnetServer.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + // GET api/values + [HttpGet] + public ActionResult> Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api/values/5 + [HttpGet("{id}")] + public ActionResult Get(int id) + { + return "value"; + } + + // POST api/values + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api/values/5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Source/MQTTnetServer/MQTTnetServer.csproj b/Source/MQTTnetServer/MQTTnetServer.csproj new file mode 100644 index 0000000..2dbf608 --- /dev/null +++ b/Source/MQTTnetServer/MQTTnetServer.csproj @@ -0,0 +1,41 @@ + + + + netcoreapp2.2 + InProcess + MQTTnetServer + MQTTnetServer + False + + + + + + false + false + latest + + + + MQTTnetServer.xml + NU1605 + 1701;1702 + + + + + Always + + + + + + + + + + + + + + diff --git a/Source/MQTTnetServer/MQTTnetServer.xml b/Source/MQTTnetServer/MQTTnetServer.xml new file mode 100644 index 0000000..762bc40 --- /dev/null +++ b/Source/MQTTnetServer/MQTTnetServer.xml @@ -0,0 +1,116 @@ + + + + MQTTnetServer + + + + + Main Entry point + + + + + Main + + + + + + Configure and Start Kestrel + + + + + + + Read Application Settings + + + + + + Listen Entry Settings Model + + + + + Constructor + + + + + Listen Address + + + + + Listen Port + + + + + Protocol Type + + + + + Listen Protocol Types + + + + + HTTP + + + + + HTTPS + + + + + MQTT + + + + + Main Settings Model + + + + + Listen Settings + + + + + Web App Startup + + + + + Constructor + + + + + + Application Settings + + + + + This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + + + + + + + This method gets called by the runtime. Use this method to add services to the container. + + + + + diff --git a/Source/MQTTnetServer/Program.cs b/Source/MQTTnetServer/Program.cs new file mode 100644 index 0000000..cdcb35d --- /dev/null +++ b/Source/MQTTnetServer/Program.cs @@ -0,0 +1,118 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using MQTTnet.AspNetCore; +using MQTTnetServer.Settings; +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; + +namespace MQTTnetServer +{ + /// + /// Main Entry point + /// + public class Program + { + /// + /// Main + /// + /// + public static void Main(string[] args) + { + try + { + CreateWebHostBuilder(args).Build().Run(); + } + catch (FileNotFoundException e) + { + Console.WriteLine("Could not find application settings file in: " + e.FileName); + return; + } + } + + /// + /// Configure and Start Kestrel + /// + /// + /// + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + { + var webHost = WebHost.CreateDefaultBuilder(args); + var listen = ReadListenSettings(); + webHost + .UseKestrel(o => + { + if (listen?.Length > 0) + { + foreach (var item in listen) + { + if (item.Address?.Trim() == "*") + { + if (item.Protocol == ListenProtocolTypes.MQTT) + { + o.ListenAnyIP(item.Port, c => c.UseMqtt()); + } + else + { + o.ListenAnyIP(item.Port); + } + } + else if (item.Address?.Trim() == "localhost") + { + if (item.Protocol == ListenProtocolTypes.MQTT) + { + o.ListenLocalhost(item.Port, c => c.UseMqtt()); + } + else + { + o.ListenLocalhost(item.Port); + } + } + else + { + if (IPAddress.TryParse(item.Address, out var ip)) + { + if (item.Protocol == ListenProtocolTypes.MQTT) + { + o.Listen(ip, item.Port, c => c.UseMqtt()); + } + else + { + o.Listen(ip, item.Port); + } + } + } + } + } + else + { + o.ListenAnyIP(1883, l => l.UseMqtt()); + o.ListenAnyIP(5000); + } + }); + + webHost.UseStartup(); + + return webHost; + } + + /// + /// Read Application Settings + /// + /// + public static ListenModel[] ReadListenSettings() + { + var builder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddEnvironmentVariables() + .Build(); + + var listen = new List(); + builder.Bind("MQTTnetServer:Listen", listen); + + return listen.ToArray(); + } + } +} diff --git a/Source/MQTTnetServer/Settings/ListenModel.cs b/Source/MQTTnetServer/Settings/ListenModel.cs new file mode 100644 index 0000000..5af984f --- /dev/null +++ b/Source/MQTTnetServer/Settings/ListenModel.cs @@ -0,0 +1,30 @@ +namespace MQTTnetServer.Settings +{ + /// + /// Listen Entry Settings Model + /// + public class ListenModel + { + /// + /// Constructor + /// + public ListenModel() + { + } + + /// + /// Listen Address + /// + public string Address { get; set; } + + /// + /// Listen Port + /// + public int Port { get; set; } + + /// + /// Protocol Type + /// + public ListenProtocolTypes Protocol { get; set; } = ListenProtocolTypes.HTTP; + } +} \ No newline at end of file diff --git a/Source/MQTTnetServer/Settings/ListenProtocolTypes.cs b/Source/MQTTnetServer/Settings/ListenProtocolTypes.cs new file mode 100644 index 0000000..5ca4d74 --- /dev/null +++ b/Source/MQTTnetServer/Settings/ListenProtocolTypes.cs @@ -0,0 +1,23 @@ +namespace MQTTnetServer.Settings +{ + /// + /// Listen Protocol Types + /// + public enum ListenProtocolTypes + { + /// + /// HTTP + /// + HTTP = 0, + + /// + /// HTTPS + /// + HTTPS = 1, + + /// + /// MQTT + /// + MQTT = 20 + } +} \ No newline at end of file diff --git a/Source/MQTTnetServer/Settings/SettingsModel.cs b/Source/MQTTnetServer/Settings/SettingsModel.cs new file mode 100644 index 0000000..267ace1 --- /dev/null +++ b/Source/MQTTnetServer/Settings/SettingsModel.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace MQTTnetServer.Settings +{ + /// + /// Main Settings Model + /// + public class SettingsModel + { + /// + /// Listen Settings + /// + public IEnumerable Listen { get; set; } + } +} \ No newline at end of file diff --git a/Source/MQTTnetServer/Startup.cs b/Source/MQTTnetServer/Startup.cs new file mode 100644 index 0000000..4c02756 --- /dev/null +++ b/Source/MQTTnetServer/Startup.cs @@ -0,0 +1,61 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace MQTTnetServer +{ + /// + /// Web App Startup + /// + public class Startup + { + /// + /// Constructor + /// + /// + public Startup(IConfiguration configuration) + { + var builder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddEnvironmentVariables(); + Configuration = builder.Build(); + } + + /// + /// Application Settings + /// + public IConfigurationRoot Configuration { get; } + + /// + /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + /// + /// + /// + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + app.UseMvc(); + } + + /// + /// This method gets called by the runtime. Use this method to add services to the container. + /// + /// + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + } + } +} \ No newline at end of file diff --git a/Source/MQTTnetServer/appsettings.Development.json b/Source/MQTTnetServer/appsettings.Development.json new file mode 100644 index 0000000..e203e94 --- /dev/null +++ b/Source/MQTTnetServer/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/Source/MQTTnetServer/appsettings.json b/Source/MQTTnetServer/appsettings.json new file mode 100644 index 0000000..0bf2d9e --- /dev/null +++ b/Source/MQTTnetServer/appsettings.json @@ -0,0 +1,32 @@ +{ + "MQTTnetServer": { + /* + Wildcard Addresses: + * - All local IP addresses + localhost - Localhost only + + Supported Protocols: + 0 - HTTP + 1 - HTTPS + 20 - MQTT + */ + "Listen": [ + { + "Address": "localhost", + "Port": 5000, + "Protocol": 0 + }, + { + "Address": "*", + "Port": 1883, + "Protocol": 20 + } + ] + }, + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/package.json b/Tests/MQTTnet.TestApp.AspNetCore2/package.json index 56c9018..9363db9 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/package.json +++ b/Tests/MQTTnet.TestApp.AspNetCore2/package.json @@ -2,13 +2,11 @@ "version": "1.0.0", "name": "mqtt.test", "private": true, - "devDependencies": { - }, - + "devDependencies": {}, "dependencies": { "mqtt": "2.15.1", "@types/node": "8.0.46", "systemjs": "0.20.19", "typescript": "2.3.4" } -} \ No newline at end of file +}