DDoS attacks are scored and SLA-charged at their victim only

ddosSf() took the worst service of every city with an ingress port, so on a big
network every attack counted as lost and charged SLA payouts to protected customers
anywhere. Held/lost, the SLA check and the far-scrubbing warning now look at the
attacked city.
This commit is contained in:
Frederic Jung 2026-10-28 20:11:06 +01:00
parent 675c63d561
commit 25e3e6db33
No known key found for this signature in database
3 changed files with 25 additions and 3 deletions

View file

@ -37,7 +37,8 @@ function ddosSlices(a){if(!a.entries?.length)return [a];const groups={};
return Object.entries(groups).map(([city,entries])=>({...a,parent:a,city,entries,gbps:a.gbps*entries.reduce((n,en)=>n+en.share,0)}));}
const ddosCities=a=>[...new Set([a.city,...(a.entries||[]).map(en=>en.city)])].filter(c=>S.pops[c]);
const ddosHasIx=a=>ddosCities(a).some(hasIxAt);
const ddosSf=a=>Math.min(1,...ddosCities(a).map(c=>sfOf(R,c)));
// An attack is held or lost at its victim: the junk lands there, and other POPs' bad days are not the attacker's doing.
const ddosSf=a=>sfOf(R,a.city);
function ddosReport(a){const ms=(R.ddos||[]).filter(m=>m.attack===a||m.attack.parent===a);if(!ms.length)return ddosMitigation(a);
const parts={};for(const m of ms)for(const x of m.sp?.parts||[]){const q=parts[x.city]||(parts[x.city]={city:x.city,tier:x.tier,a:0,cleaned:0});q.a+=x.a;q.cleaned+=x.cleaned;}
return {attack:a,blackhole:ms.some(m=>m.blackhole),upstream:ms.some(m=>m.upstream),reaching:ms.reduce((n,m)=>n+m.reaching,0),flowspec:ms.reduce((n,m)=>n+m.flowspec,0),fsMissing:ms.some(m=>m.fsMissing),filtered:ms.reduce((n,m)=>n+m.filtered,0),sp:Object.keys(parts).length?{parts:Object.values(parts)}:null,near:ms.find(m=>m.near)?.near||null,leak:ms.reduce((n,m)=>n+m.leak,0)};}
@ -53,7 +54,7 @@ const fsAt=c=>{const p=S.pops[c];return p?(RG[p.rgen||0].fs||0):0;};
function scrubEngaged(atk){return hasScrub()&&(S.scrubMode==='always'||(S.day-(atk.start??S.day))>=SCRUB_DELAY);}
// Several centers (#117): the flood goes to the nearest working one, and what it cannot take spills to the next.
// A center in another city only helps if the backbone can carry the flood there; routing works that out.
const scrubFar=c=>ddosAll().some(a=>ddosCities(a).includes(c))&&(R.haul||[]).find(q=>q.city===c&&q.ms!==null&&q.ms>=SCRUB_FAR_MS)||null;
const scrubFar=c=>{for(const a of ddosAll())if(a.city===c){const q=(R.haul||[]).find(q=>q.attack===a.id&&q.city===c&&q.ms!==null&&q.ms>=SCRUB_FAR_MS);if(q)return q;}return null;};
function scrubPlan(atk,g=atk.gbps||0,room={},take=null){if(!scrubEngaged(atk))return null;const c=atk.city;const parts=[];
for(const x of S.scrubs.filter(x=>popUp(x.city)&&(x.city===c||pathExists(c,x.city))).sort((a,b)=>(a.city===c?0:kmBetween(c,a.city))-(b.city===c?0:kmBetween(c,b.city)))){if(g<=0)break;const cap=SCRUB_TIERS[x.tier||0].g,a=Math.min(g,room[x.city]??cap);if(a<=0)continue;const cleaned=take?take(x,a):a;room[x.city]=(room[x.city]??cap)-cleaned;parts.push({city:x.city,tier:x.tier||0,a,cleaned});g-=cleaned;}
return parts.length?{parts,leak:g}:null;}

View file

@ -36,7 +36,7 @@ function dayTick(){
if(l.own&&l.ready>d&&Math.random()<0.012){const extra=10+Math.floor(Math.random()*40);setCableReady(l,l.ready+leadDays(extra));toast(tr('day_tick.construction_delay_n_days_why_a',{n:leadDays(extra),why:pick(l.sub?SEA_DELAYS.map(x=>tr(x)):[tr('day_tick.archaeologists_found_a_roman'),tr('day_tick.a_protected_hamster_species_lives'),tr('day_tick.the_railway_changed_its_mind_about'),tr('day_tick.the_drilling_crew_hit_a_granite'),tr('day_tick.a_town_council_wants_a_public'),tr('day_tick.somebody_else_s_gas_main_was'),tr('day_tick.the_permit_office_lost_your_permit')]),a:CITY[l.a].name,b:CITY[l.b].name}),'bad');}}
// DDoS protection SLA: if a protected customer's own city is hit and still goes down, you pay out (once per attack).
for(const a of ddosAll()){
for(const b of S.biz)if(b.type==='scrub'&&ddosCities(a).includes(b.city)&&!(a.sla||[]).includes(b.id)&&(a.id||b.slaFor!==a.start)&&sfOf(R,b.city)<0.85){
for(const b of S.biz)if(b.type==='scrub'&&b.city===a.city&&!(a.sla||[]).includes(b.id)&&(a.id||b.slaFor!==a.start)&&sfOf(R,b.city)<0.85){
(a.sla||(a.sla=[])).push(b.id);b.slaFor=a.start;S.cash-=SLA_PEN;b.mood=Math.max(0,b.mood-18);S.stats.sla=(S.stats.sla||0)+1;
toast(tr('day_tick.sla_breach_name_the_attack_on',{name:esc(b.name),city:CITY[a.city].name,pen:fmt$(SLA_PEN)}),'bad');sfx('err');}}
// businesses mood

View file

@ -38,3 +38,24 @@ test('a flood entering in another city crosses the backbone to its victim, whate
}
assert.equal(out[0], out[1], 'POP build order does not change the outcome');
});
test('an attack is held or lost at its victim; other POPs and their DDoS-protected customers are not charged', () => {
const { ev, errors } = load({ seed: 5 });
ev("ACT.start({});S.cash=1e9;S.lvl=6;S.nextFail=1e9;S.nextEvent=1e9;ACT.buildPop({city:'nyc'});for(const c of ['fra','nyc']){S.pops[c].bb={d:50};S.pops[c].rgen=3}");
ev("S.transit=[{id:'tf',city:'fra',net:'cog',tier:2},{id:'tn',city:'nyc',net:'cog',tier:1}];S.scrubs=[{city:'fra',tier:1}];S.scrubMode='always'");
// New York is struggling on its own (a 10G port for 50G of eyeballs), and has a DDoS-protected customer.
ev("S.pops.nyc.bb.d=200;S.biz.push({id:'sc',type:'scrub',name:'Shield',city:'nyc',bw:1,rate:1000,mood:80,since:0})");
ev("S.day=60;S.ev.ddos={id:'a',city:'fra',vec:'http',gbps:20,start:S.day,until:S.day+3,entry:{k:'distributed',lab:''},entries:[{k:'t',id:'tf',city:'fra',share:.5},{k:'t',id:'tn',city:'nyc',share:.5}]};compute()");
assert.ok(ev("sfOf(R,'nyc')") < 0.85, 'New York is under water anyway');
assert.ok(ev("sfOf(R,'fra')") >= 0.97, 'the victim is fine');
assert.equal(ev('ddosSf(S.ev.ddos)'), ev("sfOf(R,'fra')"));
const cash = ev('S.cash');
ev('dayTick()');
assert.deepEqual(JSON.parse(ev('JSON.stringify(S.ev.ddos.sla||[])')), [], 'no SLA payout for a customer in another city');
assert.equal(ev('S.stats.sla||0'), 0);
ev('S.day=S.ev.ddos.until;ddosExpire()');
assert.equal(ev('S.stats.ddosOk||0'), 1, 'held at the victim');
assert.equal(ev('S.stats.ddosLost||0'), 0);
assert.ok(cash > 0);
assert.deepEqual(errors, []);
});