package handlers import ( "net" "net/http" "strconv" "strings" "time" ) const contactSeenCookie = "tk_contact_seen" func clientIP(r *http.Request) string { if xff := r.Header.Get("X-Forwarded-For"); xff != "" { if i := strings.Index(xff, ","); i >= 0 { xff = xff[:i] } if ip := strings.TrimSpace(xff); ip != "" { return ip } } host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { return r.RemoteAddr } return host } func setContactFormSeen(w http.ResponseWriter) { http.SetCookie(w, &http.Cookie{ Name: contactSeenCookie, Value: strconv.FormatInt(time.Now().Unix(), 10), Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode, MaxAge: int((10 * time.Minute).Seconds()), }) } func contactFormSeenUnix(r *http.Request) int64 { c, err := r.Cookie(contactSeenCookie) if err != nil { return 0 } v, err := strconv.ParseInt(c.Value, 10, 64) if err != nil { return 0 } return v }