Module: DLDInternet::Net::FindMyIPs

Defined in:
lib/dldinternet/net/findmyips.rb

Instance Method Summary collapse

Instance Method Details

#getLocalObject Also known as: getInternal

Find IP using simple query to DYNDNS.org & Internal facing interface



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dldinternet/net/findmyips.rb', line 16

def getLocal
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  #RevDNS = Off, so dont resolve!

  UDPSocket.open do |sox|
    sox.connect '8.8.8.8', 1 #74.125.227.32', 1 #this is Google, but we dont actually send anything, just looking for how machine would if we did :p
    sox.addr.last
  end
rescue SocketError => e # sox shit happens?
  puts "Socket Error!".light_red
  puts "\t=> #{e}".light_red
ensure
  Socket.do_not_reverse_lookup = orig
end

#getPublic(url = 'checkip.dyndns.org', timeout = 5, maxtries = 10, report_progress = true) ⇒ Object Also known as: getExternal

Simply fetch the external from dyndns, not so much magic :p



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dldinternet/net/findmyips.rb', line 32

def getPublic(url = 'checkip.dyndns.org', timeout = 5, maxtries = 10, report_progress = true)
  tries= 0
  uri = URI.parse(url)
  uri = URI.parse("http://#{url}") unless uri and uri.scheme
  while true
    begin
      dyn = ::Net::HTTP.new(uri.host, uri.port)
      dyn.open_timeout = timeout
      dyn.read_timeout = timeout
      unless dyn.nil?
        resp = dyn.get(uri.path)
        unless resp.nil?
          ip = resp.read_body.match(/\d+\.\d+\.\d+\.\d+/)
          return ip[0]
        end
      end
    rescue Timeout::Error => e
      #puts e.class.to_s+" "+e.message
      puts "No response from #{uri.host}:#{uri.port} within #{timeout} seconds".light_red if report_progress
      tries += 1
      raise StandardError.new("Max retries exceeded") if tries >= maxtries
    end
  end
end