2019-05-12 19:05:56 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
2019-05-12 19:21:22 +02:00
|
|
|
"milter-experiment/logging"
|
2019-11-23 19:46:31 +01:00
|
|
|
"os"
|
2019-05-12 19:21:22 +02:00
|
|
|
)
|
|
|
|
|
2019-05-12 19:05:56 +02:00
|
|
|
var (
|
2019-05-12 19:11:59 +02:00
|
|
|
// Global logger for application wide logging. Not sure if this is a good pattern yet.
|
2019-05-12 19:21:22 +02:00
|
|
|
logger logging.Logger
|
2019-05-12 19:05:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleMtaConnection(clientConnection *bufio.ReadWriter) {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
|
|
|
bytesRead, err := clientConnection.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("Error reading from clientConnection:", err.Error())
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
logger.Infof("Read %d bytes from connection\n", bytesRead)
|
2019-05-12 19:11:59 +02:00
|
|
|
logger.Debugf("Hexdump of bytes read:\n%s", hex.Dump(buf[0:bytesRead]))
|
2019-05-12 19:05:56 +02:00
|
|
|
|
|
|
|
// First 4 Bytes will be some integer thing "length"
|
|
|
|
// Let's parse that
|
|
|
|
var packetLength uint32
|
|
|
|
bytesReader := bytes.NewReader(buf[0:4])
|
|
|
|
err := binary.Read(bytesReader, binary.BigEndian, &packetLength)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln(err.Error())
|
|
|
|
}
|
|
|
|
logger.Debugf("Parsed length: %d\n", packetLength)
|
2019-11-23 19:47:03 +01:00
|
|
|
|
|
|
|
// Next up is a character, indicating the milter command
|
|
|
|
logger.Debugf("Parsing command from byte: %X\n", buf[4])
|
|
|
|
command := string(buf[4])
|
|
|
|
logger.Debugf("Parsed packet command: %s\n", command)
|
|
|
|
|
|
|
|
// Last part is data with lenght len-1
|
|
|
|
data := make([]byte, packetLength)
|
|
|
|
data = buf[5 : 5+packetLength-1]
|
|
|
|
logger.Debugf("Hexdump of packet payload read:\n%s", hex.Dump(data))
|
|
|
|
|
2019-05-12 19:05:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-05-12 19:21:22 +02:00
|
|
|
logger = logging.NewLogger(os.Stdout, os.Stderr, logging.DEBUG)
|
2019-11-23 19:46:31 +01:00
|
|
|
err := runServer("127.0.0.1:7777")
|
2019-05-12 21:14:15 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Could not run server: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-05-12 19:05:56 +02:00
|
|
|
}
|