-
Notifications
You must be signed in to change notification settings - Fork 9
/
smtpproxy.go
78 lines (70 loc) · 1.74 KB
/
smtpproxy.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
package main
import (
"fmt"
"net"
"os"
"github.com/jorgenschaefer/smtpproxy/argerror"
"github.com/jorgenschaefer/smtpproxy/config"
"github.com/jorgenschaefer/smtpproxy/proxy"
"github.com/jorgenschaefer/smtpproxy/smtpd"
)
func main() {
config.Check()
ln, err := listen()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("SMTP proxy started; address=\"%s\"\n", ln.Addr())
defer fmt.Printf("SMTP proxy stopped; address=\"%s\"\n", ln.Addr())
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
go handleConnection(smtpd.NewConnection(conn))
}
}
func listen() (net.Listener, error) {
if config.ListenMode() == "address" {
return net.Listen("tcp", config.ListenAddress())
} else {
f := os.NewFile(config.ListenFD(), "LISTEN_FD")
defer f.Close()
return net.FileListener(f)
}
}
func handleConnection(conn smtpd.Connection) {
defer conn.Close()
fmt.Println(argerror.New("New connection",
map[string]string{"client": conn.RemoteAddr().String()}))
defer fmt.Println(argerror.New("Connection finished",
map[string]string{"client": conn.RemoteAddr().String()}))
state, err := proxy.Greet(conn)
if err != nil {
fmt.Println(err.Error())
maybeTarpit(err, conn)
return
}
for {
if err := state.HandleCommand(); err != nil {
fmt.Println(err.Error())
maybeTarpit(err, conn)
return
}
}
}
func maybeTarpit(err error, conn smtpd.Connection) {
_, ok := err.(proxy.TarpitError)
if ok {
args := map[string]string{
"client": conn.RemoteAddr().String(),
}
bytesread, duration, err := conn.Tarpit()
args["bytesread"] = fmt.Sprintf("%d", bytesread)
args["duration"] = duration.String()
args["error"] = err.Error()
fmt.Println(argerror.New("Client escaped tarpit", args))
}
}