Support picking if ipv6 or ipv4 should be used
This commit is contained in:
parent
a87dbbc4c9
commit
bacef415fe
@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ISPChk.Models;
|
||||
using System.Net;
|
||||
|
||||
namespace ISPChk.Controllers
|
||||
{
|
||||
@ -100,6 +101,23 @@ namespace ISPChk.Controllers
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Host>> PostHost(Host host)
|
||||
{
|
||||
IPAddress ipAddr;
|
||||
if (!IPAddress.TryParse(host.HostName, out ipAddr))
|
||||
{
|
||||
try {
|
||||
IPHostEntry hostInfo = Dns.GetHostEntry(host.HostName);
|
||||
} catch(System.Net.Sockets.SocketException e)
|
||||
{
|
||||
return BadRequest(new { errmsg = "Could not resolve hostname." });
|
||||
}
|
||||
} else
|
||||
{
|
||||
if(ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && host.IPv6 == true ||
|
||||
ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && host.IPv6 == false)
|
||||
{
|
||||
return BadRequest(new { errmsg = "Given IP was not of correct family." });
|
||||
}
|
||||
}
|
||||
_context.Hosts.Add(host);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
|
@ -10,6 +10,7 @@ namespace ISPChk.Models
|
||||
public long HostId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string HostName { get; set; }
|
||||
public bool IPv6 { get; set; }
|
||||
public bool Ping { get; set; }
|
||||
public bool TCP { get; set; }
|
||||
|
||||
|
@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@ -13,9 +14,11 @@ namespace ISPChk
|
||||
public class NetworkTest : INetworkTest
|
||||
{
|
||||
private ISPChkContext _context;
|
||||
private List<Task> runningTasks;
|
||||
|
||||
public NetworkTest() {
|
||||
_context = CreateDbContext();
|
||||
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
|
||||
}
|
||||
|
||||
public async void LoadHostsFromDatabase()
|
||||
@ -32,7 +35,7 @@ namespace ISPChk
|
||||
var optionsBuilder = new DbContextOptionsBuilder<ISPChkContext>();
|
||||
optionsBuilder.UseSqlite("Data Source=ispchk.db");
|
||||
var ctx = new ISPChkContext(optionsBuilder.Options);
|
||||
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
|
||||
System.Diagnostics.Debug.WriteLine("DbContext created.");
|
||||
ctx.Database.EnsureCreated();
|
||||
return ctx;
|
||||
}
|
||||
@ -46,6 +49,30 @@ namespace ISPChk
|
||||
var h = await ctx.Hosts.FindAsync(host.HostId);
|
||||
ctx.Entry(h).Collection(hst => hst.PingItems).Load();
|
||||
|
||||
IPAddress pingAddress;
|
||||
|
||||
|
||||
if (!IPAddress.TryParse(h.HostName, out pingAddress))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Resolving "+h.HostName);
|
||||
IPHostEntry hostInfo = Dns.GetHostEntry(h.HostName);
|
||||
foreach(var a in hostInfo.AddressList)
|
||||
{
|
||||
if((a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && h.IPv6 == true) ||
|
||||
a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && h.IPv6 == false)
|
||||
{
|
||||
pingAddress = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(pingAddress == null)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("No address to ping found...");
|
||||
return;
|
||||
}
|
||||
|
||||
Ping pingSender = new Ping();
|
||||
|
||||
// Create a buffer of 32 bytes of data to be transmitted.
|
||||
@ -61,7 +88,7 @@ namespace ISPChk
|
||||
// cannot be fragmented.
|
||||
PingOptions options = new PingOptions(64, true);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine("Pinging " + host.Name);
|
||||
System.Diagnostics.Debug.WriteLine("Initiate ping to " + pingAddress);
|
||||
|
||||
while (true)
|
||||
{
|
||||
@ -73,30 +100,35 @@ namespace ISPChk
|
||||
int failures = 0;
|
||||
for (var i = 0; i < numPings; ++i)
|
||||
{
|
||||
// Send the request.
|
||||
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
|
||||
try {
|
||||
// Send the request.
|
||||
PingReply reply = pingSender.Send(pingAddress, timeout, buffer, options);
|
||||
|
||||
if (reply.Status == IPStatus.Success)
|
||||
{
|
||||
successes++;
|
||||
//System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() +
|
||||
// ": bytes=" + reply.Buffer.Length +
|
||||
// " time=" + reply.RoundtripTime + "ms");
|
||||
avg += reply.RoundtripTime;
|
||||
if (reply.RoundtripTime < min)
|
||||
if (reply.Status == IPStatus.Success)
|
||||
{
|
||||
min = reply.RoundtripTime;
|
||||
successes++;
|
||||
//System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() +
|
||||
// ": bytes=" + reply.Buffer.Length +
|
||||
// " time=" + reply.RoundtripTime + "ms");
|
||||
avg += reply.RoundtripTime;
|
||||
if (reply.RoundtripTime < min)
|
||||
{
|
||||
min = reply.RoundtripTime;
|
||||
}
|
||||
if (reply.RoundtripTime > max)
|
||||
{
|
||||
max = reply.RoundtripTime;
|
||||
}
|
||||
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
|
||||
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
||||
}
|
||||
if (reply.RoundtripTime > max)
|
||||
else
|
||||
{
|
||||
max = reply.RoundtripTime;
|
||||
System.Diagnostics.Debug.WriteLine(reply.Status);
|
||||
failures++;
|
||||
}
|
||||
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
|
||||
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(reply.Status);
|
||||
} catch(Exception e) {
|
||||
System.Diagnostics.Debug.WriteLine("Found exception while pinging"+e);
|
||||
failures++;
|
||||
}
|
||||
Thread.Sleep(500);
|
||||
|
@ -22,6 +22,11 @@
|
||||
<label for="inpHostNameAdd" class="form-label">Hostname</label>
|
||||
<input type="text" class="form-control" id="inpHostNameAdd">
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" id="chkIPv6">
|
||||
<label class="form-check-label" for="chkIPv6">IPv6?</label>
|
||||
</div>
|
||||
<hr />
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" id="chkPing">
|
||||
<label class="form-check-label" for="chkPing">Ping</label>
|
||||
@ -55,7 +60,7 @@
|
||||
<form id="frmHistory">
|
||||
<div>
|
||||
<label for="inpHistoryPast" class="form-label">Show past x minutes</label>
|
||||
<input type="text" class="form-control" id="inpHistoryPast" aria-describedby="inpNameAddHelp" value="1440">
|
||||
<input type="text" class="form-control" id="inpHistoryPast" aria-describedby="inpNameAddHelp" value="60">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Show me</button>
|
||||
</form>
|
||||
|
@ -222,11 +222,13 @@ $("#frmHostAdd").submit((event) => {
|
||||
event.preventDefault();
|
||||
var name = $("#inpNameAdd").val();
|
||||
var hostname = $("#inpHostNameAdd").val();
|
||||
var ping = $('#chkPing').prop('checked')
|
||||
var tcp = $('#chkPing').prop('checked')
|
||||
var ipv6 = $('#chkIPv6').prop('checked');
|
||||
var ping = $('#chkPing').prop('checked');
|
||||
var tcp = $('#chkPing').prop('checked');
|
||||
var host = {
|
||||
name: name,
|
||||
hostname: hostname,
|
||||
ipv6: ipv6,
|
||||
ping: ping,
|
||||
tcp: tcp
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user