-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopopo.go
185 lines (150 loc) · 4.06 KB
/
gopopo.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"log"
"log/syslog"
"net"
"os"
"os/signal"
"runtime/debug"
"syscall"
"strconv"
"strings"
"gopopo/postfix"
"github.com/spf13/viper"
)
var rsw *(postfix.RatelimitSlidingWindow)
var logger *(log.Logger)
func main() {
debug.SetTraceback("crash")
logger, err := syslog.NewLogger(syslog.LOG_INFO|syslog.LOG_DAEMON, 0)
if err != nil {
fmt.Println("Failed to open connection to syslog")
}
viper.SetDefault("ListenAddress", "localhost")
viper.SetDefault("Port", "27091")
viper.SetDefault("DomainWhiteList", "gopopo.wl")
viper.SetDefault("DomainList", "gopopo.domains")
viper.SetDefault("DefaultRate", "110")
viper.SetDefault("Interval", "3660")
viper.SetConfigName("gopopo")
viper.SetConfigType("toml")
viper.AddConfigPath("/etc/gopopo")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("error parsing config file: %s", err))
}
host := viper.GetString("ListenAddress")
port := viper.GetString("Port")
wlfn := viper.GetString("DomainWhiteList")
dlfn := viper.GetString("DomainList")
wlmap := postfix.Load(wlfn, logger)
dommap := postfix.Load(dlfn, logger)
tmap := postfix.NewRatelimitTokenMap()
tmap.SetLogger(logger)
rsw = postfix.NewRatelimitSlidingWindow(wlmap, dommap, tmap)
rsw.SetLogger(logger)
cacheFileName := viper.GetString("CacheFileName")
if cacheFileName != "" {
if _, err := os.Stat(cacheFileName); err == nil {
logger.Println("Found previously saved token cache, loading ...")
rsw.LoadTokens(cacheFileName)
rsw.Report()
} else if os.IsNotExist(err) {
logger.Println("No cache file found, not loading previous tokens")
} else {
panic(fmt.Errorf("error checking for cache file: %s", err))
}
}
rsw.SetDefaultLimit(viper.GetInt("DefaultRate"))
rsw.SetInterval(viper.GetString("Interval"))
defm := viper.GetString("DeferMessage")
if defm != "" {
rsw.SetDeferMessage(defm)
}
sock, err := net.Listen("tcp", host+":"+port)
if err != nil {
logger.Println("Cannot create listening socket: " + err.Error())
os.Exit(1)
}
defer sock.Close()
logger.Println("Listening on " + host + ":" + port)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
sig := <-sigs
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
logger.Println("Saving tokens to ",cacheFileName)
rsw.SaveTokens(cacheFileName)
os.Exit(0)
} else {
logger.Println("Received signal",sig,"reloading data files")
wmap := postfix.Load(wlfn, logger)
dmap := postfix.Load(dlfn, logger)
rsw.SetWhiteList(wmap)
rsw.SetDomainList(dmap)
}
}
}()
for {
conn, err := sock.Accept()
if err != nil {
logger.Println("Cannot accept client: " + err.Error())
os.Exit(1)
}
go handleClient(conn, logger)
}
}
func equal(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func processMessage(buf []byte, logger *log.Logger) string {
parameters := strings.Fields(string(buf))
result := postfix.NewPolicy()
for _, param := range parameters {
pv := strings.Split(param, "=")
if len(pv) < 2 {
logger.Println("ERROR: got a bad parameter from postfix in buffer ->", buf, "at parameter:", param, "Trying to continue anyway.");
result.SetAttribute(param,"");
} else {
result.SetAttribute(pv[0], pv[1])
}
}
rcnt, err := strconv.Atoi(result.Attribute("recipient_count"))
if err != nil {
return "action=dunno\n\n"
}
sender := result.Attribute("sasl_username")
if sender == "" {
sender = result.Attribute("sender")
}
rsw.Report()
return rsw.RateLimit(sender, rcnt)
}
func handleClient(conn net.Conn, logger *log.Logger) {
defer conn.Close()
buf := make([]byte, 0, 2048)
lbuf := make([]byte, 256)
for {
rLen, err := conn.Read(lbuf)
if err != nil {
logger.Println("Error reading from client: " + err.Error())
return
}
buf = append(buf, lbuf[:rLen]...)
if equal(buf[len(buf)-2:], []byte("\n\n")) {
break
}
}
data := processMessage(buf, logger)
conn.Write([]byte(data))
}