main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2011 Dmitry Chestnykh. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // example of HTTP server that uses the captcha package.
  5. package main
  6. import (
  7. "fmt"
  8. "github.com/dchest/captcha"
  9. "io"
  10. "log"
  11. "net/http"
  12. "text/template"
  13. )
  14. var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))
  15. func showFormHandler(w http.ResponseWriter, r *http.Request) {
  16. if r.URL.Path != "/" {
  17. http.NotFound(w, r)
  18. return
  19. }
  20. d := struct {
  21. CaptchaId string
  22. }{
  23. captcha.New(),
  24. }
  25. if err := formTemplate.Execute(w, &d); err != nil {
  26. http.Error(w, err.Error(), http.StatusInternalServerError)
  27. }
  28. }
  29. func processFormHandler(w http.ResponseWriter, r *http.Request) {
  30. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  31. if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
  32. io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
  33. } else {
  34. io.WriteString(w, "Great job, human! You solved the captcha.\n")
  35. }
  36. io.WriteString(w, "<br><a href='/'>Try another one</a>")
  37. }
  38. func main() {
  39. http.HandleFunc("/", showFormHandler)
  40. http.HandleFunc("/process", processFormHandler)
  41. http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
  42. fmt.Println("Server is at localhost:8666")
  43. if err := http.ListenAndServe("localhost:8666", nil); err != nil {
  44. log.Fatal(err)
  45. }
  46. }
  47. const formTemplateSrc = `<!doctype html>
  48. <head><title>Captcha Example</title></head>
  49. <body>
  50. <script>
  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. function playAudio() {
  60. var le = document.getElementById("lang");
  61. var lang = le.options[le.selectedIndex].value;
  62. var e = document.getElementById('audio')
  63. setSrcQuery(e, "lang=" + lang)
  64. e.style.display = 'block';
  65. e.autoplay = 'true';
  66. return false;
  67. }
  68. function changeLang() {
  69. var e = document.getElementById('audio')
  70. if (e.style.display == 'block') {
  71. playAudio();
  72. }
  73. }
  74. function reload() {
  75. setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
  76. setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
  77. return false;
  78. }
  79. </script>
  80. <select id="lang" onchange="changeLang()">
  81. <option value="en">English</option>
  82. <option value="ja">Japanese</option>
  83. <option value="ru">Russian</option>
  84. <option value="zh">Chinese</option>
  85. </select>
  86. <form action="/process" method=post>
  87. <p>Type the numbers you see in the picture below:</p>
  88. <p><img id=image src="/captcha/{{.CaptchaId}}.png" alt="Captcha image"></p>
  89. <a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
  90. <audio id=audio controls style="display:none" src="/captcha/{{.CaptchaId}}.wav" preload=none>
  91. You browser doesn't support audio.
  92. <a href="/captcha/download/{{.CaptchaId}}.wav">Download file</a> to play it in the external player.
  93. </audio>
  94. <input type=hidden name=captchaId value="{{.CaptchaId}}"><br>
  95. <input name=captchaSolution>
  96. <input type=submit value=Submit>
  97. </form>
  98. `