-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
105 lines (81 loc) · 2.6 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strings"
)
// Service is the service listener for email validation
type Service struct {
Config *Configuration
validEmailUser *regexp.Regexp
validEmailHost *regexp.Regexp
validEmailHostIP *regexp.Regexp
}
// Listen for connections and respond
func (service *Service) Listen() {
service.buildRegularExpressions()
http.Handle("/", service)
fmt.Printf("Listening on port %d...\n", service.Config.Port)
serverInfo := fmt.Sprintf(":%d", service.Config.Port)
log.Fatal(http.ListenAndServe(serverInfo, nil))
}
func (service *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request := request{}
testingEmail := strings.TrimSpace(r.PostFormValue("email"))
if testingEmail == "" {
log.Output(0, "Failed to process request without email post value.")
response := service.getResponseError(&request, "You must post an email address with the variable name 'email'.")
service.printOutput(w, response)
return
}
request.buildFromEmail(testingEmail)
if !request.validPreliminary {
response := service.getResponseError(&request, "Invalid email: "+request.invalidReason)
service.printOutput(w, response)
return
}
complete := make(chan bool, 3)
go request.validateHost(complete, service.validEmailHost, service.validEmailHostIP)
go request.validateUser(complete, service.validEmailUser)
go request.validateBlackList(complete, service.Config)
<-complete
<-complete
<-complete
response := service.getResponseOutput(&request, request.validHost && request.validUser && request.validPreliminary && request.validBlacklist)
service.printOutput(w, response)
}
func (service *Service) printOutput(w http.ResponseWriter, r *Response) {
output, err := json.Marshal(r)
if err != nil {
log.Fatal(err)
}
w.WriteHeader(r.Status)
w.Write(output)
}
func (service *Service) getResponseError(req *request, errorString string) *Response {
r := Response{}
r.Status = 500
r.Message = errorString
r.Email = req.inputEmail
r.Host = req.inputHost
r.User = req.inputUser
return &r
}
func (service *Service) getResponseOutput(req *request, isValid bool) *Response {
r := Response{}
r.Status = 200
r.Message = "OK"
r.Email = req.inputEmail
r.Valid = isValid
r.Host = req.inputHost
r.User = req.inputUser
return &r
}
func (service *Service) buildRegularExpressions() {
service.validEmailUser = regexp.MustCompile(`^[a-zA-Z0-9!#$%&'*+/=\?^_\{\}|~\.-]+$`)
service.validEmailHost = regexp.MustCompile(`^[a-zA-Z0-9\.-]+$`)
service.validEmailHostIP = regexp.MustCompile(`^[1-9][0-9]{0,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`)
}