diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fdc37b9..ff1c34f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3bd76e5..b60f845 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/cmd/apiserver/main.go b/cmd/apiserver/main.go index 582ebc7..1331905 100644 --- a/cmd/apiserver/main.go +++ b/cmd/apiserver/main.go @@ -5,9 +5,11 @@ import ( "fmt" "log" "net/http" + "time" - emailVerifier "github.com/AfterShip/email-verifier" "github.com/julienschmidt/httprouter" + + emailVerifier "github.com/AfterShip/email-verifier" ) 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()) } diff --git a/cmd/build_metadata/main.go b/cmd/build_metadata/main.go index 1d92b17..009216f 100644 --- a/cmd/build_metadata/main.go +++ b/cmd/build_metadata/main.go @@ -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) } diff --git a/go.mod b/go.mod index 1ba3dfd..b0de3d2 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index 32b33aa..e25797e 100644 --- a/go.sum +++ b/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= diff --git a/gravatar.go b/gravatar.go index 14b373a..891c9f7 100644 --- a/gravatar.go +++ b/gravatar.go @@ -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 } diff --git a/handler.go b/handler.go index 434aa1b..d63ca88 100644 --- a/handler.go +++ b/handler.go @@ -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 } diff --git a/smtp_by_api.go b/smtp_by_api.go index 2d73d2b..2bf2db1 100644 --- a/smtp_by_api.go +++ b/smtp_by_api.go @@ -1,7 +1,6 @@ package emailverifier const ( - GMAIL = "gmail" YAHOO = "yahoo" ) diff --git a/smtp_by_api_gmail.go b/smtp_by_api_gmail.go deleted file mode 100644 index b173afa..0000000 --- a/smtp_by_api_gmail.go +++ /dev/null @@ -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 -} diff --git a/smtp_by_api_gmail_test.go b/smtp_by_api_gmail_test.go deleted file mode 100644 index fc76332..0000000 --- a/smtp_by_api_gmail_test.go +++ /dev/null @@ -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) - }) -} diff --git a/smtp_by_api_yahoo.go b/smtp_by_api_yahoo.go index 5e0119c..4b76751 100644 --- a/smtp_by_api_yahoo.go +++ b/smtp_by_api_yahoo.go @@ -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 } diff --git a/smtp_test.go b/smtp_test.go index f9495da..12a6332 100644 --- a/smtp_test.go +++ b/smtp_test.go @@ -21,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", @@ -76,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 diff --git a/verifier.go b/verifier.go index 2ede140..192f85e 100644 --- a/verifier.go +++ b/verifier.go @@ -145,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: