-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
778 lines (699 loc) · 20.7 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package main
import (
"bufio"
"encoding/base64"
"encoding/csv"
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
"net/smtp"
"net/url"
"os"
"strconv"
"strings"
"text/template"
"time"
"unicode/utf8"
"github.com/skip2/go-qrcode"
)
var ScamThreshold float64 = 0.005 // MINIMUM DONATION AMOUNT
var MediaMin float64 = 0.025 // Currently unused
var MessageMaxChar int = 250
var NameMaxChar int = 25
var rpcURL string = "http://127.0.0.1:28088/json_rpc"
var username string = "admin" // chat log /view page
var AlertWidgetRefreshInterval string = "10" //seconds
// this is the password for both the /view page and the OBS /alert page
// example OBS url: https://example.com/alert?auth=adminadmin
var password string = "adminadmin"
var checked string = ""
// Email settings
var enableEmail bool = false
var smtpHost string = "smtp.purelymail.com"
var smtpPort string = "587"
var smtpUser string = "example@purelymail.com"
var smtpPass string = "[y7EQ(xgTW_~{CUpPhO6(#"
var sendTo = []string{"example@purelymail.com"} // Comma separated recipient list
var indexTemplate *template.Template
var payTemplate *template.Template
var checkTemplate *template.Template
var alertTemplate *template.Template
var viewTemplate *template.Template
var topWidgetTemplate *template.Template
type configJson struct {
MinimumDonation float64 `json:"MinimumDonation"`
MaxMessageChars int `json:"MaxMessageChars"`
MaxNameChars int `json:"MaxNameChars"`
RPCWalletURL string `json:"RPCWalletURL"`
WebViewUsername string `json:"WebViewUsername"`
WebViewPassword string `json:"WebViewPassword"`
OBSWidgetRefresh string `json:"OBSWidgetRefresh"`
Checked bool `json:"ShowAmountCheckedByDefault"`
EnableEmail bool `json:"EnableEmail"`
SMTPServer string `json:"SMTPServer"`
SMTPPort string `json:"SMTPPort"`
SMTPUser string `json:"SMTPUser"`
SMTPPass string `json:"SMTPPass"`
SendToEmail []string `json:"SendToEmail"`
}
type checkPage struct {
Addy string
PayID string
Received float64
Meta string
Name string
Msg string
Receipt string
Media string
}
type superChat struct {
Name string
Message string
Media string
Amount string
Address string
QRB64 string
PayID string
CheckURL string
}
type csvLog struct {
ID string
Name string
Message string
Amount string
DisplayToggle string
Refresh string
}
type indexDisplay struct {
MaxChar int
MinAmnt float64
Checked string
}
type viewPageData struct {
ID []string
Name []string
Message []string
Amount []string
Display []string
}
type rpcResponse struct {
ID string `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result struct {
IntegratedAddress string `json:"integrated_address"`
PaymentID string `json:"payment_id"`
} `json:"result"`
}
type getAddress struct {
ID string `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result struct {
Address string `json:"address"`
Addresses []struct {
Address string `json:"address"`
AddressIndex int `json:"address_index"`
Label string `json:"label"`
Used bool `json:"used"`
} `json:"addresses"`
} `json:"result"`
}
type MoneroPrice struct {
Monero struct {
Usd float64 `json:"usd"`
} `json:"monero"`
}
type GetTransfersResponse struct {
ID string `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result struct {
In []struct {
Address string `json:"address"`
Amount int64 `json:"amount"`
Amounts []int64 `json:"amounts"`
Confirmations int `json:"confirmations"`
DoubleSpendSeen bool `json:"double_spend_seen"`
Fee int `json:"fee"`
Height int `json:"height"`
Locked bool `json:"locked"`
Note string `json:"note"`
PaymentID string `json:"payment_id"`
SubaddrIndex struct {
Major int `json:"major"`
Minor int `json:"minor"`
} `json:"subaddr_index"`
SubaddrIndices []struct {
Major int `json:"major"`
Minor int `json:"minor"`
} `json:"subaddr_indices"`
SuggestedConfirmationsThreshold int `json:"suggested_confirmations_threshold"`
Timestamp int `json:"timestamp"`
Txid string `json:"txid"`
Type string `json:"type"`
UnlockTime int `json:"unlock_time"`
} `json:"in"`
Pool []struct {
Address string `json:"address"`
Amount int64 `json:"amount"`
Amounts []int64 `json:"amounts"`
DoubleSpendSeen bool `json:"double_spend_seen"`
Fee int `json:"fee"`
Height int `json:"height"`
Locked bool `json:"locked"`
Note string `json:"note"`
PaymentID string `json:"payment_id"`
SubaddrIndex struct {
Major int `json:"major"`
Minor int `json:"minor"`
} `json:"subaddr_index"`
SubaddrIndices []struct {
Major int `json:"major"`
Minor int `json:"minor"`
} `json:"subaddr_indices"`
SuggestedConfirmationsThreshold int `json:"suggested_confirmations_threshold"`
Timestamp int `json:"timestamp"`
Txid string `json:"txid"`
Type string `json:"type"`
UnlockTime int `json:"unlock_time"`
} `json:"pool"`
} `json:"result"`
}
func main() {
jsonFile, err := os.Open("config.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("reading config.json")
defer func(jsonFile *os.File) {
err := jsonFile.Close()
if err != nil {
fmt.Println(err)
}
}(jsonFile)
byteValue, _ := io.ReadAll(jsonFile)
var conf configJson
err = json.Unmarshal(byteValue, &conf)
if err != nil {
panic(err) // Fatal error, stop program
}
ScamThreshold = conf.MinimumDonation
MessageMaxChar = conf.MaxMessageChars
NameMaxChar = conf.MaxNameChars
rpcURL = conf.RPCWalletURL
username = conf.WebViewUsername
password = conf.WebViewPassword
AlertWidgetRefreshInterval = conf.OBSWidgetRefresh
enableEmail = conf.EnableEmail
smtpHost = conf.SMTPServer
smtpPort = conf.SMTPPort
smtpUser = conf.SMTPUser
smtpPass = conf.SMTPPass
sendTo = conf.SendToEmail
if conf.Checked == true {
checked = " checked"
}
fmt.Println(fmt.Sprintf("email notifications enabled?: %t", enableEmail))
fmt.Println(fmt.Sprintf("OBS Alert path: /alert?auth=%s", password))
http.HandleFunc("/style.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/style.css")
})
http.HandleFunc("/xmr.svg", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/xmr.svg")
})
http.HandleFunc("/", indexHandler)
http.HandleFunc("/pay", paymentHandler)
http.HandleFunc("/check", checkHandler)
http.HandleFunc("/alert", alertHandler)
http.HandleFunc("/view", viewHandler)
http.HandleFunc("/top", topwidgetHandler)
// Create files if they don't exist
_, err = os.OpenFile("log/paid.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
_, err = os.OpenFile("log/alertqueue.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
_, err = os.OpenFile("log/superchats.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
indexTemplate, _ = template.ParseFiles("web/index.html")
payTemplate, _ = template.ParseFiles("web/pay.html")
checkTemplate, _ = template.ParseFiles("web/check.html")
alertTemplate, _ = template.ParseFiles("web/alert.html")
viewTemplate, _ = template.ParseFiles("web/view.html")
topWidgetTemplate, _ = template.ParseFiles("web/top.html")
err = http.ListenAndServe(":8900", nil)
if err != nil {
panic(err)
}
}
func mail(name string, amount string, message string) {
body := []byte(fmt.Sprintf("From: %s\n"+
"Subject: %s sent %s XMR\nDate: %s\n\n"+
"%s", smtpUser, name, amount, fmt.Sprint(time.Now().Format(time.RFC1123Z)), message))
auth := smtp.PlainAuth("", smtpUser, smtpPass, smtpHost)
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, smtpUser, sendTo, body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("email sent")
}
func condenseSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func truncateStrings(s string, n int) string {
if len(s) <= n {
return s
}
for !utf8.ValidString(s[:n]) {
n--
}
return s[:n]
}
func reverse(ss []string) {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
var a viewPageData
var displayTemp string
u, p, ok := r.BasicAuth()
if !ok {
w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
if (u == username) && (p == password) {
csvFile, err := os.Open("log/superchats.csv")
if err != nil {
fmt.Println(err)
}
defer func(csvFile *os.File) {
err := csvFile.Close()
if err != nil {
fmt.Println(err)
}
}(csvFile)
csvLines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
fmt.Println(err)
}
for _, line := range csvLines {
a.ID = append(a.ID, line[0])
a.Name = append(a.Name, line[1])
a.Message = append(a.Message, line[2])
a.Amount = append(a.Amount, line[3])
displayTemp = fmt.Sprintf("<h3><b>%s</b> sent <b>%s</b> XMR:</h3><p>%s</p>", html.EscapeString(line[1]), html.EscapeString(line[3]), line[2])
a.Display = append(a.Display, displayTemp)
}
} else {
w.WriteHeader(http.StatusUnauthorized)
return // return http 401 unauthorized error
}
reverse(a.Display)
err := viewTemplate.Execute(w, a)
if err != nil {
fmt.Println(err)
}
}
func checkHandler(w http.ResponseWriter, r *http.Request) {
payload := strings.NewReader(`{"jsonrpc":"2.0","id":"0","method":"get_address"}`)
req, _ := http.NewRequest("POST", rpcURL, payload)
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
resp := &getAddress{}
if err := json.NewDecoder(res.Body).Decode(resp); err != nil {
fmt.Println(err.Error())
}
var c checkPage
c.Meta = `<meta http-equiv="Refresh" content="3">`
c.Addy = resp.Result.Address
c.PayID = r.FormValue("id")
c.Name = truncateStrings(r.FormValue("name"), NameMaxChar)
c.Msg = truncateStrings(r.FormValue("msg"), MessageMaxChar)
c.Media = r.FormValue("media")
c.Receipt = "Waiting for payment..."
payload2 := strings.NewReader(`{"jsonrpc":"2.0","id":"0","method":"get_transfers","params":{"in":true,"pool":true,"account_index":0}}`)
req2, _ := http.NewRequest("POST", "http://127.0.0.1:28088/json_rpc", payload2)
req2.Header.Set("Content-Type", "application/json")
res2, _ := http.DefaultClient.Do(req2)
resp2 := &GetTransfersResponse{}
if err := json.NewDecoder(res2.Body).Decode(resp2); err != nil {
fmt.Println(err.Error())
}
for _, tx := range resp2.Result.In {
if tx.PaymentID == c.PayID {
var logged = false
file, err := os.Open("log/paid.log")
if err != nil {
log.Fatalf("failed to open ")
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var text []string
for scanner.Scan() {
text = append(text, scanner.Text())
}
err = file.Close()
if err != nil {
fmt.Println(err)
}
for _, eachLn := range text {
if eachLn == tx.PaymentID {
logged = true
}
}
if !logged {
f, err := os.OpenFile("log/paid.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
fmt.Println(err)
}
}(f)
if _, err := f.WriteString(tx.PaymentID + "\n"); err != nil {
log.Println(err)
}
c.Meta = ""
c.Received = float64(tx.Amount) / 1000000000000
if c.Received < ScamThreshold {
c.Receipt = "<b style='color:red'>Scammed! " + fmt.Sprint(c.Received) + " is below minimum</b>"
} else {
c.Receipt = "<b>" + fmt.Sprint(c.Received) + " XMR Received! Superchat sent</b>"
}
if c.Received < MediaMin {
c.Media = ""
}
if c.Msg == "" {
c.Msg = "⠀"
}
if c.Received >= ScamThreshold {
f, err := os.OpenFile("log/superchats.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
fmt.Println(err)
}
}(f)
csvAppend := fmt.Sprintf(`"%s","%s","%s","%s"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg), fmt.Sprint(c.Received))
if r.FormValue("show") != "true" {
csvAppend = fmt.Sprintf(`"%s","%s","%s","%s (hidden)"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg), fmt.Sprint(c.Received))
}
a, err := os.OpenFile("log/alertqueue.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(a *os.File) {
err := a.Close()
if err != nil {
fmt.Println(err)
}
}(a)
fmt.Println(csvAppend)
if _, err := f.WriteString(csvAppend + "\n"); err != nil {
log.Println(err)
}
if r.FormValue("show") != "true" {
csvAppend = fmt.Sprintf(`"%s","%s","%s","???"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg))
}
if _, err := a.WriteString(csvAppend + "\n"); err != nil {
log.Println(err)
}
if enableEmail {
if r.FormValue("show") != "true" {
mail(c.Name, fmt.Sprint(c.Received)+" (hidden)", c.Msg)
} else {
mail(c.Name, fmt.Sprint(c.Received), c.Msg)
}
}
}
} else {
c.Received = 0.000
}
if logged {
c.Receipt = "Found old payment"
c.Meta = ""
}
}
}
for _, tx := range resp2.Result.Pool {
if tx.PaymentID == c.PayID {
var logged = false
file, err := os.Open("log/paid.log")
if err != nil {
log.Fatalf("failed to open ")
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var text []string
for scanner.Scan() {
text = append(text, scanner.Text())
}
err = file.Close()
if err != nil {
fmt.Println(err)
}
for _, eachLn := range text {
if eachLn == tx.PaymentID {
logged = true
}
}
if !logged {
f, err := os.OpenFile("log/paid.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
fmt.Println(err)
}
}(f)
if _, err := f.WriteString(tx.PaymentID + "\n"); err != nil {
log.Println(err)
}
c.Meta = ""
c.Receipt = strconv.FormatInt(tx.Amount, 10) + "Payment received! It is safe to close the tab"
c.Received = float64(tx.Amount) / 1000000000000
if c.Received < ScamThreshold {
c.Receipt = "<b style='color:red'>Scammed! " + fmt.Sprint(c.Received) + " is below minimum</b>"
} else {
c.Receipt = "<b>" + fmt.Sprint(c.Received) + " XMR Received! Superchat sent</b>"
}
if c.Received < MediaMin {
c.Media = "" // remove media if chatter didn't pay the minimum
}
if c.Msg == "" {
c.Msg = "⠀" // unicode blank space because discord doesnt accept empty messages
}
if c.Received >= ScamThreshold {
f, err := os.OpenFile("log/superchats.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
fmt.Println(err)
}
}(f)
csvAppend := fmt.Sprintf(`"%s","%s","%s","%s"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg), fmt.Sprint(c.Received))
if r.FormValue("show") != "true" {
csvAppend = fmt.Sprintf(`"%s","%s","%s","%s (hidden)"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg), fmt.Sprint(c.Received))
}
a, err := os.OpenFile("log/alertqueue.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer func(a *os.File) {
err := a.Close()
if err != nil {
fmt.Println(err)
}
}(a)
fmt.Println(csvAppend)
if _, err := f.WriteString(csvAppend + "\n"); err != nil {
log.Println(err)
}
if r.FormValue("show") != "true" {
csvAppend = fmt.Sprintf(`"%s","%s","%s","???"`, c.PayID, html.EscapeString(c.Name), html.EscapeString(c.Msg))
}
if _, err := a.WriteString(csvAppend + "\n"); err != nil {
log.Println(err)
}
if enableEmail {
if r.FormValue("show") != "true" {
mail(c.Name, fmt.Sprint(c.Received)+" (hidden)", c.Msg)
} else {
mail(c.Name, fmt.Sprint(c.Received), c.Msg)
}
}
}
} else {
c.Received = 0.000
}
if logged {
c.Receipt = "Found old payment"
c.Meta = ""
}
}
}
err := checkTemplate.Execute(w, c)
if err != nil {
fmt.Println(err)
}
}
func indexHandler(w http.ResponseWriter, _ *http.Request) {
var i indexDisplay
i.MaxChar = MessageMaxChar
i.MinAmnt = ScamThreshold
i.Checked = checked
err := indexTemplate.Execute(w, i)
if err != nil {
fmt.Println(err)
}
}
func topwidgetHandler(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok {
w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
if (u == username) && (p == password) {
csvFile, err := os.Open("log/superchats.csv")
if err != nil {
fmt.Println(err)
}
defer func(csvFile *os.File) {
err := csvFile.Close()
if err != nil {
fmt.Println(err)
}
}(csvFile)
// TODO: Add an OBS widget displaying top n donors. Don't include amounts set as hidden by donor
//csvLines, err := csv.NewReader(csvFile).ReadAll()
//if err != nil {
// fmt.Println(err)
//}
} else {
w.WriteHeader(http.StatusUnauthorized)
return // return http 401 unauthorized error
}
err := topWidgetTemplate.Execute(w, nil)
if err != nil {
fmt.Println(err)
}
}
func alertHandler(w http.ResponseWriter, r *http.Request) {
var v csvLog
v.Refresh = AlertWidgetRefreshInterval
if r.FormValue("auth") == password {
csvFile, err := os.Open("log/alertqueue.csv")
if err != nil {
fmt.Println(err)
}
csvLines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
fmt.Println(err)
}
defer func(csvFile *os.File) {
err := csvFile.Close()
if err != nil {
fmt.Println(err)
}
}(csvFile)
// Remove top line of CSV file after displaying it
if csvLines != nil {
popFile, _ := os.OpenFile("log/alertqueue.csv", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
popFirst := csvLines[1:]
w := csv.NewWriter(popFile)
err := w.WriteAll(popFirst)
if err != nil {
fmt.Println(err)
}
defer func(popFile *os.File) {
err := popFile.Close()
if err != nil {
fmt.Println(err)
}
}(popFile)
v.ID = csvLines[0][0]
v.Name = csvLines[0][1]
v.Message = csvLines[0][2]
v.Amount = csvLines[0][3]
v.DisplayToggle = ""
} else {
v.DisplayToggle = "display: none;"
}
} else {
w.WriteHeader(http.StatusUnauthorized)
return // return http 401 unauthorized error
}
err := alertTemplate.Execute(w, v)
if err != nil {
fmt.Println(err)
}
}
func paymentHandler(w http.ResponseWriter, r *http.Request) {
payload := strings.NewReader(`{"jsonrpc":"2.0","id":"0","method":"make_integrated_address"}`)
req, err := http.NewRequest("POST", rpcURL, payload)
if err == nil {
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err == nil {
resp := &rpcResponse{}
if err := json.NewDecoder(res.Body).Decode(resp); err != nil {
fmt.Println(err.Error())
}
var s superChat
s.Amount = html.EscapeString(r.FormValue("amount"))
if r.FormValue("amount") == "" {
s.Amount = fmt.Sprint(ScamThreshold)
}
if r.FormValue("name") == "" {
s.Name = "Anonymous"
} else {
s.Name = html.EscapeString(truncateStrings(condenseSpaces(r.FormValue("name")), NameMaxChar))
}
s.Message = html.EscapeString(truncateStrings(condenseSpaces(r.FormValue("message")), MessageMaxChar))
s.Media = html.EscapeString(r.FormValue("media"))
s.PayID = html.EscapeString(resp.Result.PaymentID)
s.Address = resp.Result.IntegratedAddress
params := url.Values{}
params.Add("id", resp.Result.PaymentID)
params.Add("name", s.Name)
params.Add("msg", r.FormValue("message"))
params.Add("media", condenseSpaces(s.Media))
params.Add("show", html.EscapeString(r.FormValue("showAmount")))
s.CheckURL = params.Encode()
tmp, _ := qrcode.Encode(fmt.Sprintf("monero:%s?tx_amount=%s", resp.Result.IntegratedAddress, s.Amount), qrcode.Low, 320)
s.QRB64 = base64.StdEncoding.EncodeToString(tmp)
err := payTemplate.Execute(w, s)
if err != nil {
fmt.Println(err)
}
} else {
w.WriteHeader(http.StatusInternalServerError)
return // return http 401 unauthorized error
}
}
}