Ruby and Bonjour - ruby

I am new to ruby and its library, but how do i combine DNSSD and TCPServer together?
I know i can register DNSSD service via
registration = DNSSD.register("My Files", hostname, "local.", port)
And I can create a DNSSD service for my TCPServer this way, but how do i specify the hostname (service name) as the above?
server = TCPServer.open(port)
DNSSD.announce server, 'my awesome HTTP server'
I want to broadcast my server, so that the client can resolve the DNSSD service and establish a connection.
Pardon me if thats a stupid question.

I manage to setup it up via
Register a DNSSD aka Bonjour Service
DNSSD.register("", hostname, "local.", port) do |register_reply|
puts "Registration result: #{register_reply.inspect}"
end
Setup TCPServer to listen on the same port
blackjack = TCPServer.open("",port)
loop do
socket = blackjack.accept
peeraddr = socket.peeraddr
puts "Connection from %s:%d" % socket.peeraddr.values_at(2, 1)
end

Related

JMeter - Listen to a port on a given IP

I want to run a jmeter test, which listens to a port on a given ip, and prints the messages which are being sent to that port. I have tried using this:
SocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getByName("<client ipAddress>"),<port number>);
def server = new ServerSocket();
server.bind(inetSocketAddress);
while(true) {
server.accept { socket ->
log.info('Someone is connected')
socket.withStreams { input, output ->
def line = input.newReader().readLine()
log.info('Received message: ' + line)
}
log.info("Connection processed")
}
}
But this is giving me error - "Cannot assign requested address: JVM_Bind
"
Is there any alternate way to approach this? Or what changes do i need to do for the current approach to work?
You copied and pasted this code from the right place and it should work just fine. Evidence:
as per the BindException documentation
Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned.
So I can think of 2 options:
Your <client ipAddress> is not correct and cannot be resolved.
Something is already running on the <port number>, you cannot have 2 applications listening to the same port, the first one will be successful and another one will fail
More information:
Fixing java.net.BindException: Cannot assign requested address: JVM_Bind in Tomcat, Jetty
Apache Groovy - Why and How You Should Use It

Setting address of whois service for ruby whois gem

Using the ruby whois gem, how do I set the server address of the whois service?
Setting the bind_host, I get an error.
> whois_client = Whois::Client.new(bind_host: "192.0.47.59", bind_port: 43)
=> #<Whois::Client:0x00000008188e7e50 #timeout=10, #settings={:bind_host=>"192.0.47.59", :bind_port=>43}>
> record = whois_client.lookup('wandajackson.com')
Whois::ConnectionError: Errno::EADDRNOTAVAIL: Can't assign requested address - bind(2) for "192.0.47.59" port 43
from (irb):4
I'm pretty sure bind_host doesn't refer to the host used for the whois lookup, but instead refers to the adapter binding on the server running your code. By default it binds to 0.0.0.0, or all the adapters on the local server.
If you want to have the whois gem use a custom server address for looking up whois information then it appears that you have to specify it in one of the following ways:
# Define a server for the .com TLD
Whois::Server.define :tld, "com", "your.whois.server.address"
Whois.whois("google.com")
# Define a new server for an range of IPv4 addresses
Whois::Server.define :ipv4, "10.0.0.0/8", "your.whois.server.address"
Whois.whois("10.0.0.1")
# Define a new server for an range of IPv6 addresses
Whois::Server.define :ipv6, "2001:2000::/19", "your.whois.server.address"
Whois.whois("2001:2000:85a3:0000:0000:8a2e:0370:7334")
These examples were taken from https://www.rubydoc.info/gems/whois/Whois/Server.

OpenSSL::SSL::SSLError while testing the net/smtp library

I am trying to send emails using the net/smtp library but I receive the following error message:
SSL_connect returned=1 errno=0 state=unknown state: unknown protocol (OpenSSL::SSL::SSLError)
The code I am using is an extract from Peter Cooper's Beginning Ruby book, modified to use SSL, as required by yahoo servers:
require 'net/smtp'
message = %q{
From: Private Person <me#privacy.net>
To: myself <username#yahoo.com>
Subject: SMTP e-mail test
This is a test e-mail message.
}
smtp = Net::SMTP.new('smtp.mail.yahoo.com', 587)
smtp.enable_ssl
smtp.start('example.org', 'username#yahoo.com', 'password', :login) do |s|
s.send_message message, 'me#privacy.net', 'username#yahoo.com'
end
I tested the above code with Ruby 2.3.1 and Ruby 2.5.1 but I keep receiving the same error message.
By using Net::SMTP#enable_ssl (which is an alias to Net::SMTP#enable_tls), you are instructing the Ruby SMTP client to open a TLS tunnel as the very first step after connecting to the server. This requires the server to also support this on the used server port.
With Yahoos SMTP servers, they support this mode on port 465. With many other providers, you will see that they only support opportunistic TLS on port 587.
What happens there is that the client first starts with plain text SMTP and then negotiates with the server whether they support to update the connection to a TLS tunnel by using the STARTTLS command.
With net/smtp in Ruby, you can use Net::SMTP#enable_starttls instead.
If you are not sure of the server supports STARTTLS and you want to gracefully fallbvack to plaintext transfer of your password and the email to the server, you can also use Net::SMTP#enable_starttls_auto.
Thus, with Yahoo, you have two options:
you can use Net::SMTP#enable_tls on port 465
or you can use Net::SMTP#enable_starttls on port 587

How to find the port of a TcpServer started using TcpServer.new(0) in ruby

How can I find the port being allocated by windows to a TcpServer started using TcpServer.new(0) in Ruby
You can get it using addr:
tcp_server = TCPServer.new(0)
port = tcp_server.addr[1]

suddenly PossibleAuthenticationFailureError in amqp

I'm using the ruby amqp gem. I ran a AMQP.start event loop, but 'suddenlyit raised aPossibleAuthenticationFailureError` during the loop.
AMQP.start(amqp_config) do |connection|
channel = AMQP::Channel.new connection
channel.on_error do |channel, channel_close|
puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
end
my_worker = MyWorker.new
my_worker.start
end
[amqp] Detected TCP connection failure
/home/raincole/.rvm/gems/ruby-1.9.3-p125/gems/amq-client-0.9.3/lib/amq/client/async/adapters/event_machine.rb:164:in `block in initialize': AMQP broker closed TCP connection before authentication succeeded: this usually means authentication failure due to misconfiguration. Settings are {:host=>"localhost", :port=>5672, :user=>"guest", :pass=>"guest", :vhost=>"/", :timeout=>nil, :logging=>false, :ssl=>false, :broker=>nil, :frame_max=>131072} (AMQP::PossibleAuthenticationFailureError)
The weird part is, my worker have received some messages before I got PossibleAuthenticationFailureError. It seems like that the configuration should be correct(and I checked it over and over again).
Are there other potential reasons for PossibleAuthenticationFailureError?
I recommend a 4 step approach to investigating this issue:
a) Eliminate the obvious - Are your credentials correct and is the user account alive and well (default = 'guest')? Are you connecting to the appropriate vhost (default = '/')?
$ rabbitmqctl list_users
Listing users ...
guest [administrator]
...done.
$ rabbitmqctl list_user_permissions guest
Listing permissions for user "guest" ...
/ .* .* .*
<your_vhost> .* .* .*
...done.
b) What do the rabbitmq connection logs say?
On a Mac OS installation of rabbitmq (using brew), the logs can be found in /usr/local/var/log/rabbitmq, but your log location could be elsewhere depending on OS and installation preferences.
You may see the following lines in the rabbit#localhost.log file. Not a lot of help...and so proceed to step (c). Otherwise, investigate as per what you see in the log.
=INFO REPORT==== 15-Feb-2013::00:42:21 ===
accepting AMQP connection <0.691.0> (127.0.0.1:53108 -> 127.0.0.1:5672)
=WARNING REPORT==== 15-Feb-2013::00:42:21 ===
closing AMQP connection <0.691.0> (127.0.0.1:53108 -> 127.0.0.1:5672):
connection_closed_abruptly
c) Is rabbitmq's listener (Erlang client) alive. Default port = 5672. Simplest way to check is to send a garbage message to that port and look for an 'AMQP' response:
$ telnet localhost 5672
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
asdasd
AMQP
Connection closed by foreign host.
(d) Is the event loop reactor closing prematurely, before the AMQP.connect (or AMQP.start) actions have had a chance to complete authentication?
EM.run
connection = AMQP.connect(:host => 'localhost', :vhost => '/') do
# your code here
end
EM.stop
end
With all 'your code' sitting in a callback, the EM.stop runs instantaneously after the AMQP.connect instruction. This gives no time for the connection to be suitably established.
What worked for me here was to add a timer and handle disconnects gracefully.
EM.run
connection = AMQP.connect(:host => 'localhost', :vhost => '/')
# your code here
end
graceful_exit = Proc.new {
connection.close { EM.stop }
}
EM.add_timer(3, graceful_exit)
end
The reason I put the EM.stop block in a Proc is so that I can reuse it for other graceful exits (say, when trapping 'TERM' and 'INT' signals)
Hope this helps.

Resources