Ruby Net::HTTP.start giving connection refused - ruby-on-rails

I have a ruby class in which I am making Net::HTTP.start call inside perform method.This is the code:
class Poller
def self.perform(args)
uri = URI('http://localhost:8081/adhoc/dummy_poll?noAuth=true')
begin
Net::HTTP.start(uri.host, uri.port, :read_timeout=>30) do |http|
request = Net::HTTP::Get.new uri
#response = http.request request
#responsecode = #response.code
end
rescue Exception => e
Rails.logger.error "error mess==>"+e.message()
#responsecode = "408"
end
When I enqueue this within resque from another class using this line:
Resque.enqueue_in_with_queue(:data_workflow_manager,10,Poller,args)
I get this error:
Connection refused - connect(2) for "::1" port 8081.
However, HTTP.start works fine without any errors when the perform method is called in some another class using:
Poller.perform(args)
Please help me understand, why is this happening?

try explicitly connecting to the loop back address. There may be resolution issues with localhost.
uri = URI('http://127.0.0.1:8081/adhoc/dummy_poll?noAuth=true')

It's likely you do not have any server running on port 8081 which is why the connection will be refused.
Check it with lsof -i and look for programs bound to 8081 on a linux machine.

Related

Ruby on Rails configure faraday http client correctly for SSL connections

I am constantly getting Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL) while using the ruby faraday gem. I don't have alot of experience with ruby on rails.
I have a docker ruby on rails service running on elastic beanstalk that is using puma with ssl. CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"] I have a network load balancer configured with it to forward 443 -> 8443 (i've experimented with both self signed certs and real wild card certs).
ssl_bind '0.0.0.0', '8443', {
key: '/var/app/ssl/something.key',
cert: '/var/app/ssl/something.crt'
}
This configuration works as expected
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: staging
* Listening on tcp://0.0.0.0:3000
* Listening on ssl://0.0.0.0:8443?cert=/var/app/ssl/something.crt&key=/var/app/ssl/something.key&verify_mode=none
and I can get the healthz status page with both types of certs. Using httpie and --verify=no for the self signed.
$ http https://backend.something.com/healthz
HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
{
"name": "my-backend-service",
"version": "0.0.1"
}
I have another ruby on rails backend service that makes api requests to this service using faraday. Ive removed some of the request/response code from my actual.
def connection(baseUrl, options = {})
conn = Faraday.new(url: baseUrl) do |c|
# dont really know if this is needed or not
# http.use_ssl? is always false
c.adapter :net_http do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # if http.use_ssl?
end
end
I don't know if this http.verify_mode is actually working. I cant really find that method anywhere around here on ruby-doc.org
If you try to make a request it will just end up faiing.
conn = connection(https://backend.something.com)
response = conn.post '/foo', params[:foo].to_json
The logs show from starting to parameters and then the http.rb:939 error. I realize the parameters aren't valid here but they aren't my problem.
Started POST "/foo"
Processing by FooController#create as */*
Parameters: {"paramter"=>"something", "paramter"=>"something", "paramter"=>"something"}
ERROR -- : /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect': Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL)
If I make the same request from httpie or curl to this service I get the expected results over both http/https.
$ http POST https://backend.something.com parameter="something" parameter="something" parameter="something"
HTTP/1.1 201 Created
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
If you inspect the conn object it kinda still seems like it is sitting on its default value for #url_prefix = http:/. Found in the docs above, but I don't know if I'm looking at the correct thing the correct ruby way. I imagined that Faraday.new(url: baseUrl) would parse the correct schema, which is https.
#<Faraday::Connection:0x0000565351701430 #parallel_manager=nil, #headers={"User-Agent"=>"Faraday v0.15.4"}, #params={}, #options=#<Faraday::RequestOptions (empty)>, #ssl=#<Faraday::SSLOptions (empty)>, #default_parallel_manager=nil, #builder=#<Faraday::RackBuilder:0x0000565351700f58 #handlers=[FaradayMiddleware::EncodeJson, FaradayMiddleware::ParseJson, Faraday::Adapter::NetHttp]>, #url_prefix=#<URI::HTTP http:/>, #manual_proxy=false, #proxy=nil, #temp_proxy=nil>
It seems that the the address you are trying to connect to is invalid. From what I can see in the error:
ERROR -- : /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect': Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL)
it is nil. Ensure that the host is properly set.

Faraday::ConnectionFailed (Failed to open TCP connection to api.arknode.net:4001

I am using ark-ruby gem and that code
client = Ark::Client.new(
:ip => 'api.arknode.net',
:port => '4001',
:nethash => '6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988',
:version => '1.0.1'
)
But it gives an error on heroku that Faraday::ConnectionFailed (Failed to open TCP connection to api.arknode.net:4001 (Connection refused - connect(2) for "api.arknode.net" port 4001)):
How can I fix it?
api.arknode.net has been non-responsive for about a week. HTTPS should be used for that URL. You can try changing that to 5.39.9.240 port 4001 for now and just HTTP.
Note: The network is set up with a list of seed servers. You should use a seed server to get a list of peers in which to make API calls. https://ark.brianfaust.me/#/Peer/peers_getPeers.

Net::HTTP::Proxy requests... how do you do it?

I am having trouble making a proxy call. How on earth do you make this happen?
Here is what I have so far:
proxy_addr = '162.243.105.128'
proxy_port = 6170
Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.google.com') {|http| http}
I get:
#<#<Class:0x007f85d8a092d0> www.google.com:80 open=false>
When:
Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.google.com') {|http| http.get('www.google.com')}
I get
#<Net::HTTPNotImplemented 501 Tor is not an HTTP Proxy readbody=true>
How do I make this work!
Tor is a SOCKS proxy, not an HTTP proxy.
I ran into the same issue. I don't think it is possible with plain Net::HTTP. Install http://socksify.rubyforge.org/
require 'socksify/http'
http = Net::HTTP::SOCKSProxy(addr, port)
puts http.get(URI('http://echoip.com'))

TCP connection not establishing for bunny on ruby on rails

i am trying to do simple "hello world" program with bunny on ruby on rails.As basic am trying to get connection with tcp its throwing an error.
Could not estabilish TCP connection to 127.0.0.1:5672:
My code:
require "bunny"
begin
conn = Bunny.new
conn.start
rescue => e
puts e
end
what i have to do, am completely new to this.

net-ldap authentication format in rails?

def authenticate(username, password)
require 'net-ldap'
ldap = Net::LDAP.new
ldap.host = 'server.local'
ldap.port = 389
ldap.base = 'cn=users, dc=server, dc=local'
ldap.auth username, password
if ldap.bind
puts "authentication succeeded"
else
puts "authentication failed"
end
The above is the code i use in my method and i am not sure why my attempts fail. I am trying to authenticate user. I could not find where i am going wrong? It puts authentication failed every time. why? please help me.
First up see if the computer you are using can talk to the LDAP server
telnet server.local 389
Obviously you want to be replacing server.local with your actual server details. If you can't log into the server this way then port 389 isn't open and you may need to be running on the SSL port 636. Try the previous command with 636 rather than 389 to see if that is the case.
If you are unable to telnet into the server on either of those ports you've either got a firewall rule blocking you from talking to it, LDAP is configured on a non standard port or something else is seriously wrong.
A working port 636 will probably mean you need to run something like the following.
require "net-ldap"
ldap = Net::LDAP.new(
host: "server.local"
port: 636
encryption: :simple_tls
)
ldap.auth username, password
Failing all of that an error message is going to be pretty useful so try running
if ldap.bind
# Yay!
else
puts ldap.get_operation_result
end
With some results from this maybe we can help you a bit more.

Resources