php editor Zimo introduces you a method to connect to the Golang code of the FTP server by using a proxy. In some cases, we may need to use a proxy server when accessing the FTP server in order to increase security or resolve network restrictions. Golang is a powerful programming language with easy-to-use network libraries, so we can implement this feature easily. Next, we will use a simple sample code to demonstrate how to use Golang to connect to an FTP server through a proxy.
I need to connect to ftp server through proxy and download files from Golang code. I have implemented simple client:
<code>package main import ( "bufio" "fmt" "github.com/jlaffaye/ftp" "io" "log" "net" "net/http" "os" "strings" "time" ) var targetAddr = "ftp.dlptest.com" var proxyAddr = "146.19.106.109:3128" var user = "dlpuser" var pass = "rNrKYTX9g7z3RgJRmxWuGHbeu" var path = "/xxx.txt" func connectViaProxy(network, address string) (net.Conn, error) { parts := strings.Split(address, ":") addr := fmt.Sprintf("%s:%s", targetAddr, parts[1]) proxyConn, err := net.Dial(network, proxyAddr) if err != nil { return nil, err } req, err := http.NewRequest(http.MethodConnect, "http://"+addr, nil) if err != nil { proxyConn.Close() return nil, err } req.Header.Set("Accept", "*/*") req.Header.Set("User-Agent", "curl/8.1.2") req.Header.Set("Host", targetAddr+":21") err = req.Write(proxyConn) if err != nil { return nil, err } resp, err := http.ReadResponse(bufio.NewReader(proxyConn), req) if err != nil { proxyConn.Close() return nil, err } if resp.StatusCode != 200 { x, _ := io.ReadAll(resp.Body) fmt.Println(string(x)) proxyConn.Close() return nil, fmt.Errorf("non-200 status code received from proxy: %v", resp.Status) } return proxyConn, nil } func main() { c, err := ftp.Dial(targetAddr+":21", ftp.DialWithTimeout(5*time.Second), ftp.DialWithDebugOutput(os.Stdin), ftp.DialWithDialFunc(connectViaProxy), ) if err != nil { log.Fatal(err) } err = c.Login(user, pass) if err != nil { log.Fatal(err) } if r, err := c.Retr(path); err != nil { fmt.Println(err, ":<") } else { if reader, err := io.ReadAll(r); err != nil { fmt.Println(err, ":(") } else { fmt.Println(string(reader)) } } if err = c.Quit(); err != nil { log.Fatal(err) } } </code>
Unfortunately, I receive a 403 Forbidden when trying to connect. Normally I would assume this is a server limitation, but it works perfectly fine via curl:
curl -vvv -x 146.19.106.109:3128 -u dlpuser:rNrKYTX9g7z3RgJRmxWuGHbeu ftp://ftp.dlptest.com/xxx.txt
Does anyone know why this happens? As far as I know, curl also uses http CONNECT under the hood, so I'm wondering what I'm missing.
PS Don't worry about the credentials in the code - these are exposed for network testing
FTP over the HTTP proxy is not done with a CONNECT request. Instead, the proxy is instructed to obtain the ftp://...
URL using a normal GET request. This is also what curl is doing. In Go, it can be done like this (error handling omitted for simplicity):
// create the TCP connection to the proxy conn, err := net.Dial("tcp", "146.19.106.109:3128") // create the request with the ftp:// URL req, err := http.NewRequest(http.MethodGet, "ftp://ftp.dlptest.com/xxx.txt", nil) req.SetBasicAuth("dlpuser","rNrKYTX9g7z3RgJRmxWuGHbeu") // don't use req.Write but req.WriteProxy, since the request line must // contain the full URL and not only the path err = req.WriteProxy(conn) // read response resp, err := http.ReadResponse(bufio.NewReader(conn), req) body, _ := io.ReadAll(resp.Body)
The above is the detailed content of Connect to ftp through proxy from golang. For more information, please follow other related articles on the PHP Chinese website!