@@ -579,6 +579,74 @@ func runNucleiCommand(targetFile, templateDir string, threads int, outputFile st
579579 return err
580580}
581581
582+ // RunGlobalTemplate executes a specific nuclei template against a list of targets using the SDK.
583+ func RunGlobalTemplate (targetFile , templatePath , outPath string , threads int , onResult func (event * nucleiOutput.ResultEvent )) error {
584+ targets , err := readTargetLines (targetFile )
585+ if err != nil {
586+ return fmt .Errorf ("failed to read targets: %w" , err )
587+ }
588+ if len (targets ) == 0 {
589+ return fmt .Errorf ("no targets found in %s" , targetFile )
590+ }
591+ if err := utils .EnsureDir (filepath .Dir (outPath )); err != nil {
592+ return fmt .Errorf ("failed to ensure output dir: %w" , err )
593+ }
594+ fh , err := os .Create (outPath )
595+ if err != nil {
596+ return fmt .Errorf ("failed to create output file: %w" , err )
597+ }
598+ defer fh .Close ()
599+
600+ jsonWriter , err := nucleiOutput .NewWriter (
601+ nucleiOutput .WithWriter (fh ),
602+ nucleiOutput .WithJson (true , false ),
603+ )
604+ if err != nil {
605+ return fmt .Errorf ("failed to initialize JSON writer: %w" , err )
606+ }
607+ defer jsonWriter .Close ()
608+
609+ engine , err := nucleiSDK .NewNucleiEngineCtx (
610+ context .Background (),
611+ nucleiSDK .DisableUpdateCheck (),
612+ nucleiSDK .WithVerbosity (nucleiSDK.VerbosityOptions {Silent : true }),
613+ nucleiSDK .WithTemplatesOrWorkflows (nucleiSDK.TemplateSources {
614+ Templates : []string {templatePath },
615+ }),
616+ nucleiSDK .WithConcurrency (nucleiSDK.Concurrency {
617+ TemplateConcurrency : max (1 , min (threads , 25 )),
618+ HostConcurrency : max (1 , threads ),
619+ HeadlessHostConcurrency : max (1 , min (threads , 10 )),
620+ HeadlessTemplateConcurrency : max (1 , min (threads , 10 )),
621+ JavascriptTemplateConcurrency : max (1 , min (threads , 10 )),
622+ TemplatePayloadConcurrency : 25 ,
623+ ProbeConcurrency : max (1 , min (threads , 50 )),
624+ }),
625+ )
626+ if err != nil {
627+ return fmt .Errorf ("failed to initialize SDK: %w" , err )
628+ }
629+ defer engine .Close ()
630+
631+ engine .LoadTargets (targets , false )
632+ writeErr := error (nil )
633+ err = engine .ExecuteWithCallback (func (event * nucleiOutput.ResultEvent ) {
634+ if event == nil || writeErr != nil {
635+ return
636+ }
637+ if wErr := jsonWriter .Write (event ); wErr != nil {
638+ writeErr = wErr
639+ }
640+ if onResult != nil {
641+ onResult (event )
642+ }
643+ })
644+ if writeErr != nil {
645+ return writeErr
646+ }
647+ return err
648+ }
649+
582650func extractDomainFromURL (url string ) string {
583651 // Simple extraction: remove http:// or https://, then take first part before /
584652 url = strings .TrimPrefix (url , "http://" )
0 commit comments