|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/smtp" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// 3 submissions per hour per IP. Sits on top of the global 120/min limiter. |
| 15 | +var contactLimiter = newRateLimiter(3, time.Hour) |
| 16 | + |
| 17 | +type contactPageData struct { |
| 18 | + Sent bool |
| 19 | + Error string |
| 20 | +} |
| 21 | + |
| 22 | +func handleContactPage(w http.ResponseWriter, r *http.Request) { |
| 23 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 24 | + data := contactPageData{ |
| 25 | + Sent: r.URL.Query().Get("sent") == "1", |
| 26 | + Error: r.URL.Query().Get("error"), |
| 27 | + } |
| 28 | + if err := templates.ExecuteTemplate(w, "contact.html", data); err != nil { |
| 29 | + slog.Error("contact template", "error", err) |
| 30 | + http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func handleContactSubmit(w http.ResponseWriter, r *http.Request) { |
| 35 | + ip, _, err := net.SplitHostPort(r.RemoteAddr) |
| 36 | + if err != nil { |
| 37 | + ip = r.RemoteAddr |
| 38 | + } |
| 39 | + |
| 40 | + if err := r.ParseForm(); err != nil { |
| 41 | + http.Redirect(w, r, "/contact?error=invalid", http.StatusSeeOther) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + // Honeypot: real users leave this blank; bots fill it in. |
| 46 | + if r.PostForm.Get("website") != "" { |
| 47 | + slog.Info("contact honeypot", "ip", ip) |
| 48 | + // Pretend success so bots don't learn the trick. |
| 49 | + http.Redirect(w, r, "/contact?sent=1", http.StatusSeeOther) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if !contactLimiter.allow(ip) { |
| 54 | + slog.Warn("contact rate limit exceeded", "ip", ip) |
| 55 | + http.Redirect(w, r, "/contact?error=rate", http.StatusSeeOther) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + message := strings.TrimSpace(r.PostForm.Get("message")) |
| 60 | + replyEmail := strings.TrimSpace(r.PostForm.Get("reply_email")) |
| 61 | + |
| 62 | + if len(message) < 5 || len(message) > 5000 { |
| 63 | + http.Redirect(w, r, "/contact?error=invalid", http.StatusSeeOther) |
| 64 | + return |
| 65 | + } |
| 66 | + if replyEmail != "" && !looksLikeEmail(replyEmail) { |
| 67 | + http.Redirect(w, r, "/contact?error=invalid", http.StatusSeeOther) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + if err := sendContactEmail(replyEmail, message, ip); err != nil { |
| 72 | + slog.Error("contact send", "error", err, "ip", ip) |
| 73 | + http.Redirect(w, r, "/contact?error=smtp", http.StatusSeeOther) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + http.Redirect(w, r, "/contact?sent=1", http.StatusSeeOther) |
| 78 | +} |
| 79 | + |
| 80 | +func looksLikeEmail(s string) bool { |
| 81 | + at := strings.IndexByte(s, '@') |
| 82 | + if at < 1 || at == len(s)-1 { |
| 83 | + return false |
| 84 | + } |
| 85 | + if !strings.Contains(s[at+1:], ".") { |
| 86 | + return false |
| 87 | + } |
| 88 | + if strings.ContainsAny(s, " \t\r\n<>") { |
| 89 | + return false |
| 90 | + } |
| 91 | + return true |
| 92 | +} |
| 93 | + |
| 94 | +func sendContactEmail(replyEmail, message, ip string) error { |
| 95 | + smtpUser := os.Getenv("SMTP_USER") |
| 96 | + smtpPass := os.Getenv("SMTP_PASS") |
| 97 | + to := os.Getenv("CONTACT_TO_EMAIL") |
| 98 | + if to == "" { |
| 99 | + to = smtpUser |
| 100 | + } |
| 101 | + |
| 102 | + // Local-dev shortcut: if SMTP isn't configured, log the message so the |
| 103 | + // form is testable end-to-end without credentials. |
| 104 | + if smtpUser == "" || smtpPass == "" { |
| 105 | + slog.Info("contact form skipped", |
| 106 | + "reason", "smtp not configured", |
| 107 | + "reply_email", replyEmail, "ip", ip, "message", message) |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + subject := "Train contact" |
| 112 | + if preview := firstLine(message); preview != "" { |
| 113 | + subject = "Train contact: " + truncate(preview, 60) |
| 114 | + } |
| 115 | + |
| 116 | + replyTo := smtpUser |
| 117 | + if replyEmail != "" { |
| 118 | + replyTo = replyEmail |
| 119 | + } |
| 120 | + |
| 121 | + body := fmt.Sprintf( |
| 122 | + "Reply email: %s\nIP: %s\nSubmitted: %s\n\n%s\n", |
| 123 | + valueOrDash(replyEmail), ip, time.Now().UTC().Format(time.RFC3339), message, |
| 124 | + ) |
| 125 | + |
| 126 | + msg := []byte(strings.Join([]string{ |
| 127 | + "From: Train <" + smtpUser + ">", |
| 128 | + "To: " + to, |
| 129 | + "Reply-To: " + replyTo, |
| 130 | + "Subject: " + subject, |
| 131 | + "MIME-Version: 1.0", |
| 132 | + "Content-Type: text/plain; charset=UTF-8", |
| 133 | + "", |
| 134 | + body, |
| 135 | + }, "\r\n")) |
| 136 | + |
| 137 | + auth := smtp.PlainAuth("", smtpUser, smtpPass, "smtp.gmail.com") |
| 138 | + return smtp.SendMail("smtp.gmail.com:587", auth, smtpUser, []string{to}, msg) |
| 139 | +} |
| 140 | + |
| 141 | +func firstLine(s string) string { |
| 142 | + if i := strings.IndexAny(s, "\r\n"); i >= 0 { |
| 143 | + return strings.TrimSpace(s[:i]) |
| 144 | + } |
| 145 | + return strings.TrimSpace(s) |
| 146 | +} |
| 147 | + |
| 148 | +func truncate(s string, n int) string { |
| 149 | + if len(s) <= n { |
| 150 | + return s |
| 151 | + } |
| 152 | + return s[:n] + "..." |
| 153 | +} |
| 154 | + |
| 155 | +func valueOrDash(s string) string { |
| 156 | + if s == "" { |
| 157 | + return "(not provided)" |
| 158 | + } |
| 159 | + return s |
| 160 | +} |
0 commit comments