Files
MeWeb/tools/knowledge.cs
T
thepraandClaude Opus 5.5 0dffbc27d6 The checks engine, the path and the cards: me-web? 0.1 reads the connection
MeWeb.Core runs seven checks concurrently and reports each as it settles:
online or behind a login page, this connection, the public IPv4 and IPv6
seen from v4./v6.meweb, the provider from a built-in map, the router
(UPnP IGD, NAT-PMP and PCP, read-only), address sharing (router WAN
against the echo, TTL hops only as a hint), and the history kept on the
device. The app draws the path from this device to the internet and lights
it node by node, with a card per check and a sheet listing every host it
contacts.

The provider map is iptoasn's table compressed to 2.1 MB by
tools/knowledge.cs and built before every app release; eleven Italian
provider profiles carry each fact with who said it, the source and the
month. The server gains a download page (en, it) and /download/{platform}
from the latest Gitea release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
2026-10-15 04:39:03 +02:00

63 lines
3.0 KiB
C#

#:project ../src/MeWeb.Core/MeWeb.Core.csproj
using System.IO.Compression;
using System.Net;
using System.Security.Cryptography;
using MeWeb.Core.Knowledge;
const string Source = "https://iptoasn.com/data/ip2asn-combined.tsv.gz";
var root = FindRoot();
var cache = Path.Combine(root, "tools", "cache");
var output = Path.Combine(root, "artifacts", "knowledge", "ip2asn.bin");
Directory.CreateDirectory(cache);
Directory.CreateDirectory(Path.GetDirectoryName(output));
var download = Path.Combine(cache, "ip2asn-combined.tsv.gz");
using var http = new HttpClient();
http.DefaultRequestHeaders.UserAgent.ParseAdd("me-web-knowledge/1 (+https://git.thepra.dev/Arasaka/MeWeb)");
var fresh = !File.Exists(download) || File.GetLastWriteTimeUtc(download) < DateTime.UtcNow.AddHours(-12) || args.Contains("--refresh");
var sourceDate = File.Exists(download) ? new DateTimeOffset(File.GetLastWriteTimeUtc(download), TimeSpan.Zero) : DateTimeOffset.UtcNow;
if (fresh)
{
using var response = await http.GetAsync(Source, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
sourceDate = response.Content.Headers.LastModified ?? DateTimeOffset.UtcNow;
await using (var file = File.Create(download + ".tmp"))
await response.Content.CopyToAsync(file);
File.Move(download + ".tmp", download, overwrite: true);
File.SetLastWriteTimeUtc(download, sourceDate.UtcDateTime);
}
var hash = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(download)));
var ranges = new List<AsnRange>();
using (var reader = new StreamReader(new GZipStream(File.OpenRead(download), CompressionMode.Decompress)))
{
string line;
while ((line = reader.ReadLine()) is not null)
{
var parts = line.Split('\t');
if (parts.Length < 5 || !uint.TryParse(parts[2], out var number) || number == 0) continue;
if (!IPAddress.TryParse(parts[0], out var start) || !IPAddress.TryParse(parts[1], out var end)) continue;
ranges.Add(new AsnRange { Start = start, End = end, Number = number, Country = parts[3] == "None" ? "" : parts[3], Name = parts[4] });
}
}
await using (var file = File.Create(output + ".tmp"))
AsnTable.Write(file, sourceDate, ranges);
File.Move(output + ".tmp", output, overwrite: true);
using var check = File.OpenRead(output);
var table = AsnTable.Read(check);
var probe = table.Find(IPAddress.Parse("1.1.1.1"));
Console.WriteLine($"knowledge: {ranges.Count} routed ranges, {table.Count} networks, source {sourceDate:yyyy-MM-dd HH:mm} UTC, sha256 {hash}");
Console.WriteLine($"knowledge: {output} {new FileInfo(output).Length / 1024} KiB; 1.1.1.1 -> AS{probe?.Number} {probe?.Name}");
File.WriteAllText(Path.Combine(root, "artifacts", "knowledge", "SOURCE.txt"), $"{Source}\nlast-modified {sourceDate:O}\nsha256 {hash}\n");
return 0;
static string FindRoot()
{
var dir = new DirectoryInfo(Directory.GetCurrentDirectory());
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "MeWeb.slnx"))) dir = dir.Parent;
return dir?.FullName ?? throw new InvalidOperationException("run from inside the repository (MeWeb.slnx not found)");
}