| 1 |  |  | package scan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	"fmt" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	"io" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	"net/http" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	"sort" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	"strings" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	"sync" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	gotree "github.com/DiSiqueira/GoTree" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | func NewResultSummarizer(out io.Writer) *ResultSummarizer { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	return &ResultSummarizer{out: out} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | type ResultSummarizer struct { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	out             io.Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	results         []Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	resultsReceived int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	mux             sync.RWMutex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | func (s *ResultSummarizer) Add(result Result) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	s.mux.Lock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	defer s.mux.Unlock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	s.resultsReceived++ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	if result.Response.StatusCode == http.StatusNotFound { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	s.results = append(s.results, result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | func (s *ResultSummarizer) Summarize() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	s.mux.Lock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	defer s.mux.Unlock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	s.printSummary() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	s.printTree() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	for _, r := range s.results { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		fmt.Fprintln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			s.out, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			fmt.Sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 				"%s [%d] [%s]", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 				r.Response.Request.URL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				r.Response.StatusCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				r.Response.Request.Method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | func (s *ResultSummarizer) printSummary() { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	fmt.Fprintln( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 		s.out, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		fmt.Sprintf("%d requests made, %d results found", s.resultsReceived, len(s.results)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | func (s *ResultSummarizer) printTree() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	root := gotree.New("/") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	sort.Slice(s.results, func(i, j int) bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return s.results[i].Target.Path > s.results[j].Target.Path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	// TODO: improve efficiency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	for _, r := range s.results { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		currentBranch := root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		parts := strings.Split(r.Response.Request.URL.Path, "/") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		for _, p := range parts { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			if len(p) == 0 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 				continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			found := false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			for _, item := range currentBranch.Items() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				if item.Text() != p { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 					continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				currentBranch = item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				found = true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			if found { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			newTree := gotree.New(p) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			currentBranch.AddTree(newTree) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			currentBranch = newTree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	fmt.Fprintln(s.out, root.Print()) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |