feat(cmd/form2mail): production contact-form server

Wires formmailer + ipcohort + geoip + gitshallow into a long-running
HTTP server. Adds graceful SIGTERM via signal.NotifyContext +
http.Server.Shutdown so deploys complete in seconds instead of waiting
for the systemd 90s SIGKILL. Trusts RFC1918 CIDRs (loopback + 10/8 +
172.16/12 + 192.168/16) for X-Forwarded-For so the upstream TLS router
can pass through the real client IP.

Includes a rename-old-first deploy script (avoids ETXTBSY on running
ELFs), HTML success/error templates that hot-reload from disk on each
request, and a silent-drop spam trap for .ru sender addresses.
This commit is contained in:
AJ ONeal 2026-04-30 16:49:23 -06:00
parent 637addf87c
commit 783fd774c6
No known key found for this signature in database
9 changed files with 668 additions and 0 deletions

2
cmd/form2mail/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/form2mail
/form2mail-*

View File

1
cmd/form2mail/README.md Normal file
View File

@ -0,0 +1 @@
https://grok.com/c/3d268fcf-e236-49f3-8f74-c007bdc795b5?rid=c834f57c-ff18-4dd6-8be8-115223ae3801

View File

@ -0,0 +1,95 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'/>
</head>
<body class='GF_AJAX_POSTBACK'>
<div class='gf_browser_chrome gform_wrapper gform-theme gform-theme--foundation gform-theme--framework gform-theme--orbital gform_validation_error' data-form-theme='orbital' data-form-index='0' id='gform_wrapper_1'>
<style>
#gform_wrapper_1[data-form-index="0"].gform-theme,[data-parent-form="1_0"] {
--gf-color-primary: #204ce5;
--gf-color-primary-rgb: 32, 76, 229;
--gf-color-primary-contrast: #fff;
--gf-color-primary-contrast-rgb: 255, 255, 255;
--gf-color-primary-darker: #001AB3;
--gf-color-primary-lighter: #527EFF;
--gf-color-secondary: #fff;
--gf-color-secondary-rgb: 255, 255, 255;
--gf-color-secondary-contrast: #112337;
--gf-color-secondary-contrast-rgb: 17, 35, 55;
--gf-color-secondary-darker: #F5F5F5;
--gf-color-secondary-lighter: #FFFFFF;
--gf-color-out-ctrl-light: rgba(17, 35, 55, 0.1);
--gf-color-out-ctrl-light-rgb: 17, 35, 55;
--gf-color-out-ctrl-light-darker: rgba(104, 110, 119, 0.35);
--gf-color-out-ctrl-light-lighter: #F5F5F5;
--gf-color-out-ctrl-dark: #585e6a;
--gf-color-out-ctrl-dark-rgb: 88, 94, 106;
--gf-color-out-ctrl-dark-darker: #112337;
--gf-color-out-ctrl-dark-lighter: rgba(17, 35, 55, 0.65);
--gf-color-in-ctrl: #fff;
--gf-color-in-ctrl-rgb: 255, 255, 255;
--gf-color-in-ctrl-contrast: #112337;
--gf-color-in-ctrl-contrast-rgb: 17, 35, 55;
--gf-color-in-ctrl-darker: #F5F5F5;
--gf-color-in-ctrl-lighter: #FFFFFF;
--gf-color-in-ctrl-primary: #204ce5;
--gf-color-in-ctrl-primary-rgb: 32, 76, 229;
--gf-color-in-ctrl-primary-contrast: #fff;
--gf-color-in-ctrl-primary-contrast-rgb: 255, 255, 255;
--gf-color-in-ctrl-primary-darker: #001AB3;
--gf-color-in-ctrl-primary-lighter: #527EFF;
--gf-color-in-ctrl-light: rgba(17, 35, 55, 0.1);
--gf-color-in-ctrl-light-rgb: 17, 35, 55;
--gf-color-in-ctrl-light-darker: rgba(104, 110, 119, 0.35);
--gf-color-in-ctrl-light-lighter: #F5F5F5;
--gf-color-in-ctrl-dark: #585e6a;
--gf-color-in-ctrl-dark-rgb: 88, 94, 106;
--gf-color-in-ctrl-dark-darker: #112337;
--gf-color-in-ctrl-dark-lighter: rgba(17, 35, 55, 0.65);
--gf-radius: 3px;
--gf-font-size-secondary: 14px;
--gf-font-size-tertiary: 13px;
--gf-icon-ctrl-number: url("data:image/svg+xml,%3Csvg width='8' height='14' viewBox='0 0 8 14' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M4 0C4.26522 5.96046e-08 4.51957 0.105357 4.70711 0.292893L7.70711 3.29289C8.09763 3.68342 8.09763 4.31658 7.70711 4.70711C7.31658 5.09763 6.68342 5.09763 6.29289 4.70711L4 2.41421L1.70711 4.70711C1.31658 5.09763 0.683417 5.09763 0.292893 4.70711C-0.0976311 4.31658 -0.097631 3.68342 0.292893 3.29289L3.29289 0.292893C3.48043 0.105357 3.73478 0 4 0ZM0.292893 9.29289C0.683417 8.90237 1.31658 8.90237 1.70711 9.29289L4 11.5858L6.29289 9.29289C6.68342 8.90237 7.31658 8.90237 7.70711 9.29289C8.09763 9.68342 8.09763 10.3166 7.70711 10.7071L4.70711 13.7071C4.31658 14.0976 3.68342 14.0976 3.29289 13.7071L0.292893 10.7071C-0.0976311 10.3166 -0.0976311 9.68342 0.292893 9.29289Z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-icon-ctrl-select: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M0.292893 0.292893C0.683417 -0.097631 1.31658 -0.097631 1.70711 0.292893L5 3.58579L8.29289 0.292893C8.68342 -0.0976311 9.31658 -0.0976311 9.70711 0.292893C10.0976 0.683417 10.0976 1.31658 9.70711 1.70711L5.70711 5.70711C5.31658 6.09763 4.68342 6.09763 4.29289 5.70711L0.292893 1.70711C-0.0976311 1.31658 -0.0976311 0.683418 0.292893 0.292893Z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-icon-ctrl-search: url("data:image/svg+xml,%3Csvg width='640' height='640' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M256 128c-70.692 0-128 57.308-128 128 0 70.691 57.308 128 128 128 70.691 0 128-57.309 128-128 0-70.692-57.309-128-128-128zM64 256c0-106.039 85.961-192 192-192s192 85.961 192 192c0 41.466-13.146 79.863-35.498 111.248l154.125 154.125c12.496 12.496 12.496 32.758 0 45.254s-32.758 12.496-45.254 0L367.248 412.502C335.862 434.854 297.467 448 256 448c-106.039 0-192-85.962-192-192z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-label-space-y-secondary: var(--gf-label-space-y-md-secondary);
--gf-ctrl-border-color: #686e77;
--gf-ctrl-size: var(--gf-ctrl-size-md);
--gf-ctrl-label-color-primary: #112337;
--gf-ctrl-label-color-secondary: #112337;
--gf-ctrl-choice-size: var(--gf-ctrl-choice-size-md);
--gf-ctrl-checkbox-check-size: var(--gf-ctrl-checkbox-check-size-md);
--gf-ctrl-radio-check-size: var(--gf-ctrl-radio-check-size-md);
--gf-ctrl-btn-font-size: var(--gf-ctrl-btn-font-size-md);
--gf-ctrl-btn-padding-x: var(--gf-ctrl-btn-padding-x-md);
--gf-ctrl-btn-size: var(--gf-ctrl-btn-size-md);
--gf-ctrl-btn-border-color-secondary: #686e77;
--gf-ctrl-file-btn-bg-color-hover: #EBEBEB;
--gf-field-img-choice-size: var(--gf-field-img-choice-size-md);
--gf-field-img-choice-card-space: var(--gf-field-img-choice-card-space-md);
--gf-field-img-choice-check-ind-size: var(--gf-field-img-choice-check-ind-size-md);
--gf-field-img-choice-check-ind-icon-size: var(--gf-field-img-choice-check-ind-icon-size-md);
--gf-field-pg-steps-number-color: rgba(17, 35, 55, 0.8);
}
</style>
<div id='gf_1' class='gform_anchor' tabindex='-1'></div>
<div class="gform_validation_errors" id="gform_1_validation_container" data-js="gform-focus-validation-error" autofocus>
<h2 class='gform_submission_error hide_summary'>
<span class='gform-icon gform-icon--circle-error'></span>
<!-- NOTE: this is response managed by a mailer on the static server, not the WP instance -->
There was a problem with your submission.
<br/>
<br/>
Please email {.SupportEmail} directly.
<br/>
<br/>
For reference, this was the error:
<br/>
{.Error}
</h2>
</div>
</div>
</body>
</html>

395
cmd/form2mail/form2mail.go Normal file
View File

@ -0,0 +1,395 @@
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/netip"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"golang.org/x/term"
"github.com/joho/godotenv"
"github.com/therootcompany/golib/net/formmailer"
"github.com/therootcompany/golib/net/geoip"
"github.com/therootcompany/golib/net/gitshallow"
"github.com/therootcompany/golib/net/httpcache"
"github.com/therootcompany/golib/net/ipcohort"
"github.com/therootcompany/golib/sync/dataset"
)
const (
name = "form2email"
licenseYear = "2026"
licenseOwner = "AJ ONeal"
licenseType = "CC0-1.0"
defaultBlocklistRepo = "https://github.com/bitwire-it/ipblocklist.git"
refreshInterval = 47 * time.Minute
requestsPerMinute = 5
burstSize = 3
)
var (
version = "0.0.0-dev"
commit = "0000000"
date = "0001-01-01T00:00:00Z"
)
func printVersion(out io.Writer) {
if len(commit) > 7 {
commit = commit[:7]
}
_, _ = fmt.Fprintf(out, "%s v%s %s (%s)\n", name, version, commit, date)
_, _ = fmt.Fprintf(out, "Copyright (C) %s %s\n", licenseYear, licenseOwner)
_, _ = fmt.Fprintf(out, "Licensed under the %s license\n", licenseType)
}
type MainConfig struct {
showVersion bool
listenAddr string
smtpHost string
smtpFrom string
smtpToList string
smtpUser string
smtpPass string
smtpSubject string
successFile string
errorFile string
responseType string
blocklistRepo string
cacheDir string
geoipConfPath string
}
func main() {
home, _ := os.UserHomeDir()
_ = godotenv.Load()
_ = godotenv.Load(filepath.Join(home, ".config/form2mail/env"))
cfg := MainConfig{
listenAddr: "localhost:3081",
smtpHost: os.Getenv("SMTP_HOST"),
smtpFrom: os.Getenv("SMTP_FROM"),
smtpToList: os.Getenv("SMTP_TO"),
smtpUser: os.Getenv("SMTP_USER"),
smtpSubject: "Website contact request from {.Email}",
successFile: "success-file.html",
errorFile: "error-file.html",
responseType: "text/plain",
blocklistRepo: defaultBlocklistRepo,
}
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.BoolVar(&cfg.showVersion, "version", false, "Print version and exit")
fs.StringVar(&cfg.listenAddr, "listen", cfg.listenAddr, "Address to listen on")
fs.StringVar(&cfg.smtpHost, "smtp-host", cfg.smtpHost, "SMTP server:port e.g. smtp.gmail.com:587 (required)")
fs.StringVar(&cfg.smtpFrom, "smtp-from", cfg.smtpFrom, "Sender email e.g. you@gmail.com (required)")
fs.StringVar(&cfg.smtpToList, "smtp-to", cfg.smtpToList, "Recipient email e.g. alerts@yourdomain.com (required)")
fs.StringVar(&cfg.smtpUser, "smtp-user", cfg.smtpUser, "SMTP username (defaults to smtp-from if not set)")
fs.StringVar(&cfg.successFile, "success-file", cfg.successFile, "HTML or JSON file to reply with on success.")
fs.StringVar(&cfg.errorFile, "error-file", cfg.errorFile, "HTML or JSON file to reply with on failure.")
fs.StringVar(&cfg.blocklistRepo, "blocklist-repo", cfg.blocklistRepo, "git URL of the bitwire-it-compatible blocklist repo")
fs.StringVar(&cfg.cacheDir, "cache-dir", "", "cache parent dir (default: ~/.cache)")
fs.StringVar(&cfg.geoipConfPath, "geoip-conf", "", "path to GeoIP.conf (default: ./GeoIP.conf or ~/.config/maxmind/GeoIP.conf)")
fs.Usage = func() {
printVersion(os.Stderr)
fmt.Fprintln(os.Stderr, "\nUSAGE")
fmt.Fprintln(os.Stderr, " form2email [options]")
fs.PrintDefaults()
fmt.Fprintln(os.Stderr, "\nEnv vars (overrides flags): SMTP_HOST, SMTP_FROM, SMTP_TO, SMTP_USER, SMTP_PASS")
}
if err := fs.Parse(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err)
fs.Usage()
os.Exit(1)
}
if cfg.showVersion {
printVersion(os.Stdout)
return
}
if cfg.smtpHost == "" || cfg.smtpFrom == "" || cfg.smtpToList == "" {
fmt.Fprintf(os.Stderr, "\nError: missing required SMTP settings\n\n")
fs.Usage()
fmt.Fprintf(os.Stderr, "\nError: missing required SMTP settings\n\n")
os.Exit(1)
}
printVersion(os.Stderr)
// Verify templates are readable at startup; re-read on each request so
// operators can edit HTML without restarting (matches legacy behavior).
successFallback, err := os.ReadFile(cfg.successFile)
if err != nil {
fmt.Fprintf(os.Stderr, "\nError: couldn't read success response file %q: %v\n\n", cfg.successFile, err)
os.Exit(1)
}
errorFallback, err := os.ReadFile(cfg.errorFile)
if err != nil {
fmt.Fprintf(os.Stderr, "\nError: couldn't read error response file %q: %v\n\n", cfg.errorFile, err)
os.Exit(1)
}
successBody := hotReload(cfg.successFile, successFallback)
errorBody := hotReload(cfg.errorFile, errorFallback)
if cfg.smtpUser == "" {
cfg.smtpUser = cfg.smtpFrom
}
if cfg.smtpFrom == "" {
cfg.smtpFrom = cfg.smtpUser
}
if pass, hasPass := os.LookupEnv("SMTP_PASS"); !hasPass {
fmt.Fprintf(os.Stderr, "SMTP_PASS not set → ")
pwBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("Failed to read password: %v", err)
}
fmt.Fprintln(os.Stderr)
cfg.smtpPass = strings.TrimSpace(string(pwBytes))
} else {
cfg.smtpPass = pass
}
cfg.responseType = inferContentType(cfg.successFile)
if cfg.cacheDir == "" {
cfg.cacheDir = filepath.Join(home, ".cache")
}
// GeoIP config discovery: explicit --geoip-conf wins; otherwise default paths.
if cfg.geoipConfPath == "" {
for _, p := range geoip.DefaultConfPaths() {
if _, err := os.Stat(p); err == nil {
cfg.geoipConfPath = p
break
}
}
}
if cfg.geoipConfPath == "" {
log.Fatalf("geoip-conf: not found; set --geoip-conf or place GeoIP.conf in a default location.\n"+
"GeoLite2 registration is free at https://www.maxmind.com/en/geolite2/signup\n"+
"Default search paths: %v", geoip.DefaultConfPaths())
}
confData, err := os.ReadFile(cfg.geoipConfPath)
if err != nil {
log.Fatalf("geoip-conf: %v", err)
}
conf, err := geoip.ParseConf(string(confData))
if err != nil {
log.Fatalf("geoip-conf: %v", err)
}
geoipBasicAuth := httpcache.BasicAuth(conf.AccountID, conf.LicenseKey)
// Blocklist: gitshallow-backed cohort, reloaded on each git HEAD change.
repo := gitshallow.New(cfg.blocklistRepo, filepath.Join(cfg.cacheDir, "bitwire-it"), 1, "")
repo.MaxAge = refreshInterval
// Aggressive GC every 24 fetches (~roughly daily at 47min cadence).
// bitwire-it auto-commits hourly with large blobs; without prune=now the
// 2-week default grace window lets orphaned objects accumulate into GB.
repo.GCInterval = 24
blocklistSet := dataset.NewSet(repo)
blacklist := dataset.AddInitial(blocklistSet, ipcohort.New(), func(_ context.Context) (*ipcohort.Cohort, error) {
return ipcohort.LoadFiles(
repo.FilePath("tables/inbound/single_ips.txt"),
repo.FilePath("tables/inbound/networks.txt"),
)
})
fmt.Fprint(os.Stderr, "Syncing git repo ... ")
tBL := time.Now()
if err := blocklistSet.Load(context.Background()); err != nil {
fmt.Fprintln(os.Stderr)
fmt.Fprintf(os.Stderr, "error: ip cohort: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "%s (entries=%d)\n",
time.Since(tBL).Round(time.Millisecond), blacklist.Value().Size())
}
// GeoIP: City + ASN tarballs via httpcache conditional GETs.
maxmindDir := filepath.Join(cfg.cacheDir, "maxmind")
authHeader := http.Header{"Authorization": []string{geoipBasicAuth}}
geoSet := dataset.NewSet(
&httpcache.Cacher{
URL: geoip.DownloadBase + "/GeoLite2-City/download?suffix=tar.gz",
Path: filepath.Join(maxmindDir, geoip.TarGzName(geoip.CityEdition)),
MaxAge: 3 * 24 * time.Hour,
Header: authHeader,
},
&httpcache.Cacher{
URL: geoip.DownloadBase + "/GeoLite2-ASN/download?suffix=tar.gz",
Path: filepath.Join(maxmindDir, geoip.TarGzName(geoip.ASNEdition)),
MaxAge: 3 * 24 * time.Hour,
Header: authHeader,
},
)
geo := dataset.Add(geoSet, func(_ context.Context) (*geoip.Databases, error) {
return geoip.Open(maxmindDir)
})
fmt.Fprint(os.Stderr, "Loading geoip... ")
tGeo := time.Now()
if err := geoSet.Load(context.Background()); err != nil {
fmt.Fprintln(os.Stderr)
log.Fatalf("geoip: %v", err)
}
fmt.Fprintf(os.Stderr, "%s\n", time.Since(tGeo).Round(time.Millisecond))
fields := []formmailer.Field{
{Label: "Name", FormName: "input_1", Kind: formmailer.KindText},
{Label: "Email", FormName: "input_3", Kind: formmailer.KindEmail},
{Label: "Phone", FormName: "input_4", Kind: formmailer.KindPhone},
{Label: "Company", FormName: "input_5", Kind: formmailer.KindText},
{Label: "Message", FormName: "input_7", Kind: formmailer.KindMessage},
}
fm := &formmailer.FormMailer{
SMTPHost: cfg.smtpHost,
SMTPFrom: cfg.smtpFrom,
SMTPTo: strings.Split(cfg.smtpToList, ","),
SMTPUser: cfg.smtpUser,
SMTPPass: cfg.smtpPass,
Subject: cfg.smtpSubject,
SuccessBody: successBody,
ErrorBody: errorBody,
ContentType: cfg.responseType,
// Bot/blacklist rejections render {.SupportEmail} as "[REDACTED]"
// rather than leaking the real address. Validation errors (wrong
// format, missing field) still show the real support email so users
// know where to write in.
HiddenSupportValue: "[REDACTED]",
// Honor X-Forwarded-For from loopback (dev/ssh-tunnel) and RFC1918
// ranges (the TLS router lives on a private 10.x / 172.16.x address
// and fronts this service). Any other peer has XFF ignored, so a
// direct public request can't forge its origin IP.
TrustedProxies: []netip.Prefix{
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("::1/128"),
netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("172.16.0.0/12"),
netip.MustParsePrefix("192.168.0.0/16"),
},
Blacklist: blacklist,
Geo: geo,
// North America + unknown. Unknown ("") is always allowed by formmailer.
AllowedCountries: []string{"US", "CA", "MX", "CR", "VI"},
Fields: fields,
RPM: requestsPerMinute,
Burst: burstSize,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
defer func() { _ = geoSet.Close() }()
go blocklistSet.Tick(ctx, refreshInterval, func(err error) {
log.Printf("blocklist refresh: %v", err)
})
go geoSet.Tick(ctx, refreshInterval, func(err error) {
log.Printf("geoip refresh: %v", err)
})
mux := http.NewServeMux()
emailFormName := ""
for _, f := range fields {
if f.Kind == formmailer.KindEmail {
emailFormName = f.FormName
break
}
}
contact := silentDropRU(fm, emailFormName, successBody, cfg.responseType)
mux.Handle("POST /contact", contact)
mux.Handle("POST /contact/", contact)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "form2email server running. POST form data to /contact")
})
srv := &http.Server{Addr: cfg.listenAddr, Handler: mux}
fmt.Printf("form2email listening on http://%s\n", cfg.listenAddr)
fmt.Printf("Forwarding submissions from %s → %s via %s\n", cfg.smtpFrom, cfg.smtpToList, cfg.smtpHost)
fmt.Printf("Rate limit: ~%d req/min per IP (burst %d)\n", requestsPerMinute, burstSize)
fmt.Println("CTRL+C to stop")
serveErr := make(chan error, 1)
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
serveErr <- err
}
close(serveErr)
}()
select {
case <-ctx.Done():
log.Printf("shutdown: %v", ctx.Err())
case err := <-serveErr:
if err != nil {
log.Fatalf("listen: %v", err)
}
}
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("shutdown: %v", err)
}
}
// silentDropRU returns a handler that silently returns the success body for
// submissions whose email (emailFormName form input) ends with ".ru" —
// legacy spam-trap behavior from the original form2mail. All other
// submissions fall through to fm.
func silentDropRU(fm http.Handler, emailFormName string, successBody func() []byte, contentType string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ParseMultipartForm is safe to call twice; formmailer will see the
// already-parsed form. Use a bounded reader to match formmailer's cap.
r.Body = http.MaxBytesReader(w, r.Body, 10*1024)
if err := r.ParseMultipartForm(10 * 1024); err == nil {
email := strings.ToLower(strings.TrimSpace(r.FormValue(emailFormName)))
if strings.HasSuffix(email, ".ru") {
w.Header().Set("Content-Type", contentType)
_, _ = w.Write(successBody())
return
}
}
fm.ServeHTTP(w, r)
})
}
// hotReload returns a function that re-reads path on each call, falling back
// to the provided bytes on read error (logged). Used for templates that
// operators may edit out-of-band.
func hotReload(path string, fallback []byte) func() []byte {
return func() []byte {
b, err := os.ReadFile(path)
if err != nil {
log.Printf("%s read: %v", path, err)
return fallback
}
return b
}
}
func inferContentType(path string) string {
switch strings.ToLower(filepath.Ext(path)) {
case ".html", ".htm":
return "text/html; charset=utf-8"
case ".json":
return "application/json"
default:
return "text/plain; charset=utf-8"
}
}

30
cmd/form2mail/go.mod Normal file
View File

@ -0,0 +1,30 @@
module github.com/therootcompany/golib/cmd/form2mail
go 1.26.0
require (
github.com/joho/godotenv v1.5.1
github.com/therootcompany/golib/net/formmailer v0.0.0
github.com/therootcompany/golib/net/geoip v0.0.0
github.com/therootcompany/golib/net/gitshallow v0.0.0
github.com/therootcompany/golib/net/httpcache v0.0.0
github.com/therootcompany/golib/net/ipcohort v0.0.0
github.com/therootcompany/golib/sync/dataset v0.0.0
golang.org/x/term v0.39.0
)
require (
github.com/oschwald/geoip2-golang v1.13.0 // indirect
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/time v0.15.0 // indirect
)
replace (
github.com/therootcompany/golib/net/formmailer v0.0.0 => ../../net/formmailer
github.com/therootcompany/golib/net/geoip v0.0.0 => ../../net/geoip
github.com/therootcompany/golib/net/gitshallow v0.0.0 => ../../net/gitshallow
github.com/therootcompany/golib/net/httpcache v0.0.0 => ../../net/httpcache
github.com/therootcompany/golib/net/ipcohort v0.0.0 => ../../net/ipcohort
github.com/therootcompany/golib/sync/dataset v0.0.0 => ../../sync/dataset
)

20
cmd/form2mail/go.sum Normal file
View File

@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/oschwald/geoip2-golang v1.13.0 h1:Q44/Ldc703pasJeP5V9+aFSZFmBN7DKHbNsSFzQATJI=
github.com/oschwald/geoip2-golang v1.13.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

42
cmd/form2mail/scripts/deploy.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/sh
# Cross-compile form2mail for linux-amd64 and deploy to <user>@<host>.
# Usage: scripts/deploy.sh <user>@<host> [service-name]
#
# Uses the rename-old-first pattern (avoids scp ETXTBSY on running ELFs),
# restarts via systemctl, verifies --version runs from the new inode, then
# removes the .old backup. Leaves .old in place if verification fails so
# rollback is `mv ~/bin/<bin>.old ~/bin/<bin>`.
set -eu
g_remote="${1:?usage: $0 <user>@<host> [service-name]}"
g_service="${2:-form2mail}"
g_bin="form2mail"
cd "$(dirname "$0")/.."
echo "Building ${g_bin} for linux-amd64..."
GOOS=linux GOARCH=amd64 go build -o "${g_bin}-linux" .
echo "Renaming old binary on ${g_remote}..."
ssh "${g_remote}" "mv ~/bin/${g_bin} ~/bin/${g_bin}.old"
echo "Copying new binary..."
scp "${g_bin}-linux" "${g_remote}:~/bin/${g_bin}"
echo "Restarting ${g_service}..."
ssh "${g_remote}" "sudo systemctl restart ${g_service}"
sleep 2
echo "Verifying..."
if ! ssh "${g_remote}" "systemctl is-active ${g_service}" > /dev/null; then
echo " FAILED: ${g_service} not active. Logs:"
ssh "${g_remote}" "sudo journalctl -u ${g_service} -n 20 --no-pager"
echo " Backup still at ~/bin/${g_bin}.old — roll back with:"
echo " ssh ${g_remote} 'mv ~/bin/${g_bin}.old ~/bin/${g_bin} && sudo systemctl restart ${g_service}'"
exit 1
fi
echo "Removing backup..."
ssh "${g_remote}" "rm ~/bin/${g_bin}.old"
echo "OK: ${g_service} deployed and active."

View File

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'/>
</head>
<body class='GF_AJAX_POSTBACK'>
<div id='gf_1' class='gform_anchor' tabindex='-1'></div>
<div id='gform_confirmation_wrapper_1' data-form-theme='orbital' class='gform_confirmation_wrapper gform_wrapper gform-theme gform-theme--foundation gform-theme--framework gform-theme--orbital '>
<style>
#gform_confirmation_wrapper_1.gform-theme {
--gf-color-primary: #204ce5;
--gf-color-primary-rgb: 32, 76, 229;
--gf-color-primary-contrast: #fff;
--gf-color-primary-contrast-rgb: 255, 255, 255;
--gf-color-primary-darker: #001AB3;
--gf-color-primary-lighter: #527EFF;
--gf-color-secondary: #fff;
--gf-color-secondary-rgb: 255, 255, 255;
--gf-color-secondary-contrast: #112337;
--gf-color-secondary-contrast-rgb: 17, 35, 55;
--gf-color-secondary-darker: #F5F5F5;
--gf-color-secondary-lighter: #FFFFFF;
--gf-color-out-ctrl-light: rgba(17, 35, 55, 0.1);
--gf-color-out-ctrl-light-rgb: 17, 35, 55;
--gf-color-out-ctrl-light-darker: rgba(104, 110, 119, 0.35);
--gf-color-out-ctrl-light-lighter: #F5F5F5;
--gf-color-out-ctrl-dark: #585e6a;
--gf-color-out-ctrl-dark-rgb: 88, 94, 106;
--gf-color-out-ctrl-dark-darker: #112337;
--gf-color-out-ctrl-dark-lighter: rgba(17, 35, 55, 0.65);
--gf-color-in-ctrl: #fff;
--gf-color-in-ctrl-rgb: 255, 255, 255;
--gf-color-in-ctrl-contrast: #112337;
--gf-color-in-ctrl-contrast-rgb: 17, 35, 55;
--gf-color-in-ctrl-darker: #F5F5F5;
--gf-color-in-ctrl-lighter: #FFFFFF;
--gf-color-in-ctrl-primary: #204ce5;
--gf-color-in-ctrl-primary-rgb: 32, 76, 229;
--gf-color-in-ctrl-primary-contrast: #fff;
--gf-color-in-ctrl-primary-contrast-rgb: 255, 255, 255;
--gf-color-in-ctrl-primary-darker: #001AB3;
--gf-color-in-ctrl-primary-lighter: #527EFF;
--gf-color-in-ctrl-light: rgba(17, 35, 55, 0.1);
--gf-color-in-ctrl-light-rgb: 17, 35, 55;
--gf-color-in-ctrl-light-darker: rgba(104, 110, 119, 0.35);
--gf-color-in-ctrl-light-lighter: #F5F5F5;
--gf-color-in-ctrl-dark: #585e6a;
--gf-color-in-ctrl-dark-rgb: 88, 94, 106;
--gf-color-in-ctrl-dark-darker: #112337;
--gf-color-in-ctrl-dark-lighter: rgba(17, 35, 55, 0.65);
--gf-radius: 3px;
--gf-font-size-secondary: 14px;
--gf-font-size-tertiary: 13px;
--gf-icon-ctrl-number: url("data:image/svg+xml,%3Csvg width='8' height='14' viewBox='0 0 8 14' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M4 0C4.26522 5.96046e-08 4.51957 0.105357 4.70711 0.292893L7.70711 3.29289C8.09763 3.68342 8.09763 4.31658 7.70711 4.70711C7.31658 5.09763 6.68342 5.09763 6.29289 4.70711L4 2.41421L1.70711 4.70711C1.31658 5.09763 0.683417 5.09763 0.292893 4.70711C-0.0976311 4.31658 -0.097631 3.68342 0.292893 3.29289L3.29289 0.292893C3.48043 0.105357 3.73478 0 4 0ZM0.292893 9.29289C0.683417 8.90237 1.31658 8.90237 1.70711 9.29289L4 11.5858L6.29289 9.29289C6.68342 8.90237 7.31658 8.90237 7.70711 9.29289C8.09763 9.68342 8.09763 10.3166 7.70711 10.7071L4.70711 13.7071C4.31658 14.0976 3.68342 14.0976 3.29289 13.7071L0.292893 10.7071C-0.0976311 10.3166 -0.0976311 9.68342 0.292893 9.29289Z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-icon-ctrl-select: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M0.292893 0.292893C0.683417 -0.097631 1.31658 -0.097631 1.70711 0.292893L5 3.58579L8.29289 0.292893C8.68342 -0.0976311 9.31658 -0.0976311 9.70711 0.292893C10.0976 0.683417 10.0976 1.31658 9.70711 1.70711L5.70711 5.70711C5.31658 6.09763 4.68342 6.09763 4.29289 5.70711L0.292893 1.70711C-0.0976311 1.31658 -0.0976311 0.683418 0.292893 0.292893Z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-icon-ctrl-search: url("data:image/svg+xml,%3Csvg width='640' height='640' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M256 128c-70.692 0-128 57.308-128 128 0 70.691 57.308 128 128 128 70.691 0 128-57.309 128-128 0-70.692-57.309-128-128-128zM64 256c0-106.039 85.961-192 192-192s192 85.961 192 192c0 41.466-13.146 79.863-35.498 111.248l154.125 154.125c12.496 12.496 12.496 32.758 0 45.254s-32.758 12.496-45.254 0L367.248 412.502C335.862 434.854 297.467 448 256 448c-106.039 0-192-85.962-192-192z' fill='rgba(17, 35, 55, 0.65)'/%3E%3C/svg%3E");
--gf-label-space-y-secondary: var(--gf-label-space-y-md-secondary);
--gf-ctrl-border-color: #686e77;
--gf-ctrl-size: var(--gf-ctrl-size-md);
--gf-ctrl-label-color-primary: #112337;
--gf-ctrl-label-color-secondary: #112337;
--gf-ctrl-choice-size: var(--gf-ctrl-choice-size-md);
--gf-ctrl-checkbox-check-size: var(--gf-ctrl-checkbox-check-size-md);
--gf-ctrl-radio-check-size: var(--gf-ctrl-radio-check-size-md);
--gf-ctrl-btn-font-size: var(--gf-ctrl-btn-font-size-md);
--gf-ctrl-btn-padding-x: var(--gf-ctrl-btn-padding-x-md);
--gf-ctrl-btn-size: var(--gf-ctrl-btn-size-md);
--gf-ctrl-btn-border-color-secondary: #686e77;
--gf-ctrl-file-btn-bg-color-hover: #EBEBEB;
--gf-field-img-choice-size: var(--gf-field-img-choice-size-md);
--gf-field-img-choice-card-space: var(--gf-field-img-choice-card-space-md);
--gf-field-img-choice-check-ind-size: var(--gf-field-img-choice-check-ind-size-md);
--gf-field-img-choice-check-ind-icon-size: var(--gf-field-img-choice-check-ind-icon-size-md);
--gf-field-pg-steps-number-color: rgba(17, 35, 55, 0.8);
}
</style>
<div id='gform_confirmation_message_1' class='gform_confirmation_message_1 gform_confirmation_message'>
<!-- NOTE: this is response managed by a mailer on the static server, not the WP instance -->
Thanks for contacting us! We will get in touch with you shortly.
</div>
</div>
</body>
</html>