This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
83 lines (65 loc) · 1.84 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
package main
import (
"fmt"
recaptcha "github.com/dpapathanasiou/go-recaptcha"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"time"
)
var file *os.File
func submit(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println(err)
}
recaptchaResponse := r.FormValue("g-recaptcha-response")
ip := getRealIp(r)
result, err := recaptcha.Confirm(ip, recaptchaResponse)
if err != nil || result != true {
if err != nil {
log.Println("recaptcha server error", err)
}
c := &http.Cookie{Name: "form-submit", Value: "notok"}
http.SetCookie(w, c)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
email := r.FormValue("email")
username := r.FormValue("username")
if email == "" || username == "" {
c := &http.Cookie{Name: "form-submit", Value: "notok"}
http.SetCookie(w, c)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
if _, err := file.WriteString(fmt.Sprintf("%s,%s\n", email, username)); err != nil {
log.Println(err)
}
c := &http.Cookie{Name: "form-submit", Value: "ok"}
http.SetCookie(w, c)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
func main() {
captchaSecret := os.Getenv("RECAPTCHA_SECRET")
listenAddr := os.Getenv("LISTEN_ADDR")
r := mux.NewRouter()
recaptcha.Init(captchaSecret)
var err error
file, err = os.OpenFile("users.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer file.Close()
r.HandleFunc("/submit", submit).Methods("POST")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("src/")))
srv := &http.Server{
Handler: handlers.RecoveryHandler()(handlers.CompressHandler(handlers.LoggingHandler(os.Stdout, r))),
Addr: listenAddr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}