Files
fasthttp/fasthttpproxy/socks5.go
T
2019-09-13 21:47:26 +02:00

31 lines
828 B
Go

package fasthttpproxy
import (
"net"
"github.com/valyala/fasthttp"
"golang.org/x/net/proxy"
)
// FasthttpSocksDialer returns a fasthttp.DialFunc that dials using
// the provided SOCKS5 proxy.
//
// Example usage:
// c := &fasthttp.Client{
// Dial: fasthttpproxy.FasthttpSocksDialer("localhost:9050"),
// }
func FasthttpSocksDialer(proxyAddr string) fasthttp.DialFunc {
dialer, err := proxy.SOCKS5("tcp", proxyAddr, nil, proxy.Direct)
// It would be nice if we could return the error here. But we can't
// change our API so just keep returning it in the returned Dial function.
// Besides the implementation of proxy.SOCKS5() at the time of writing this
// will always return nil as error.
return func(addr string) (net.Conn, error) {
if err != nil {
return nil, err
}
return dialer.Dial("tcp", addr)
}
}