signup: add DNS checker and run against suspicious hosts #3
59
external/cmd/signup/main.go
vendored
59
external/cmd/signup/main.go
vendored
@ -3,11 +3,15 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -110,6 +114,45 @@ func (c *character) Say(msg string) string {
|
||||
strings.TrimSpace(msg))
|
||||
}
|
||||
|
||||
// TODO: move this into an admin-editable world-unreadable file somewhere
|
||||
var suspiciousHosts = []string{
|
||||
"mx1.cock.li",
|
||||
"mx2.cock.li",
|
||||
}
|
||||
|
||||
var ErrNoSuchDomain = errors.New("no host found for email address")
|
||||
var ErrNoSuchMailserver = errors.New("no mail server found for email address")
|
||||
|
||||
// DigMX does some grubbing around to attempt to find valid email hosts, and
|
||||
// then runs then through [net.LookupMX] and returns their mailserver domains.
|
||||
// may return [ErrNoSuchDomain] or [ErrNoSuchMailserver].
|
||||
func DigMX(raw string) (domains []string, err error) {
|
||||
re := regexp.MustCompile(`@[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)+\b`) // good enough
|
||||
candidates := re.FindAllString(raw, -1)
|
||||
|
||||
// the error checking tries to be very generous: if anything comes up
|
||||
// positive we will throw no errors and just assume the rest was a fluke.
|
||||
ok := false
|
||||
for _, host := range candidates {
|
||||
records, e := net.LookupMX(host[1:])
|
||||
if e != nil {
|
||||
err = ErrNoSuchDomain
|
||||
} else if len(records) == 0 {
|
||||
err = ErrNoSuchMailserver
|
||||
} else {
|
||||
ok = true
|
||||
for _, record := range records {
|
||||
domains = append(domains, record.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ok {
|
||||
return domains, nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
logFile := path.Join(logDir, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
logF, err := os.Create(logFile)
|
||||
@ -201,9 +244,23 @@ func _main(l *log.Logger, db *sql.DB) error {
|
||||
`),
|
||||
"i'm sorry, before going further could you share an email with me?",
|
||||
|
nbsp marked this conversation as resolved
Outdated
|
||||
newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
|
||||
func(s *scene) { su.Email = string(s.Input.Bytes()) },
|
||||
func(s *scene) {
|
||||
su.Email = string(s.Input.Bytes())
|
||||
if records, err := DigMX(su.Email); err != nil {
|
||||
for _, record := range records {
|
||||
if slices.Contains(suspiciousHosts, record) {
|
||||
su.Notes = append(su.Notes, models.SignupNote{
|
||||
Author: "dns",
|
||||
Content: fmt.Sprintf("email address has suspicious host %s", record),
|
||||
SignupID: su.ID,
|
||||
|
vilmibm
commented
I love using notes for this I love using notes for this
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
func(s *scene, tv *tview.TextView, msg string) {
|
||||
// TODO could check and see if it's email shaped and admonish if not
|
||||
// NOTE(nbsp): DigMX call can see if email is invalid but this isn't used yet
|
||||
trimmed := strings.TrimSpace(msg)
|
||||
fmt.Fprintln(tv, s.Host.Say(fmt.Sprintf("I heard '%s'. Is that right? if so, /nod", trimmed)))
|
||||
}),
|
||||
|
vilmibm
commented
I predict splitting out the regex from DigMX but it's fine the way it is now. The signing up user should not have any notion of suspicious email hosts but we should gently prod them until we see an email shaped thing I predict splitting out the regex from DigMX but it's fine the way it is now.
The signing up user should not have any notion of suspicious email hosts but we should gently prod them until we see an email shaped thing
nbsp
commented
yep, what i mean is this could also be used to prod them (look at the error without caring about the domains). i think the regex shouldn't be split from DigMX because i've seen the odd yep, what i mean is this could also be used to prod them (look at the error without caring about the domains). i think the regex shouldn't be split from DigMX because i've seen the odd `ajskdhadhalksd@hjkashdkashkj.com` which will parse as an email but won't resolve. this'll hopefully get people to actually put their emails
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user
logging good, see lockingwriter package and its use
sounds good; this also moves the logging from
/town/var/signups/log/to the unified/town/var/log/external.log, which is great.