Browse Source

V0.5.10
1、添加了对SFTP的支持,但是因为CURL的问题,暂时未验证成功

Apple 3 months ago
parent
commit
007124246a
2 changed files with 51 additions and 0 deletions
  1. 40 0
      common/CurlFtp/CurlFtp.cpp
  2. 11 0
      common/CurlFtp/CurlFtp.h

+ 40 - 0
common/CurlFtp/CurlFtp.cpp

@@ -403,10 +403,34 @@ bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
     m_port = port;
     m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
     FMTLOG_INFO("Set ftpUrl = {}", m_ftpUrl);
+    m_isSftp = false;
+    return true;
+}
 
+/* 设置SFTP的IP和端口 */
+bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
+{
+    m_IP = IP;
+    m_port = port;
+    m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
+    FMTLOG_INFO("Set sftpUrl = {}", m_ftpUrl);
+    m_isSftp = true;
     return true;
 }
 
+/* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
+void CurlFtp::setIgnoreSSLCert(bool isIgnore)
+{
+    m_isIgnoreSSLCert = isIgnore;
+}
+
+/* 设置CA证书文件 */
+void CurlFtp::setCaCertFile(const std::string& caCertFile)
+{
+    m_caCertFile = caCertFile;
+}
+
+
 
 /* 设置用户名和密码 */
 bool CurlFtp::setFtpUsernameAndPassword(const std::string& username, const std::string& password)
@@ -685,6 +709,22 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     curl_easy_setopt(curl, CURLOPT_XFERINFODATA, nullptr);
     /* 启用下载进度回调函数 */
     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
+    /* 判断是否是SFTP */
+    if(m_isSftp)
+    {
+        /* 判断是否需要设置CA证书 */
+        if(m_isIgnoreSSLCert)
+        {
+            /* 忽略证书验证 */
+            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+            /* 忽略主机名验证 */
+            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+        } else
+        {
+            /* 设置CA证书 */
+            curl_easy_setopt(curl, CURLOPT_CAINFO, m_caCertFile.c_str());
+        }
+    }
 
     /* 发送请求 */
     CURLcode res = curl_easy_perform(curl);

+ 11 - 0
common/CurlFtp/CurlFtp.h

@@ -35,6 +35,12 @@ public:
     /*********************** 成员函数 *********************/
     /* 设置IP和端口 */
     bool setFtpIPAndPort(const std::string& IP, const int port);
+    /* 设置SFTP的IP和端口 */
+    bool setSftpIPAndPort(const std::string& IP, const int port);
+    /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
+    void setIgnoreSSLCert(bool isIgnore);
+    /* 设置CA证书文件 */
+    void setCaCertFile(const std::string& caCertFile);
     /* 设置用户名和密码 */
     bool setFtpUsernameAndPassword(const std::string& username, const std::string& password);
     /* 列出文件列表 */
@@ -73,6 +79,11 @@ private:
 
     std::string m_username;
     std::string m_password;
+
+    bool m_isSftp = false;                  /* 是否是SFTP */
+    bool m_isIgnoreSSLCert = false;         /* 是否忽略SSL证书 */
+
+    std::string m_caCertFile;               /* CA证书文件 */
 };