2022-03-09 14:06:14 -05:00
import { ArgError , ArgParser } from './scripts/lib/arg-parser' ;
2022-02-11 01:08:32 -05:00
2022-03-09 14:06:14 -05:00
/ * *
* Search the network for targets to execute a script against .
* @ param ns { NS } - BitBurner API
* /
export async function main ( ns ) {
// Setup
ns . disableLog ( 'ALL' ) ;
const argParser = new ArgParser ( 'crawler.js' , 'Search the network for targets to execute a script against.' , null , [
{ name : 'script' , desc : 'Script to copy & execute' , type : 'string' } ,
{ name : 'args' , desc : 'Arguments for script. Forward the current target with: {{TARGET}}' , optional : true , extras : true , type : 'string' } ,
{ name : 'cpu' , desc : 'Number of CPU threads to use with script' , flags : [ '-c' , '--cpu' ] , default : 1 , type : 'num' } ,
{ name : 'depth' , desc : 'Depth to scan to, defaults to 3' , flags : [ '-d' , '--depth' ] , default : Infinity , type : 'num' } ,
{ name : 'level' , desc : 'Exclude targets with higher hack level, defaults to current hack level' , flags : [ '-l' , '--level' ] , default : ns . getHackingLevel ( ) , type : 'num' } ,
{ name : 'ports' , desc : 'Exclute targets with too many closed ports' , flags : [ '-p' , '--ports' ] , optional : true , default : Infinity , type : 'num' } ,
2022-03-13 21:44:47 -04:00
] , true ) ;
2022-03-09 14:06:14 -05:00
let args ;
try {
args = argParser . parse ( ns . args ) ;
} catch ( err ) {
if ( err instanceof ArgError ) return ns . tprint ( argParser . help ( err . message ) ) ;
throw err ;
2022-02-11 01:08:32 -05:00
}
2022-02-04 11:49:05 -05:00
2022-02-11 01:08:32 -05:00
/ * *
2022-03-09 14:06:14 -05:00
* Recursively search network & build a tree
* @ param host { string } - Point to scan from
* @ param depth { number } - Current scanning depth
* @ param blacklist { String [ ] } - Devices already discovered
* @ returns Dicionary of discovered devices
2022-02-11 01:08:32 -05:00
* /
2022-03-09 14:06:14 -05:00
function scan ( target = 'home' , depth = 1 , found = new Set ( ) ) {
if ( found . size == 0 ) found . add ( target ) ;
ns . scan ( target ) . filter ( t => ! found . has ( t ) ) . forEach ( t => {
found . add ( t ) ;
scan ( t , depth + 1 , found ) ;
} ) ;
found . delete ( 'home' ) ;
return Array . from ( found ) ;
2022-02-11 01:08:32 -05:00
}
2022-03-09 14:06:14 -05:00
// Run
const targets = scan ( ) ;
let complete = 0 , failed = 0 , skipped = 0 ;
for ( let target of targets ) {
if ( target == 'home' ) continue ;
2022-02-11 01:08:32 -05:00
2022-03-09 14:06:14 -05:00
if ( args [ 'level' ] < ns . getServerRequiredHackingLevel ( target ) || args [ 'ports' ] < ns . getServerNumPortsRequired ( target ) ) {
skipped ++ ;
continue ;
}
2022-02-11 01:08:32 -05:00
2022-03-09 14:06:14 -05:00
const scriptArgs = args [ 'args' ] . map ( arg => arg == '{{TARGET}}' ? target : arg ) ;
const pid = ns . run ( args [ 'script' ] , args [ 'cpu' ] , ... scriptArgs ) ;
if ( pid == 0 ) {
failed ++ ;
continue ;
}
2022-02-11 01:08:32 -05:00
// Wait for script to finish
2022-03-09 14:06:14 -05:00
do { await ns . sleep ( 1000 ) ; }
while ( ns . scriptRunning ( args [ 'script' ] , 'home' ) ) ;
complete ++ ;
2022-02-11 01:08:32 -05:00
}
2022-03-09 14:06:14 -05:00
// Output report
ns . tprint ( '===================================================' ) ;
ns . tprint ( ` Crawler Report: ${ targets . length } Device ${ targets . length > 1 ? 's' : '' } ` ) ;
ns . tprint ( '===================================================' ) ;
ns . tprint ( ` Complete: ${ complete } \t Failed: ${ failed } \t Skipped: ${ skipped } ` ) ;
ns . tprint ( '' ) ;
2022-02-04 11:49:05 -05:00
}