Prevent multiple instances of the client on the same machine

28 views
Skip to first unread message

Vito Botta

unread,
May 23, 2011, 6:27:13 AM5/23/11
to ruby-brb
hello pals

does anyone have any suggestion on how to prevent multiple instances
of a BrB client on the same machine?

cheers
Vito

Guillaume Luccisano

unread,
May 23, 2011, 10:32:18 AM5/23/11
to ruby...@googlegroups.com
Hi, why do you mean exactly, that your server will accept only one client per host ?

2011/5/23 Vito Botta <ma...@vitobotta.com>

Vito Botta

unread,
May 23, 2011, 10:52:05 AM5/23/11
to ruby...@googlegroups.com
Hi Guillaume,

Yes, correct. I think I figured it out. On the server, when a client connects and a tunnel is initialised, I can find out the client's ip and port through EventMachine, and then use this information to see if a client is already connected from the same IP address and, if this is the case, I call close_connection on the tunnel so the other instance on the client knows it has to shut down.

So this seems to be working well, plus it gave me another idea: since I can get the IP address of the client easily, I can also use then drb's acl to implement some sort of app-firewall :)

For now I am just monkey patching to see how this works but I will add to some other changes I mentioned earlier and send a pull request:

class BrB::Tunnel::Handler
  attr_reader :ip_address, :port
  alias_method :post_init_without_client_ip_and_port, :post_init

  def post_init
    @port, @ip_address = Socket.unpack_sockaddr_in(get_peername) 
    post_init_without_client_ip_and_port
  end
end

then to access and use this information:

    BrB::Service.start_service(@settings) do |event, client| 
      if event == :register
        if allowed_client? client
          if already_connected? client
            client.close_connection
          else
            logger.info  "Client connected on #{client.ip_address}:#{client.port}"
          end
        else
          logger.warn  "Blocked connection from unauthorised client on #{client.ip_address}:#{client.port}"
        end
      else 
     # do something else
      end
    end


Cheers
Vito
Reply all
Reply to author
Forward
0 new messages