/
api.go
76 lines (70 loc) · 1.92 KB
/
api.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
package system
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/aerth/webd/i/captcha"
)
func (s *System) ApiHandler(w http.ResponseWriter, r *http.Request) {
log.Println("api request:", logr(r))
serveJsonError(w, "not implemented", 500)
return
}
type JSONError struct {
Error string `json:"error"`
}
func serveJsonError(w http.ResponseWriter, e string, code int) {
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(JSONError{e}); err != nil {
log.Println(err)
}
}
func getip(r *http.Request) string {
return r.RemoteAddr
}
func (s *System) HandleForm(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
default:
serveJsonError(w, "not implemented", 500)
return
case "/contact", "/contact.json":
if r.Method == http.MethodGet && r.URL.Path == "/contact" {
s.serveTemplate(w, r, "contact.html", nil)
return
}
if r.Method != http.MethodPost {
serveJsonError(w, "not implemented", 500)
return
}
if err := r.ParseForm(); err != nil {
log.Printf("error parsing form: %v", err)
serveJsonError(w, "form parse error", 500)
return
}
if !captcha.VerifyString(r.FormValue("captchaID"), r.FormValue("captchaSol")) {
log.Printf("bad captcha.", r.FormValue("captchaID"), r.FormValue("captchaSol"))
serveJsonError(w, "bad captcha. are you human? go back and refresh the captcha image", 500)
return
}
str := &strings.Builder{}
fmt.Fprintf(str, "```\n")
fmt.Fprintf(str, "time: %s\n", time.Now().UTC().Truncate(time.Second))
fmt.Fprintf(str, "ip: %s\n", getip(r))
fmt.Fprintf(str, "referer: %s\n", r.Referer())
for k, v := range r.Form {
if k == "_csrf" || k == "submit" || k == "captchaID" || k == "captchaSol" {
continue
}
fmt.Fprintf(str, "%s: %s\n", k, v)
}
fmt.Fprintf(str, "```\n")
s.i.tgsend(str.String())
if r.URL.Path == "/contact" { // just give 200 for jsonrpc
http.Redirect(w, r, "/", http.StatusFound)
}
return
}
}