main.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 map[string]*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 = map[string]*regexp.Regexp{
  37. "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"),
  38. "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
  39. "SSH": regexp.MustCompile("-----BEGIN OPENSSH PRIVATE KEY-----"),
  40. "Facebook": regexp.MustCompile("(?i)facebook.*['|\"][0-9a-f]{32}['|\"]"),
  41. "Twitter": regexp.MustCompile("(?i)twitter.*['|\"][0-9a-zA-Z]{35,44}['|\"]"),
  42. "Github": regexp.MustCompile("(?i)github.*[['|\"]0-9a-zA-Z]{35,40}['|\"]"),
  43. "AWS": regexp.MustCompile("AKIA[0-9A-Z]{16}"),
  44. "Reddit": regexp.MustCompile("(?i)reddit.*['|\"][0-9a-zA-Z]{14}['|\"]"),
  45. "Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"),
  46. // "Custom": regexp.MustCompile(".*")
  47. }
  48. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  49. }
  50. func main() {
  51. args := os.Args[1:]
  52. opts = parseOptions(args)
  53. if opts.RepoURL != "" {
  54. start(opts)
  55. } else if opts.UserURL != "" || opts.OrgURL != "" {
  56. repoList := repoScan(opts)
  57. for _, repo := range repoList {
  58. opts.RepoURL = repo.RepoURL
  59. start(opts)
  60. }
  61. }
  62. }
  63. // repoScan attempts to parse all repo urls from an organization or user
  64. func repoScan(opts *Options) []RepoElem {
  65. var (
  66. targetURL string
  67. target string
  68. targetType string
  69. repoList []RepoElem
  70. )
  71. if opts.UserURL != "" {
  72. targetURL = opts.UserURL
  73. targetType = "users"
  74. } else {
  75. targetURL = opts.OrgURL
  76. targetType = "orgs"
  77. }
  78. splitTargetURL := strings.Split(targetURL, "/")
  79. target = splitTargetURL[len(splitTargetURL)-1]
  80. resp, err := http.Get(fmt.Sprintf("https://api.github.com/%s/%s/repos", targetType, target))
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. defer resp.Body.Close()
  85. json.NewDecoder(resp.Body).Decode(&repoList)
  86. return repoList
  87. }