mirror of
https://github.com/AfterShip/email-verifier.git
synced 2025-02-06 10:24:28 +00:00
Merge remote-tracking branch 'upstream/main' into chore/update_free_domain_source
This commit is contained in:
commit
20d5012e06
13
.github/workflows/ci.yml
vendored
13
.github/workflows/ci.yml
vendored
@ -16,7 +16,7 @@ jobs:
|
||||
name: Lint/Test
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [1.17.x]
|
||||
go-version: [1.22.x]
|
||||
os: [ubuntu-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
@ -41,21 +41,12 @@ jobs:
|
||||
run: |
|
||||
export GOPATH=$HOME/go
|
||||
export PATH=$PATH:$GOPATH/bin
|
||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.31.0
|
||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2
|
||||
make lint
|
||||
|
||||
- name: Run Unit tests
|
||||
run: |
|
||||
make test
|
||||
- name: Install goveralls
|
||||
env:
|
||||
GO111MODULE: off
|
||||
run: go get github.com/mattn/goveralls
|
||||
- name: Send coverage
|
||||
env:
|
||||
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: goveralls -coverprofile=covprofile -service=github
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
repos:
|
||||
- repo: 'https://github.com/golangci/golangci-lint'
|
||||
rev: v1.46.2
|
||||
rev: v1.54.2
|
||||
hooks:
|
||||
- id: golangci-lint
|
||||
entry: golangci-lint run
|
||||
|
@ -5,9 +5,11 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
|
||||
emailVerifier "github.com/AfterShip/email-verifier"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func GetEmailVerification(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
@ -37,5 +39,12 @@ func main() {
|
||||
|
||||
router.GET("/v1/:email/verification", GetEmailVerification)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
server := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: router,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
log.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -19,7 +18,7 @@ func writeFile(filePath string, data []byte) {
|
||||
}
|
||||
|
||||
fmt.Printf("Writing new %s\n", filePath)
|
||||
err := ioutil.WriteFile(filePath, data, os.FileMode(0664))
|
||||
err := os.WriteFile(filePath, data, os.FileMode(0664))
|
||||
if err != nil {
|
||||
log.Fatalf("Error writing '%s': %s", filePath, err)
|
||||
}
|
||||
@ -115,7 +114,7 @@ func updateMetaData() {
|
||||
if err = cmd.Start(); err != nil {
|
||||
log.Fatalf("error executing update.sh to update meta data: %s", err.Error())
|
||||
}
|
||||
data, err := ioutil.ReadAll(stderr)
|
||||
data, err := io.ReadAll(stderr)
|
||||
if err != nil {
|
||||
log.Fatalf("error reading update.sh result: %s : %s", err.Error(), data)
|
||||
}
|
||||
|
@ -1,14 +1,11 @@
|
||||
package emailverifier
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
|
||||
defaultFromEmail = "user@example.org"
|
||||
defaultHelloName = "localhost"
|
||||
|
||||
smtpTimeout = 30 * time.Second
|
||||
smtpPort = ":25"
|
||||
smtpPort = ":25"
|
||||
|
||||
reachableYes = "yes"
|
||||
reachableNo = "no"
|
||||
|
14
go.mod
14
go.mod
@ -1,13 +1,21 @@
|
||||
module github.com/AfterShip/email-verifier
|
||||
|
||||
go 1.15
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/hbollon/go-edlib v1.6.0
|
||||
github.com/julienschmidt/httprouter v1.3.0
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
github.com/stretchr/testify v1.8.4
|
||||
golang.org/x/net v0.19.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/h2non/gock.v1 v1.1.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
44
go.sum
44
go.sum
@ -1,4 +1,3 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
|
||||
@ -16,59 +15,16 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy
|
||||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
||||
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
|
||||
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
|
||||
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -2,7 +2,7 @@ package emailverifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@ -10,8 +10,8 @@ import (
|
||||
|
||||
// Gravatar is detail about the Gravatar
|
||||
type Gravatar struct {
|
||||
HasGravatar bool // whether has gravatar
|
||||
GravatarUrl string // gravatar url
|
||||
HasGravatar bool `json:"has_gravatar"` // whether has gravatar
|
||||
GravatarUrl string `json:"gravatar_url"` // gravatar url
|
||||
}
|
||||
|
||||
// CheckGravatar will return the Gravatar records for the given email.
|
||||
@ -36,7 +36,7 @@ func (v *Verifier) CheckGravatar(email string) (*Gravatar, error) {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@ -30,7 +30,7 @@ func updateDisposableDomains(source string) error {
|
||||
|
||||
var domains []string
|
||||
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
78
smtp.go
78
smtp.go
@ -1,6 +1,7 @@
|
||||
package emailverifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
@ -38,7 +39,7 @@ func (v *Verifier) CheckSMTP(domain, username string) (*SMTP, error) {
|
||||
email := fmt.Sprintf("%s@%s", username, domain)
|
||||
|
||||
// Dial any SMTP server that will accept a connection
|
||||
client, mx, err := newSMTPClient(domain, v.proxyURI)
|
||||
client, mx, err := newSMTPClient(domain, v.proxyURI, v.connectTimeout, v.operationTimeout)
|
||||
if err != nil {
|
||||
return &ret, ParseSMTPError(err)
|
||||
}
|
||||
@ -112,7 +113,7 @@ func (v *Verifier) CheckSMTP(domain, username string) (*SMTP, error) {
|
||||
}
|
||||
|
||||
// newSMTPClient generates a new available SMTP client
|
||||
func newSMTPClient(domain, proxyURI string) (*smtp.Client, *net.MX, error) {
|
||||
func newSMTPClient(domain, proxyURI string, connectTimeout, operationTimeout time.Duration) (*smtp.Client, *net.MX, error) {
|
||||
domain = domainToASCII(domain)
|
||||
mxRecords, err := net.LookupMX(domain)
|
||||
if err != nil {
|
||||
@ -137,7 +138,7 @@ func newSMTPClient(domain, proxyURI string) (*smtp.Client, *net.MX, error) {
|
||||
addr := r.Host + smtpPort
|
||||
index := i
|
||||
go func() {
|
||||
c, err := dialSMTP(addr, proxyURI)
|
||||
c, err := dialSMTP(addr, proxyURI, connectTimeout, operationTimeout)
|
||||
if err != nil {
|
||||
if !done {
|
||||
ch <- err
|
||||
@ -181,48 +182,28 @@ func newSMTPClient(domain, proxyURI string) (*smtp.Client, *net.MX, error) {
|
||||
// dialSMTP is a timeout wrapper for smtp.Dial. It attempts to dial an
|
||||
// SMTP server (socks5 proxy supported) and fails with a timeout if timeout is reached while
|
||||
// attempting to establish a new connection
|
||||
func dialSMTP(addr, proxyURI string) (*smtp.Client, error) {
|
||||
// Channel holding the new smtp.Client or error
|
||||
ch := make(chan interface{}, 1)
|
||||
|
||||
func dialSMTP(addr, proxyURI string, connectTimeout, operationTimeout time.Duration) (*smtp.Client, error) {
|
||||
// Dial the new smtp connection
|
||||
go func() {
|
||||
var conn net.Conn
|
||||
var err error
|
||||
var conn net.Conn
|
||||
var err error
|
||||
|
||||
if proxyURI != "" {
|
||||
conn, err = establishProxyConnection(addr, proxyURI)
|
||||
} else {
|
||||
conn, err = establishConnection(addr)
|
||||
}
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(addr)
|
||||
client, err := smtp.NewClient(conn, host)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
ch <- client
|
||||
}()
|
||||
|
||||
// Retrieve the smtp client from our client channel or timeout
|
||||
select {
|
||||
case res := <-ch:
|
||||
switch r := res.(type) {
|
||||
case *smtp.Client:
|
||||
return r, nil
|
||||
case error:
|
||||
return nil, r
|
||||
default:
|
||||
return nil, errors.New("Unexpected response dialing SMTP server")
|
||||
}
|
||||
case <-time.After(smtpTimeout):
|
||||
return nil, errors.New("Timeout connecting to mail-exchanger")
|
||||
if proxyURI != "" {
|
||||
conn, err = establishProxyConnection(addr, proxyURI, connectTimeout)
|
||||
} else {
|
||||
conn, err = establishConnection(addr, connectTimeout)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set specific timeouts for writing and reading
|
||||
err = conn.SetDeadline(time.Now().Add(operationTimeout))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(addr)
|
||||
return smtp.NewClient(conn, host)
|
||||
}
|
||||
|
||||
// GenerateRandomEmail generates a random email address using the domain passed. Used
|
||||
@ -237,13 +218,13 @@ func GenerateRandomEmail(domain string) string {
|
||||
}
|
||||
|
||||
// establishConnection connects to the address on the named network address.
|
||||
func establishConnection(addr string) (net.Conn, error) {
|
||||
return net.Dial("tcp", addr)
|
||||
func establishConnection(addr string, timeout time.Duration) (net.Conn, error) {
|
||||
return net.DialTimeout("tcp", addr, timeout)
|
||||
}
|
||||
|
||||
// establishProxyConnection connects to the address on the named network address
|
||||
// via proxy protocol
|
||||
func establishProxyConnection(addr, proxyURI string) (net.Conn, error) {
|
||||
func establishProxyConnection(addr, proxyURI string, timeout time.Duration) (net.Conn, error) {
|
||||
u, err := url.Parse(proxyURI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -252,5 +233,10 @@ func establishProxyConnection(addr, proxyURI string) (net.Conn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dialer.Dial("tcp", addr)
|
||||
|
||||
// https://github.com/golang/go/issues/37549#issuecomment-1178745487
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
return dialer.(proxy.ContextDialer).DialContext(ctx, "tcp", addr)
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package emailverifier
|
||||
|
||||
const (
|
||||
GMAIL = "gmail"
|
||||
YAHOO = "yahoo"
|
||||
)
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
package emailverifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
glxuPageFormat = "https://mail.google.com/mail/gxlu?email=%s"
|
||||
)
|
||||
|
||||
// See the link below to know why we can use this way to check if a gmail exists.
|
||||
// https://blog.0day.rocks/abusing-gmail-to-get-previously-unlisted-e-mail-addresses-41544b62b2
|
||||
func newGmailAPIVerifier(client *http.Client) smtpAPIVerifier {
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
return gmail{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type gmail struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (g gmail) isSupported(host string) bool {
|
||||
return strings.HasSuffix(host, ".google.com.")
|
||||
}
|
||||
|
||||
func (g gmail) check(domain, username string) (*SMTP, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
email := fmt.Sprintf("%s@%s", username, domain)
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf(glxuPageFormat, email), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := g.client.Do(request)
|
||||
if err != nil {
|
||||
return &SMTP{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
emailExists := len(resp.Cookies()) > 0
|
||||
|
||||
return &SMTP{
|
||||
HostExists: true,
|
||||
Deliverable: emailExists,
|
||||
}, nil
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package emailverifier
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGmailCheckByAPI(t *testing.T) {
|
||||
gmailAPIVerifier := newGmailAPIVerifier(nil)
|
||||
|
||||
t.Run("email exists", func(tt *testing.T) {
|
||||
res, err := gmailAPIVerifier.check("gmail.com", "someone")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, true, res.HostExists)
|
||||
assert.Equal(t, true, res.Deliverable)
|
||||
})
|
||||
t.Run("invalid email not exists", func(tt *testing.T) {
|
||||
// username must greater than 6 characters
|
||||
res, err := gmailAPIVerifier.check("gmail.com", "hello")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, true, res.HostExists)
|
||||
assert.Equal(t, false, res.Deliverable)
|
||||
})
|
||||
}
|
@ -5,7 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -142,7 +142,7 @@ func (y yahoo) sendValidateRequest(req yahooValidateReq) (yahooErrorResp, error)
|
||||
return res, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@ -162,7 +162,7 @@ func (y yahoo) toSignUpPage() ([]*http.Cookie, []byte, error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
return resp.Cookies(), respBytes, err
|
||||
}
|
||||
|
||||
|
36
smtp_test.go
36
smtp_test.go
@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -20,24 +21,6 @@ func TestCheckSMTPOK_ByApi(t *testing.T) {
|
||||
username string
|
||||
expected *SMTP
|
||||
}{
|
||||
{
|
||||
name: "gmail exists",
|
||||
domain: "gmail.com",
|
||||
username: "someone",
|
||||
expected: &SMTP{
|
||||
HostExists: true,
|
||||
Deliverable: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "gmail no exists",
|
||||
domain: "gmail.com",
|
||||
username: "hello",
|
||||
expected: &SMTP{
|
||||
HostExists: true,
|
||||
Deliverable: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "yahoo exists",
|
||||
domain: "yahoo.com",
|
||||
@ -75,9 +58,7 @@ func TestCheckSMTPOK_ByApi(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
_ = verifier.EnableAPIVerifier(GMAIL)
|
||||
_ = verifier.EnableAPIVerifier(YAHOO)
|
||||
defer verifier.DisableAPIVerifier(GMAIL)
|
||||
defer verifier.DisableAPIVerifier(YAHOO)
|
||||
for _, c := range cases {
|
||||
test := c
|
||||
@ -214,7 +195,8 @@ func TestCheckSMTPOK_HostNotExists(t *testing.T) {
|
||||
|
||||
func TestNewSMTPClientOK(t *testing.T) {
|
||||
domain := "gmail.com"
|
||||
ret, _, err := newSMTPClient(domain, "")
|
||||
timeout := 5 * time.Second
|
||||
ret, _, err := newSMTPClient(domain, "", timeout, timeout)
|
||||
assert.NotNil(t, ret)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@ -222,21 +204,24 @@ func TestNewSMTPClientOK(t *testing.T) {
|
||||
func TestNewSMTPClientFailed_WithInvalidProxy(t *testing.T) {
|
||||
domain := "gmail.com"
|
||||
proxyURI := "socks5://user:password@127.0.0.1:1080?timeout=5s"
|
||||
ret, _, err := newSMTPClient(domain, proxyURI)
|
||||
timeout := 5 * time.Second
|
||||
ret, _, err := newSMTPClient(domain, proxyURI, timeout, timeout)
|
||||
assert.Nil(t, ret)
|
||||
assert.Error(t, err, syscall.ECONNREFUSED)
|
||||
}
|
||||
|
||||
func TestNewSMTPClientFailed(t *testing.T) {
|
||||
domain := "zzzz171777.com"
|
||||
ret, _, err := newSMTPClient(domain, "")
|
||||
timeout := 5 * time.Second
|
||||
ret, _, err := newSMTPClient(domain, "", timeout, timeout)
|
||||
assert.Nil(t, ret)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDialSMTPFailed_NoPortIsConfigured(t *testing.T) {
|
||||
disposableDomain := "zzzz1717.com"
|
||||
ret, err := dialSMTP(disposableDomain, "")
|
||||
timeout := 5 * time.Second
|
||||
ret, err := dialSMTP(disposableDomain, "", timeout, timeout)
|
||||
assert.Nil(t, ret)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "missing port"))
|
||||
@ -244,7 +229,8 @@ func TestDialSMTPFailed_NoPortIsConfigured(t *testing.T) {
|
||||
|
||||
func TestDialSMTPFailed_NoSuchHost(t *testing.T) {
|
||||
disposableDomain := "zzzzyyyyaaa123.com:25"
|
||||
ret, err := dialSMTP(disposableDomain, "")
|
||||
timeout := 5 * time.Second
|
||||
ret, err := dialSMTP(disposableDomain, "", timeout, timeout)
|
||||
assert.Nil(t, ret)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "no such host"))
|
||||
|
20
verifier.go
20
verifier.go
@ -17,6 +17,10 @@ type Verifier struct {
|
||||
schedule *schedule // schedule represents a job schedule
|
||||
proxyURI string // use a SOCKS5 proxy to verify the email,
|
||||
apiVerifiers map[string]smtpAPIVerifier // currently support gmail & yahoo, further contributions are welcomed.
|
||||
|
||||
// Timeouts
|
||||
connectTimeout time.Duration // Timeout for establishing connections
|
||||
operationTimeout time.Duration // Timeout for SMTP operations (e.g., EHLO, MAIL FROM, etc.)
|
||||
}
|
||||
|
||||
// Result is the result of Email Verification
|
||||
@ -50,6 +54,8 @@ func NewVerifier() *Verifier {
|
||||
helloName: defaultHelloName,
|
||||
catchAllCheckEnabled: true,
|
||||
apiVerifiers: map[string]smtpAPIVerifier{},
|
||||
connectTimeout: 10 * time.Second,
|
||||
operationTimeout: 10 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,8 +145,6 @@ func (v *Verifier) EnableSMTPCheck() *Verifier {
|
||||
// If you use this feature in a production environment, please ensure that you have sufficient backup measures in place, as this may encounter rate limiting or other API issues.
|
||||
func (v *Verifier) EnableAPIVerifier(name string) error {
|
||||
switch name {
|
||||
case GMAIL:
|
||||
v.apiVerifiers[GMAIL] = newGmailAPIVerifier(http.DefaultClient)
|
||||
case YAHOO:
|
||||
v.apiVerifiers[YAHOO] = newYahooAPIVerifier(http.DefaultClient)
|
||||
default:
|
||||
@ -223,6 +227,18 @@ func (v *Verifier) Proxy(proxyURI string) *Verifier {
|
||||
return v
|
||||
}
|
||||
|
||||
// ConnectTimeout sets the timeout for establishing connections.
|
||||
func (v *Verifier) ConnectTimeout(timeout time.Duration) *Verifier {
|
||||
v.connectTimeout = timeout
|
||||
return v
|
||||
}
|
||||
|
||||
// OperationTimeout sets the timeout for SMTP operations (e.g., EHLO, MAIL FROM, etc.).
|
||||
func (v *Verifier) OperationTimeout(timeout time.Duration) *Verifier {
|
||||
v.operationTimeout = timeout
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *Verifier) calculateReachable(s *SMTP) string {
|
||||
if !v.smtpCheckEnabled {
|
||||
return reachableUnknown
|
||||
|
Loading…
x
Reference in New Issue
Block a user