/* */ #include "FtpInitiateConnectionCommand.h" #include #include "DownloadEngine.h" #include "Option.h" #include "Request.h" #include "FtpNegotiationCommand.h" #include "HttpRequest.h" #include "Segment.h" #include "HttpRequestCommand.h" #include "FtpTunnelRequestCommand.h" #include "DlAbortEx.h" #include "Logger.h" #include "LogFactory.h" #include "message.h" #include "prefs.h" #include "HttpConnection.h" #include "SocketCore.h" #include "util.h" #include "AuthConfigFactory.h" #include "AuthConfig.h" #include "fmt.h" #include "SocketRecvBuffer.h" #include "BackupIPv4ConnectCommand.h" #include "FtpNegotiationConnectChain.h" #include "FtpTunnelRequestConnectChain.h" #include "HttpRequestConnectChain.h" #ifdef HAVE_LIBSSH2 # include "SftpNegotiationConnectChain.h" # include "SftpNegotiationCommand.h" #endif // HAVE_LIBSSH2 namespace aria2 { FtpInitiateConnectionCommand::FtpInitiateConnectionCommand (cuid_t cuid, const std::shared_ptr& req, const std::shared_ptr& fileEntry, RequestGroup* requestGroup, DownloadEngine* e) :InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {} FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {} std::unique_ptr FtpInitiateConnectionCommand::createNextCommandProxied (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses, const std::shared_ptr& proxyRequest) { std::string options; std::shared_ptr pooledSocket; std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol()); // sftp always use tunnel mode if(proxyMethod == V_GET) { pooledSocket = getDownloadEngine()->popPooledSocket (getRequest()->getHost(), getRequest()->getPort(), proxyRequest->getHost(), proxyRequest->getPort()); } else { pooledSocket = getDownloadEngine()->popPooledSocket (options, getRequest()->getHost(), getRequest()->getPort(), getDownloadEngine()->getAuthConfigFactory()->createAuthConfig (getRequest(), getOption().get())->getUser(), proxyRequest->getHost(), proxyRequest->getPort()); } if(!pooledSocket) { A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port)); createSocket(); getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); auto c = make_unique(getCuid(), getRequest(), proxyRequest, getFileEntry(), getRequestGroup(), getDownloadEngine(), getSocket()); if(proxyMethod == V_GET) { // Use GET for FTP via HTTP proxy. getRequest()->setMethod(Request::METHOD_GET); c->setControlChain(std::make_shared()); } else if(proxyMethod == V_TUNNEL) { c->setControlChain(std::make_shared()); } else { // Unreachable assert(0); return nullptr; } setupBackupConnection(hostname, addr, port, c.get()); return std::move(c); } setConnectedAddrInfo(getRequest(), hostname, pooledSocket); if(proxyMethod == V_TUNNEL) { #ifdef HAVE_LIBSSH2 if (getRequest()->getProtocol() == "sftp") { return make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, SftpNegotiationCommand::SEQ_SFTP_OPEN); } #endif // HAVE_LIBSSH2 // options contains "baseWorkingDir" return make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options); } assert(getRequest()->getProtocol() == "ftp"); if(proxyMethod != V_GET) { assert(0); return nullptr; } // Use GET for FTP via HTTP proxy. getRequest()->setMethod(Request::METHOD_GET); auto socketRecvBuffer = std::make_shared(pooledSocket); auto hc = std::make_shared(getCuid(), pooledSocket, socketRecvBuffer); auto c = make_unique(getCuid(), getRequest(), getFileEntry(), getRequestGroup(), hc, getDownloadEngine(), pooledSocket); c->setProxyRequest(proxyRequest); return std::move(c); } std::unique_ptr FtpInitiateConnectionCommand::createNextCommandPlain (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses) { std::string options; std::shared_ptr pooledSocket = getDownloadEngine()->popPooledSocket (options, resolvedAddresses, getRequest()->getPort(), getDownloadEngine()->getAuthConfigFactory()->createAuthConfig (getRequest(), getOption().get())->getUser()); if(!pooledSocket) { A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), addr.c_str(), port)); createSocket(); getSocket()->establishConnection(addr, port); getRequest()->setConnectedAddrInfo(hostname, addr, port); auto c = make_unique(getCuid(), getRequest(), nullptr, getFileEntry(), getRequestGroup(), getDownloadEngine(), getSocket()); if(getRequest()->getProtocol() == "sftp") { #ifdef HAVE_LIBSSH2 c->setControlChain(std::make_shared()); #else // !HAVE_LIBSSH2 assert(0); #endif // !HAVE_LIBSSH2 } else { c->setControlChain(std::make_shared()); } setupBackupConnection(hostname, addr, port, c.get()); return std::move(c); } setConnectedAddrInfo(getRequest(), hostname, pooledSocket); #ifdef HAVE_LIBSSH2 if (getRequest()->getProtocol() == "sftp") { return make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, SftpNegotiationCommand::SEQ_SFTP_OPEN); } #endif // HAVE_LIBSSH2 // options contains "baseWorkingDir" return make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine(), pooledSocket, FtpNegotiationCommand::SEQ_SEND_CWD_PREP, options); } std::unique_ptr FtpInitiateConnectionCommand::createNextCommand (const std::string& hostname, const std::string& addr, uint16_t port, const std::vector& resolvedAddresses, const std::shared_ptr& proxyRequest) { if (proxyRequest) { return createNextCommandProxied(hostname, addr, port, resolvedAddresses, proxyRequest); } return createNextCommandPlain(hostname, addr, port, resolvedAddresses); } } // namespace aria2