-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
137 lines (114 loc) · 3.44 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
package main
import (
"bytes"
"gopkg.in/gcfg.v1"
"github.com/pborman/uuid"
"fmt"
"github.com/dpapathanasiou/go-recaptcha"
"github.com/justinas/nosurf"
"gitlab.com/worr/chrooter"
"html/template"
"log"
"math/rand"
"net/http"
"net/mail"
"net/smtp"
"time"
txttemplate "text/template"
)
type Config struct {
Mail struct {
Email string
Username string
Password string
Hostname string
}
Recaptcha struct {
Private string
}
}
var t = template.Must(template.ParseFiles("template/index.html"))
var emailTemplate = txttemplate.Must(txttemplate.New("email").Parse("Here is your exclusive Vim download link: http://www.vim.org/download.php?code={{.Code}}"))
var c = make(chan string)
var conf Config
// Default handler
func dispatch(w http.ResponseWriter, r *http.Request) {
context := map[string]string{
"token": nosurf.Token(r),
}
if r.Method == "POST" {
context["email"] = r.FormValue("email")
if res, err := recaptcha.Confirm(r.RemoteAddr, r.FormValue("recaptcha_response_field")); err != nil || !res {
http.Error(w, "Failed captcha", http.StatusBadRequest)
return
}
if context["email"] == "" {
http.Error(w, "Empty email address", http.StatusBadRequest)
return
}
c <- context["email"]
}
if err := t.Execute(w, context); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Handler for all bad CSRF requests
func failedCSRF(w http.ResponseWriter, r *http.Request) {
http.Error(w, nosurf.Reason(r).Error(), http.StatusBadRequest)
}
// Pulls email off of the channel and possibly sends download codes
func email() {
auth := smtp.PlainAuth("", conf.Mail.Username, conf.Mail.Password, conf.Mail.Hostname)
for addr := range c {
// Exclusivity
if r := rand.Intn(3); r != 0 {
continue
}
var emailAddr *mail.Address
var err error
if emailAddr, err = mail.ParseAddress(addr); err != nil {
log.Printf("Failed to send email to %v: %v", emailAddr.Address, err)
continue
}
fromAddr := mail.Address{Name:"Vim Download Czar", Address: conf.Mail.Email}
headers := make(map[string]string)
headers["To"] = emailAddr.String()
headers["From"] = fromAddr.String()
headers["Date"] = time.Now().String()
headers["Subject"] = "Your Vim invite is ready"
headers["MIME-Version"] = "1.0"
headers["Content-Type"] = "text/plain; charset=\"utf-8\""
msg := ""
for key, val := range(headers) {
msg += fmt.Sprintf("%v: %v\r\n", key, val)
}
buf := bytes.NewBuffer(make([]byte, 0))
if err := emailTemplate.Execute(buf, struct{ Code string }{uuid.NewUUID().String()}); err != nil {
log.Printf("Can't execute email template: %v", err)
continue
}
msg += fmt.Sprintf("\r\n%s", buf)
if err = smtp.SendMail(conf.Mail.Hostname, auth, fromAddr.Address, []string{emailAddr.Address}, []byte(msg)); err != nil {
log.Printf("Failed to send email to %v: %v", emailAddr.Address, err)
continue
}
}
}
func main() {
if err := gcfg.ReadFileInto(&conf, "vim.sexy.ini"); err != nil {
log.Fatalf("Can't read config file: %v", err)
}
if err := chrooter.Chroot("www", "/var/chroot/vim.sexy"); err != nil {
log.Fatalf("Can't chroot: %v", err)
}
recaptcha.Init(conf.Recaptcha.Private)
go email()
csrf := nosurf.New(http.HandlerFunc(dispatch))
cookie := http.Cookie{HttpOnly: true}
csrf.SetBaseCookie(cookie)
csrf.SetFailureHandler(http.HandlerFunc(failedCSRF))
if err := http.ListenAndServe("127.0.0.1:8000", csrf); err != nil {
log.Fatalf("Cannot listen: %v", err)
}
}