|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | +// Copyright (C) 2026 The Somewhere Authors |
| 3 | + |
| 4 | +package eu.nodepass.somewhere.vpn |
| 5 | + |
| 6 | +import eu.nodepass.somewhere.protocol.frame.SetupResult |
| 7 | +import eu.nodepass.somewhere.protocol.target.Target |
| 8 | +import eu.nodepass.somewhere.ui.state.ConnectionLogEntry |
| 9 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 10 | +import kotlinx.coroutines.flow.StateFlow |
| 11 | +import kotlinx.coroutines.flow.asStateFlow |
| 12 | +import java.text.SimpleDateFormat |
| 13 | +import java.util.Date |
| 14 | +import java.util.Locale |
| 15 | + |
| 16 | +/** |
| 17 | + * What the Portal answered, flow by flow (NW-A-06). |
| 18 | + * |
| 19 | + * ## Bounded, and what that costs |
| 20 | + * |
| 21 | + * A tunnel opens a flow per connection and a busy device opens hundreds a |
| 22 | + * minute, so this keeps the most recent [CAPACITY] and drops the rest. A log |
| 23 | + * that grew with the tunnel would be a memory leak with a user interface. |
| 24 | + * |
| 25 | + * ## What must never appear here, and why it is checked |
| 26 | + * |
| 27 | + * People paste logs into issues. This repository is public, and a shared key or |
| 28 | + * a subscription token in a pasted log is a credential published by someone |
| 29 | + * trying to be helpful — which is exactly how this project already shipped one |
| 30 | + * defect, through a subscription title that could rewrite the stored URL. |
| 31 | + * |
| 32 | + * So an entry carries a `SetupResult`, a timestamp, a flow id, a carrier name |
| 33 | + * and **a target that has been reduced to a host and port**. No URL, no key, no |
| 34 | + * subscription. That is not enforced by care; `ConnectionLogTest` fuzzes |
| 35 | + * credential-shaped values through every field and fails if one comes out. |
| 36 | + */ |
| 37 | +class ConnectionLog( |
| 38 | + private val now: () -> Long = System::currentTimeMillis, |
| 39 | +) { |
| 40 | + private val entries = ArrayDeque<ConnectionLogEntry>() |
| 41 | + private val mutable = MutableStateFlow<List<ConnectionLogEntry>>(emptyList()) |
| 42 | + |
| 43 | + /** Most recent first, which is the order a reader wants and the screen draws. */ |
| 44 | + val recent: StateFlow<List<ConnectionLogEntry>> = mutable.asStateFlow() |
| 45 | + |
| 46 | + fun record( |
| 47 | + result: SetupResult, |
| 48 | + target: Target?, |
| 49 | + flowId: UInt?, |
| 50 | + carrier: String, |
| 51 | + ) { |
| 52 | + val entry = |
| 53 | + ConnectionLogEntry( |
| 54 | + result = result, |
| 55 | + timestamp = FORMAT.format(Date(now())), |
| 56 | + target = target?.let(::describe), |
| 57 | + flowId = flowId?.toInt(), |
| 58 | + carrier = carrier, |
| 59 | + ) |
| 60 | + synchronized(entries) { |
| 61 | + entries.addFirst(entry) |
| 62 | + while (entries.size > CAPACITY) entries.removeLast() |
| 63 | + mutable.value = entries.toList() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fun clear() { |
| 68 | + synchronized(entries) { |
| 69 | + entries.clear() |
| 70 | + mutable.value = emptyList() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A target as `host:port`, and nothing else. |
| 76 | + * |
| 77 | + * A domain target already is one. An address target is rendered from its |
| 78 | + * octets rather than from anything a peer supplied, so a name that arrived |
| 79 | + * carrying punctuation cannot reach a log line at all. |
| 80 | + */ |
| 81 | + private fun describe(target: Target): String = |
| 82 | + when (target) { |
| 83 | + is Target.Domain -> "${sanitise(target.host)}:${target.port}" |
| 84 | + is Target.Ip -> |
| 85 | + if (target.octets.size == 4) { |
| 86 | + target.octets.joinToString(".") { (it.toInt() and 0xFF).toString() } + ":${target.port}" |
| 87 | + } else { |
| 88 | + target.octets.joinToString(":") { "%02x".format(it) } + " port ${target.port}" |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Strips what a log line must not carry. |
| 94 | + * |
| 95 | + * A hostname cannot legally contain any of this, so removing it costs a |
| 96 | + * correct name nothing — and a value that arrived from a peer carrying a |
| 97 | + * newline, a userinfo separator or a percent-encoded byte is precisely the |
| 98 | + * thing that must not reach a line someone copies. |
| 99 | + */ |
| 100 | + private fun sanitise(value: String): String = |
| 101 | + value |
| 102 | + .asSequence() |
| 103 | + .filter { it.isLetterOrDigit() || it == '.' || it == '-' } |
| 104 | + .take(MAX_HOST_LENGTH) |
| 105 | + .joinToString("") |
| 106 | + |
| 107 | + private companion object { |
| 108 | + const val CAPACITY = 200 |
| 109 | + const val MAX_HOST_LENGTH = 253 |
| 110 | + val FORMAT = SimpleDateFormat("HH:mm:ss", Locale.US) |
| 111 | + } |
| 112 | +} |
0 commit comments