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.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ISPChk.Models;
|
using ISPChk.Models;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace ISPChk.Controllers
|
namespace ISPChk.Controllers
|
||||||
{
|
{
|
||||||
@ -100,6 +101,23 @@ namespace ISPChk.Controllers
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<Host>> PostHost(Host host)
|
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);
|
_context.Hosts.Add(host);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ namespace ISPChk.Models
|
|||||||
public long HostId { get; set; }
|
public long HostId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string HostName { get; set; }
|
public string HostName { get; set; }
|
||||||
|
public bool IPv6 { get; set; }
|
||||||
public bool Ping { get; set; }
|
public bool Ping { get; set; }
|
||||||
public bool TCP { get; set; }
|
public bool TCP { get; set; }
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -13,9 +14,11 @@ namespace ISPChk
|
|||||||
public class NetworkTest : INetworkTest
|
public class NetworkTest : INetworkTest
|
||||||
{
|
{
|
||||||
private ISPChkContext _context;
|
private ISPChkContext _context;
|
||||||
|
private List<Task> runningTasks;
|
||||||
|
|
||||||
public NetworkTest() {
|
public NetworkTest() {
|
||||||
_context = CreateDbContext();
|
_context = CreateDbContext();
|
||||||
|
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void LoadHostsFromDatabase()
|
public async void LoadHostsFromDatabase()
|
||||||
@ -32,7 +35,7 @@ namespace ISPChk
|
|||||||
var optionsBuilder = new DbContextOptionsBuilder<ISPChkContext>();
|
var optionsBuilder = new DbContextOptionsBuilder<ISPChkContext>();
|
||||||
optionsBuilder.UseSqlite("Data Source=ispchk.db");
|
optionsBuilder.UseSqlite("Data Source=ispchk.db");
|
||||||
var ctx = new ISPChkContext(optionsBuilder.Options);
|
var ctx = new ISPChkContext(optionsBuilder.Options);
|
||||||
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated.");
|
System.Diagnostics.Debug.WriteLine("DbContext created.");
|
||||||
ctx.Database.EnsureCreated();
|
ctx.Database.EnsureCreated();
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
@ -46,6 +49,30 @@ namespace ISPChk
|
|||||||
var h = await ctx.Hosts.FindAsync(host.HostId);
|
var h = await ctx.Hosts.FindAsync(host.HostId);
|
||||||
ctx.Entry(h).Collection(hst => hst.PingItems).Load();
|
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();
|
Ping pingSender = new Ping();
|
||||||
|
|
||||||
// Create a buffer of 32 bytes of data to be transmitted.
|
// Create a buffer of 32 bytes of data to be transmitted.
|
||||||
@ -61,7 +88,7 @@ namespace ISPChk
|
|||||||
// cannot be fragmented.
|
// cannot be fragmented.
|
||||||
PingOptions options = new PingOptions(64, true);
|
PingOptions options = new PingOptions(64, true);
|
||||||
|
|
||||||
System.Diagnostics.Debug.WriteLine("Pinging " + host.Name);
|
System.Diagnostics.Debug.WriteLine("Initiate ping to " + pingAddress);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -73,30 +100,35 @@ namespace ISPChk
|
|||||||
int failures = 0;
|
int failures = 0;
|
||||||
for (var i = 0; i < numPings; ++i)
|
for (var i = 0; i < numPings; ++i)
|
||||||
{
|
{
|
||||||
// Send the request.
|
try {
|
||||||
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options);
|
// Send the request.
|
||||||
|
PingReply reply = pingSender.Send(pingAddress, timeout, buffer, options);
|
||||||
|
|
||||||
if (reply.Status == IPStatus.Success)
|
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)
|
|
||||||
{
|
{
|
||||||
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);
|
} catch(Exception e) {
|
||||||
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
System.Diagnostics.Debug.WriteLine("Found exception while pinging"+e);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.Diagnostics.Debug.WriteLine(reply.Status);
|
|
||||||
failures++;
|
failures++;
|
||||||
}
|
}
|
||||||
Thread.Sleep(500);
|
Thread.Sleep(500);
|
||||||
|
@ -22,6 +22,11 @@
|
|||||||
<label for="inpHostNameAdd" class="form-label">Hostname</label>
|
<label for="inpHostNameAdd" class="form-label">Hostname</label>
|
||||||
<input type="text" class="form-control" id="inpHostNameAdd">
|
<input type="text" class="form-control" id="inpHostNameAdd">
|
||||||
</div>
|
</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">
|
<div class="form-check">
|
||||||
<input type="checkbox" class="form-check-input" id="chkPing">
|
<input type="checkbox" class="form-check-input" id="chkPing">
|
||||||
<label class="form-check-label" for="chkPing">Ping</label>
|
<label class="form-check-label" for="chkPing">Ping</label>
|
||||||
@ -55,7 +60,7 @@
|
|||||||
<form id="frmHistory">
|
<form id="frmHistory">
|
||||||
<div>
|
<div>
|
||||||
<label for="inpHistoryPast" class="form-label">Show past x minutes</label>
|
<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>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary">Show me</button>
|
<button type="submit" class="btn btn-primary">Show me</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -222,11 +222,13 @@ $("#frmHostAdd").submit((event) => {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
var name = $("#inpNameAdd").val();
|
var name = $("#inpNameAdd").val();
|
||||||
var hostname = $("#inpHostNameAdd").val();
|
var hostname = $("#inpHostNameAdd").val();
|
||||||
var ping = $('#chkPing').prop('checked')
|
var ipv6 = $('#chkIPv6').prop('checked');
|
||||||
var tcp = $('#chkPing').prop('checked')
|
var ping = $('#chkPing').prop('checked');
|
||||||
|
var tcp = $('#chkPing').prop('checked');
|
||||||
var host = {
|
var host = {
|
||||||
name: name,
|
name: name,
|
||||||
hostname: hostname,
|
hostname: hostname,
|
||||||
|
ipv6: ipv6,
|
||||||
ping: ping,
|
ping: ping,
|
||||||
tcp: tcp
|
tcp: tcp
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user