aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÖzgür Kesim <oec@codeblau.de>2023-06-20 12:25:33 +0200
committerÖzgür Kesim <oec@codeblau.de>2023-06-20 12:25:33 +0200
commite009cb4234f3fe70cce7dd4c3af921da962ecb52 (patch)
treee80a37330666ee27741631c72b039c67c74ac1a3
first commit
-rw-r--r--README.md0
-rw-r--r--go.mod5
-rw-r--r--go.sum14
-rw-r--r--main.go146
4 files changed, 165 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/README.md
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..cfcb2b0
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module kesim.org/goosebumps
+
+go 1.16
+
+require golang.org/x/mod v0.4.2
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..dc5796a
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,14 @@
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
+golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..a8a4e4e
--- /dev/null
+++ b/main.go
@@ -0,0 +1,146 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "io/fs"
+ "io/ioutil"
+ "log"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+
+ "golang.org/x/mod/modfile"
+)
+
+var (
+ file = flag.String("mod", "go.mod", "go.mod file")
+ cache = flag.String("modcache", getmodcache(), "location of go mod cache")
+ excheckm = flag.String("exempt", "golang.org", "domains exempt from the search, seperated by space")
+
+ checkInit = flag.Bool("ci", false, "check for implementations of init()")
+ checkUnsafe = flag.Bool("cu", false, "check for imports of unsafe")
+ checkCgo = flag.Bool("cc", false, "check for imports of cgo")
+ checkPprof = flag.Bool("cp", false, "check for imports of net/http/pprof")
+
+ exceptions []string
+
+ relcache string
+)
+
+func getmodcache() string {
+ if c := os.Getenv("GOMODCACHE"); c != "" {
+ return c
+ } else if c = os.Getenv("GOPATH"); c != "" {
+ return c + "/pkg/mod"
+ } else {
+ return os.Getenv("HOME") + "/pkg/mod"
+ }
+
+}
+
+func main() {
+ flag.Parse()
+
+ exceptions = strings.Fields(*excheckm)
+ relcache = strings.Replace(*cache, os.Getenv("HOME"), "~", 1)
+
+ if !(*checkInit || *checkUnsafe || *checkCgo || *checkPprof) {
+ fmt.Println("Nothing to check. Use -ci|-cu|-cc|-cp")
+ return
+ }
+
+ data, err := ioutil.ReadFile(*file)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ mf, err := modfile.Parse(*file, data, nil)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var wg sync.WaitGroup
+ wg.Add(len(mf.Require))
+ for _, r := range mf.Require {
+ path := strings.Join(r.Syntax.Token, "@")
+ go func() {
+ defer wg.Done()
+ checkPath(*cache + "/" + path)
+ }()
+ }
+
+ wg.Wait()
+
+}
+
+func isExempt(path string) bool {
+ for _, pattern := range exceptions {
+ if strings.Contains(path, pattern) {
+ return true
+ }
+ }
+ return false
+}
+
+func modpath(filename string) string {
+ return filepath.Join(relcache, strings.TrimPrefix(filename, *cache+"/"))
+}
+
+func checkPath(path string) {
+ var fset = token.NewFileSet()
+
+ filter := func(inf fs.FileInfo) bool {
+ return !strings.HasSuffix(inf.Name(), "_test.go")
+ }
+
+ filepath.WalkDir(path, func(p string, info fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ } else if !info.IsDir() {
+ return nil
+ } else if isExempt(p) {
+ return nil
+ }
+
+ pkgs, err := parser.ParseDir(fset, p, filter, parser.Mode(0))
+ if err != nil {
+ return err
+ }
+
+ for _, pkg := range pkgs {
+ for filename, file := range pkg.Files {
+ if *checkUnsafe || *checkCgo || *checkPprof {
+ for _, imp := range file.Imports {
+ if *checkUnsafe && imp.Path.Value == `"unsafe"` {
+ fmt.Println("unsafe in", modpath(filename))
+ } else if *checkCgo && imp.Path.Value == `"C"` {
+ fmt.Println("cgo in", modpath(filename))
+ } else if *checkPprof && imp.Path.Value == `"net/http/pprof"` {
+ fmt.Println("pprof in", modpath(filename))
+ }
+ }
+ }
+
+ if !*checkInit {
+ break
+ }
+ INIT:
+ for _, decl := range file.Decls {
+ if f, ok := decl.(*ast.FuncDecl); ok {
+ if f.Name.Name == "init" {
+ fmt.Println("init in", modpath(filename))
+ break INIT
+ }
+ }
+ }
+ }
+ }
+
+ return nil
+ })
+}