A simple way to handle form submissions from static websites.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

  1. package main
  2. import (
  3. "github.com/google/safebrowsing"
  4. "net/url"
  5. "strings"
  6. )
  7. // Returns true when it spam
  8. func checkValues(values FormValues) bool {
  9. var urlsToCheck []string
  10. for _, value := range values {
  11. for _, singleValue := range value {
  12. if strings.Contains(singleValue, "http") {
  13. parsed, e := url.Parse(singleValue)
  14. if parsed != nil && e == nil {
  15. urlsToCheck = append(urlsToCheck, singleValue)
  16. }
  17. }
  18. }
  19. }
  20. return checkUrls(urlsToCheck)
  21. }
  22. // Only tests when GOOGLE_API_KEY is set
  23. // Returns true when it spam
  24. func checkUrls(urlsToCheck []string) bool {
  25. if len(appConfig.GoogleApiKey) < 1 || len(urlsToCheck) == 0 {
  26. return false
  27. }
  28. sb, err := safebrowsing.NewSafeBrowser(safebrowsing.Config{
  29. APIKey: appConfig.GoogleApiKey,
  30. ID: "MailyGo",
  31. })
  32. if err != nil {
  33. return false
  34. }
  35. allThreats, err := sb.LookupURLs(urlsToCheck)
  36. if err != nil {
  37. return false
  38. }
  39. for _, threats := range allThreats {
  40. if len(threats) > 0 {
  41. // Unsafe url, mark as spam
  42. return true
  43. }
  44. }
  45. return false
  46. }