-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
121 lines (105 loc) · 2.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"strings"
"github.com/nicheinc/recaptcha"
)
var (
secretKey = flag.String("secret-key", "", "reCAPTCHA secret key")
siteKey = flag.String("site-key", "", "reCAPTCHA site key")
hostnames = flag.String("hostname", "localhost", "Valid hostnames (comma separated)")
action = flag.String("action", "example", "Action to be associated with reCAPTCHA tokens")
score = flag.Float64("score", 0.5, "Minimum score threshold")
port = flag.Int("port", 80, "Port to run example server on")
client recaptcha.Client
)
func main() {
flag.Parse()
client = recaptcha.NewClient(*secretKey)
http.HandleFunc("/", handler)
http.HandleFunc("/submit", submitHandler)
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
log.Fatalf("Error serving requests: %s\n", err)
}
}
type data struct {
SiteKey string
Action string
}
func handler(w http.ResponseWriter, r *http.Request) {
data := data{
SiteKey: *siteKey,
Action: *action,
}
tmpl, err := template.New("template.html").ParseFiles("./template.html")
if err != nil {
http.Error(w,
fmt.Sprintf("Error parsing html template: %s\n", err),
http.StatusInternalServerError,
)
return
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w,
fmt.Sprintf("Error executing template: %s", err),
http.StatusInternalServerError,
)
return
}
}
func submitHandler(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
if token == "" {
http.Error(w, "Form submission missing 'token' parameter", http.StatusBadRequest)
return
}
userIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w,
fmt.Sprintf("Error parsing remote addr: %s", err),
http.StatusBadRequest,
)
return
}
response, err := client.Fetch(context.Background(), token, userIP)
if err != nil {
http.Error(w,
fmt.Sprintf("Error making request to token verification endpoint: %s", err),
http.StatusInternalServerError,
)
return
}
out, err := json.MarshalIndent(response, "", " ")
if err != nil {
http.Error(w,
fmt.Sprintf("Error marshalling verification endpoint response: %s\n", err),
http.StatusInternalServerError,
)
return
}
var message string
if err := response.Verify(
recaptcha.Hostname(strings.Split(*hostnames, ",")...),
recaptcha.Action(*action),
recaptcha.Score(*score),
); err != nil {
message = fmt.Sprintf("\n\nToken is invalid: %s", err)
} else {
message = fmt.Sprintf("\n\nToken is valid")
}
if _, err := io.WriteString(w, string(out)+message); err != nil {
http.Error(w,
fmt.Sprintf("Error writing response: %s\n", err),
http.StatusInternalServerError,
)
return
}
}