From eff2dda7a45de8c578be242d95aecebda1f51ae8 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Fri, 20 Oct 2023 12:58:23 -0700 Subject: [PATCH] Add PolyProv Dynamic configuration for Polycom (VVX?) phones. --- PolyProv/Controllers/ConfigController.cs | 73 ++++++++++++++++++++++++ PolyProv/Models/ErrorViewModel.cs | 8 +++ PolyProv/PolyProv.csproj | 31 ++++++++++ PolyProv/Program.cs | 35 ++++++++++++ PolyProv/Properties/launchSettings.json | 28 +++++++++ PolyProv/Views/Config/Directory.cshtml | 15 +++++ PolyProv/Views/Config/Init.cshtml | 13 +++++ PolyProv/Views/Config/Phone.cshtml | 17 ++++++ PolyProv/Views/Config/Sip.cshtml | 12 ++++ PolyProv/appsettings.json | 9 +++ ptmx-asp.sln | 15 +++++ 11 files changed, 256 insertions(+) create mode 100644 PolyProv/Controllers/ConfigController.cs create mode 100644 PolyProv/Models/ErrorViewModel.cs create mode 100644 PolyProv/PolyProv.csproj create mode 100644 PolyProv/Program.cs create mode 100644 PolyProv/Properties/launchSettings.json create mode 100644 PolyProv/Views/Config/Directory.cshtml create mode 100644 PolyProv/Views/Config/Init.cshtml create mode 100644 PolyProv/Views/Config/Phone.cshtml create mode 100644 PolyProv/Views/Config/Sip.cshtml create mode 100644 PolyProv/appsettings.json diff --git a/PolyProv/Controllers/ConfigController.cs b/PolyProv/Controllers/ConfigController.cs new file mode 100644 index 0000000..60c2a16 --- /dev/null +++ b/PolyProv/Controllers/ConfigController.cs @@ -0,0 +1,73 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using PhoneToolMX.Data; +using PhoneToolMX.Models; +using System.Net.NetworkInformation; +using System.Text.RegularExpressions; + +namespace PolyProv.Controllers +{ + public class ConfigController : Controller + { + private PTMXContext _ctx; + private static Regex userConf = new Regex("^([0-9a-fA-F]{12})-user$"); + private static Regex initConf = new Regex("^([0-9a-fA-F]{12})$"); + private static Regex sipConf = new Regex("^([0-9a-fA-F]{12})-sip$"); + + /// + /// Gets a by its MAC Address. + /// + /// The MAC address of the phone + /// The with the given MAC, or null if none was found + private Phone? GetByMacStr(string addr) + { + if (!PhysicalAddress.TryParse(addr, out var parsed)) return null; + return _ctx.Phones + .Include(m => m.Extensions) + .Include(m => m.Model) + .FirstOrDefault(p => p.MacAddress.Equals(parsed)); + } + + public ConfigController(PTMXContext context) + { + _ctx = context; + } + + [HttpGet("{param}.cfg")] + public IActionResult GetConfig(string param) + { + return param switch + { + _ when initConf.Match(param) is { Success: true } match => InitSettings(match.Groups[1].Value), + _ when userConf.Match(param) is { Success: true } match => PhoneSettings(match.Groups[1].Value), + _ when sipConf.Match(param) is { Success: true } match => SipSettings(match.Groups[1].Value), + _ => NotFound(), + }; + } + + public IActionResult InitSettings(string addr) + { + var phone = GetByMacStr(addr); + return phone != null ? View("Init", phone) : NotFound(); + } + + public IActionResult PhoneSettings(string addr) + { + var phone = GetByMacStr(addr); + return phone != null ? View("Phone", phone) : NotFound(); + } + + public IActionResult SipSettings(string addr) + { + var phone = GetByMacStr(addr); + return phone != null ? View("Sip", phone) : NotFound(); + } + + [HttpGet("000000000000-directory.xml")] + public IActionResult ExtDirectory() + { + var extensions = _ctx.Extensions.Where(x => x.Listed == true); + return View("Directory", extensions.ToList()); + } + } +} diff --git a/PolyProv/Models/ErrorViewModel.cs b/PolyProv/Models/ErrorViewModel.cs new file mode 100644 index 0000000..bb6d141 --- /dev/null +++ b/PolyProv/Models/ErrorViewModel.cs @@ -0,0 +1,8 @@ +namespace PolyProv.Models; + +public class ErrorViewModel +{ + public string? RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); +} diff --git a/PolyProv/PolyProv.csproj b/PolyProv/PolyProv.csproj new file mode 100644 index 0000000..4b67030 --- /dev/null +++ b/PolyProv/PolyProv.csproj @@ -0,0 +1,31 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + <_ContentIncludedByDefault Remove="Views\Shared\Error.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Shared\_Layout.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Shared\_ValidationScriptsPartial.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Home\Index.cshtml" /> + <_ContentIncludedByDefault Remove="Views\Home\Privacy.cshtml" /> + <_ContentIncludedByDefault Remove="wwwroot\js\site.js" /> + + + + + + + diff --git a/PolyProv/Program.cs b/PolyProv/Program.cs new file mode 100644 index 0000000..c0ea831 --- /dev/null +++ b/PolyProv/Program.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore; +using PhoneToolMX.Data; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddControllersWithViews(); +builder.Services.AddDbContext( +options => options.UseNpgsql(builder.Configuration.GetConnectionString("DbConnection"), + b => b.MigrationsAssembly("PhoneToolMX.Models"))); +builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + +if (!builder.Environment.IsDevelopment()) { + builder.Host.UseSystemd(); +} + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) { + app.UseExceptionHandler("/Home/Error"); +} + +app.UseStaticFiles(); + +app.UseRouting(); + +app.UseAuthorization(); + +app.MapControllerRoute( + name: "default", + pattern: "{action}", + defaults: new { controller = "Config" }); + +app.Run(); diff --git a/PolyProv/Properties/launchSettings.json b/PolyProv/Properties/launchSettings.json new file mode 100644 index 0000000..3b10817 --- /dev/null +++ b/PolyProv/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:44863", + "sslPort": 44352 + } + }, + "profiles": { + "PolyProv": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7041;http://localhost:5082", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/PolyProv/Views/Config/Directory.cshtml b/PolyProv/Views/Config/Directory.cshtml new file mode 100644 index 0000000..6fa0846 --- /dev/null +++ b/PolyProv/Views/Config/Directory.cshtml @@ -0,0 +1,15 @@ +@model IList +@{ + Layout = null; +} + + + + @foreach (var ext in Model) { + + @ext.DirectoryName + @ext.ExtId + + } + + \ No newline at end of file diff --git a/PolyProv/Views/Config/Init.cshtml b/PolyProv/Views/Config/Init.cshtml new file mode 100644 index 0000000..93bee2b --- /dev/null +++ b/PolyProv/Views/Config/Init.cshtml @@ -0,0 +1,13 @@ +@model PhoneToolMX.Models.Phone +@{ + Layout = null; +} + + + \ No newline at end of file diff --git a/PolyProv/Views/Config/Phone.cshtml b/PolyProv/Views/Config/Phone.cshtml new file mode 100644 index 0000000..2908f99 --- /dev/null +++ b/PolyProv/Views/Config/Phone.cshtml @@ -0,0 +1,17 @@ +@model PhoneToolMX.Models.Phone +@{ + Layout = null; +} + + + + @{ var extensions = Model.Extensions.ToList(); } + @for (var i = 0; i < extensions.Count; i++) { + + } + \ No newline at end of file diff --git a/PolyProv/Views/Config/Sip.cshtml b/PolyProv/Views/Config/Sip.cshtml new file mode 100644 index 0000000..534cf40 --- /dev/null +++ b/PolyProv/Views/Config/Sip.cshtml @@ -0,0 +1,12 @@ +@using PhoneToolMX.Models +@model Phone +@{ + Layout = null; +} + + + @for (var i = 0; i < Model.Extensions.Count; i++) { + + } + + diff --git a/PolyProv/appsettings.json b/PolyProv/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/PolyProv/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ptmx-asp.sln b/ptmx-asp.sln index 95b2a66..005ca1a 100644 --- a/ptmx-asp.sln +++ b/ptmx-asp.sln @@ -7,10 +7,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneToolMX", "PhoneToolMX\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneToolMX.Models", "PhoneToolMX.Models\PhoneToolMX.Models.csproj", "{84FB1CE2-A4B6-4251-9427-8AEB175D6874}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolyProv", "PolyProv\PolyProv.csproj", "{18199094-90AE-4C58-BE9E-65D486ECCC38}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -20,9 +23,21 @@ Global {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Debug|Any CPU.Build.0 = Debug|Any CPU {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Release|Any CPU.ActiveCfg = Release|Any CPU {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Release|Any CPU.Build.0 = Release|Any CPU + {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Release|x64.ActiveCfg = Release|x64 + {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Release|x64.Build.0 = Release|x64 + {130DD8E2-5E99-47A9-8B30-2FF54A0DA89D}.Release|x64.Deploy.0 = Release|x64 {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Debug|Any CPU.Build.0 = Debug|Any CPU {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Release|Any CPU.ActiveCfg = Release|Any CPU {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Release|Any CPU.Build.0 = Release|Any CPU + {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Release|x64.ActiveCfg = Release|x64 + {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Release|x64.Build.0 = Release|x64 + {84FB1CE2-A4B6-4251-9427-8AEB175D6874}.Release|x64.Deploy.0 = Release|x64 + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Release|Any CPU.Build.0 = Release|Any CPU + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Release|x64.ActiveCfg = Release|Any CPU + {18199094-90AE-4C58-BE9E-65D486ECCC38}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection EndGlobal