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
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-10-15 04:39:03 +02:00
1 parent 16a1cecaae
commit 0dffbc27d6
61 files changed
+3750 -149

No files matched your search

+177
View File
@@ -0,0 +1,177 @@
using System.Buffers.Binary;
using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace MeWeb.Core.Knowledge;
public class AsnEntry
{
public uint Number { get; set; }
public string Country { get; set; }
public string Name { get; set; }
}
public class AsnRange
{
public IPAddress Start { get; set; }
public IPAddress End { get; set; }
public uint Number { get; set; }
public string Country { get; set; }
public string Name { get; set; }
}
public sealed class AsnTable
{
const uint Magic = 0x314B574D;
AsnEntry[] systems;
uint[] v4Start;
int[] v4System;
UInt128[] v6Start;
int[] v6System;
public DateTimeOffset SourceDate { get; private set; }
public int Count => systems.Length;
public AsnEntry Find(IPAddress address)
{
if (address is null) return null;
if (address.IsIPv4MappedToIPv6) address = address.MapToIPv4();
var index = address.AddressFamily == AddressFamily.InterNetwork
? Search(v4Start, BinaryPrimitives.ReadUInt32BigEndian(address.GetAddressBytes()), v4System)
: Search(v6Start, BinaryPrimitives.ReadUInt128BigEndian(address.GetAddressBytes()), v6System);
return index < 0 ? null : systems[index];
}
public static AsnTable Read(Stream compressed)
{
using var brotli = new BrotliStream(compressed, CompressionMode.Decompress);
using var reader = new BinaryReader(brotli, Encoding.UTF8);
if (reader.ReadUInt32() != Magic) throw new InvalidDataException("not a me-web? provider table");
var table = new AsnTable { SourceDate = DateTimeOffset.FromUnixTimeSeconds(reader.ReadInt64()) };
table.systems = new AsnEntry[reader.Read7BitEncodedInt()];
for (var i = 0; i < table.systems.Length; i++)
table.systems[i] = new AsnEntry { Number = (uint)reader.Read7BitEncodedInt64(), Country = reader.ReadString(), Name = reader.ReadString() };
var count = reader.Read7BitEncodedInt();
table.v4Start = new uint[count];
table.v4System = new int[count];
var previous4 = 0u;
for (var i = 0; i < count; i++)
{
previous4 += (uint)reader.Read7BitEncodedInt64();
table.v4Start[i] = previous4;
table.v4System[i] = reader.Read7BitEncodedInt() - 1;
}
count = reader.Read7BitEncodedInt();
table.v6Start = new UInt128[count];
table.v6System = new int[count];
var previous6 = UInt128.Zero;
for (var i = 0; i < count; i++)
{
previous6 += ReadBig(reader);
table.v6Start[i] = previous6;
table.v6System[i] = reader.Read7BitEncodedInt() - 1;
}
return table;
}
public static void Write(Stream output, DateTimeOffset sourceDate, IEnumerable<AsnRange> ranges)
{
var list = ranges.Where(r => r.Number != 0).ToList();
var systems = list.GroupBy(r => r.Number).OrderBy(g => g.Key)
.Select(g => new AsnEntry { Number = g.Key, Country = g.First().Country ?? "", Name = g.First().Name ?? "" }).ToList();
var index = systems.Select((s, i) => (s.Number, i)).ToDictionary(p => p.Number, p => p.i);
using var brotli = new BrotliStream(output, CompressionLevel.SmallestSize, leaveOpen: true);
using var writer = new BinaryWriter(brotli, Encoding.UTF8);
writer.Write(Magic);
writer.Write(sourceDate.ToUnixTimeSeconds());
writer.Write7BitEncodedInt(systems.Count);
foreach (var system in systems)
{
writer.Write7BitEncodedInt64(system.Number);
writer.Write(system.Country);
writer.Write(system.Name);
}
var v4 = Flatten(list.Where(r => r.Start.AddressFamily == AddressFamily.InterNetwork), a => BinaryPrimitives.ReadUInt32BigEndian(a.GetAddressBytes()), uint.MaxValue, index);
writer.Write7BitEncodedInt(v4.Count);
var previous4 = 0u;
foreach (var (start, system) in v4)
{
writer.Write7BitEncodedInt64((uint)start - previous4);
writer.Write7BitEncodedInt(system + 1);
previous4 = (uint)start;
}
var v6 = Flatten(list.Where(r => r.Start.AddressFamily == AddressFamily.InterNetworkV6), a => BinaryPrimitives.ReadUInt128BigEndian(a.GetAddressBytes()), UInt128.MaxValue, index);
writer.Write7BitEncodedInt(v6.Count);
var previous6 = UInt128.Zero;
foreach (var (start, system) in v6)
{
WriteBig(writer, start - previous6);
writer.Write7BitEncodedInt(system + 1);
previous6 = start;
}
}
static List<(UInt128 Start, int System)> Flatten(IEnumerable<AsnRange> ranges, Func<IPAddress, UInt128> number, UInt128 max, Dictionary<uint, int> index)
{
var result = new List<(UInt128 Start, int System)>();
var nextFree = UInt128.Zero;
var lastSystem = -1;
var full = false;
foreach (var range in ranges.Select(r => (Start: number(r.Start), End: number(r.End), System: index[r.Number])).OrderBy(r => r.Start))
{
if (full || range.End < nextFree) continue;
var start = range.Start < nextFree ? nextFree : range.Start;
if (start > nextFree && lastSystem != -1)
{
result.Add((nextFree, -1));
lastSystem = -1;
}
if (range.System != lastSystem) result.Add((start, range.System));
lastSystem = range.System;
full = range.End == max;
nextFree = full ? max : range.End + 1;
}
if (lastSystem != -1 && !full) result.Add((nextFree, -1));
return result;
}
static int Search<T>(T[] starts, T value, int[] systems) where T : IComparable<T>
{
var position = Array.BinarySearch(starts, value);
if (position < 0) position = ~position - 1;
return position < 0 ? -1 : systems[position];
}
static void WriteBig(BinaryWriter writer, UInt128 value)
{
do
{
var low = (byte)(value & 0x7F);
value >>= 7;
writer.Write(value == UInt128.Zero ? low : (byte)(low | 0x80));
}
while (value != UInt128.Zero);
}
static UInt128 ReadBig(BinaryReader reader)
{
var value = UInt128.Zero;
var shift = 0;
while (true)
{
var b = reader.ReadByte();
value |= (UInt128)(b & 0x7F) << shift;
if ((b & 0x80) == 0) return value;
shift += 7;
}
}
}
+65
View File
@@ -0,0 +1,65 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MeWeb.Core.Knowledge;
public class ProviderFact
{
public string Value { get; set; }
public decimal? Price { get; set; }
public string Currency { get; set; }
public string Basis { get; set; }
public string Source { get; set; }
public string Date { get; set; }
}
public class ProviderProfile
{
public string Name { get; set; }
public string Country { get; set; }
public List<uint> Asns { get; set; } = [];
public string Sheet { get; set; }
public ProviderFact Ipv4 { get; set; }
public ProviderFact PublicAddress { get; set; }
public ProviderFact Ipv6 { get; set; }
public ProviderFact Port25 { get; set; }
}
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
[JsonSerializable(typeof(ProviderProfile))]
public partial class KnowledgeJsonContext : JsonSerializerContext
{
}
public sealed class KnowledgeBase
{
readonly Lazy<AsnTable> asn = new(LoadAsn);
readonly Lazy<List<ProviderProfile>> providers = new(LoadProviders);
public bool HasAsnTable => typeof(KnowledgeBase).Assembly.GetManifestResourceInfo("ip2asn.bin") is not null;
public AsnTable Asn => asn.Value;
public IReadOnlyList<ProviderProfile> Providers => providers.Value;
public ProviderProfile ProviderFor(uint number) => number == 0 ? null : Providers.FirstOrDefault(p => p.Asns.Contains(number));
static AsnTable LoadAsn()
{
using var stream = typeof(KnowledgeBase).Assembly.GetManifestResourceStream("ip2asn.bin");
return stream is null ? null : AsnTable.Read(stream);
}
static List<ProviderProfile> LoadProviders()
{
var assembly = typeof(KnowledgeBase).Assembly;
var list = new List<ProviderProfile>();
foreach (var name in assembly.GetManifestResourceNames().Where(n => n.StartsWith("isp.", StringComparison.Ordinal)).Order(StringComparer.Ordinal))
{
using var stream = assembly.GetManifestResourceStream(name);
var profile = JsonSerializer.Deserialize(stream, KnowledgeJsonContext.Default.ProviderProfile);
if (profile is not null) list.Add(profile);
}
return list;
}
}