Attacks keep their 12 biggest ingress ports and fold the rest, so saves stay small

Every attack stored every ingress port with traffic (over a thousand on a big
network), which grew the save from 272 KB to 1.8 MB with 14 attacks. The rest is now
one share at the victim, routed like the classic flood over its usual exits; old
saves are folded on load.
This commit is contained in:
Frederic Jung 2026-10-28 20:16:20 +01:00
parent 25e3e6db33
commit 40e4c79139
No known key found for this signature in database
5 changed files with 35 additions and 4 deletions

View file

@ -61,7 +61,8 @@ function compute(){
for(const part of ddosSlices(a)){const c=part.city;(atkBy[c]||(atkBy[c]=[])).push(part);
const m=ddosMitigation(part,{},bhCities.has(v),false),group={m,flows:[]};R2.ddos.push(m);junkGroups.push(group);if(!popUp(c))m.offline=true;
const src=popUp(v)&&SPD[v][CIDX[c]]<Infinity?v:c,si=CIDX[src],sn=cnCity(src);
if(part.entries?.length){for(const q of m.ingress){const f=flow(src,si,sn,0,q.g,false,true);f.pin=q.entry.k+':'+q.entry.id;group.flows.push({f,share:m.leak>0?q.g/m.leak:0});}}
if(part.entries?.length){for(const q of m.ingress){const sh=m.leak>0?q.g/m.leak:0;if(q.entry.k==='rest'){for(let j=0;j<NN;j++)group.flows.push({f:flow(src,si,sn,j,q.g*NSH[NETS[j].id],false,true),share:sh*NSH[NETS[j].id]});continue;}
const f=flow(src,si,sn,0,q.g,false,true);f.pin=q.entry.k+':'+q.entry.id;group.flows.push({f,share:sh});}}
else for(let j=0;j<NN;j++)group.flows.push({f:flow(src,si,sn,j,m.leak*NSH[NETS[j].id],false,true),share:NSH[NETS[j].id]});}}
for(const c of Object.keys(S.pops)){
const p=S.pops[c],ci=CIDX[c],cn=cnCity(c),SH=cn?NSH_CN:ruCity(c)?NSH_RU:CITY[c].kr&&S.krExit?NSH_KRX:NSH;const eye=(p.bb?p.bb.d:0)*viral*(cn&&ev.golden>day?1.4:1);const biz=bizBw[c]??0;const d0=eye+biz;

View file

@ -66,7 +66,7 @@ function sanitize(s){
// Keep the legacy primary attack so old saves and imports remain compatible; additional floods overlap it.
const repairAttack=a=>{if(!a||typeof a!=='object'||!isCity(a.city)||!s.pops[a.city]||!own(DDOS_VEC,a.vec))return null;
a.id=sid(a.id);a.gbps=Math.max(0,num(a.gbps,2));a.until=num(a.until,s.day);a.bh=!!a.bh;a.ixbh=!!a.ixbh;a.start=Math.max(0,Math.min(s.day,num(a.start,s.day)));a.until=Math.min(a.until,a.start+7);
if(Array.isArray(a.entries)){a.entries=a.entries.filter(en=>en&&['t','ix','p'].includes(en.k)&&isCity(en.city)&&typeof en.id==='string'&&Number.isFinite(+en.share)&&+en.share>0).slice(0,3000).map(en=>({k:en.k,id:en.id.slice(0,40),city:en.city,lab:String(en.lab||'').slice(0,60),share:+en.share}));const sum=a.entries.reduce((n,en)=>n+en.share,0);if(Number.isFinite(sum)&&sum>0)for(const en of a.entries)en.share/=sum;else delete a.entries;}else delete a.entries;
if(Array.isArray(a.entries)){a.entries=a.entries.slice(0,3000).filter(en=>en&&isCity(en.city)&&Number.isFinite(+en.share)&&+en.share>0&&(en.k==='rest'||['t','ix','p'].includes(en.k)&&typeof en.id==='string')).map(en=>en.k==='rest'?{k:'rest',city:en.city,share:+en.share}:{k:en.k,id:en.id.slice(0,40),city:en.city,lab:String(en.lab||'').slice(0,60),share:+en.share});const sum=a.entries.reduce((n,en)=>n+en.share,0);if(Number.isFinite(sum)&&sum>0){for(const en of a.entries)en.share/=sum;a.entries=ddosFold(a.entries,a.city);}else delete a.entries;}else delete a.entries;
a.sla=Array.isArray(a.sla)?a.sla.filter(x=>typeof x==='string').slice(0,s.biz.length):[];
if(!a.entry||typeof a.entry!=='object')a.entry={k:'edge',lab:''};else{a.entry.lab=String(a.entry.lab||'').slice(0,60);a.entry.id=a.entry.id==null?undefined:String(a.entry.id).slice(0,40);}return a;};
const attacks=[s.ev.ddos,...(Array.isArray(s.ev.ddosMore)?s.ev.ddosMore:[])].map(repairAttack).filter(Boolean).slice(0,14);

View file

@ -32,6 +32,11 @@ function ddosDrop(a){const left=ddosAll(S,false).filter(x=>x!==a);S.ev.ddos=left
function ddosExpire(){for(const a of ddosAll(S,false))if(a.until<=S.day||!S.pops[a.city]){ddosDone(a);ddosDrop(a);}}
function ddosEntries(){const ports=Object.values(R.ingressPorts||{}).filter(p=>p.cap>0),busy=ports.some(p=>p.traffic>1e-6),weight=p=>busy?p.traffic:p.cap,total=ports.reduce((n,p)=>n+weight(p),0);
return total>0?ports.filter(p=>weight(p)>0).map(p=>({k:p.k,id:p.id,city:p.city,lab:p.lab,share:weight(p)/total})):[];}
// A save keeps the biggest ingress ports by name; the long tail is one share (k:'rest') that arrives over the victim's
// usual exits, so a big network's attacks do not each carry hundreds of ports.
const DDOS_KEEP=12;
function ddosFold(entries,city){const L=entries.slice().sort((a,b)=>b.share-a.share);if(L.length<=DDOS_KEEP+1)return L;
const top=L.slice(0,DDOS_KEEP),rest=L.slice(DDOS_KEEP).reduce((n,en)=>n+en.share,0);if(rest>0)top.push({k:'rest',city,share:rest});return top;}
function ddosSlices(a){if(!a.entries?.length)return [a];const groups={};
for(const en of a.entries){const c=S.pops[en.city]?en.city:a.city;(groups[c]||(groups[c]=[])).push(en);}
return Object.entries(groups).map(([city,entries])=>({...a,parent:a,city,entries,gbps:a.gbps*entries.reduce((n,en)=>n+en.share,0)}));}

View file

@ -146,10 +146,10 @@ function ddosAttack(recompute=true){const type='ddos',fl=scnRule('ddos');ddosExp
const c=pick(Object.keys(S.pops)),vec=pick(ddosVectors()),V=DDOS_VEC[vec];
const gbps=ddosSize(vec);
const dur=1+Math.floor(Math.random()*(1+6*ddosPressure())),entry=ddosEntry(c);
const entries=ddosEntries(),a={id:uid(),city:c,vec,gbps,until:S.day+dur,entry:entries.length>1?{k:'distributed',lab:tr('common.distributed_ingress')}:entries[0]||entry,entries,bh:false,start:S.day};
const ports=ddosEntries(),entries=ddosFold(ports,c),a={id:uid(),city:c,vec,gbps,until:S.day+dur,entry:entries.length>1?{k:'distributed',lab:tr('common.distributed_ingress')}:entries[0]||entry,entries,bh:false,start:S.day};
if(!S.ev.ddos)S.ev.ddos=a;else(S.ev.ddosMore||(S.ev.ddosMore=[])).push(a);
const soaked=(V.fs&&fsAt(c))||hasScrub();
const how=entries.length>1?tr('failures.across_n_transit_and_peering_ports',{n:entries.length}):entry&&entry.lab?tr('failures.in_via_e',{e:entry.lab}):'';
const how=ports.length>1?tr('failures.across_n_transit_and_peering_ports',{n:ports.length}):entry&&entry.lab?tr('failures.in_via_e',{e:entry.lab}):'';
const msg=tr('failures.a_size_vec_attack_is_hitting',{size:fmtG(gbps),vec:V.n,city:CITY[c].name,how})+' '+(soaked?tr('failures.your_mitigation_is_eating_most_of'):tr('failures.junk_traffic_is_eating_your_capacity'));
if(fl)a.who=pick(FLOOD_WHO);
addInc({type,ref:c,atk:a.id,until:S.day+dur,msg:fl?msg+' '+tr('failures.attacker_who',{who:tr(a.who)}):msg,cost:0});

View file

@ -59,3 +59,28 @@ test('an attack is held or lost at its victim; other POPs and their DDoS-protect
assert.ok(cash > 0);
assert.deepEqual(errors, []);
});
test('an attack keeps its biggest ingress ports and folds the long tail, so saves stay small', () => {
const { ev, errors } = load({ seed: 5 });
ev("ACT.start({});S.cash=1e9;S.lvl=6;for(const c of ['fra','ams','lon','par'])if(!S.pops[c])ACT.buildPop({city:c});for(const c in S.pops){S.pops[c].bb={d:100};S.pops[c].rgen=3}");
ev("S.transit=[];const nets=NETS.filter(n=>n.type!=='t1').slice(0,40);for(const c in S.pops)for(const n of nets.slice(0,8))S.pni.push({id:n.id+'@'+c,net:n.id,city:c,tier:1});compute()");
ev("S.day=60;S.ddosStarts=[];ddosAttack()");
const ports = ev('ddosEntries().length');
assert.ok(ports > 20, `many ports carry traffic (${ports})`);
const en = JSON.parse(ev('JSON.stringify(S.ev.ddos.entries)'));
assert.equal(en.length, 13, 'twelve named ports and one remainder');
assert.equal(en.at(-1).k, 'rest');
assert.equal(en.at(-1).city, ev('S.ev.ddos.city'));
assert.ok(Math.abs(en.reduce((n, x) => n + x.share, 0) - 1) < 1e-9);
assert.match(ev("S.inc.at(-1).msg"), new RegExp(`across ${ports} transit and peering ports`), 'the message still counts every port');
// The remainder is routed like the rest of the flood, and survives a save.
ev('compute()');
assert.ok(Math.abs(ev('R.ddos.reduce((n,m)=>n+m.reaching,0)') - ev('S.ev.ddos.gbps')) < 1e-6);
ev('S=migrate(JSON.parse(JSON.stringify(S)));compute()');
assert.equal(ev('S.ev.ddos.entries.length'), 13);
// An old save with every port is folded on load.
ev("S.ev.ddos.entries=Array.from({length:300},(_,i)=>({k:'p',id:'x'+i,city:'fra',lab:'',share:1}));S=migrate(JSON.parse(JSON.stringify(S)))");
assert.equal(ev('S.ev.ddos.entries.length'), 13);
assert.equal(ev("S.ev.ddos.entries.find(e=>e.k==='rest').share.toFixed(4)"), (288/300).toFixed(4));
assert.deepEqual(errors, []);
});