Skip to content

Commit

Permalink
WIP: feat: exclude file (ariary#3)
Browse files Browse the repository at this point in the history
Specify an exclude file with -e or --exclude
  • Loading branch information
jensschulze committed Nov 11, 2021
1 parent 96d2028 commit 451db77
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/tsfinder/main.go
Expand Up @@ -25,7 +25,7 @@ func main() {
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
cfg := &config.Config{Verbose: verbose, Color: color, Sibling: &sibling, OnlyText: onlyText, Exclude: exclude}
cfg := &config.Config{Verbose: verbose, Color: color, Sibling: &sibling, OnlyText: onlyText, ExcludelistFilename: exclude}

homoglyph.Scan(path, cfg)
},
Expand All @@ -41,7 +41,7 @@ func main() {
Run: func(cmd *cobra.Command, args []string) {

name := args[0]
cfg := &config.Config{Verbose: verbose, Color: color, OnlyText: onlyText, Exclude: exclude}
cfg := &config.Config{Verbose: verbose, Color: color, OnlyText: onlyText, ExcludelistFilename: exclude}

bidirectional.Scan(name, cfg)
},
Expand All @@ -51,7 +51,7 @@ func main() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "make tsfinder more verbose")
rootCmd.PersistentFlags().BoolVarP(&color, "color", "c", false, "make tsfinder print with color")
rootCmd.PersistentFlags().BoolVarP(&onlyText, "text-file", "t", false, "make tsfinder scan only on 'human readable' file (ie looks like correct UTF-8). Add verbosity (-v) to see which files has been skipped. This could help to rule out false positives")
rootCmd.PersistentFlags().StringVarP(&exclude, "exclude", "e", "", "specify an exclude file containing one path per line to be excluded from scanning")
rootCmd.PersistentFlags().StringVarP(&exclude, "exclude", "e", "", "specify an exclude file containing one path per line to be excluded from scanning. The paths are relative to the working directory.")

rootCmd.AddCommand(cmdSHomoglyph)
rootCmd.Execute()
Expand Down
30 changes: 26 additions & 4 deletions pkg/bidirectional/bidirectionnal.go
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/tools/godoc/vfs"

"github.com/ariary/TrojanSourceFinder/pkg/config"
"github.com/ariary/TrojanSourceFinder/pkg/excludelist"
"github.com/ariary/TrojanSourceFinder/pkg/utils"
)

Expand Down Expand Up @@ -113,9 +114,19 @@ func Scan(path string, cfg *config.Config) {
log.Fatal(err)
}

excludedPaths, err := excludelist.GetExcludelist(*&cfg.ExcludelistFilename)
if err != nil {
log.Fatal(err)
}

// Skip the given path if it is contained in the exclude list.
if _, ignoreEntry := (*excludedPaths)[filepath.Clean(path)]; ignoreEntry {
os.Exit(0)
}

var detected int
if fileInfo.IsDir() {
detected = scanDirectory(path, cfg)
detected = scanDirectory(path, cfg, excludedPaths)
} else {
detected = scanFile(path, cfg)
}
Expand Down Expand Up @@ -216,13 +227,24 @@ func scanFile(filename string, cfg *config.Config) int {
// Browse the directory using filepath.Walk package => does not follow symbolic link
// and for very large directories Walk can be inefficient
// return 0 if no trojan source was detected
func scanDirectory(pathD string, cfg *config.Config) (result int) {
err := filepath.Walk(pathD, func(path string, info os.FileInfo, err error) error {
func scanDirectory(pathD string, cfg *config.Config, excludedPaths *map[string]struct{}) (result int) {
err := filepath.WalkDir(pathD, func(path string, dirEntry os.DirEntry, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if !info.IsDir() {

if dirEntry.IsDir() {
// Skip the whole directory if it is contained in the exclude list.
if _, ignoreEntry := (*excludedPaths)[path]; ignoreEntry {
return filepath.SkipDir
}
} else {
// Skip the file if it is contained in the exclude list.
if _, ignoreEntry := (*excludedPaths)[path]; ignoreEntry {
return nil
}

result += scanFile(path, cfg)
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config.go
Expand Up @@ -2,9 +2,9 @@ package config

// Config holds the tsfinder configuration
type Config struct {
Verbose bool
Color bool
Sibling *[]string
OnlyText bool
Exclude string
Verbose bool
Color bool
Sibling *[]string
OnlyText bool
ExcludelistFilename string
}
11 changes: 9 additions & 2 deletions excludelist/excludelist.go → pkg/excludelist/excludelist.go
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)

func GetExcludelist(excludelistFilename string) (*map[string]struct{}, error) {
Expand All @@ -18,7 +20,7 @@ func GetExcludelist(excludelistFilename string) (*map[string]struct{}, error) {
}

if excludelistFile.IsDir() {
return nil, fmt.Errorf("'%s' must not be a directory", excludelistFilename)
return nil, fmt.Errorf("excludefile %s must not be a directory", excludelistFilename)
}

readfile, err := os.Open(excludelistFilename)
Expand All @@ -29,7 +31,12 @@ func GetExcludelist(excludelistFilename string) (*map[string]struct{}, error) {

scanner := bufio.NewScanner(readfile)
for scanner.Scan() {
excludelist[scanner.Text()] = struct{}{}
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}

excludelist[filepath.Clean(line)] = struct{}{}
}

return &excludelist, scanner.Err()
Expand Down

0 comments on commit 451db77

Please sign in to comment.