Initial server setup and performs initial greeting and hello
This commit is contained in:
parent
3bb6a2e8b9
commit
a213bfe34e
6 changed files with 284 additions and 0 deletions
32
cmd/server/main.go
Normal file
32
cmd/server/main.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* 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 main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"forge.monodon.me/Gnarwhal/diodemail/smtp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := smtp.Run(":4650", false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
8
go.mod
8
go.mod
|
@ -1,3 +1,11 @@
|
|||
module forge.monodon.me/Gnarwhal/diodemail
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require github.com/rs/zerolog v1.33.0
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
)
|
||||
|
|
15
go.sum
Normal file
15
go.sum
Normal file
|
@ -0,0 +1,15 @@
|
|||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
57
smtp/connection.go
Normal file
57
smtp/connection.go
Normal 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
103
smtp/handlers.go
Normal 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
69
smtp/server.go
Normal 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()
|
||||
}
|
Loading…
Add table
Reference in a new issue