FtpInitiateConnectionCommand.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "FtpInitiateConnectionCommand.h"
  36. #include <map>
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "Request.h"
  40. #include "FtpNegotiationCommand.h"
  41. #include "HttpRequest.h"
  42. #include "Segment.h"
  43. #include "HttpRequestCommand.h"
  44. #include "FtpTunnelRequestCommand.h"
  45. #include "DlAbortEx.h"
  46. #include "Logger.h"
  47. #include "LogFactory.h"
  48. #include "message.h"
  49. #include "prefs.h"
  50. #include "HttpConnection.h"
  51. #include "SocketCore.h"
  52. #include "util.h"
  53. #include "AuthConfigFactory.h"
  54. #include "AuthConfig.h"
  55. #include "fmt.h"
  56. #include "SocketRecvBuffer.h"
  57. #include "BackupIPv4ConnectCommand.h"
  58. #include "FtpNegotiationConnectChain.h"
  59. #include "FtpTunnelRequestConnectChain.h"
  60. #include "HttpRequestConnectChain.h"
  61. #ifdef HAVE_LIBSSH2
  62. #include "SftpNegotiationConnectChain.h"
  63. #include "SftpNegotiationCommand.h"
  64. #endif // HAVE_LIBSSH2
  65. namespace aria2 {
  66. FtpInitiateConnectionCommand::FtpInitiateConnectionCommand(
  67. cuid_t cuid, const std::shared_ptr<Request>& req,
  68. const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
  69. DownloadEngine* e)
  70. : InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e)
  71. {
  72. }
  73. FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() = default;
  74. std::unique_ptr<Command> FtpInitiateConnectionCommand::createNextCommandProxied(
  75. const std::string& hostname, const std::string& addr, uint16_t port,
  76. const std::vector<std::string>& resolvedAddresses,
  77. const std::shared_ptr<Request>& proxyRequest)
  78. {
  79. std::string options;
  80. std::shared_ptr<SocketCore> pooledSocket;
  81. std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol());
  82. // sftp always use tunnel mode
  83. if (proxyMethod == V_GET) {
  84. pooledSocket = getDownloadEngine()->popPooledSocket(
  85. getRequest()->getHost(), getRequest()->getPort(),
  86. proxyRequest->getHost(), proxyRequest->getPort());
  87. }
  88. else {
  89. pooledSocket = getDownloadEngine()->popPooledSocket(
  90. options, getRequest()->getHost(), getRequest()->getPort(),
  91. getDownloadEngine()
  92. ->getAuthConfigFactory()
  93. ->createAuthConfig(getRequest(), getOption().get())
  94. ->getUser(),
  95. proxyRequest->getHost(), proxyRequest->getPort());
  96. }
  97. if (!pooledSocket) {
  98. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port));
  99. createSocket();
  100. getSocket()->establishConnection(addr, port);
  101. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  102. auto c = make_unique<ConnectCommand>(getCuid(), getRequest(), proxyRequest,
  103. getFileEntry(), getRequestGroup(),
  104. getDownloadEngine(), getSocket());
  105. if (proxyMethod == V_GET) {
  106. // Use GET for FTP via HTTP proxy.
  107. getRequest()->setMethod(Request::METHOD_GET);
  108. c->setControlChain(std::make_shared<HttpRequestConnectChain>());
  109. }
  110. else if (proxyMethod == V_TUNNEL) {
  111. c->setControlChain(std::make_shared<FtpTunnelRequestConnectChain>());
  112. }
  113. else {
  114. // Unreachable
  115. assert(0);
  116. return nullptr;
  117. }
  118. setupBackupConnection(hostname, addr, port, c.get());
  119. return std::move(c);
  120. }
  121. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  122. if (proxyMethod == V_TUNNEL) {
  123. #ifdef HAVE_LIBSSH2
  124. if (getRequest()->getProtocol() == "sftp") {
  125. return make_unique<SftpNegotiationCommand>(
  126. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  127. getDownloadEngine(), pooledSocket,
  128. SftpNegotiationCommand::SEQ_SFTP_OPEN);
  129. }
  130. #endif // HAVE_LIBSSH2
  131. // options contains "baseWorkingDir"
  132. return make_unique<FtpNegotiationCommand>(
  133. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  134. getDownloadEngine(), pooledSocket,
  135. FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options);
  136. }
  137. assert(getRequest()->getProtocol() == "ftp");
  138. if (proxyMethod != V_GET) {
  139. assert(0);
  140. return nullptr;
  141. }
  142. // Use GET for FTP via HTTP proxy.
  143. getRequest()->setMethod(Request::METHOD_GET);
  144. auto socketRecvBuffer = std::make_shared<SocketRecvBuffer>(pooledSocket);
  145. auto hc = std::make_shared<HttpConnection>(getCuid(), pooledSocket,
  146. socketRecvBuffer);
  147. auto c = make_unique<HttpRequestCommand>(
  148. getCuid(), getRequest(), getFileEntry(), getRequestGroup(), hc,
  149. getDownloadEngine(), pooledSocket);
  150. c->setProxyRequest(proxyRequest);
  151. return std::move(c);
  152. }
  153. std::unique_ptr<Command> FtpInitiateConnectionCommand::createNextCommandPlain(
  154. const std::string& hostname, const std::string& addr, uint16_t port,
  155. const std::vector<std::string>& resolvedAddresses)
  156. {
  157. std::string options;
  158. std::shared_ptr<SocketCore> pooledSocket =
  159. getDownloadEngine()->popPooledSocket(
  160. options, resolvedAddresses, getRequest()->getPort(),
  161. getDownloadEngine()
  162. ->getAuthConfigFactory()
  163. ->createAuthConfig(getRequest(), getOption().get())
  164. ->getUser());
  165. if (!pooledSocket) {
  166. A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port));
  167. createSocket();
  168. getSocket()->establishConnection(addr, port);
  169. getRequest()->setConnectedAddrInfo(hostname, addr, port);
  170. auto c = make_unique<ConnectCommand>(getCuid(), getRequest(), nullptr,
  171. getFileEntry(), getRequestGroup(),
  172. getDownloadEngine(), getSocket());
  173. if (getRequest()->getProtocol() == "sftp") {
  174. #ifdef HAVE_LIBSSH2
  175. c->setControlChain(std::make_shared<SftpNegotiationConnectChain>());
  176. #else // !HAVE_LIBSSH2
  177. assert(0);
  178. #endif // !HAVE_LIBSSH2
  179. }
  180. else {
  181. c->setControlChain(std::make_shared<FtpNegotiationConnectChain>());
  182. }
  183. setupBackupConnection(hostname, addr, port, c.get());
  184. return std::move(c);
  185. }
  186. setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
  187. #ifdef HAVE_LIBSSH2
  188. if (getRequest()->getProtocol() == "sftp") {
  189. return make_unique<SftpNegotiationCommand>(
  190. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  191. getDownloadEngine(), pooledSocket,
  192. SftpNegotiationCommand::SEQ_SFTP_OPEN);
  193. }
  194. #endif // HAVE_LIBSSH2
  195. // options contains "baseWorkingDir"
  196. return make_unique<FtpNegotiationCommand>(
  197. getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  198. getDownloadEngine(), pooledSocket,
  199. FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options);
  200. }
  201. std::unique_ptr<Command> FtpInitiateConnectionCommand::createNextCommand(
  202. const std::string& hostname, const std::string& addr, uint16_t port,
  203. const std::vector<std::string>& resolvedAddresses,
  204. const std::shared_ptr<Request>& proxyRequest)
  205. {
  206. if (proxyRequest) {
  207. return createNextCommandProxied(hostname, addr, port, resolvedAddresses,
  208. proxyRequest);
  209. }
  210. return createNextCommandPlain(hostname, addr, port, resolvedAddresses);
  211. }
  212. } // namespace aria2