main.go 2.4 KB

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