Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
Merge pull request #4 from hekkaaa/dev
  • Loading branch information
hekkaaa authored Aug 8, 2022
2 parents 3412959 + 5d4a5ca commit 2e98f6c
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 61 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# WhoisObserver
The program aggregates a number of whois servers. Creates the necessary request and interprets the response into a common object.

## Servers used:
- **[ip-api](https://ip-api.com/)**
- **[ru-center](https://www.nic.ru/whois/?searchWord=)**

## Example
### Async version
```csharp
using Whois.NET;
using WhoisObserver;
using WhoisObserver.Services.Model;
...

async void Test1()
Expand All @@ -20,9 +25,34 @@ Test1();
![image](https://user-images.githubusercontent.com/46771781/183240324-a46d1e0a-1fbc-4422-906c-02360a754186.png)


```csharp
using WhoisObserver;
using WhoisObserver.Services.Model;
...

async void Test2()
{
var st0 = new WhoisClient();
var st1 = st0.GetResponceModelAsync("13.228.116.142", ServersClientFamily.IpApi).Result;
Console.WriteLine(st1.CountryCode);
}

Test2());
```

![image](https://user-images.githubusercontent.com/46771781/183470488-c0448b79-6e18-48d9-9506-98cf927d0901.png)


## Download
**[Nuget reference](#)**
**[Download it now](#)**


## Dependencies
- .NETStandard 2.0
- AutoMapper (>= 10.1.1)
- Newtonsoft.Json (>= 13.0.1)
- System.Net.Http.Json (>= 6.0.0)
- System.Text.Json (>= 6.0.5)

## Feedback and bug reports

Expand Down
10 changes: 3 additions & 7 deletions WhoisObserver/Services/Context.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Threading.Tasks;
using WhoisObserver.Services.Mapper;
using WhoisObserver.Services.Model;
using WhoisObserver.Services.WhoisServersClients.Interfaces;

Expand All @@ -29,9 +25,9 @@ public void SetStrategy(IWhoisClient strategy)
public async Task<string> GetResponseJsonAsync(string host)
{
var result = await this._strategy.ResponceJson(host);

if (!String.IsNullOrWhiteSpace(result))
{
{
return result;
}
else
Expand Down
68 changes: 63 additions & 5 deletions WhoisObserver/Services/Helpers/RuCenterResponseParserHtml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using WhoisObserver.Services.Model;
using WhoisObserver.Services.Model.ClientModel;
using WhoisObserver.Services.Model.InputModel;

namespace WhoisObserver.Services.Helpers
{
Expand All @@ -15,7 +17,6 @@ public static Dictionary<string, string> ParseHtmlResponseContentWithIpInfo(stri
char[] delimiterChars = { '\n', '\r' };
string[] splitHtmlContent = htmlContent.Split(delimiterChars, System.StringSplitOptions.RemoveEmptyEntries);

//List<string> tmpCollection = new List<string>();
Dictionary<string, string> resultDict = new Dictionary<string, string>();

foreach (string valuesContents in splitHtmlContent)
Expand Down Expand Up @@ -53,20 +54,77 @@ public static Dictionary<string, string> ParseHtmlResponseContentWithIpInfo(stri
return null;
}

public static Dictionary<string, string> ParseHtmlResponseContentWithNoFree(List<RuCenterListFormatterInputModel> formatterContent)
{
if (formatterContent != null)
{
Dictionary<string, string> resultDict = new Dictionary<string, string>();

public static string ConvertDictInJsonResponceString(Dictionary<string, string> convertDict, IMapper mapper)
foreach (var valuesContents in formatterContent)
{
if (valuesContents.name is null) continue;

else
{
try
{
// clearing the key of unnecessary characters '-'.
string[] tmpClearedKeyArrayFromChar = valuesContents.name.Split('-');
valuesContents.name = string.Empty;

// rebuild key after Split.
foreach (var key in tmpClearedKeyArrayFromChar)
{
valuesContents.name = string.Concat(valuesContents.name, key.ToLower());
}

resultDict.Add(valuesContents.name, valuesContents.value.ToString());
}
catch (Exception)
{
resultDict[valuesContents.name] += ", " + valuesContents.value.ToString();
}
}
}

return resultDict;
}

return null;
}


public static string ConvertDictInJsonHostnameResponceString(Dictionary<string, string> convertDict, IMapper mapper)
{
string json = JsonConvert.SerializeObject(convertDict);
RuCenterHostnameModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterHostnameModel>(json);
WhoisResponseModel WhoisModel = mapper.Map<WhoisResponseModel>(objConvertNativeModel);

return JsonConvert.SerializeObject(WhoisModel);
}

public static string ConvertDictInJsonIpResponceString(Dictionary<string, string> convertDict, IMapper mapper)
{
string json = JsonConvert.SerializeObject(convertDict);
RuCenterModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterModel>(json);
RuCenterIpAddressModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterIpAddressModel>(json);
WhoisResponseModel WhoisModel = mapper.Map<WhoisResponseModel>(objConvertNativeModel);

return JsonConvert.SerializeObject(WhoisModel);
}

public static WhoisResponseModel ConvertDictInWhoisModel(Dictionary<string, string> convertDict, IMapper mapper)
public static WhoisResponseModel ConvertDictInWhoisIpModel(Dictionary<string, string> convertDict, IMapper mapper)
{
string json = JsonConvert.SerializeObject(convertDict);
RuCenterIpAddressModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterIpAddressModel>(json);
WhoisResponseModel resultWhoisModel = mapper.Map<WhoisResponseModel>(objConvertNativeModel);

return resultWhoisModel;
}

public static WhoisResponseModel ConvertDictInWhoisHosnameModel(Dictionary<string, string> convertDict, IMapper mapper)
{
string json = JsonConvert.SerializeObject(convertDict);
RuCenterModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterModel>(json);
RuCenterHostnameModel objConvertNativeModel = System.Text.Json.JsonSerializer.Deserialize<RuCenterHostnameModel>(json);
WhoisResponseModel resultWhoisModel = mapper.Map<WhoisResponseModel>(objConvertNativeModel);

return resultWhoisModel;
Expand Down
11 changes: 8 additions & 3 deletions WhoisObserver/Services/Mapper/ModelMapping .cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ public ModelMapping()
{
CreateMap<WhoisResponseModel, IpApiModel>().ReverseMap();
//.ForMember(w => w.City, x => x.MapFrom(ip => ip.region));
CreateMap<RuCenterModel, WhoisResponseModel>()
CreateMap<RuCenterIpAddressModel, WhoisResponseModel>()
.ConvertUsing<PersonConverterJsonRuCenter>();
CreateMap<RuCenterHostnameModel, WhoisResponseModel>()
.ForMember(w => w.Status, x => x.MapFrom(ip => ip.state))
.ForMember(w => w.Orgabusename, x => x.MapFrom(ip => ip.registrar))
.ForMember(w => w.Inetnum, x => x.MapFrom(ip => ip.nserver))
.ForMember(w => w.Orgabusephone, x => x.MapFrom(ip => ip.admincontact));
}
}

public class PersonConverterJsonRuCenter : ITypeConverter<RuCenterModel, WhoisResponseModel>
public class PersonConverterJsonRuCenter : ITypeConverter<RuCenterIpAddressModel, WhoisResponseModel>
{
public WhoisResponseModel Convert(RuCenterModel source, WhoisResponseModel destination, ResolutionContext context)
public WhoisResponseModel Convert(RuCenterIpAddressModel source, WhoisResponseModel destination, ResolutionContext context)
{

if(source.created is null)
Expand Down
16 changes: 16 additions & 0 deletions WhoisObserver/Services/Model/ClientModel/RuCenterHostnameModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace WhoisObserver.Services.Model.ClientModel
{
public class RuCenterHostnameModel
{
public string domain { get; set; }
public string nserver { get; set; }
public string state { get; set; }
public string org { get; set; }
public string registrar { get; set; }
public string admincontact { get; set; }
public string created { get; set; }
public string paidtill { get; set; }
public string freedate { get; set; }
public string source { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace WhoisObserver.Services.Model.ClientModel
{
public class RuCenterModel
public class RuCenterIpAddressModel
{
public string adminc { get; set; }
public string address { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace WhoisObserver.Services.Model.InputModel
{
public class RuCenterListFormatterInputModel
{
public string name { get; set; }
public string value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace WhoisObserver.Services.Model.InputModel
using System.Collections.Generic;

namespace WhoisObserver.Services.Model.InputModel
{
public class RuCenterListInputModel
{
public string registry { get; set; }
public string html { get; set; }
public List<RuCenterListFormatterInputModel> formatted { get; set; }
}
}
4 changes: 3 additions & 1 deletion WhoisObserver/Services/Model/ServersClientFamily.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace WhoisObserver.Services.Model
{
/// <summary>
/// List of available servers available for operation
/// </summary>
public enum ServersClientFamily
{
IpApi = 1,
RuCenter,
TestClient,
}
}
6 changes: 4 additions & 2 deletions WhoisObserver/Services/Model/WhoisResponseModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace WhoisObserver.Services.Model
{
public class WhoisResponseModel
{
{
public string Address { get; set; } = null;
public string As { get; set; } = null;
public string Adminc { get; set; } = null;
Expand Down Expand Up @@ -29,6 +29,8 @@ public class WhoisResponseModel
public string Orgabusephone { get; set; } = null;
public string Orgid { get; set; } = null;
public string Orgtechhandle { get; set; } = null;

public string source { get; set; } = null;
public string paidtill { get; set; } = null;
public string freedate { get; set; } = null;
}
}
52 changes: 29 additions & 23 deletions WhoisObserver/Services/WhoisServersClients/RuCenterClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using WhoisObserver.Services.Helpers;
using WhoisObserver.Services.Model;
Expand All @@ -19,8 +17,9 @@ public class RuCenterClient : IWhoisClient
private const string request = "https://www.nic.ru/app/v1/get/whois";
private IMapper _mapper;

private string htmlContent = null;
private string statusRequest = null;
private string _htmlContent = null;
private string _statusRequest = null;
private List<RuCenterListFormatterInputModel> _formattedContent;

public RuCenterClient(IMapper mapper)
{
Expand All @@ -33,32 +32,32 @@ public async Task<string> ResponceJson(string host)
await CreateRequest(host);

// hostname type ip-address - '8.8.8.8'
if (statusRequest == "ip_info")
if (_statusRequest == "ip_info")
{

Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithIpInfo(htmlContent);
Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithIpInfo(_htmlContent);

if (result != null)
{
return RuCenterResponseParserHtml.ConvertDictInJsonResponceString(result, _mapper);
return RuCenterResponseParserHtml.ConvertDictInJsonIpResponceString(result, _mapper);
}
return null;
}

// hostname type - 'google.com'
if (statusRequest == "not_free")
if (_statusRequest == "not_free")
{
//Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithIpInfo(htmlContent);
Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithNoFree(_formattedContent);

//if (result != null)
//{
// return RuCenterResponseParserHtml.ConvertDictInJsonResponceString(result, _mapper);
//}
//return null;
if (result != null)
{
return RuCenterResponseParserHtml.ConvertDictInJsonHostnameResponceString(result, _mapper);
}
return null;
}

// not valid hostname\ip-address - '169.254.169.253:2323'
if (statusRequest == "fail") return null;
if (_statusRequest == "fail") return null;

return null;
}
Expand All @@ -69,26 +68,32 @@ public async Task<WhoisResponseModel> ResponceObject(string host)
await CreateRequest(host);

// hostname type ip-address - '8.8.8.8'
if (statusRequest == "ip_info")
if (_statusRequest == "ip_info")
{

Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithIpInfo(htmlContent);
Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithIpInfo(_htmlContent);

if (result != null)
{
return RuCenterResponseParserHtml.ConvertDictInWhoisModel(result, _mapper);
return RuCenterResponseParserHtml.ConvertDictInWhoisIpModel(result, _mapper);
}
return null;
}

// hostname type - 'google.com'
if (statusRequest == "not_free")
if (_statusRequest == "not_free")
{
// TODO
Dictionary<string, string> result = RuCenterResponseParserHtml.ParseHtmlResponseContentWithNoFree(_formattedContent);

if (result != null)
{
return RuCenterResponseParserHtml.ConvertDictInWhoisHosnameModel(result, _mapper);
}
return null;
}

// not valid hostname\ip-address - '169.254.169.253:2323'
if (statusRequest == "fail") return null;
if (_statusRequest == "fail") return null;

return null;
}
Expand All @@ -107,8 +112,9 @@ private async Task<bool> CreateRequest(string host)

RuCenterOriginInputModel account = System.Text.Json.JsonSerializer.Deserialize<RuCenterOriginInputModel>(responseBody);

statusRequest = account.body.status;
htmlContent = account.body.list.First().html;
_statusRequest = account.body.status;
_formattedContent = account.body.list.First().formatted;
_htmlContent = account.body.list.First().html;
return true;
}
}
Expand Down
Loading

0 comments on commit 2e98f6c

Please sign in to comment.