main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "regexp"
  9. "strings"
  10. )
  11. var (
  12. appRoot string
  13. regexes []*regexp.Regexp
  14. stopWords []string
  15. base64Chars string
  16. hexChars string
  17. opts *Options
  18. assignRegex *regexp.Regexp
  19. )
  20. // RepoElem used for parsing json from github api
  21. type RepoElem struct {
  22. RepoURL string `json:"html_url"`
  23. }
  24. func init() {
  25. var (
  26. err error
  27. )
  28. appRoot, err = os.Getwd()
  29. if err != nil {
  30. log.Fatalf("Can't get working dir: %s", err)
  31. }
  32. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  33. hexChars = "1234567890abcdefABCDEF"
  34. stopWords = []string{"setting", "Setting", "SETTING", "info",
  35. "Info", "INFO", "env", "Env", "ENV", "environment", "Environment", "ENVIRONMENT"}
  36. regexes = []*regexp.Regexp{
  37. regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
  38. regexp.MustCompile("-----BEGIN OPENSSH PRIVATE KEY-----"),
  39. regexp.MustCompile("[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*['|\"][0-9a-f]{32}['|\"]"),
  40. regexp.MustCompile("[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*['|\"][0-9a-zA-Z]{35,44}['|\"]"),
  41. regexp.MustCompile("[g|G][i|I][t|T][h|H][u|U][b|B].*[['|\"]0-9a-zA-Z]{35,40}['|\"]"),
  42. regexp.MustCompile("AKIA[0-9A-Z]{16}"),
  43. regexp.MustCompile("[r|R][e|E][d|D][d|D][i|I][t|T].*['|\"][0-9a-zA-Z]{14}['|\"]"),
  44. regexp.MustCompile("[h|H][e|E][r|R][o|O][k|K][u|U].*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"),
  45. }
  46. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  47. }
  48. func main() {
  49. args := os.Args[1:]
  50. opts = parseOptions(args)
  51. if opts.RepoURL != "" {
  52. start(opts)
  53. } else if opts.UserURL != "" || opts.OrgURL != "" {
  54. repoList := repoScan(opts)
  55. for _, repo := range repoList {
  56. opts.RepoURL = repo.RepoURL
  57. start(opts)
  58. }
  59. }
  60. }
  61. // repoScan attempts to parse all repo urls from an organization or user
  62. func repoScan(opts *Options) []RepoElem {
  63. var (
  64. targetURL string
  65. target string
  66. targetType string
  67. repoList []RepoElem
  68. )
  69. if opts.UserURL != "" {
  70. targetURL = opts.UserURL
  71. targetType = "users"
  72. } else {
  73. targetURL = opts.OrgURL
  74. targetType = "orgs"
  75. }
  76. splitTargetURL := strings.Split(targetURL, "/")
  77. target = splitTargetURL[len(splitTargetURL)-1]
  78. resp, err := http.Get(fmt.Sprintf("https://api.github.com/%s/%s/repos", targetType, target))
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. defer resp.Body.Close()
  83. json.NewDecoder(resp.Body).Decode(&repoList)
  84. return repoList
  85. }