1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Add cookie to prevent captcha constantly

This commit is contained in:
Marcos Lilljedahl 2017-03-14 14:08:46 -03:00
parent 0770eb689b
commit a875bbe23e
4 changed files with 41 additions and 7 deletions

View File

@ -2,7 +2,7 @@ package config
import "flag" import "flag"
var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName string var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey string
var MaxLoadAvg float64 var MaxLoadAvg float64
func ParseFlags() { func ParseFlags() {
@ -13,6 +13,7 @@ func ParseFlags() {
flag.StringVar(&SessionsFile, "save", "./pwd/sessions", "Tell where to store sessions file") flag.StringVar(&SessionsFile, "save", "./pwd/sessions", "Tell where to store sessions file")
flag.StringVar(&PWDContainerName, "name", "pwd", "Container name used to run PWD (used to be able to connect it to the networks it creates)") flag.StringVar(&PWDContainerName, "name", "pwd", "Container name used to run PWD (used to be able to connect it to the networks it creates)")
flag.StringVar(&PWDCName, "cname", "host1", "CNAME given to this host") flag.StringVar(&PWDCName, "cname", "host1", "CNAME given to this host")
flag.StringVar(&HashKey, "hash_key", "salmonrosado", "Hash key to use for cookies")
flag.Float64Var(&MaxLoadAvg, "maxload", 100, "Maximum allowed load average before failing ping requests") flag.Float64Var(&MaxLoadAvg, "maxload", 100, "Maximum allowed load average before failing ping requests")
flag.Parse() flag.Parse()
} }

View File

@ -17,10 +17,9 @@ type NewSessionResponse struct {
func NewSession(rw http.ResponseWriter, req *http.Request) { func NewSession(rw http.ResponseWriter, req *http.Request) {
req.ParseForm() req.ParseForm()
if !services.IsHuman(req) { if !services.IsHuman(req, rw) {
// User it not a human // User it not a human
rw.WriteHeader(http.StatusConflict) http.Redirect(rw, req, "/", http.StatusFound)
rw.Write([]byte("Only humans are allowed!"))
return return
} }

View File

@ -2,11 +2,17 @@ package services
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time"
"github.com/franela/play-with-docker/config"
"github.com/gorilla/securecookie"
"github.com/twinj/uuid"
) )
func GetGoogleRecaptchaSiteKey() string { func GetGoogleRecaptchaSiteKey() string {
@ -31,10 +37,22 @@ type recaptchaResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
} }
func IsHuman(req *http.Request) bool { var s = securecookie.New([]byte(config.HashKey), nil)
func IsHuman(req *http.Request, rw http.ResponseWriter) bool {
if os.Getenv("GOOGLE_RECAPTCHA_DISABLED") != "" { if os.Getenv("GOOGLE_RECAPTCHA_DISABLED") != "" {
return true return true
} }
if cookie, _ := req.Cookie("session_id"); cookie != nil {
fmt.Println(cookie)
var value string
if err := s.Decode("session_id", cookie.Value, &value); err != nil {
return false
}
return true
}
challenge := req.Form.Get("g-recaptcha-response") challenge := req.Form.Get("g-recaptcha-response")
// Of X-Forwarded-For exists, it means we are behind a loadbalancer and we should use the real IP address of the user // Of X-Forwarded-For exists, it means we are behind a loadbalancer and we should use the real IP address of the user
@ -57,5 +75,16 @@ func IsHuman(req *http.Request) bool {
var r recaptchaResponse var r recaptchaResponse
json.NewDecoder(resp.Body).Decode(&r) json.NewDecoder(resp.Body).Decode(&r)
return r.Success if !r.Success {
return false
}
encoded, _ := s.Encode("session_id", uuid.NewV4().String())
http.SetCookie(rw, &http.Cookie{
Name: "session_id",
Value: encoded,
Expires: time.Now().Add(10 * time.Second),
})
return true
} }

View File

@ -12,8 +12,9 @@
<h1>Welcome!</h1> <h1>Welcome!</h1>
<h2>Before starting we need to verify you are a human</h2> <h2>Before starting we need to verify you are a human</h2>
<form id="welcomeForm" method="POST" action="/"> <form id="welcomeForm" method="POST" action="/">
<div class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div> <div id="recaptcha" class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div>
<input type="hidden" name="session-duration" value="4h"/> <input type="hidden" name="session-duration" value="4h"/>
<button id="create" style="display:none;">Create session</button>
</form> </form>
<img src="/assets/large_h.png" /> <img src="/assets/large_h.png" />
</div> </div>
@ -22,6 +23,10 @@
function iAmHuman(resp) { function iAmHuman(resp) {
document.getElementById('welcomeForm').submit(); document.getElementById('welcomeForm').submit();
} }
if (document.cookie.indexOf('session_id') > -1) {
document.getElementById('create').style = "";
document.getElementById('recaptcha').style = "display:none;";
}
</script> </script>
</body> </body>
</html> </html>