1
0
Fork 0

Initial commit

master
Ambrose Chua 2018-07-01 15:52:51 +08:00
commit 037cdfe7b6
10 changed files with 255 additions and 0 deletions

7
.drone.yml Normal file
View File

@ -0,0 +1,7 @@
pipeline:
docker:
image: plugins/docker
registry: registry.labs.0x.no
repo: registry.labs.0x.no/email-collector
tags:
- latest

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
email-collector

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM golang:1.10-alpine as go
WORKDIR /go/src/email-collector
COPY . .
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
RUN go build -ldflags '-extldflags "-static"' -o email-collector
FROM scratch
EXPOSE 8080
COPY --from=go /go/src/email-collector/email-collector email-collector
ENTRYPOINT ["/email-collector"]

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Ambrose Chua
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# email-collector
A simple email collector

84
checkmail/checkmail.go Normal file
View File

@ -0,0 +1,84 @@
package checkmail
import (
"errors"
"fmt"
"net"
"net/smtp"
"regexp"
"strings"
"time"
)
type SmtpError struct {
Err error
}
func (e SmtpError) Error() string {
return e.Err.Error()
}
func (e SmtpError) Code() string {
return e.Err.Error()[0:3]
}
func NewSmtpError(err error) SmtpError {
return SmtpError{
Err: err,
}
}
const forceDisconnectAfter = time.Second * 10
var (
ErrBadFormat = errors.New("invalid format")
ErrUnresolvableHost = errors.New("unresolvable host")
//emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
emailRegexp = regexp.MustCompile("^.+?@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)
func ValidateFormat(email string) error {
if !emailRegexp.MatchString(email) {
return ErrBadFormat
}
return nil
}
func ValidateHost(email string) error {
_, host := split(email)
mx, err := net.LookupMX(host)
if err != nil {
return ErrUnresolvableHost
}
client, err := smtp.Dial(fmt.Sprintf("%s:%d", mx[0].Host, 25))
if err != nil {
return NewSmtpError(err)
}
defer client.Close()
t := time.AfterFunc(forceDisconnectAfter, func() { client.Close() })
defer t.Stop()
err = client.Hello("checkmail.me")
if err != nil {
return NewSmtpError(err)
}
err = client.Mail("lansome-cowboy@gmail.com")
if err != nil {
return NewSmtpError(err)
}
err = client.Rcpt(email)
if err != nil {
return NewSmtpError(err)
}
return nil
}
func split(email string) (account, host string) {
i := strings.LastIndexByte(email, '@')
account = email[:i]
host = email[i+1:]
return
}

View File

@ -0,0 +1,58 @@
package checkmail_test
import (
"testing"
"github.com/badoux/checkmail"
)
var (
samples = []struct {
mail string
format bool
account bool //host+user
}{
{mail: "florian@carrere.cc", format: true, account: true},
{mail: " florian@carrere.cc", format: false, account: false},
{mail: "florian@carrere.cc ", format: false, account: false},
{mail: "test@912-wrong-domain902.com", format: true, account: false},
{mail: "0932910-qsdcqozuioqkdmqpeidj8793@gmail.com", format: true, account: false},
{mail: "@gmail.com", format: false, account: false},
{mail: "test@gmail@gmail.com", format: false, account: false},
{mail: "test test@gmail.com", format: false, account: false},
{mail: " test@gmail.com", format: false, account: false},
{mail: "test@wrong domain.com", format: false, account: false},
{mail: "é&ààà@gmail.com", format: false, account: false},
{mail: "admin@jalopyjournal.com", format: true, account: true},
{mail: "admin@busyboo.com", format: true, account: true},
{mail: "a@gmail.fi", format: true, account: false},
}
)
func TestValidateHost(t *testing.T) {
for _, s := range samples {
if !s.format {
continue
}
err := checkmail.ValidateHost(s.mail)
if err != nil && s.account == true {
t.Errorf(`"%s" => unexpected error: "%v"`, s.mail, err)
}
if err == nil && s.account == false {
t.Errorf(`"%s" => expected error`, s.mail)
}
}
}
func TestValidateFormat(t *testing.T) {
for _, s := range samples {
err := checkmail.ValidateFormat(s.mail)
if err != nil && s.format == true {
t.Errorf(`"%s" => unexpected error: "%v"`, s.mail, err)
}
if err == nil && s.format == false {
t.Errorf(`"%s" => expected error`, s.mail)
}
}
}

58
email-collector.go Normal file
View File

@ -0,0 +1,58 @@
package main // import "github.com/productionwentdown/email-collector"
import (
"encoding/csv"
"log"
"net/http"
"os"
"sync"
"time"
// modified from @badoux's checkmail
"github.com/productionwentdown/email-collector/checkmail"
)
func main() {
csvMutex := &sync.Mutex{}
csvFile, err := os.OpenFile("list.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer csvFile.Close()
if err != nil {
log.Fatal(err)
}
csvWriter := csv.NewWriter(csvFile)
http.HandleFunc("/subscribe", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println(err)
w.WriteHeader(400)
w.Write([]byte("An error occurred. Please try again"))
return
}
email := r.Form.Get("email")
if err := checkmail.ValidateFormat(email); err != nil {
log.Println(email, err)
w.WriteHeader(400)
w.Write([]byte("Email is not valid"))
return
}
err = checkmail.ValidateHost(email)
if _, ok := err.(checkmail.SmtpError); !ok && err != nil {
log.Println(email, err)
w.WriteHeader(400)
w.Write([]byte("Email is not valid"))
return
}
log.Println(email, "success")
csvMutex.Lock()
csvWriter.Write([]string{email, time.Now().String()})
csvWriter.Flush()
csvMutex.Unlock()
w.Header().Add("Location", "/subscribed")
w.WriteHeader(303)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}

1
go.mod Normal file
View File

@ -0,0 +1 @@
module github.com/productionwentdown/email-collector

4
list.csv Normal file
View File

@ -0,0 +1,4 @@
ambrose.chua.cm@gmail.com,2018-07-01 15:44:58.882682933 +0800 +08 m=+6.293630824
ambrose.chua.cm@gmail.com,2018-07-01 15:45:54.537033441 +0800 +08 m=+2.937844629
ambrose.chua.cm@gmail.com,2018-07-01 15:45:59.691625135 +0800 +08 m=+8.092381750
ambrose.chua.cm@gmail.com,2018-07-01 15:46:06.580296689 +0800 +08 m=+2.869616704
1 ambrose.chua.cm@gmail.com 2018-07-01 15:44:58.882682933 +0800 +08 m=+6.293630824
2 ambrose.chua.cm@gmail.com 2018-07-01 15:45:54.537033441 +0800 +08 m=+2.937844629
3 ambrose.chua.cm@gmail.com 2018-07-01 15:45:59.691625135 +0800 +08 m=+8.092381750
4 ambrose.chua.cm@gmail.com 2018-07-01 15:46:06.580296689 +0800 +08 m=+2.869616704