背景
公司某项业务受限于政策,要求实现跨网数据传输,最终上层反馈使用ftp服务来实现内外网数据交互,要求技术人员进行功能验证。
验证
安装ftp服务
因个人长期在外出差,公司vpn网络比较卡,于是在利用手上的两台电脑环境搭建ftp服务器。访问dockerhub,搜索vsftp关键字,决定使用下面
fauria/vsftpd latest 007276d7208b 22 months ago 255MB
查看详情,正常复制启动命令
docker run -d -v /Users/tianwen/data:/home/vsftpd -p 20:20 -p 21:21 -p 21100-21110:21100-21110 -e FTP_USER=dgh -e FTP_PASS=woshidoudou -e PASV_ADDRESS=127.0.0.1 -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 --name vsftpd --restart=always fauria/vsftpd
检测到服务启动后,寻思写个测试代码用于验证功能,确认文件上传、下载功能正常。
import "github.com/jlaffaye/ftp"
func storeFtp() {
c, err := ftp.Dial("10.1.83.17:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
err = c.Login("dgh", "woshidoudou")
if err != nil {
log.Fatal(err)
}
data := bytes.NewBufferString("Hello World")
err = c.Stor("test-file.txt", data)
if err != nil {
panic(err)
}
// Do something with the FTP conn
if err := c.Quit(); err != nil {
log.Fatal(err)
}
}
func readFtp() {
c, err := ftp.Dial("10.1.83.17:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
err = c.Login("dgh", "woshidoudou")
if err != nil {
log.Fatal(err)
}
r, err := c.Retr("test-file.txt")
if err != nil {
panic(err)
}
defer r.Close()
buf, err := ioutil.ReadAll(r)
println(string(buf))
// Do something with the FTP conn
if err := c.Quit(); err != nil {
log.Fatal(err)
}
}
经验证,文件读写功能正常,其他主要写业务代码就正常了。
因公司主语言是JAVA,因此,需要将测试代码从golang转为JAVA,原本以为分分钟的事情,没想到翻车了,断断续续的捣腾了好久。
刚开始,我使用的是hutool工具包,复制代码进行测试
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import java.io.IOException;
public class DemoApplication {
public static void main(String[] args) throws IOException {
// 匿名登录(无需帐号密码的FTP服务器)
Ftp ftp = new Ftp("10.1.83.17", 21, "dgh", "woshidoudou");
// 上传本地文件
boolean success = ftp.upload("", FileUtil.file("/Users/tianwen/Downloads/aa.txt"));
//关闭连接
ftp.close();
}
}
发现success 始终返回false,经过debug,了解登录成功,但是文件上传始终返回false,很疑惑。然后疯狂的查询ftp相关资料,了解了诸如ftp主动模式、被动模式等信息后,做了如下调整:
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import java.io.IOException;
public class DemoApplication {
public static void main(String[] args) throws IOException {
// 匿名登录(无需帐号密码的FTP服务器)
Ftp ftp = new Ftp("10.1.83.17", 21, "dgh", "woshidoudou");
//设置为被动模式
ftp.setMode(FtpMode.Passive);
// 上传本地文件
boolean success = ftp.upload("", FileUtil.file("/Users/tianwen/Downloads/aa.txt"));
//关闭连接
ftp.close();
}
}
测试后还是无效,完全懵逼。我初步怀疑是hutool工具包出现问题(因为hutool进行了二次封住,只返回了false,并未给出错误信息,我也没进行debug),于是我换了工具包,采用 org.apache.commons.net.ftp.FTPClient,代码如下:
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DemoApplication {
public static void main(String[] args) throws IOException {
FTPClient client = new FTPClient();
String sFTP = "10.1.83.17";
String sUser = "dgh";
String sPassword = "woshidoudou";
client.connect(sFTP);
client.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
client.setControlEncoding("UTF-8");
client.enterLocalPassiveMode();
client.setBufferSize(1024);
boolean login = client.login(sUser, sPassword);
boolean a = client.storeFile("dgh.txt", new FileInputStream("/Users/tianwen/Downloads/aa.txt"));
boolean b = client.retrieveFile("dgh.txt", new FileOutputStream("/Users/tianwen/Downloads/aa-1.txt"));
client.pwd();
}
}
这次依旧失败,不过打出了错误日志给我定位问题,
/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:51760,suspend=y,server=n -javaagent:/Users/tianwen/Library/Caches/JetBrains/IntelliJIdea2020.3/captureAgent/debugger-agent.jar -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/fmcardjni-1.2.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/tianwen/Documents/project/demo-hello/target/classes:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.4.3/spring-boot-starter-web-2.4.3.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-starter/2.4.3/spring-boot-starter-2.4.3.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot/2.4.3/spring-boot-2.4.3.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.4.3/spring-boot-autoconfigure-2.4.3.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.4.3/spring-boot-starter-logging-2.4.3.jar:/Users/tianwen/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/tianwen/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/tianwen/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/Users/tianwen/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/Users/tianwen/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/Users/tianwen/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.4.3/spring-boot-starter-json-2.4.3.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.11.4/jackson-datatype-jdk8-2.11.4.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.4/jackson-datatype-jsr310-2.11.4.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.11.4/jackson-module-parameter-names-2.11.4.jar:/Users/tianwen/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.4.3/spring-boot-starter-tomcat-2.4.3.jar:/Users/tianwen/.m2/repository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar:/Users/tianwen/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.43/tomcat-embed-websocket-9.0.43.jar:/Users/tianwen/.m2/repository/org/springframework/spring-web/5.3.4/spring-web-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-beans/5.3.4/spring-beans-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-webmvc/5.3.4/spring-webmvc-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-aop/5.3.4/spring-aop-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-context/5.3.4/spring-context-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-expression/5.3.4/spring-expression-5.3.4.jar:/Users/tianwen/.m2/repository/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar:/Users/tianwen/.m2/repository/net/minidev/json-smart/2.3/json-smart-2.3.jar:/Users/tianwen/.m2/repository/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar:/Users/tianwen/.m2/repository/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar:/Users/tianwen/.m2/repository/org/assertj/assertj-core/3.18.1/assertj-core-3.18.1.jar:/Users/tianwen/.m2/repository/org/springframework/spring-core/5.3.4/spring-core-5.3.4.jar:/Users/tianwen/.m2/repository/org/springframework/spring-jcl/5.3.4/spring-jcl-5.3.4.jar:/Users/tianwen/.m2/repository/org/redisson/redisson/3.15.4/redisson-3.15.4.jar:/Users/tianwen/.m2/repository/io/netty/netty-common/4.1.59.Final/netty-common-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-codec/4.1.59.Final/netty-codec-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-buffer/4.1.59.Final/netty-buffer-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-transport/4.1.59.Final/netty-transport-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-resolver/4.1.59.Final/netty-resolver-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-resolver-dns/4.1.59.Final/netty-resolver-dns-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-codec-dns/4.1.59.Final/netty-codec-dns-4.1.59.Final.jar:/Users/tianwen/.m2/repository/io/netty/netty-handler/4.1.59.Final/netty-handler-4.1.59.Final.jar:/Users/tianwen/.m2/repository/javax/cache/cache-api/1.1.1/cache-api-1.1.1.jar:/Users/tianwen/.m2/repository/io/projectreactor/reactor-core/3.4.3/reactor-core-3.4.3.jar:/Users/tianwen/.m2/repository/org/reactivestreams/reactive-streams/1.0.3/reactive-streams-1.0.3.jar:/Users/tianwen/.m2/repository/io/reactivex/rxjava3/rxjava/3.0.12/rxjava-3.0.12.jar:/Users/tianwen/.m2/repository/org/jboss/marshalling/jboss-marshalling-river/2.0.11.Final/jboss-marshalling-river-2.0.11.Final.jar:/Users/tianwen/.m2/repository/org/jboss/marshalling/jboss-marshalling/2.0.11.Final/jboss-marshalling-2.0.11.Final.jar:/Users/tianwen/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/tianwen/.m2/repository/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.11.4/jackson-dataformat-yaml-2.11.4.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/Users/tianwen/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar:/Users/tianwen/.m2/repository/net/bytebuddy/byte-buddy/1.10.20/byte-buddy-1.10.20.jar:/Users/tianwen/.m2/repository/org/jodd/jodd-bean/5.1.6/jodd-bean-5.1.6.jar:/Users/tianwen/.m2/repository/org/jodd/jodd-core/5.1.6/jodd-core-5.1.6.jar:/Users/tianwen/.m2/repository/org/apache/thrift/libthrift/0.14.1/libthrift-0.14.1.jar:/Users/tianwen/.m2/repository/org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar:/Users/tianwen/.m2/repository/commons-codec/commons-codec/1.15/commons-codec-1.15.jar:/Users/tianwen/.m2/repository/org/apache/httpcomponents/httpcore/4.4.14/httpcore-4.4.14.jar:/Users/tianwen/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.43/tomcat-embed-core-9.0.43.jar:/Users/tianwen/.m2/repository/org/apache/tomcat/tomcat-annotations-api/9.0.43/tomcat-annotations-api-9.0.43.jar:/Users/tianwen/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/tianwen/.m2/repository/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar:/Users/tianwen/.m2/repository/commons-net/commons-net/3.6/commons-net-3.6.jar:/Users/tianwen/.m2/repository/cn/hutool/hutool-all/5.8.5/hutool-all-5.8.5.jar:/Users/tianwen/.m2/repository/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar:/Users/tianwen/.m2/repository/org/apache/commons/commons-pool2/2.9.0/commons-pool2-2.9.0.jar:/Users/tianwen/.m2/repository/us/codecraft/webmagic-core/0.7.3/webmagic-core-0.7.3.jar:/Users/tianwen/.m2/repository/us/codecraft/xsoup/0.3.1/xsoup-0.3.1.jar:/Users/tianwen/.m2/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar:/Users/tianwen/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/tianwen/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/tianwen/.m2/repository/org/jsoup/jsoup/1.10.3/jsoup-1.10.3.jar:/Users/tianwen/.m2/repository/commons-io/commons-io/1.3.2/commons-io-1.3.2.jar:/Users/tianwen/.m2/repository/com/alibaba/fastjson/1.2.28/fastjson-1.2.28.jar:/Users/tianwen/.m2/repository/us/codecraft/webmagic-extension/0.7.3/webmagic-extension-0.7.3.jar:/Users/tianwen/.m2/repository/redis/clients/jedis/3.3.0/jedis-3.3.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar com.example.demo.DemoApplication
Connected to the target VM, address: '127.0.0.1:51760', transport: 'socket'
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:606)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:920)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
at com.example.demo.DemoApplication.main(DemoApplication.java:24)
Disconnected from the target VM, address: '127.0.0.1:51760', transport: 'socket'
Process finished with exit code 1
我按照日志信息,逐步debug,发现了一个反常的信息,
image.png
经过前面ftp各种知识恶补,我已经知道ftp在被动模式下,在客户端上传文件后,服务端会告知客户端自己的ip和打开的端口,让客户端自己把文件推送上门。但是debug中socket 尝试 connect的主机和端口是127.0.0.1:21103 ,按照正常逻辑,这里的socket应该尝试和我另一台电脑的上的ftp server进行连接,而不是127.0.0.1。简单说,代码尝试访问ftp server(10.1.83.17)时,端口正确但是ip不正确。
咋回事呢,不可能两个工具包都出问题了呀。难不成是ftp server 出现了问题,虽然前面golang代码验证了一次,但是加入是巧合怎么办。于是我下载了一个ftp客户端软件,在我写代码的那台电脑上进行验证,经过验证,客户端在被动模式下,文件的读写都是ok的呀。
没招了,感觉,已经想到最差解决方案了,是在不行,我把golang写的打成可执行文件,让java代码直接调用,放弃,扔下电脑,看小说去了。
过了一会,又猜想,难道是docker引起的,vsftp container 在被动模式下,返回自身ip时读取错了ip?
一想,感觉很有这个可能,然后将注意力转到 ftp server那边去。因为使用docker 容器部署,为了调整默认账户信息,我电脑上还存在当时的启动脚本,重新看了下,我自定义了账户、密码、挂在目录,突然发现了PASV_ADDRESS 这个默认值是127.0.0.1,恍然大悟。
我停止和删除了当前运行的container,修改启动脚本,将PASV_ADDRESS设置为10.1.83.17。
docker run -d -v /Users/tianwen/data:/home/vsftpd -p 20:20 -p 21:21 -p 21100-21110:21100-21110 -e FTP_USER=dgh -e FTP_PASS=woshidoudou -e PASV_ADDRESS=10.1.83.17 -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 --name vsftpd --restart=always fauria/vsftpd
服务启动后,客户端工具链接,验证读写功能都正常。然后重新启动java程序,发现这次上传文件也正常,没有其他异常信息。
于是我明白原因了,java的ftp工具在被动模式下,客户端在请求上传文件时,在接收到服务端反馈的的主机和端口后,并没有验证是否和前面登录的服务ip是否一致。因在docker 启动时,我指定了PASV_ADDRESS=127.0.0.1,所以java一直在尝试和本机的某个端口建立联系,我本机对应端口并无服务监听,所以才会connnect refuced。
我将代码恢复,继续验证使用了hutool工具的方法,发现读写也是正常的。
总结
以后copy别人代码时,一定要看清理和理解,不然坑死自己。
网友评论