Files
MeWeb/src/MeWeb.Server/SpeedService.cs
T
thepraandClaude Opus 5.5 7cfc8b6de5
Build / Desktop, server and tools (push) Successful in 59s
Release / Every platform, the server, the release (push) Successful in 7m27s
me-web? 0.2: the test from outside, calls and games, mail, and what you can host
Phases two and three ship together, because their code grew in the same
files. With consent the app asks the router to open 80, 443 and a high
port for ten minutes (UPnP, NAT-PMP or PCP), and the server, holding a
single-use ticket bound to the caller's own address, connects back and
must get an HMAC only the app can give; the mappings are removed after the
run and remembered until then, so a crash cannot leave one open. Hairpin
is tried from inside. Two STUN servers, the outside one first, tell easy
from hard NAT.

Mail gets port 25 to Gmail and Outlook, the reverse name and five
blocklists through the user's own resolver, network-wide UCEPROTECT
listings counted as a caution. DNS, speed against the server (four at a
time, twelve an hour per address) and the device follow, and six uses
(website, home from outside, files, game server, fediverse, mail) close
the run as possible, possible with a named workaround, or can't tell.

Findings can be shared after reading the exact JSON: a file per submission
under the unit's state directory, ten a day per address, deletable by the
token the app keeps, swept after twelve months; /privacy says all of it in
English and Italian. QR codes lead to the downloads. Every language carries
every string.

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

66 lines
1.6 KiB
C#

using System.Collections.Concurrent;
using System.Net;
using System.Security.Cryptography;
using MeWeb.Shared;
namespace MeWeb.Server;
public class SpeedService
{
const int PerHour = 12;
const int AtOnce = 4;
int active;
static readonly byte[] Noise = RandomNumberGenerator.GetBytes(1024 * 1024);
readonly ConcurrentDictionary<string, List<DateTimeOffset>> uses = new();
public bool Allow(IPAddress caller)
{
var now = DateTimeOffset.UtcNow;
var times = uses.GetOrAdd(ProbeService.NetworkOf(caller), _ => []);
lock (times)
{
times.RemoveAll(t => now - t > TimeSpan.FromHours(1));
if (times.Count >= PerHour) return false;
times.Add(now);
return true;
}
}
public bool Enter()
{
if (Interlocked.Increment(ref active) <= AtOnce) return true;
Interlocked.Decrement(ref active);
return false;
}
public void Leave() => Interlocked.Decrement(ref active);
public static async Task Stream(HttpResponse response, int bytes, CancellationToken ct)
{
response.ContentType = "application/octet-stream";
response.ContentLength = bytes;
response.Headers.CacheControl = "no-store";
var left = bytes;
while (left > 0)
{
var chunk = Math.Min(left, Noise.Length);
await response.Body.WriteAsync(Noise.AsMemory(0, chunk), ct);
left -= chunk;
}
}
public static async Task<long> Drain(Stream body, CancellationToken ct)
{
var buffer = new byte[64 * 1024];
long total = 0;
int read;
while ((read = await body.ReadAsync(buffer, ct)) > 0)
{
total += read;
if (total > Endpoints.Speed.MaxUpBytes) return -1;
}
return total;
}
}