2015-11-14 03:25:30 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2003-09-26 05:35:44 -04:00
|
|
|
require 'socket'
|
2001-05-30 05:10:30 -04:00
|
|
|
require 'resolv'
|
|
|
|
|
|
|
|
class << IPSocket
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
alias original_resolv_getaddress getaddress
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def getaddress(host)
|
2004-02-01 10:21:24 -05:00
|
|
|
begin
|
|
|
|
return Resolv.getaddress(host).to_s
|
|
|
|
rescue Resolv::ResolvError
|
2004-02-01 12:42:08 -05:00
|
|
|
raise SocketError, "Hostname not known: #{host}"
|
2004-02-01 10:21:24 -05:00
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-21 08:10:53 -04:00
|
|
|
class TCPSocket < IPSocket
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
alias original_resolv_initialize initialize
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
def initialize(host, serv, *rest)
|
2013-03-11 09:47:04 -04:00
|
|
|
rest[0] = IPSocket.getaddress(rest[0]) if rest[0]
|
2004-02-01 10:21:24 -05:00
|
|
|
original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-21 08:10:53 -04:00
|
|
|
class UDPSocket < IPSocket
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
alias original_resolv_bind bind
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
def bind(host, port)
|
2008-08-07 21:58:40 -04:00
|
|
|
host = IPSocket.getaddress(host) if host != ""
|
|
|
|
original_resolv_bind(host, port)
|
2003-09-26 05:35:44 -04:00
|
|
|
end
|
|
|
|
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
alias original_resolv_connect connect
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def connect(host, port)
|
2004-02-01 10:21:24 -05:00
|
|
|
original_resolv_connect(IPSocket.getaddress(host), port)
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
alias original_resolv_send send
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2001-05-30 05:10:30 -04:00
|
|
|
def send(mesg, flags, *rest)
|
2004-02-01 10:21:24 -05:00
|
|
|
if rest.length == 2
|
|
|
|
host, port = rest
|
|
|
|
begin
|
|
|
|
addrs = Resolv.getaddresses(host)
|
|
|
|
rescue Resolv::ResolvError
|
2004-02-01 12:42:08 -05:00
|
|
|
raise SocketError, "Hostname not known: #{host}"
|
2004-02-01 10:21:24 -05:00
|
|
|
end
|
|
|
|
addrs[0...-1].each {|addr|
|
|
|
|
begin
|
|
|
|
return original_resolv_send(mesg, flags, addr, port)
|
|
|
|
rescue SystemCallError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
original_resolv_send(mesg, flags, addrs[-1], port)
|
|
|
|
else
|
|
|
|
original_resolv_send(mesg, flags, *rest)
|
|
|
|
end
|
2001-05-30 05:10:30 -04:00
|
|
|
end
|
|
|
|
end
|
2003-09-26 05:35:44 -04:00
|
|
|
|
2010-03-21 08:10:53 -04:00
|
|
|
class SOCKSSocket < TCPSocket
|
2010-03-21 23:33:14 -04:00
|
|
|
# :stopdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
alias original_resolv_initialize initialize
|
2010-03-21 23:33:14 -04:00
|
|
|
# :startdoc:
|
2003-09-26 05:35:44 -04:00
|
|
|
def initialize(host, serv)
|
2004-02-01 10:21:24 -05:00
|
|
|
original_resolv_initialize(IPSocket.getaddress(host), port)
|
2003-09-26 05:35:44 -04:00
|
|
|
end
|
|
|
|
end if defined? SOCKSSocket
|