Class: IP::RandomWAN

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wan-ip-gen.rb

Overview

WAN range IP generator

Example:

IP::RandomWAN.new.first(5)

Constant Summary collapse

RANDOM_RANGE =

exclude current network and local broadcast

(0x01000000...0xffffffff).freeze
EXCLUDE_RANGES =
[
  (0xe0000000..0xefffffff), # 224.0.0.0 - 239.255.255.255
  (0xf0000000..0xfffffffe), # 240.0.0.0 - 255.255.255.254
  (0xA000000..0xAFFFFFF),   # 10.0.0.0 - 10.255.255.255
  (0x7F000000..0x7FFFFFFF), # 127.0.0.0 - 127.255.255.255
  (0x64400000..0x647FFFFF), # 100.64.0.0 - 100.127.255.255
  (0xAC100000..0xAC1FFFFF), # 172.16.0.0 - 172.31.255.255
  (0xC6120000..0xC613FFFF), # 198.18.0.0 - 198.19.255.255
  (0xA9FE0000..0xA9FEFFFF), # 169.254.0.0 - 169.254.255.255
  (0xC0A80000..0xC0A8FFFF), # 192.168.0.0 - 192.168.255.255
  (0xC0000000..0xC00000FF), # 192.0.0.0 - 192.0.0.255
  (0xC0000200..0xC00002FF), # 192.0.2.0 - 192.0.2.255
  (0xc0586300..0xc05863ff), # 192.88.99.0 - 192.88.99.255
  (0xC6336400..0xC63364FF), # 198.51.100.0 - 198.51.100.255
  (0xCB007100..0xCB0071FF), # 203.0.113.0 - 203.0.113.255
  (0xe9fc0000..0xe9fc00ff)  # 233.252.0.0 - 233.252.0.255
].freeze

Instance Method Summary collapse

Instance Method Details

#each(&block) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/wan-ip-gen.rb', line 33

def each(&block)
  loop do
    intip = rand(RANDOM_RANGE)
    next if EXCLUDE_RANGES.any? { |r| r.cover? intip }

    block.call [intip].pack('N').unpack('CCCC').join('.')
  end
end

#each_socket(port = 80, connect_timeout = 0.75) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wan-ip-gen.rb', line 42

def each_socket(port = 80, connect_timeout = 0.75)
  each do |ip|
    Socket.tcp(ip, port, connect_timeout: connect_timeout) do |s|
      yield(s, ip)
    end
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
    next
  rescue Errno::ECONNRESET, Errno::ENOPROTOOPT
    next
  end
end