2012-07-27 7 views
6

Sto utilizzando la libreria rubino Net :: FTP per connettersi a un server FTP e scaricare file. Funziona tutto bene, ma ora ho bisogno di usare un proxy in uscita dal momento che il loro firewall annulla gli indirizzi IP, e sto usando Heroku per ospitare il sito. Sto provando il nuovo add-on Proximo che sembra promettente, ma non posso ottenere Net :: FTP per usarlo.Come utilizzare il server proxy con Ruby Net :: FTP?

compaiono i seguenti nella Net::FTP docs:

connect (host, port = ftp_port)

realizza un collegamento FTP ad ospitare, eventualmente sovrascrivendo la porta predefinita. Se la variabile di ambiente SOCKS_SERVER è impostata, imposta la connessione tramite un proxy SOCKS. Solleva un'eccezione (tipicamente Errno :: ECONNREFUSED) se non è possibile stabilire la connessione.

Ma provare a impostare questa variabile di ambiente o l'impostazione ENV['SOCKS_SERVER'] = proximo_url non funziona neanche. Qualcuno sa come farlo correttamente? Non deve essere un proxy SOCKS, un proxy HTTP lo farà, ma non sono sicuro che la libreria Net :: FTP supporti questo.

... dopo alcune ricerche ...

ho scoperto che Net :: FTP cerca una classe chiamata SOCKSSocket, per la quale ho trovato these docs. Ma non posso includerlo. include 'socket' restituisce false che sono abbastanza sicuro significa che è già caricato.

+0

Lo hai mai capito? –

+0

@JohnWright purtroppo non :( –

risposta

1

È possibile utilizzare la gemma come in combinazione con es. Quotaguard. Assicurati di utilizzare uno degli IP statici forniti (e non il nome host DNS) come server proxy, poiché FTP e loadbalancing possono causare problemi.

Socksify::debug = true 
proxy_uri = URI(ENV['QUOTAGUARDSTATIC_URL']) 
proxy_hostname = proxy_uri.hostname 
proxy_port = 1080 
TCPSocket::socks_username = proxy_uri.user 
TCPSocket::socks_password = proxy_uri.password 
Socksify::proxy(proxy_hostname, proxy_port) do |soc| 
     Net::FTP.open(server) do |ftp| 
      ftp.passive = true 
      ftp.debug_mode = true 
      ftp.login user, password 
      ftp.get(export, storelocation) 
      ftp.close 
     end 
end 
0

Questa è la mia opinione sul problema. La piastra della caldaia:

# Connects to ftp through a socks proxy 
# 
# TODO: testing, use ssl, capture debugging output, recovery on network failure... 
# 
# @param ftpHost [String ] Domain name or ip address of the ftp server 
# @param proxyHost [String ] Host name or ip address of the socks proxy 
# @param proxyPort [FixNum ] Port number of the socks proxy 
# @param proxyUser [String ] User name for connecting to the proxy if needed 
# @param proxyPass [String ] Password for connecting to the proxy if needed 
# @param ftpPort [FixNum ] Ftp port, defaults to 21 
# @param ftpUser [String ] Ftp username 
# @param ftpPass [String ] Ftp password 
# @param debug  [Boolean] Whether to turn on debug output of both socksify and Net::FTP, will be sent to STDOUT 
# @param passive [Boolean] Whether to use passive FTP mode 
# 
def ftp(

     ftpHost:      , 
     proxyHost:     , 
     proxyPort:     , 
     proxyUser: nil    , 
     proxyPass: nil    , 
     ftpPort: Net::FTP::FTP_PORT , 
     ftpUser: 'anonymous'  , 
     ftpPass: '[email protected]'  , 
     debug:  false    , 
     passive: true 

    ) 

    Socksify::debug   = debug 
    TCPSocket::socks_username = proxyUser 
    TCPSocket::socks_password = proxyPass 

    Socksify::proxy(proxyHost, proxyPort) do |_| 

     begin 

      # Check whether we got an ip address or a domain name. 
      # If needed we'll do dns through the socket to avoid dns leaks. 
      # 
      Regexp.new(URI::RFC2396_Parser.new.pattern[ :IPV4ADDR ]) =~ ftpHost or 

       ftpHost = Socksify::resolve(ftpHost) 


      ftp   = Net::FTP.new 
      ftp.debug_mode = debug 
      ftp.passive = passive 

      ftp.connect ftpHost, ftpPort 
      ftp.login ftpUser, ftpPass 

      yield ftp 

     ensure 

      ftp.close 

     end 

    end 

end 

Ora nell'applicazione effettiva si può semplicemente fare:

def listRemoteFiles 

    connect do |ftp| 

     puts ftp.list.inspect # or other cool functions from Net::FTP 

    end 

end 


def connect &block 

    ftp({ 

     debug:  true   , 
     proxyHost: your.proxy.host , 
     proxyPort: your.proxy.port , 
     ftpHost: your.ftpHost 

    }, &block) 

end 

Grazie a tvgriek per puntare nella giusta direzione.