-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (103 loc) · 2.26 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
)
// Emails : email struct
type Emails struct {
Emails []Email `json:"emails"`
}
// Email : email struct
type Email struct {
Email string `json:"email"`
}
type inputLoad struct {
Action string `json:"action"`
Value string `json:"value"`
}
// badEmailCount : bad email counts
type badEmailCount struct {
a int
}
// CheckSpamEmail : will check if email is spam
func CheckSpamEmail(emailInput string) bool {
jsonFile, err := os.Open("emails.json")
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var emails Emails
json.Unmarshal(byteValue, &emails)
var emailIn = GetSlug(emailInput)
badEmails := []badEmailCount{}
for i := 0; i < len(emails.Emails); i++ {
if strings.Contains(emails.Emails[i].Email, strings.ToLower(emailIn)) == true {
badEmails = append(badEmails, badEmailCount{1})
}
}
defer jsonFile.Close()
if len(badEmails) > 0 {
return true
}
return false
}
// GetSlug : pull domain out of email
func GetSlug(emailIn string) string {
components := strings.Split(emailIn, "@")
domain := components[1]
return domain
}
func dataHandler(c net.Conn) {
// we create a decoder that reads directly from the socket
d := json.NewDecoder(c)
var msg inputLoad
err := d.Decode(&msg)
if err != nil {
log.Fatal("Error decoding ", err)
return
}
if msg.Action == "url" {
spamCheck := CheckSpamEmail(msg.Value)
if spamCheck == true {
_, err = c.Write([]byte("true\n"))
} else {
_, err = c.Write([]byte("false\n"))
}
}
if err != nil {
log.Fatal("write error:", err)
}
c.Close()
}
func main() {
log.Println("Starting spamcontrol server")
const SOCK = "/path/to/spamcontrol/spam.sock"
os.Remove(SOCK)
unixListener, err := net.Listen("unix", SOCK)
if err != nil {
log.Fatal("Listen (UNIX socket): ", err)
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
go func(unixListener net.Listener, c chan os.Signal) {
sig := <-c
log.Printf("Caught signal %s: shutting down.", sig)
unixListener.Close()
os.Exit(0)
}(unixListener, sigc)
for {
fd, err := unixListener.Accept()
if err != nil {
log.Fatal(err)
return
}
go dataHandler(fd)
}
}