|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "shellboy/pkg/fuzzyhash" |
| 8 | + "shellboy/pkg/walk" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + Version = "0.0.0" |
| 14 | + BuildDate = "" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + currentPath, _ := os.Getwd() |
| 19 | + rootPath := flag.String("directory", currentPath, "Root directory for scan.") |
| 20 | + minScore := flag.Int("score", 98, "Minimum similarity score.") |
| 21 | + minBytes := flag.Int("minbytes", 4000, "Minimum file size (byte). Default 4kb") |
| 22 | + maxBytes := flag.Int("maxbytes", 1000000, "Maximum file size (byte). Default 1Mb") |
| 23 | + excludes := flag.String("excludes", "zip,exe,md,rar,gz", "Excluded file extensions. Empty is disabled") |
| 24 | + includes := flag.String("includes", "", "Included file extensions. Default empty") |
| 25 | + help := flag.Bool("h", false, "Display this help message") |
| 26 | + verbose := flag.Bool("v", false, "Verbose mode") |
| 27 | + flag.Parse() |
| 28 | + |
| 29 | + if *help { |
| 30 | + flag.PrintDefaults() |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + excludeExtensions := strings.Split(*excludes, ",") |
| 35 | + includeExtensions := strings.Split(*includes, ",") |
| 36 | + fhash := fuzzyhash.NewFuzzyHash(*minScore) |
| 37 | + walker := walk.NewWalker(*rootPath, *minBytes, *maxBytes, excludeExtensions, includeExtensions) |
| 38 | + |
| 39 | + fmt.Printf("scanning...") |
| 40 | + err := walker.Scan(checkFileHash(fhash, *verbose)) |
| 41 | + if err != nil { |
| 42 | + fmt.Printf("error: %v\r\n", err) |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Printf("done!\r\n") |
| 46 | +} |
| 47 | + |
| 48 | +func checkFileHash(f *fuzzyhash.FuzzyHash, verbose bool) walk.ScanFunc { |
| 49 | + return func(path string, fileInfo os.FileInfo) error { |
| 50 | + score, name, err := f.FindHash(path) |
| 51 | + if err != nil { |
| 52 | + if verbose { |
| 53 | + fmt.Printf("error: %s, %s\r\n", err, path) |
| 54 | + } |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + if score >= f.GetMinScore() { |
| 59 | + fmt.Printf("score: %d, name: %s, file: %s\r\n", score, name, path) |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | + } |
| 65 | +} |
0 commit comments