mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-26 04:02:29 -07:00
Implement multiple listeners and improve documentation in ini file
This commit is contained in:
48
main.go
48
main.go
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chrj/smtpd"
|
||||
"github.com/vharitonsky/iniflags"
|
||||
@@ -14,12 +16,11 @@ import (
|
||||
var (
|
||||
hostName = flag.String("hostname", "localhost.localdomain", "Server hostname")
|
||||
welcomeMsg = flag.String("welcome_msg", "", "Welcome message for SMTP session")
|
||||
localHost = flag.String("local_host", "localhost", "Address to listen for incoming SMTP")
|
||||
localPort = flag.Int("local_port", 25, "Port to listen")
|
||||
localForceTLS = flag.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
||||
listen = flag.String("listen", "127.0.0.1:25 [::1]:25", "Address and port to listen for incoming SMTP")
|
||||
localCert = flag.String("local_cert", "", "SSL certificate for STARTTLS/TLS")
|
||||
localKey = flag.String("local_key", "", "SSL private key for STARTTLS/TLS")
|
||||
remoteHost = flag.String("remote_host", "smtp.gmail.com", "Outgoing SMTP host")
|
||||
localForceTLS = flag.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
||||
remoteHost = flag.String("remote_host", "smtp.gmail.com", "Outgoing SMTP server")
|
||||
remotePort = flag.Int("remote_port", 587, "Outgoing SMTP port")
|
||||
remoteUser = flag.String("remote_user", "", "Username for authentication on outgoing SMTP server")
|
||||
remotePass = flag.String("remote_pass", "", "Password for authentication on outgoing SMTP server")
|
||||
@@ -47,16 +48,39 @@ func main() {
|
||||
ForceTLS: *localForceTLS,
|
||||
}
|
||||
|
||||
if *localCert != "" && *localKey != "" {
|
||||
cert, err := tls.LoadX509KeyPair(*localCert, *localKey)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
listeners := strings.Split(*listen, " ")
|
||||
|
||||
for i := range(listeners) {
|
||||
listener := listeners[i]
|
||||
|
||||
if strings.Index(listeners[i], "://") == -1 {
|
||||
;
|
||||
} else if strings.HasPrefix(listeners[i], "tls://") || strings.HasPrefix(listeners[i], "starttls://") {
|
||||
|
||||
listener = strings.TrimPrefix(listener, "tls://")
|
||||
listener = strings.TrimPrefix(listener, "starttls://")
|
||||
|
||||
if *localCert == "" || *localKey == "" {
|
||||
log.Fatal("TLS certificate/key not defined in config")
|
||||
}
|
||||
|
||||
cert, err := tls.LoadX509KeyPair(*localCert, *localKey)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
server.TLSConfig = &tls.Config {
|
||||
Certificates: [] tls.Certificate{cert},
|
||||
}
|
||||
} else {
|
||||
log.Fatal("Unknown protocol in listener ", listener)
|
||||
}
|
||||
|
||||
server.TLSConfig = &tls.Config {
|
||||
Certificates: [] tls.Certificate{cert},
|
||||
}
|
||||
log.Printf("Listen on %s ...\n", listener)
|
||||
go server.ListenAndServe(listener)
|
||||
}
|
||||
|
||||
server.ListenAndServe(fmt.Sprintf("%s:%d", *localHost, *localPort))
|
||||
for true {
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user