douqiu0796 2018-08-15 09:21
浏览 66
已采纳

Golang的smtp客户端的自定义拨号程序?

I'm try connect to smtp server via socks5 proxy

package main

import (
    "net"
    "net/smtp"

    "golang.org/x/net/proxy"
)

func main() { Connect() }
func Connect() {
    dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:465")
    if err != nil {
        panic(err)
    }
    client, err := smtp.NewClient(dialer, "smtp.gmail.com:465")
    if err != nil {
        panic(err)
    }
    auth := smtp.PlainAuth("", "mymailaddr@gmail.com", "", "smtp.gmail.com:465")
    if err = client.Auth(auth); err != nil {
        panic(err)
    }
}

func Socks(socks, addr string) (r net.Conn, err error) {
    Dial, err := proxy.SOCKS5("tcp", socks, nil, proxy.Direct)
    r, err = Dial.Dial("tcp", addr)
    return
}

And can't it, have error

panic: EOF

goroutine 1 [running]:
main.Connect()
        main.go:18 +0x1e5
main.main()
        main.go:10 +0x20
exit status 2

Does smtp.Client can any method to connect smtp server with socks proxy? I'm not found answer in Google and not found any library provide this features.

  • 写回答

1条回答 默认 最新

  • doujia7517 2018-08-15 10:14
    关注

    You are using port 465 which expects TLS from start (implicit TLS) and not the usual TLS after STARTTLS command (explicit TLS). This means that the Conn object you use as dialer should already have been upgraded to TLS. To do this:

    import "crypto/tls"
    ...
    func Connect() {
        dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:465")
        ...
        conf := &tls.Config{ServerName: "smtp.gmail.com"}
        tlsdialer := tls.Client(dialer, conf)
        client, err := smtp.NewClient(tlsdialer, "smtp.gmail.com:465")
    

    Alternatively you could use port 587 which requires explicit TLS:

    func Connect() {
        dialer, err := Socks("127.0.0.1:9050", "smtp.gmail.com:587")
        ...
        conf := &tls.Config{ServerName: "smtp.gmail.com"}
        err = client.StartTLS(conf)
        ...
        auth := smtp.PlainAuth("", "mymailaddr@gmail.com", "", "smtp.gmail.com:587")
        if err = client.Auth(auth); err != nil {
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?