Files
doona/mock/flows.ts
T
Zakk 6d59d639e2 feat(connections): group the detail panel and name a node's subscription
The connection detail panel listed about sixteen fields in one column,
each label above its value, many of them empty or unknown. It now groups
them into route, connection, traffic and source sections with the route
first, puts each value beside its label, leaves out fields a connection
lacks, and shows each traffic total with its rate. DNS and flow detail
panels use the same list.

A row under the chain names the subscription the node came from, read
from the nodes query the page already holds, and the connection filter
matches it. The demo's auto group flows now reach the subscription node
it selects.
2026-11-18 05:23:34 +11:00

277 lines
9.6 KiB
TypeScript

import type {Connection, FlowDetail, FlowStep} from '../src/api/model';
import {addressPort, sourceIp} from '../src/api/selectors';
import {rules} from './rules';
import {configRules, flowLeaf} from './fixtures/configuration';
import {generationId} from './fixtures/clock';
type FlowFields = Pick<Connection, 'chain' | 'chain_source' | 'rule_id' | 'rule_expression' | 'rule_source' | 'ingress' | 'domain_source'>;
export type ConnectionSeed = Omit<Connection, keyof FlowFields>;
export function flowFields(input: FlowDetail['input'], steps: FlowStep[]): FlowFields {
const outbound = steps.find(s => s.stage === 'outbound')?.data;
const route = steps.find(s => s.stage === 'route')?.data;
return {
chain: outbound ? [...outbound.selection_path.map(p => p.group_id), ...(outbound.leaf_node_id ? [outbound.leaf_node_id] : [])] : [],
chain_source: outbound ? 'evaluation' : 'unknown',
rule_id: route?.rule_id ?? null,
rule_expression: route?.rules.find(r => r.rule_id === route.rule_id)?.expression ?? null,
rule_source: route ? (route.plane === 'kernel' ? 'kernel' : 'recomputed') : 'unknown',
ingress: input.ingress,
domain_source: input.domain_source
};
}
// Keep mock flow evidence aligned with the shared rule dictionary.
const ruleTable: Array<[string[], string]> = [
[
[
'taobao.com',
'tmall.com',
'alipay.com',
'alicdn.com',
'weixin.qq.com',
'wechat.com',
'zhihu.com',
'zhimg.com',
'douyin.com',
'iqiyi.com',
'youku.com',
'weibo.com',
'jd.com',
'xiaohongshu.com',
'baidu.com',
'bilibili.com'
],
'r4'
],
[['telegram.org', 't.me'], 'r5'],
[['openai.com', 'chatgpt.com', 'oaistatic.com'], 'r7']
];
const ruleOf = (id: string) => rules.find(rule => rule.id === id)!;
function ruleFor(domain: string): {id: string; expression: string} | null {
const name = domain.toLowerCase();
for (const [suffixes, id] of ruleTable) if (suffixes.some(s => name === s || name.endsWith('.' + s))) return {id, expression: ruleOf(id).cond};
return null;
}
function endpoint(value: string | null): [string | null, number | null] {
const port = addressPort(value ?? undefined);
return [sourceIp(value ?? undefined) ?? null, port === undefined ? null : Number(port)];
}
export function createFlow(connection: ConnectionSeed, network: 'tcp' | 'udp', observedAt: string, instanceId: string): FlowDetail {
const input: FlowDetail['input'] = {
src: connection.src ?? null,
dst: connection.dst ?? null,
domain: connection.domain ?? null,
domain_source: connection.domain ? 'dns_mapping' : null,
pid: null,
process_path: null,
src_mac: null,
ingress: 'lan',
domain_rule_ids: null,
dscp: 0,
mark: 0
};
const [src_ip, src_port] = endpoint(input.src);
const [dst_ip, dst_port] = endpoint(input.dst);
const common = {observed_at: observedAt, elapsed_us: 0, generation_id: generationId, evidence: 'observed' as const};
const direct = connection.outbound === 'direct';
const blocked = connection.state === 'blocked';
// The rule that decided this flow: a listed domain hits its config rule; a blocked flow the advert rule; a
// direct flow without a domain the mainland IP rule; anything else the fallback.
const known = connection.domain ? ruleFor(connection.domain) : null;
const decided =
known ??
(blocked
? {id: 'r3', expression: ruleOf('r3').cond}
: direct
? {id: 'r2', expression: ruleOf('r2').cond}
: {id: 'fallback', expression: 'fallback: ' + configRules.fallback.target});
const expression = decided.expression;
const leaf = flowLeaf(connection.outbound);
const policy = connection.outbound === 'auto' ? 'urltest' : connection.outbound === 'gaming' ? 'urltest' : 'selector';
const steps: FlowStep[] = [
{...common, seq: 1, stage: 'input', data: {values: {...input, pname: connection.pname}, source: direct || blocked ? 'kernel' : 'socket'}},
{
...common,
seq: 2,
stage: 'route',
data: {
evaluation_id: 'eval-1',
chain: 'traffic',
plane: direct || blocked ? 'kernel' : 'userspace',
rule_id: decided.id,
rules: [{rule_id: decided.id, expression, result: 'matched', conditions: [], missing_inputs: []}],
outbound: connection.outbound,
must: blocked,
mark: 0,
input: {
network,
src_ip,
src_port,
dst_ip,
dst_port,
domain: input.domain,
pname: connection.pname ?? null,
src_mac: input.src_mac,
dscp: input.dscp,
mark: input.mark,
ingress: input.ingress,
domain_rule_ids: input.domain_rule_ids
},
dns_action: null
}
},
{
...common,
seq: 3,
stage: 'datapath',
data: {
plane: direct || blocked ? 'kernel' : 'userspace',
action: blocked ? 'drop' : direct ? 'activate_direct' : 'redirect',
reason: 'route_selected',
error: null
}
}
];
if (!direct && !blocked) {
steps.push({
...common,
seq: steps.length + 1,
stage: 'dial_mode',
data: {
configured: input.domain ? 'domain' : 'ip',
effective_target: input.domain ? 'domain' : 'ip',
domain: input.domain,
domain_source: input.domain_source,
verification: input.domain ? 'matched' : 'not_required',
reason: input.domain ? 'dns_mapping_matches' : 'ip_target'
}
});
if (input.domain)
steps.push({
...common,
seq: steps.length + 1,
stage: 'dns',
data: {
lookup_id: 'lookup-1',
parent_lookup_id: null,
attempt_id: null,
purpose: 'dial_target',
name: input.domain,
qtype: 'A',
source: 'cache',
upstream_transport: null,
carrier_transport: null,
cache: 'hit',
cache_entry_id: null,
upstream: null,
route_evaluation_ids: [],
status: 'NOERROR',
addresses: dst_ip ? [dst_ip] : [],
selected_ip: dst_ip,
error: null
}
});
steps.push(
{
...common,
seq: steps.length + 1,
stage: 'reroute',
data: {performed: false, reason: 'no_new_routing_input', from_evaluation_id: 'eval-1', to_evaluation_id: null}
},
{
...common,
seq: steps.length + 2,
stage: 'outbound',
data: {
attempt_id: 'attempt-1',
parent_attempt_id: null,
kind: 'leaf',
evaluation_id: 'eval-1',
routing_source: 'evaluation',
routed_outbound: connection.outbound,
effective_outbound: connection.outbound,
selection_path: [
{
group_id: connection.outbound!,
member_id: leaf,
member_name: leaf,
policy,
reason: policy === 'selector' ? 'runtime_selection' : 'policy_selection',
selection: {
previous_member_id: null,
metric: null,
tolerance_ms: null,
candidates: [
{
member_id: leaf,
member_name: leaf,
leaf_node_id: leaf,
leaf_node_name: leaf,
eligible: true,
sorting_latency_ms: null,
score: null,
selected: true,
reason: 'selected'
}
]
}
}
],
leaf_node_id: leaf,
leaf_node_name: leaf,
target: input.domain ? input.domain + ':' + dst_port : input.dst,
target_kind: input.domain ? 'domain' : 'ip',
dial_ip: dst_ip,
server_addr: '198.51.100.1:443',
resolution_location: input.domain ? 'local_dns' : 'original_ip',
status: 'succeeded',
error: null
}
}
);
}
if (!blocked)
steps.push({
...common,
seq: steps.length + 1,
stage: 'connection',
data: {state: 'active', reason: 'reply_received', milestone: 'first_reply', attempt_id: direct ? null : 'attempt-1', reply_received: true, error: null}
});
// A retained closed flow ends with its terminal step, the way closeLive records one.
if (connection.state === 'closed')
steps.push({
...common,
seq: steps.length + 1,
stage: 'connection',
data: {state: 'closed', milestone: 'terminal', reason: 'peer_closed', attempt_id: null, reply_received: null, error: null}
});
const timed = steps.map((step, i) => ({
...step,
elapsed_us: i * 1200,
observed_at: new Date(Date.parse(connection.started_at ?? observedAt) + i * 1.2).toISOString()
}));
const summary = {
id: blocked && !connection.flow_id ? 'flow-blocked' : 'flow-' + connection.id,
instance_id: instanceId,
revision: 1,
network,
state: connection.state,
pname: connection.pname,
connection_id: blocked ? null : connection.id,
outbound: connection.outbound,
...flowFields(input, timed),
// Every fixture flow was routed by the demo's first rules generation.
rule_generation_id: timed.some(step => step.stage === 'route') ? generationId : null,
observed_by: connection.observed_by,
started_at: connection.started_at,
ended_at: blocked || connection.state === 'closed' ? timed[timed.length - 1].observed_at : null,
input
};
return direct
? {...summary, trace_status: 'partial', trace: {status: 'partial', missing: ['not_instrumented'], steps: timed}}
: {...summary, trace_status: 'complete', trace: {status: 'complete', missing: [], steps: timed}};
}