Browse Source

Support picking if ipv6 or ipv4 should be used

master
amki 4 years ago
parent
commit
bacef415fe
  1. 18
      ISPChk/Controllers/HostController.cs
  2. 1
      ISPChk/Models/Host.cs
  3. 38
      ISPChk/NetworkTest.cs
  4. 7
      ISPChk/wwwroot/index.html
  5. 6
      ISPChk/wwwroot/ispchk.js

18
ISPChk/Controllers/HostController.cs

@ -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();

1
ISPChk/Models/Host.cs

@ -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; }

38
ISPChk/NetworkTest.cs

@ -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,8 +100,9 @@ namespace ISPChk
int failures = 0; int failures = 0;
for (var i = 0; i < numPings; ++i) for (var i = 0; i < numPings; ++i)
{ {
try {
// Send the request. // Send the request.
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); PingReply reply = pingSender.Send(pingAddress, timeout, buffer, options);
if (reply.Status == IPStatus.Success) if (reply.Status == IPStatus.Success)
{ {
@ -99,6 +127,10 @@ namespace ISPChk
System.Diagnostics.Debug.WriteLine(reply.Status); System.Diagnostics.Debug.WriteLine(reply.Status);
failures++; failures++;
} }
} catch(Exception e) {
System.Diagnostics.Debug.WriteLine("Found exception while pinging"+e);
failures++;
}
Thread.Sleep(500); Thread.Sleep(500);
} }
if(successes > 0) if(successes > 0)

7
ISPChk/wwwroot/index.html

@ -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>

6
ISPChk/wwwroot/ispchk.js

@ -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…
Cancel
Save