-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-api.go
239 lines (188 loc) · 5.11 KB
/
form-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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
)
type Response struct {
Response string `json:"response"`
}
type Post struct {
Success bool `json:"success"`
}
type Request struct {
Email string `json:"email"`
Name string `json:"name"`
Message string `json:"message"`
Captcha string `json:"frc-captcha-solution"`
}
type GForm struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
File string `json:"file"`
}
func getUrl() string {
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TELEGRAM_TOKEN"))
}
func SendMessage(text string) (bool, error) {
var err error
var response *http.Response
url := fmt.Sprintf("%s/sendMessage", getUrl())
body, err := json.Marshal(map[string]string{
"chat_id": os.Getenv("CHAT_ID"),
"text": text,
})
if err != nil {
return false, err
}
response, err = http.Post(
url,
"application/json",
bytes.NewBuffer(body),
)
if err != nil {
return false, err
}
defer response.Body.Close()
return true, nil
}
func handler_get(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 100)
fmt.Fprintf(w, "Ciao da %s\nList of IP addresses received (including yours, if visible):\n%s\n", r.Host, r.Header.Get("X-Forwarded-For"))
}
func handler_sheet(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 5*1024)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
content_type, err := regexp.MatchString("application/json", r.Header.Get("Content-Type"))
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if !content_type {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var data GForm
err = json.Unmarshal(body, &data)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
text := fmt.Sprintf("New message from Google Form!\n\nName:\t%s\n\nE-mail:\t%s\n\nMessage:\t%s\n\nFile link:\t%s", data.Name, data.Email, data.Message, data.File)
result, err := SendMessage(text)
if !result {
log.Printf("Error sending telegram messag, %s\n", err)
w.WriteHeader(http.StatusNotAcceptable)
} else {
w.WriteHeader(http.StatusOK)
}
}
func handler_post(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 50*1024)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
content_type, err := regexp.MatchString("multipart/form-data", r.Header.Get("Content-Type"))
if err != nil {
log.Println(err)
}
log.Printf("Content-Type is multipart/form-data? %v\n", content_type)
if content_type {
err = r.ParseMultipartForm(1000)
} else {
err = r.ParseForm()
}
if err != nil {
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
log.Fatal(err)
}
req := Request{
Email: r.FormValue("email"),
Name: r.FormValue("name"),
Message: r.FormValue("message"),
Captcha: r.FormValue("frc-captcha-solution"),
}
//log.Println(req)
data := url.Values{}
data.Add("solution", req.Captcha)
data.Add("secret", os.Getenv("CAPTCHA_SECRET"))
data.Add("sitekey", os.Getenv("CAPTCHA_SITEKEY"))
posturl := "https://api.friendlycaptcha.com/api/v1/siteverify"
resp, err := http.PostForm(posturl, data)
if err != nil {
log.Printf("Request Failed: %s", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Formatting body failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
post := Post{}
err = json.Unmarshal(body, &post)
if err != nil {
log.Printf("Reading body failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
//log.Printf("Captcha validation: %v", post.Success)
var ok string
if post.Success {
text := fmt.Sprintf("New message from HTML form!\n\nName:\t%s\n\nE-mail:\t%s\n\nMessage:\t%s\n", req.Name, req.Email, req.Message)
result, err := SendMessage(text)
if !result {
log.Printf("Error sending telegram messag, %s\n", err)
w.WriteHeader(http.StatusNotAcceptable)
ok = "Valid CAPTCHA but error on Telegram request"
} else {
w.WriteHeader(http.StatusOK)
ok = "Valid CAPTCHA and Telegram sent"
}
} else {
ok = "CAPTCHA validation failed"
w.WriteHeader(http.StatusNotAcceptable)
}
response := Response{ok}
res, err := json.Marshal(response)
if err != nil {
log.Println(err)
}
//log.Println(string(res))
w.Header().Set("Content-Type", "application/json")
w.Write(res)
}
func main() {
http.HandleFunc("/", handler_get)
http.HandleFunc("/post", handler_post)
http.HandleFunc("/sheet", handler_sheet)
log.Fatal(http.ListenAndServe(":8080", nil))
}