Bina Pelayan SMTP Anda Sendiri dalam Go

PHPz
Lepaskan: 2024-07-28 02:01:03
asal
1085 orang telah melayarinya

Build Your Own SMTP Server in Go

Di Valyent, kami sedang membina perisian sumber terbuka untuk pembangun.

Sebagai sebahagian daripada misi ini, kami sedang membangunkan Ferdinand, perkhidmatan penghantaran e-mel kami untuk pembangun (kini dalam alfa).

Infrastruktur e-mel bergantung pada beberapa protokol utama, dengan yang paling penting ialah:

  1. SMTP (Protokol Pemindahan Mel Mudah): Digunakan untuk menghantar dan menerima e-mel antara pelayan mel.
  2. IMAP (Protokol Akses Mesej Internet): Membenarkan pengguna membaca dan mengurus e-mel terus daripada pelayan.
  3. POP3 (Post Office Protocol versi 3): Memuat turun e-mel daripada pelayan ke peranti setempat, biasanya mengalih keluarnya daripada pelayan.

Dalam artikel hari ini, kami akan menumpukan pada membina pelayan SMTP keluar kami sendiri, mencerminkan pendekatan yang telah kami ambil dengan Ferdinand. Dengan berbuat demikian, kami akan mendapat pemahaman yang mendalam tentang komponen paling penting dalam infrastruktur penghantaran e-mel.

"Apa yang saya tidak boleh cipta, saya tidak faham."

— Richard Feynman

Dengan membina pelayan SMTP keluar dari awal, anda boleh memperoleh tahap cerapan tentang penghantaran e-mel yang tidak pernah dicapai oleh kebanyakan pembangun.

Untuk meneruskan, kami akan menggunakan bahasa pengaturcaraan Go, bersama-sama dengan perpustakaan mel hebat daripada Simon Ser. Kami akan mentafsirkan proses itu, menunjukkan kepada anda cara menghantar e-mel ke pelayan lain dan juga menerangkan konsep utama seperti SPF, DKIM dan DMARC yang membenarkan kebolehhantaran.

Pada akhirnya, anda akan mempunyai sekurang-kurangnya pemahaman yang lebih mendalam tentang infrastruktur e-mel, walaupun tidak mempunyai pelayan SMTP sedia pengeluaran.

Memahami SMTP: Asas

Sebelum kita menyelami kod, mari semak apa itu SMTP dan cara ia berfungsi. SMTP (Simple Mail Transfer Protocol) ialah protokol standard untuk menghantar e-mel merentasi Internet. Ia adalah protokol berasaskan teks yang agak mudah yang beroperasi pada model pelayan pelanggan.

Perintah SMTP

Protokol SMTP menggunakan arahan. Setiap arahan dalam SMTP mempunyai tujuan tertentu dalam proses penghantaran e-mel. Mereka membenarkan pelayan memperkenalkan diri mereka, menentukan penghantar dan penerima, memindahkan kandungan e-mel sebenar dan mengurus sesi komunikasi keseluruhan. Fikirkan arahan ini sebagai perbualan berstruktur antara dua pelayan e-mel, dengan setiap perintah mewakili pernyataan atau soalan tertentu dalam perbualan itu.

Apabila anda membina pelayan SMTP, anda pada asasnya mencipta program yang boleh bercakap bahasa ini dengan lancar, mentafsir arahan masuk dan bertindak balas dengan sewajarnya, serta mengeluarkan arahan yang betul semasa menghantar e-mel.

Mari terokai arahan SMTP yang paling penting untuk melihat bagaimana perbualan ini berlaku:

  • HELO/EHLO (Hello): Perintah ini memulakan perbualan SMTP. EHLO ialah versi SMTP lanjutan, menyokong ciri tambahan. Sintaksnya ialah domain HELO atau domain EHLO. Contohnya: EHLO example.com.
  • MEL DARI: Perintah ini menentukan alamat e-mel pengirim dan memulakan transaksi mel baharu. Ia menggunakan sintaks MAIL FROM:. Contohnya ialah MAIL FROM:.
  • RCPT TO: Digunakan untuk menentukan alamat e-mel penerima, arahan ini boleh digunakan berbilang kali untuk berbilang penerima. Sintaksnya ialah RCPT TO:. Contohnya: RCPT TO:.
  • DATA: Perintah ini menunjukkan permulaan kandungan mesej. Ia diakhiri dengan baris yang mengandungi hanya satu noktah (.). Selepas arahan DATA, anda akan memasukkan kandungan mesej. Contohnya:
DATA
From: john@example.com
To: jane@example.com
Subject: Hello

This is the body of the email.
.
Salin selepas log masuk
  • BERHENTI: Perintah mudah ini menamatkan sesi SMTP. Sintaksnya hanya BERHENTI.
  • RSET (Tetapkan Semula): Perintah RSET membatalkan transaksi mel semasa tetapi memastikan sambungan terbuka. Ia berguna untuk memulakan semula tanpa memulakan sambungan baharu. Sintaksnya hanyalah RSET.
  • AUTH (Pengesahan): Perintah ini digunakan untuk mengesahkan klien ke pelayan dan menyokong pelbagai mekanisme pengesahan. Sintaks ialah mekanisme AUTH, contohnya: AUTH LOGIN.

Perbualan SMTP biasa mungkin kelihatan seperti ini:

C: EHLO client.example.com
S: 250-smtp.example.com Hello client.example.com
S: 250-SIZE 14680064
S: 250-AUTH LOGIN PLAIN
S: 250 HELP

C: MAIL FROM:<sender@example.com>
S: 250 OK

C: RCPT TO:<recipient@example.com>
S: 250 OK

C: DATA
S: 354 Start mail input; end with <CRLF>.<CRLF>

C: From: sender@example.com
C: To: recipient@example.com
C: Subject: Test Email
C:
C: This is a test email.
C: .

S: 250 OK: queued as 12345

C: QUIT
S: 221 Bye
Salin selepas log masuk

Pengesahan dalam SMTP

Pengesahan ialah aspek penting SMTP, terutamanya untuk pelayan e-mel keluar. Ia membantu menghalang penggunaan pelayan tanpa kebenaran dan mengurangkan spam. Terdapat beberapa kaedah pengesahan yang digunakan dalam SMTP:

  1. PLAIN: This is a simple authentication method where the username and password are sent in clear text. It should only be used over encrypted connections.
  2. LOGIN: Similar to PLAIN, but the username and password are sent in separate commands.
  3. CRAM-MD5: This method uses a challenge-response mechanism to avoid sending the password in clear text.
  4. OAUTH2: This method allows the use of OAuth 2.0 tokens for authentication.

Here's an example of how PLAIN authentication looks in an SMTP conversation:

C: EHLO example.com
S: 250-STARTTLS
S: 250 AUTH PLAIN LOGIN
C: AUTH PLAIN AGVtYWlsQGV4YW1wbGUuY29tAHBhc3N3b3Jk
S: 235 2.7.0 Authentication successful
Salin selepas log masuk

In this example, AGVtYWlsQGV4YW1wbGUuY29tAHBhc3N3b3Jk is the base64-encoded version of \0email@example.com\0password.

When implementing authentication in your SMTP server, you'll need to:

  1. Advertise supported authentication methods in response to the EHLO command.
  2. Implement handlers for the AUTH command that can process the chosen authentication method.
  3. Verify the provided credentials against your user database.
  4. Maintain the authenticated state for the duration of the SMTP session.

Now, let's move on to implementing these concepts in our Go SMTP server.

Achieving deliverability: DKIM, SPF, DMARC

Imagine sending a letter through the postal service without a return address or an official stamp. It might reach its destination, but there's a good chance it'll end up in the "suspicious mail" pile. In the digital world of email, we face a similar challenge.

How do we ensure our emails aren't just sent, but actually delivered and trusted?

Enter the holy trinity of email authentication: DKIM, SPF, and DMARC.

DKIM: Your Email's Digital Signature

DKIM (DomainKeys Identified Mail) is like a wax seal on a medieval letter. It proves the email hasn't been tampered with during transit.

How it works:

  • Your email server adds a digital signature to every outgoing email.
  • The receiving server checks this signature against a public key published in your DNS records.
  • If the signature is valid, the email passes the DKIM check.

Think of it as your email's passport, stamped and verified at each checkpoint.

Example DKIM DNS Record:

<selector>._domainkey.<domain>.<tld>. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3QEKyU1fSma0axspqYK5iAj+54lsAg4qRRCnpKK68hawSd8zpsDz77ntGCR0X2mHVvkHbX6dX...oIDAQAB"
Salin selepas log masuk

Here, 'selector' is a unique identifier for this DKIM key, and the long string is your public key.

SPF: The Guest List for Your Domain's Party

SPF (Sender Policy Framework) is like the bouncer at an exclusive club. It specifies which email servers are allowed to send emails on behalf of your domain.

How it works:

  • You publish a list of authorized IP addresses in your DNS records.
  • When an email arrives claiming to be from your domain, the receiving server checks if it came from an IP on your list.
  • If it matches, the email passes the SPF check.

It's like saying, "If the email didn't come from one of these guys, it's not with us!"

Example SPF DNS Record:

<domain>.<tld>. IN TXT "v=spf1 ip4:192.0.2.0/24 include:_spf.google.com ~all"
Salin selepas log masuk

This record says:

  • Emails can come from IP addresses in the range 192.0.2.0 to 192.0.2.255.
  • Emails can also come from servers specified in Google's SPF record.
  • The ~all means to soft-fail emails from other sources (treat as suspicious but don't reject).

DMARC: The Rule Maker and Enforcer

DMARC (Domain-based Message Authentication, Reporting & Conformance) is the wise judge that decides what happens to emails that fail DKIM or SPF checks.

How it works:

  • You set a policy in your DNS records specifying how to handle emails that fail authentication.
  • Options range from "let it through anyway" to "reject it outright."
  • DMARC also provides reports on email authentication results, helping you monitor and improve your email security.

Think of DMARC as your email bouncer's rulebook and incident report.

Example DMARC DNS Record:

_dmarc.<domain>.<tld>. IN TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc-reports@<domain>.<tld>"
Salin selepas log masuk

This record says:

  • If an email fails DKIM and SPF checks, quarantine it (typically send to spam folder).
  • Send aggregate reports about email authentication results to dmarc-reports@example.com.

Why This Trinity Matters

Together, DKIM, SPF, and DMARC form a powerful shield against email spoofing and phishing. They tell receiving servers, "This email is really from us, sent by someone we trust, and here's what to do if something seems fishy."

Implementing this trinity not only improves your email deliverability but also protects your domain's reputation. It's like having a state-of-the-art security system for your email infrastructure.

As we build our SMTP server, keeping these authentication methods in mind will be crucial for ensuring our emails don't just get sent, but actually reach their destination and are trusted when they arrive. Remember, when implementing these records on a production domain, start with permissive policies and gradually tighten them as you confirm everything is working correctly.

Building the SMTP Server with Go

1. Project Initialization

First, let's create a new directory for our project and initialize a Go module:

mkdir go-smtp-server
cd go-smtp-server
go mod init github.com/yourusername/go-smtp-server
Salin selepas log masuk

2. Installing Dependencies

We'll need a few dependencies for our SMTP server. Run the following commands:

go get github.com/emersion/go-smtp
go get github.com/emersion/go-sasl
go get github.com/emersion/go-msgauth
Salin selepas log masuk

3. Basic SMTP Server Setup

  1. Create a new file named main.go and add the following code:
package main

import (
    "log"
    "time"
    "io"

    "github.com/emersion/go-smtp"
)

func main() {
    s := smtp.NewServer(&Backend{})

    s.Addr = ":2525"
    s.Domain = "localhost"
    s.WriteTimeout = 10 * time.Second
    s.ReadTimeout = 10 * time.Second
    s.MaxMessageBytes = 1024 * 1024
    s.MaxRecipients = 50
    s.AllowInsecureAuth = true

    log.Println("Starting server at", s.Addr)
    if err := s.ListenAndServe(); err != nil {
        log.Fatal(err)
    }
}

// Backend implements SMTP server methods.
type Backend struct{}

func (bkd *Backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
    return &Session{}, nil
}

// A Session is returned after EHLO.
type Session struct{}

// We'll implement the Session methods next
Salin selepas log masuk

This creates an SMTP server, listening on the 2525 port, a convenient choice for development purposes, since this port doesn’t require administrative privileges, unlike the standard ports 25 (standard SMTP), 465 (TLS), 587 (STARTTLS).

  1. Implementing EHLO/HELO

The EHLO/HELO command is handled automatically by the go-smtp library. We don't need to implement it ourselves.

  1. Implementing MAIL FROM

Add this method to the Session struct:

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
    fmt.Println("Mail from:", from) s.From = from
    return nil
}
Salin selepas log masuk

This method is called when the server receives a MAIL FROM command. It logs the sender's address and stores it in the session.

  1. Implementing RCPT TO

Add this method to the Session struct:

func (s *Session) Rcpt(to string) error {
    fmt.Println("Rcpt to:", to)
    s.To = append(s.To, to)
    return nil
}
Salin selepas log masuk

This method is called for each RCPT TO command. It logs the recipient's address and adds it to the list of recipients for this session.

  1. Implementing DATA

Add this method to the Session struct:

import (
    "fmt"
    "io"
)

func (s *Session) Data(r io.Reader) error {
    if b, err := io.ReadAll(r); err != nil {
        return err
    } else {
        fmt.Println("Received message:", string(b))

        // Here you would typically process the email
        return nil
    }
}
Salin selepas log masuk

This method is called when the server receives the DATA command. It reads the entire email message and logs it. In a real server, you would process the email here.

  1. Implementing AUTH

Add this method to the Session struct:

func (s *Session) AuthPlain(username, password string) error {
    if username != "testuser" || password != "testpass" {
        return fmt.Errorf("Invalid username or password")
    }

    return nil
}
Salin selepas log masuk

This implements a basic authentication mechanism. Note that this is for demonstration purposes only and should not be used in production.

  1. Implementing RSET

Add this method to the Session struct:

func (s *Session) Reset() {
    s.From = "" s.To = []string{}
}
Salin selepas log masuk

This method is called when the server receives a RSET command. It resets the session state.

  1. Implementing QUIT

Add this method to the Session struct:

func (s *Session) Logout() error {
    return nil
}
Salin selepas log masuk

This method is called when the server receives a QUIT command. In this simple implementation, we don't need to do anything special.

  1. Sending Emails: MX Lookup, Port Selection, and DKIM Signing

Once we've received and processed an email, the next step is to send it to its destination. This involves two key steps: finding the recipient's mail server using MX (Mail Exchanger) records, and attempting to send the email using standard SMTP ports.

First, let's add a function to look up MX records:

import "net"

func lookupMX(domain string) ([]*net.MX, error) {
    mxRecords, err := net.LookupMX(domain)
    if err != nil {
        return nil, fmt.Errorf("Error looking up MX records: %v", err)
    }

    return mxRecords, nil
}
Salin selepas log masuk

Next, let's create a function that attempts to send an email using different ports:

import (
    "crypto/tls"
    "net/smtp"
    "strings"
)

func sendMail(from string, to string, data []byte) error {
    domain := strings.Split(to, "@")[1]

    mxRecords, err := lookupMX(domain)
    if err != nil {
        return err
    }

    for _, mx := range mxRecords {
        host := mx.Host

        for _, port := range []int{25, 587, 465} {
            address := fmt.Sprintf("%s:%d", host, port)

            var c *smtp.Client

            var err error

            switch port {
            case 465:
                // SMTPS
                tlsConfig := &tls.Config{ServerName: host}
                conn, err := tls.Dial("tcp", address, tlsConfig)
                if err != nil {
                    continue
                }

                c, err = smtp.NewClient(conn, host)

            case 25, 587:
                // SMTP or SMTP with STARTTLS
                c, err = smtp.Dial(address)
                if err != nil {
                    continue
                }

                if port == 587 {
                    if err = c.StartTLS(&tls.Config{ServerName: host}); err != nil {
                        c.Close()
                        continue
                    }
                }
            }

            if err != nil {
                continue
            }

            // SMTP conversation
            if err = c.Mail(from); err != nil {
                c.Close()
                continue
            }

            if err = c.Rcpt(to); err != nil {
                c.Close()
                continue
            }

            w, err := c.Data()
            if err != nil {
                c.Close()
                continue
            }

            if _, err := w.Write(data); err != nil {
                c.Close()
                continue
            }

            err = w.Close()
            if err != nil {
                c.Close()
                continue
            }

            c.Quit()

            return nil
        }
    }

    return fmt.Errorf("Failed to send email to %s", to)
}
Salin selepas log masuk

This function does the following:

  • Looks up the MX records for the recipient's domain.
  • For each MX record, it tries to connect using ports 25, 587, and 465 in that order.
  • It uses the appropriate connection method for each port:
    • Port 25: Plain SMTP
    • Port 587: SMTP with STARTTLS
    • Port 465: SMTPS (SMTP over TLS)
  • If a connection is successful, it attempts to send the email using the SMTP protocol.
  • If the email is sent successfully, it returns. Otherwise, it tries the next port or MX record.

Now, let's modify our Data method in the Session struct to use this new sendMail function:

func (s *Session) Data(r io.Reader) error {
    if data, err := io.ReadAll(r); err != nil {
        return err
    } else {
        fmt.Println("Received message:", string(data))
        for _, recipient := range s.To {
            if err := sendMail(s.From, recipient, data); err != nil {
                fmt.Printf("Failed to send email to %s: %v", recipient, err)
            } else {
                fmt.Printf("Email sent successfully to %s", recipient)
            }

        }

        return nil
    }
}
Salin selepas log masuk

This implementation will attempt to send the received email to each recipient using the appropriate mail server and port.

Now, let's add DKIM signing to our email sending process. First, we need to import the necessary packages and set up our DKIM options:

import (
    // ... other imports ...
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "github.com/emersion/go-msgauth/dkim"
)

// Load your DKIM private key
var dkimPrivateKey *rsa.PrivateKey

func init() {
    // Load your DKIM private key from a file
    privateKeyPEM, err := ioutil.ReadFile("path/to/your/private_key.pem")
    if err != nil {
        log.Fatalf("Failed to read private key: %v", err)
    }

    block, _ := pem.Decode(privateKeyPEM)
    if block == nil {
        log.Fatalf("Failed to parse PEM block containing the private key")
    }

    privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
    if err != nil {
        log.Fatalf("Failed to parse private key: %v", err)
    }

    dkimPrivateKey = privateKey
}

// DKIM options
var dkimOptions = &dkim.SignOptions{
    Domain: "example.com",
    Selector: "default",
    Signer: dkimPrivateKey,
}
Salin selepas log masuk

Next, let's modify our sendMail function to include DKIM signing:

func sendMail(from string, to string, data []byte) error {
    // ... [previous MX lookup code] ...

    for _, mx := range mxRecords {
        host := mx.Host
        for _, port := range []int{25, 587, 465} {
            // ... [previous connection code] ...

            // DKIM sign the message
            var b bytes.Buffer
            if err := dkim.Sign(&b, bytes.NewReader(data), dkimOptions); err != nil {
                return fmt.Errorf("Failed to sign email with DKIM: %v", err)
            }
            signedData := b.Bytes()

            // SMTP conversation
            if err = c.Mail(from); err != nil {
                c.Close()
                continue
            }
            if err = c.Rcpt(to); err != nil {
                c.Close()
                continue
            }
            w, err := c.Data()
            if err != nil {
                c.Close()
                continue
            }
            _, err = w.Write(signedData) // Use the DKIM signed message
            if err != nil {
                c.Close()
                continue
            }
            err = w.Close()
            if err != nil {
                c.Close()
                continue
            }
            c.Quit()
            return nil
        }
    }

    return fmt.Errorf("Failed to send email to %s", to)
}
Salin selepas log masuk

In this updated sendMail function:

  • We sign the email data with DKIM before sending it.
  • We use the signed data (signedData) when writing to the SMTP connection.

This implementation will add a DKIM signature to your outgoing emails, which will help improve deliverability and authenticity of your emails.

Ingat untuk menggantikan "path/to/your/private_key.pem" dengan laluan sebenar ke kunci peribadi DKIM anda dan kemas kini Domain dan Pemilih dalam dkimOptions untuk memadankan rekod DNS DKIM anda.

  1. Pertimbangan dan Langkah Seterusnya

Walaupun pelaksanaan ini menyediakan pelayan SMTP berfungsi asas yang mampu menerima dan menghantar e-mel, terdapat beberapa pertimbangan penting untuk pelayan sedia pengeluaran:

  • Penghadan Kadar: Laksanakan pengehadan kadar untuk mencegah penyalahgunaan dan melindungi daripada pengeboman e-mel.
  • Pencegahan Spam: Laksanakan langkah untuk menghalang pelayan anda daripada digunakan untuk menghantar spam.
  • Pengendalian Ralat: Tingkatkan pengendalian dan pengelogan ralat untuk penyahpepijatan dan pemantauan yang lebih baik.
  • Pengurusan Baris Gilir: Laksanakan sistem baris gilir untuk cuba semula logik apabila e-mel gagal dihantar.

Kesimpulan

Kami harap anda belajar banyak dengan membaca siaran ini. Untuk mengetahui lebih lanjut tentang menghantar e-mel, sila lihat repositori GitHub Ferdinand dan terokai kod tersebut.

Atas ialah kandungan terperinci Bina Pelayan SMTP Anda Sendiri dalam Go. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan