main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // example of HTTP server that uses the captcha package.
  2. package main
  3. import (
  4. "github.com/dchest/captcha"
  5. "http"
  6. "io"
  7. "log"
  8. "template"
  9. )
  10. var formTemplate = template.MustParse(formTemplateSrc, nil)
  11. func showFormHandler(w http.ResponseWriter, r *http.Request) {
  12. if r.URL.Path != "/" {
  13. http.NotFound(w, r)
  14. return
  15. }
  16. d := struct {
  17. CaptchaId string
  18. JavaScript string
  19. }{
  20. captcha.New(captcha.StdLength),
  21. formJavaScript,
  22. }
  23. if err := formTemplate.Execute(w, &d); err != nil {
  24. http.Error(w, err.String(), http.StatusInternalServerError)
  25. }
  26. }
  27. func processFormHandler(w http.ResponseWriter, r *http.Request) {
  28. if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
  29. io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
  30. } else {
  31. io.WriteString(w, "Great job, human! You solved the captcha.\n")
  32. }
  33. io.WriteString(w, "<br><a href='/'>Try another one</a>")
  34. }
  35. func main() {
  36. http.HandleFunc("/", showFormHandler)
  37. http.HandleFunc("/process", processFormHandler)
  38. http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
  39. if err := http.ListenAndServe(":8080", nil); err != nil {
  40. log.Fatal(err)
  41. }
  42. }
  43. const formJavaScript = `
  44. function playAudio() {
  45. var e = document.getElementById('audio')
  46. e.style.display = 'block';
  47. e.play();
  48. return false;
  49. }
  50. function reload() {
  51. function setSrcQuery(e, q) {
  52. var src = e.src;
  53. var p = src.indexOf('?');
  54. if (p >= 0) {
  55. src = src.substr(0, p);
  56. }
  57. e.src = src + "?" + q
  58. }
  59. setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
  60. setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
  61. return false;
  62. }
  63. `
  64. const formTemplateSrc = `<!doctype html>
  65. <head><title>Captcha Example</title></head>
  66. <body>
  67. <script>
  68. {JavaScript}
  69. </script>
  70. <form action="/process" method=post>
  71. <p>Type the numbers you see in the picture below:</p>
  72. <p><img id=image src="/captcha/{CaptchaId}.png" alt="Captcha image"></p>
  73. <a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
  74. <audio id=audio controls style="display:none" src="/captcha/{CaptchaId}.wav" preload=none>
  75. You browser doesn't support audio.
  76. <a href="/captcha/download/{CaptchaId}.wav">Download file</a> to play it in the external player.
  77. </audio>
  78. <input type=hidden name=captchaId value="{CaptchaId}"><br>
  79. <input name=captchaSolution>
  80. <input type=submit value=Submit>
  81. </form>
  82. `