Search code examples
gosocks

how to connect to imap through socks ? Go


I'm trying to connect to an IMAP server through a socks5. To do so I've exported some functions from the Imap package but I got stuck at the socks5 dialer setup(1st step:) . I think the reason is that I'm passing nil as forward (of type Dial) argument . What is the forward argument supposed to be ? It is not documented (in godoc)

func dialSocks(socks string) (Dial proxy.Dialer, err error) {
    Dial, err = proxy.SOCKS5("tcp", socks, nil, nil)
    return
}

func dialTLS(addr string, config *tls.Config) (c *imap.Client, err error) {
    addr = defaultPort(addr, "993")
    d, err := dialSocks("101.120.113.185:1328")
    if err != nil {
        log.Error(err)
        return
    }
    conn, err := d.Dial("tcp", addr)
    if err == nil {
        host, _, _ := net.SplitHostPort(addr)
        tlsConn := tls.Client(conn, setServerName(config, host))
        if c, err = imap.NewClient(tlsConn, host, 60*time.Second); err != nil {
            conn.Close()
        }
    }
    return
}

func defaultPort(addr, port string) string {
    _, _, err := net.SplitHostPort(addr)
    if err != nil {
        addr = net.JoinHostPort(addr, port)
    }
    return addr
}

Solution

  • forward is the Dialer used to connect to the proxy.

    You can use proxy.Direct here if you want (which is the default within the package, if you look at the source), but all that is doing is delegating to net.Dial(network, addr). If you want more options, insert your own Dialer.