2023-03-03 23:24:18 +03:30
|
|
|
|
using AutoMapper;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2023-06-02 15:26:29 +03:30
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MikrotikAPI.Models;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
using MTWireGuard.Application.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 PeerMapping : Profile
|
|
|
|
|
{
|
2023-06-02 15:26:29 +03:30
|
|
|
|
private readonly IServiceProvider _provider;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
//private readonly IMemoryCache _cache;
|
2023-06-02 15:26:29 +03:30
|
|
|
|
private IServiceProvider Provider => _provider.CreateScope().ServiceProvider;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
|
2024-01-25 19:49:04 +03:30
|
|
|
|
public PeerMapping(IServiceProvider provider/*, IMemoryCache memoryCache*/)
|
2023-06-02 15:26:29 +03:30
|
|
|
|
{
|
|
|
|
|
_provider = provider;
|
2024-01-25 19:49:04 +03:30
|
|
|
|
//_cache = memoryCache;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
/*
|
|
|
|
|
* Mikrotik Peer to ViewModel
|
|
|
|
|
*/
|
|
|
|
|
CreateMap<WGPeer, WGPeerViewModel>()
|
|
|
|
|
.ForMember(dest => dest.Id,
|
|
|
|
|
opt => opt.MapFrom(src => Convert.ToInt32(src.Id.Substring(1), 16)))
|
|
|
|
|
.ForMember(dest => dest.Name,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerName(src)))
|
|
|
|
|
.ForMember(dest => dest.PrivateKey,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerPrivateKey(src)))
|
|
|
|
|
.ForMember(dest => dest.Address,
|
|
|
|
|
opt => opt.MapFrom(src => src.AllowedAddress))
|
|
|
|
|
.ForMember(dest => dest.CurrentAddress,
|
|
|
|
|
opt => opt.MapFrom(src => $"{src.CurrentEndpointAddress}:{src.CurrentEndpointPort}"))
|
|
|
|
|
.ForMember(dest => dest.IsEnabled,
|
|
|
|
|
opt => opt.MapFrom(src => !src.Disabled))
|
|
|
|
|
.ForMember(dest => dest.IsDifferent,
|
|
|
|
|
opt => opt.MapFrom(src => HasDifferences(src)))
|
|
|
|
|
.ForMember(dest => dest.Upload,
|
|
|
|
|
opt => opt.MapFrom(src => Helper.ConvertByteSize(Convert.ToInt64(src.TX), 2)))
|
|
|
|
|
.ForMember(dest => dest.Download,
|
|
|
|
|
opt => opt.MapFrom(src => Helper.ConvertByteSize(Convert.ToInt64(src.RX), 2)))
|
|
|
|
|
.ForMember(dest => dest.UploadBytes,
|
|
|
|
|
opt => opt.MapFrom(src => Convert.ToInt64(src.TX)))
|
|
|
|
|
.ForMember(dest => dest.DownloadBytes,
|
2023-06-23 17:00:49 +03:30
|
|
|
|
opt => opt.MapFrom(src => Convert.ToInt64(src.RX)))
|
|
|
|
|
.ForMember(dest => dest.Expire,
|
2024-01-25 19:49:04 +03:30
|
|
|
|
opt => opt.MapFrom(src => ExpireDateToString(src)))
|
|
|
|
|
.ForMember(dest => dest.LastHandshake,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerLastHandshake(src)))
|
|
|
|
|
.ForMember(dest => dest.DNSAddress,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerDNS(src)))
|
|
|
|
|
.ForMember(dest => dest.InheritDNS,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerInheritDNS(src)))
|
|
|
|
|
.ForMember(dest => dest.InheritIP,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerInheritIP(src)))
|
|
|
|
|
.ForMember(dest => dest.Traffic,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerTraffic(src)))
|
|
|
|
|
.ForMember(dest => dest.TrafficUsed,
|
|
|
|
|
opt => opt.MapFrom(src => GetPeerTrafficUsage(src)));
|
2023-03-03 23:24:18 +03:30
|
|
|
|
|
|
|
|
|
// WGPeer
|
|
|
|
|
CreateMap<UserCreateModel, WGPeerCreateModel>();
|
|
|
|
|
CreateMap<WGPeerCreateModel, WGPeerDBModel>();
|
|
|
|
|
CreateMap<UserUpdateModel, WGPeerUpdateModel>()
|
|
|
|
|
.ForMember(dest => dest.Id,
|
|
|
|
|
opt => opt.MapFrom(src => $"*{src.Id:X}"));
|
|
|
|
|
|
|
|
|
|
// DBUser
|
2023-06-23 17:00:49 +03:30
|
|
|
|
CreateMap<WGPeerViewModel, WGPeerDBModel>()
|
|
|
|
|
.ForMember(dest => dest.Expire,
|
|
|
|
|
opt => opt.MapFrom(src => ExpireStringToDate(src.Expire)));
|
2023-03-03 23:24:18 +03:30
|
|
|
|
CreateMap<UserSyncModel, WGPeerDBModel>();
|
|
|
|
|
CreateMap<UserSyncModel, WGPeerUpdateModel>()
|
|
|
|
|
.ForMember(dest => dest.Id,
|
|
|
|
|
opt => opt.MapFrom(src => $"*{src.Id:X}"));
|
|
|
|
|
CreateMap<UserUpdateModel, WGPeerDBModel>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 19:49:04 +03:30
|
|
|
|
private string GetPeerName(WGPeer source)
|
2023-03-03 23:24:18 +03:30
|
|
|
|
{
|
2023-06-02 15:26:29 +03:30
|
|
|
|
var db = Provider.GetService<DBContext>();
|
2024-01-25 19:49:04 +03:30
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) ? db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).Name ?? string.Empty : string.Empty;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string? GetPeerPrivateKey(WGPeer source)
|
|
|
|
|
{
|
2023-06-02 15:26:29 +03:30
|
|
|
|
var db = Provider.GetService<DBContext>();
|
2024-01-25 19:49:04 +03:30
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) ? db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).PrivateKey ?? string.Empty : string.Empty;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 17:00:49 +03:30
|
|
|
|
private DateTime GetPeerExpire(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) ? db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).Expire ?? new() : new();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 19:49:04 +03:30
|
|
|
|
private string GetPeerLastHandshake(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var api = Provider.GetService<IMikrotikRepository>();
|
|
|
|
|
var lastHandshake = api.GetUserHandshake(source.Id).Result;
|
|
|
|
|
return lastHandshake;
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return "Unknown";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string? GetPeerDNS(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) ? db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).DNSAddress ?? string.Empty : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetPeerInheritDNS(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) && db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).InheritDNS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetPeerInheritIP(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) && db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).InheritIP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetPeerTrafficUsage(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
//var dbItem = db.DataUsages.ToList().Find(u => u.UserID == Convert.ToInt32(source.Id[1..], 16));
|
|
|
|
|
//if (source.AllowedAddress == "172.16.33.29/32")
|
|
|
|
|
// Console.WriteLine();
|
|
|
|
|
//return (dbItem != null) ? dbItem.RX + dbItem.TX : 0;
|
|
|
|
|
var dbItem = db.Users.ToList().Find(u => u.Id == Helper.ParseEntityID(source.Id));
|
|
|
|
|
return dbItem != null ? dbItem.RX + dbItem.TX : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetPeerTraffic(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var db = Provider.GetService<DBContext>();
|
|
|
|
|
return (db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)) != null) ? db.Users.ToList().Find(u => u.Id == Convert.ToInt32(source.Id[1..], 16)).TrafficLimit : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 17:00:49 +03:30
|
|
|
|
private string ExpireDateToString(WGPeer source)
|
|
|
|
|
{
|
|
|
|
|
var expireDate = GetPeerExpire(source);
|
|
|
|
|
return expireDate != new DateTime() ? expireDate.ToString() : "Unlimited";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DateTime ExpireStringToDate(string expire)
|
|
|
|
|
{
|
|
|
|
|
return expire == "Unlimited" ? new() : Convert.ToDateTime(expire);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 23:24:18 +03:30
|
|
|
|
private bool HasDifferences(WGPeer source)
|
|
|
|
|
{
|
2023-06-02 15:26:29 +03:30
|
|
|
|
var db = Provider.GetService<DBContext>();
|
2023-03-03 23:24:18 +03:30
|
|
|
|
var id = Convert.ToInt32(source.Id[1..], 16);
|
|
|
|
|
var dbUser = db.Users.ToList().Find(x => x.Id == id);
|
2024-01-25 19:49:04 +03:30
|
|
|
|
var dbTraffic = db.LastKnownTraffic.ToList().Find(x => x.UserID == id);
|
|
|
|
|
if (dbUser is null || dbTraffic is null) return true;
|
2023-03-03 23:24:18 +03:30
|
|
|
|
if (dbUser.PublicKey != source.PublicKey) return true;
|
|
|
|
|
return string.IsNullOrWhiteSpace(dbUser.PrivateKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|