Initial server setup and performs initial greeting and hello

This commit is contained in:
Gnarwhal 2024-09-28 20:54:07 +00:00
parent 3bb6a2e8b9
commit a213bfe34e
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
6 changed files with 284 additions and 0 deletions

57
smtp/connection.go Normal file
View file

@ -0,0 +1,57 @@
/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
* This file is part of SSHare.
*
* SSHare is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* SSHare. If not, see <https://www.gnu.org/licenses/>.
*/
package smtp
import (
"net"
"github.com/rs/zerolog/log"
)
type Connection struct {
connection net.Conn
}
func (self Connection) Read() (string, error) {
buffer := [64]byte{}
read, err := self.connection.Read(buffer[:])
if err != nil {
return "", err
}
message := string(buffer[:read - 2])
log.Trace().Msgf("%v -> %v", self.RemoteAddr(), message)
return message, nil
}
func (self Connection) Write(message string) error {
log.Trace().Msgf("%v <- %v", self.RemoteAddr(), message[:len(message) - 1])
_, err := self.connection.Write([]byte(message))
return err
}
func (self Connection) Close() {
self.connection.Close()
}
func (self Connection) RemoteAddr() net.Addr {
return self.connection.RemoteAddr()
}

103
smtp/handlers.go Normal file
View file

@ -0,0 +1,103 @@
/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
* This file is part of SSHare.
*
* SSHare is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* SSHare. If not, see <https://www.gnu.org/licenses/>.
*/
package smtp
import (
"fmt"
"strings"
)
type Command struct {
name string
Exec func(Connection)([]Command, error)
}
func (self Command) Name() string {
return self.name
}
func (self Command) Check(message string) bool {
return strings.HasPrefix(message, self.name)
}
func (self Connection) Chain() error {
commands, err := Greet(self)
if err != nil {
return err
}
for {
message, err := self.Read()
if err != nil {
return err
}
command_found := false
for _, command := range commands {
if command.Check(message) {
commands, err = command.Exec(self)
if err != nil {
return err
}
command_found = true
break
}
}
if !command_found {
expected := make([]string, len(commands))
for index, command := range commands {
expected[index] = command.Name()
}
return fmt.Errorf("Expected one of %v, but got: %v", expected, message)
}
if len(commands) == 0 {
break
}
}
return nil
}
/* --- GREETING RESPONSE --- */
func Greet(connection Connection) ([]Command, error) {
err := connection.Write(
"220 localhost ESMTP diodemail -- Service ready" + "\n",
)
if err != nil {
return nil, err
}
return []Command{
Command{ "HELO", Hello },
Command{ "EHLO", Hello },
}, nil
}
/* --- HELO/EHLO RESPONSE --- */
func Hello(connection Connection) ([]Command, error) {
err := connection.Write(
fmt.Sprintf("250 %v is shy", connection.connection.LocalAddr()) + "\n",
)
if err != nil {
return nil, err
}
return []Command{}, nil
}

69
smtp/server.go Normal file
View file

@ -0,0 +1,69 @@
/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
* This file is part of SSHare.
*
* SSHare is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* SSHare. If not, see <https://www.gnu.org/licenses/>.
*/
package smtp
import (
"net"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type PlainListener struct {
listener net.Listener
}
func handle(connection Connection) {
log.Info().Msgf("New connection %v", connection.RemoteAddr())
defer connection.Close()
err := connection.Chain()
if err != nil {
log.Error().Msgf("Failed to serve %v: %v", connection.RemoteAddr(), err)
} else {
log.Info().Msgf("Successfully served %v", connection.RemoteAddr())
}
}
func Run(host string, implicit_tls bool) error {
log.Logger = zerolog.
New(zerolog.ConsoleWriter{Out: os.Stderr}).
With().
Timestamp().
Logger().
Level(zerolog.TraceLevel)
listener, err := net.Listen("tcp", host)
if err != nil {
return err
}
for {
connection, err := listener.Accept()
if err != nil {
return err
}
go handle(Connection{connection})
}
}
func (self PlainListener) Close() error {
return self.listener.Close()
}