This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
55 lines (51 loc) · 2 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package trojansourcedetector
import (
"fmt"
"os"
)
// Config is the base configuration structure of the trojan source detector.
type Config struct {
// Directory indicates the directory to scan.
Directory string `json:"directory"`
// Include limits the paths to only include the listed files or directories. Basic pattern matching is supported
// as described in the Go filepath pattern matching docs: https://pkg.go.dev/path/filepath#Match
Include []string `json:"include"`
// Extensions limits the paths to just include the listed extensions.
Extensions []string `json:"extensions"`
// Exclude contains a list of files or paths never to include in the scan. Basic pattern matching is supported as
// described in the Go filepath pattern matching docs: https://pkg.go.dev/path/filepath#Match
Exclude []string `json:"exclude"`
// DetectUnicode will alert if any unicode characters are found. This may cause problems if comments, author
// names, etc. are written in their national language. Use with caution.
DetectUnicode bool `json:"detect_unicode"`
// DetectBIDI will alert if any bidirectional control characters mentioned in the trojan source paper are found.
// See https://trojansource.codes/trojan-source.pdf
DetectBIDI bool `json:"detect_bidi"`
// Parallelism sets how many parallel scans should run.
Parallelism uint `json:"parallelism"`
}
// Defaults adds the default value for each filed.
func (c *Config) Defaults() {
c.Directory = "."
c.DetectBIDI = true
c.Exclude = []string{
".git/*",
".git/*/*",
".git/*/*/*",
}
c.DetectUnicode = false
c.Parallelism = 10
}
// Validate validates the configuration and returns an error if the configuration is invalid.
func (c *Config) Validate() error {
if _, err := os.Stat(c.Directory); err != nil {
return fmt.Errorf("failed to stat directory %s (%w)", c.Directory, err)
}
if !c.DetectBIDI && !c.DetectUnicode {
return fmt.Errorf("no detectors enabled")
}
if c.Parallelism == 0 {
return fmt.Errorf("parallelism can't be zero")
}
return nil
}