main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "http"
  10. "io"
  11. "log"
  12. "template"
  13. )
  14. var formTemplate = template.MustParse(formTemplateSrc, nil)
  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. JavaScript string
  23. }{
  24. captcha.New(),
  25. formJavaScript,
  26. }
  27. if err := formTemplate.Execute(w, &d); err != nil {
  28. http.Error(w, err.String(), http.StatusInternalServerError)
  29. }
  30. }
  31. func processFormHandler(w http.ResponseWriter, r *http.Request) {
  32. if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
  33. io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
  34. } else {
  35. io.WriteString(w, "Great job, human! You solved the captcha.\n")
  36. }
  37. io.WriteString(w, "<br><a href='/'>Try another one</a>")
  38. }
  39. func main() {
  40. http.HandleFunc("/", showFormHandler)
  41. http.HandleFunc("/process", processFormHandler)
  42. http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
  43. fmt.Println("Server is at localhost:8666")
  44. if err := http.ListenAndServe(":8666", nil); err != nil {
  45. log.Fatal(err)
  46. }
  47. }
  48. const formJavaScript = `
  49. function setSrcQuery(e, q) {
  50. var src = e.src;
  51. var p = src.indexOf('?');
  52. if (p >= 0) {
  53. src = src.substr(0, p);
  54. }
  55. e.src = src + "?" + q
  56. }
  57. function playAudio() {
  58. var le = document.getElementById("lang");
  59. var lang = le.options[le.selectedIndex].value;
  60. var e = document.getElementById('audio')
  61. setSrcQuery(e, "lang=" + lang)
  62. e.style.display = 'block';
  63. e.autoplay = 'true';
  64. return false;
  65. }
  66. function changeLang() {
  67. var e = document.getElementById('audio')
  68. if (e.style.display == 'block') {
  69. playAudio();
  70. }
  71. }
  72. function reload() {
  73. setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
  74. setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
  75. return false;
  76. }
  77. `
  78. const formTemplateSrc = `<!doctype html>
  79. <head><title>Captcha Example</title></head>
  80. <body>
  81. <script>
  82. {JavaScript}
  83. </script>
  84. <select id="lang" onchange="changeLang()">
  85. <option value="en">English</option>
  86. <option value="ru">Russian</option>
  87. </select>
  88. <form action="/process" method=post>
  89. <p>Type the numbers you see in the picture below:</p>
  90. <p><img id=image src="/captcha/{CaptchaId}.png" alt="Captcha image"></p>
  91. <a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
  92. <audio id=audio controls style="display:none" src="/captcha/{CaptchaId}.wav" preload=none>
  93. You browser doesn't support audio.
  94. <a href="/captcha/download/{CaptchaId}.wav">Download file</a> to play it in the external player.
  95. </audio>
  96. <input type=hidden name=captchaId value="{CaptchaId}"><br>
  97. <input name=captchaSolution>
  98. <input type=submit value=Submit>
  99. </form>
  100. `