using System; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using Application.Models; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; namespace APService.Controllers { public class HomeController : Controller { private readonly IHttpClientFactory _httpClientFactory; private readonly ApService _apService; public HomeController(IHttpClientFactory httpClientFactory, ApService apService) { this._httpClientFactory = httpClientFactory; this._apService = apService; } public IActionResult Index(string command) { var result = string.IsNullOrEmpty(command) ? "" : this._apService.RunCommand(command); ViewBag.Result = result; var model = this._apService.GetModel(); this._apService.Update(model); return View(model); } [HttpGet] [Route("/ap/api")] [ApiExplorerSettings(IgnoreApi = true)] public string Api() { var url = new UriBuilder { Host = Request.Host.Host, Port = Request.Host.Port ?? 80, Path = "/swagger/v1/swagger.json" }.ToString(); var hc = this._httpClientFactory.CreateClient(); var result = hc.GetStringAsync(url).Result; return result; } [HttpGet] [Route("/ap/21dhcps")] [SwaggerOperation("在线连接")] public ApiResponse Dhcps([SwaggerParameter("设备Id")]string id) { var result = this._apService.RunCommand("get dhcps"); var list = Regex.Matches(result, @"(?<=\s)[\d\.]{7,15}(?=\s)").Select(o => o.Value).Skip(2).ToList(); return ApiResponse.Success(list.ToJson()); } [HttpGet] [Route("/ap/23setnet")] [SwaggerOperation("设置网络")] public ApiResponse SetNet( [SwaggerParameter("IP")]string ip, [SwaggerParameter("子网掩码")]string netmask, [SwaggerParameter("网关")]string gateway, [SwaggerParameter("DNS")]string dns) { this._apService.RunCommand($"set ipaddr wan {ip} {netmask} {gateway}"); this._apService.RunCommand($"set dns {dns}"); this._apService.Update(this._apService.GetModel()); return ApiResponse.AsyncSuccess(); } [HttpGet] [Route("/ap/22getnet")] [SwaggerOperation("查看网络")] public ApiResponse GetNet() { var (ip, netmask, gateway, dns) = this._apService.GetNet(); return ApiResponse.Success(new { ip, netmask, gateway, dns }.ToJson()); } } }