2023-03-03 23:24:18 +03:30
|
|
|
|
using AutoMapper;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MikrotikAPI.Models;
|
2023-06-02 15:26:29 +03:30
|
|
|
|
using MTWireGuard.Application.Models.Mikrotik;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
using MTWireGuard.Application.Repositories;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
|
2023-06-02 15:26:29 +03:30
|
|
|
|
namespace MTWireGuard.Application.Mapper
|
2023-03-03 23:24:18 +03:30
|
|
|
|
{
|
|
|
|
|
public class ServerMapping : Profile
|
|
|
|
|
{
|
2024-01-25 19:49:04 +03:30
|
|
|
|
private readonly IServiceProvider _provider;
|
|
|
|
|
private IServiceProvider Provider => _provider.CreateScope().ServiceProvider;
|
|
|
|
|
public ServerMapping(IServiceProvider provider)
|
2023-03-03 23:24:18 +03:30
|
|
|
|
{
|
2024-01-25 19:49:04 +03:30
|
|
|
|
_provider = provider;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
/*
|
|
|
|
|
* Convert Mikrotik Server Model to ViewModel
|
|
|
|
|
*/
|
2024-01-25 19:49:04 +03:30
|
|
|
|
CreateMap<WGServer, WGServerViewModel>()
|
2023-03-03 23:24:18 +03:30
|
|
|
|
.ForMember(dest => dest.Id,
|
|
|
|
|
opt => opt.MapFrom(src => Convert.ToInt32(src.Id.Substring(1), 16)))
|
|
|
|
|
.ForMember(dest => dest.IsEnabled,
|
2024-01-25 19:49:04 +03:30
|
|
|
|
opt => opt.MapFrom(src => !src.Disabled))
|
|
|
|
|
.ForMember(dest => dest.IPAddress,
|
|
|
|
|
opt => opt.MapFrom(src => GetServerIP(src).Result))
|
|
|
|
|
.ForMember(dest => dest.InheritDNS,
|
|
|
|
|
opt => opt.MapFrom(src => GetServerInheritDNS(src)))
|
|
|
|
|
.ForMember(dest => dest.DNSAddress,
|
|
|
|
|
opt => opt.MapFrom(src => GetServerDNS(src).Result))
|
|
|
|
|
.ForMember(dest => dest.UseIPPool,
|
|
|
|
|
opt => opt.MapFrom(src => GetServerUseIPPool(src)))
|
|
|
|
|
.ForMember(dest => dest.IPPool,
|
|
|
|
|
opt => opt.MapFrom(src => GetServerIPPool(src).Result));
|
2023-03-03 23:24:18 +03:30
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Convert Wrapper CreateModel to Rest-API CreateModel
|
|
|
|
|
*/
|
2024-01-25 19:49:04 +03:30
|
|
|
|
CreateMap<ServerCreateModel, WGServerCreateModel>()
|
2023-03-03 23:24:18 +03:30
|
|
|
|
.ForMember(dest => dest.Disabled,
|
|
|
|
|
opt => opt.MapFrom(src => !src.Enabled));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Convert Wrapper UpdateModel to Rest-API UpdateModel
|
|
|
|
|
*/
|
2024-01-25 19:49:04 +03:30
|
|
|
|
CreateMap<ServerUpdateModel, WGServerUpdateModel>()
|
2023-03-03 23:24:18 +03:30
|
|
|
|
.ForMember(dest => dest.Id,
|
|
|
|
|
opt => opt.MapFrom(src => $"*{src.Id:X}"));
|
|
|
|
|
}
|
2024-01-25 19:49:04 +03:30
|
|
|
|
|
|
|
|
|
private async Task<string> GetServerIP(WGServer source)
|
|
|
|
|
{
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var ifIP = await api.GetServerIP(source.Name);
|
|
|
|
|
return ifIP.Any() ? ifIP.FirstOrDefault().Address : "0.0.0.0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetServerInheritDNS(WGServer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var dbItem = db.Servers.ToList().Find(s => s.Id == Helper.ParseEntityID(source.Id));
|
|
|
|
|
return dbItem != null && dbItem.InheritDNS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> GetServerDNS(WGServer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var mtDNS = (await api.GetDNS()).Servers;
|
|
|
|
|
var dbItem = db.Servers.ToList().Find(s => s.Id == Helper.ParseEntityID(source.Id));
|
|
|
|
|
return dbItem == null || dbItem.InheritDNS ? mtDNS : dbItem.DNSAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetServerUseIPPool(WGServer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var dbItem = db.Servers.ToList().Find(s => s.Id == Helper.ParseEntityID(source.Id));
|
|
|
|
|
return dbItem != null && dbItem.UseIPPool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> GetServerIPPool(WGServer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var dbItem = db.Servers.ToList().Find(s => s.Id == Helper.ParseEntityID(source.Id));
|
|
|
|
|
if (dbItem == null || dbItem.IPPoolId == null)
|
|
|
|
|
return string.Empty;
|
|
|
|
|
var pools = await api.GetIPPools();
|
|
|
|
|
return pools.Find(p => p.Id == dbItem.IPPoolId).Ranges.FirstOrDefault();
|
|
|
|
|
}
|
2023-03-03 23:24:18 +03:30
|
|
|
|
}
|
|
|
|
|
}
|