main.go 2.5 KB

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