@@ -145,10 +145,10 @@ type ScannerIssue struct {
145145 plugins.Issue
146146}
147147
148- func (s * Scanner ) Scan (ctx context.Context ) ([]ScannerIssue , error ) {
148+ func (s * Scanner ) Scan (ctx context.Context , parallel bool ) ([]ScannerIssue , error ) {
149149 s .mutex .Lock ()
150150 defer s .mutex .Unlock ()
151- result := make ([]ScannerIssue , 0 )
151+ results := make ([]ScannerIssue , 0 )
152152 logger := s .logger
153153
154154 totalProcessing := len (s .plugins ) + 2 // 信息收集和review插件
@@ -182,7 +182,9 @@ func (s *Scanner) Scan(ctx context.Context) ([]ScannerIssue, error) {
182182 }
183183
184184 s .csvResult = append (s .csvResult , []string {"Scan Folder" , s .codePath })
185- for _ , plugin := range s .plugins {
185+ lock := sync.Mutex {}
186+
187+ runPlugin := func (ctx context.Context , plugin plugins.McpPlugin ) ([]ScannerIssue , error ) {
186188 select {
187189 case <- ctx .Done ():
188190 return nil , ctx .Err ()
@@ -202,42 +204,74 @@ func (s *Scanner) Scan(ctx context.Context) ([]ScannerIssue, error) {
202204 }
203205 issues , err := plugin .Check (ctx , & config )
204206 if s .callback != nil {
207+ lock .Lock ()
205208 currentProcessing += 1
206209 s .callback (McpCallbackProcessing {Current : currentProcessing , Total : totalProcessing })
210+ lock .Unlock ()
207211 }
208212 if err != nil {
209213 logger .Warningf ("插件 %s 运行失败: %v" , pluginInfo .Name , err )
210- continue
211214 }
212215 logger .Infof ("插件 %s 运行成功" , pluginInfo .Name )
213216 logger .Infof ("共发现 %d 个问题" , len (issues ))
214217 logger .Infof ("插件 %s 运行时间: %v 消耗token:%d" , pluginInfo .Name , time .Since (startTime ).String (), s .aiModel .GetTotalToken ())
218+ lock .Lock ()
215219 s .csvResult = append (s .csvResult , []string {"PluginId" , pluginInfo .ID , "PluginName" , pluginInfo .Name , "UseToken" , strconv .Itoa (int (s .aiModel .GetTotalToken ())), "time" , time .Since (startTime ).String ()})
220+ lock .Unlock ()
216221 // 转换插件结果
222+ var result []ScannerIssue
217223 for _ , res := range issues {
218224 res2 := res
219225 r := ScannerIssue {
220226 PluginId : pluginInfo .ID ,
221227 Issue : res2 ,
222228 }
223229 if s .callback != nil {
230+ lock .Lock ()
224231 s .callback (r )
232+ lock .Unlock ()
225233 }
226234 result = append (result , r )
227235 }
236+ return result , nil
237+ }
238+ wg := sync.WaitGroup {}
239+ for _ , plugin := range s .plugins {
240+ if parallel {
241+ wg .Add (1 )
242+ go func (plugin plugins.McpPlugin ) {
243+ defer wg .Done ()
244+ result , err := runPlugin (ctx , plugin )
245+ if err != nil {
246+ gologger .WithError (err ).Errorln ("插件运行失败" )
247+ }
248+ lock .Lock ()
249+ results = append (results , result ... )
250+ lock .Unlock ()
251+ }(plugin )
252+ } else {
253+ result , err := runPlugin (ctx , plugin )
254+ if err != nil {
255+ gologger .WithError (err ).Errorln ("插件运行失败" )
256+ }
257+ lock .Lock ()
258+ results = append (results , result ... )
259+ lock .Unlock ()
260+ }
228261 }
262+ wg .Wait ()
229263 defer func () {
230264 // 最后的review插件
231265 if s .callback != nil {
232266 currentProcessing += 1
233267 s .callback (McpCallbackProcessing {Current : currentProcessing , Total : totalProcessing })
234268 }
235269 }()
236- if len (result ) > 0 {
270+ if len (results ) > 0 {
237271 // vuln review
238- logger .Infof ("当前漏洞数量:%d 开始进行漏洞review..." , len (result ))
272+ logger .Infof ("当前漏洞数量:%d 开始进行漏洞review..." , len (results ))
239273 origin := strings.Builder {}
240- for _ , res := range result {
274+ for _ , res := range results {
241275 origin .WriteString ("<result>" )
242276 origin .WriteString ("<title>" + res .Title + "</title>" )
243277 origin .WriteString ("<desc>" + res .Description + "</desc>" )
@@ -247,7 +281,7 @@ func (s *Scanner) Scan(ctx context.Context) ([]ScannerIssue, error) {
247281 origin .WriteString ("</result>" )
248282 origin .WriteString ("\n \n " )
249283 }
250- result = make ([]ScannerIssue , 0 )
284+ results = make ([]ScannerIssue , 0 )
251285 reviewPlugin := plugins .VulnReviewPlugin (origin .String ())
252286 issues , err := reviewPlugin .Check (context .Background (), & plugins.McpPluginConfig {
253287 Client : s .client ,
@@ -271,11 +305,11 @@ func (s *Scanner) Scan(ctx context.Context) ([]ScannerIssue, error) {
271305 if s .callback != nil {
272306 s .callback (r )
273307 }
274- result = append (result , r )
308+ results = append (results , r )
275309 }
276310 }
277311 }
278- return result , nil
312+ return results , nil
279313}
280314
281315func (s * Scanner ) GetCsvResult () [][]string {
0 commit comments