YkLib 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/YkLib.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/YkLib/Yk/__advance__.rb +151 -0
- data/lib/YkLib/Yk/__defun__.rb +44 -0
- data/lib/YkLib/Yk/__hook__.rb +244 -0
- data/lib/YkLib/Yk/__minmax__.rb +123 -0
- data/lib/YkLib/Yk/__stdlog.rb +329 -0
- data/lib/YkLib/Yk/adhocLiterals/email.rb +119 -0
- data/lib/YkLib/Yk/adhocLiterals/path.rb +402 -0
- data/lib/YkLib/Yk/adhocLiterals/tag.rb +19 -0
- data/lib/YkLib/Yk/adhocLiterals/url.rb +36 -0
- data/lib/YkLib/Yk/adhocLiterals.rb +199 -0
- data/lib/YkLib/Yk/auto_escseq.rb +5 -0
- data/lib/YkLib/Yk/auto_pstore.rb +179 -0
- data/lib/YkLib/Yk/bsearch.rb +120 -0
- data/lib/YkLib/Yk/clambda.rb +309 -0
- data/lib/YkLib/Yk/confLine.rb +423 -0
- data/lib/YkLib/Yk/create_tty_width_available.rb +24 -0
- data/lib/YkLib/Yk/crypt.rb +26 -0
- data/lib/YkLib/Yk/debug2 +1 -0
- data/lib/YkLib/Yk/debug2.rb +473 -0
- data/lib/YkLib/Yk/debugout.rb +139 -0
- data/lib/YkLib/Yk/email_tz.rb +533 -0
- data/lib/YkLib/Yk/enum_expect.rb +170 -0
- data/lib/YkLib/Yk/errlog.rb +5 -0
- data/lib/YkLib/Yk/escseq.rb +59 -0
- data/lib/YkLib/Yk/eval_alt.rb +281 -0
- data/lib/YkLib/Yk/expector.rb +93 -0
- data/lib/YkLib/Yk/fetch.rb +556 -0
- data/lib/YkLib/Yk/fetch_old.rb +290 -0
- data/lib/YkLib/Yk/fib.rb +158 -0
- data/lib/YkLib/Yk/file_aux.rb +843 -0
- data/lib/YkLib/Yk/file_aux2.rb +919 -0
- data/lib/YkLib/Yk/file_aux_old.rb +160 -0
- data/lib/YkLib/Yk/filemod.rb +19 -0
- data/lib/YkLib/Yk/force_escseq.rb +3 -0
- data/lib/YkLib/Yk/generator__.rb +144 -0
- data/lib/YkLib/Yk/generator__.rb.org +139 -0
- data/lib/YkLib/Yk/indenter/argless_case.rb +46 -0
- data/lib/YkLib/Yk/indenter/each_token.rb +671 -0
- data/lib/YkLib/Yk/indenter/free_case.rb +313 -0
- data/lib/YkLib/Yk/indenter/if_less.rb +53 -0
- data/lib/YkLib/Yk/indenter/independent_ensure.rb +23 -0
- data/lib/YkLib/Yk/indenter/independent_rescue.rb +23 -0
- data/lib/YkLib/Yk/indenter/operand_circumflex.rb +0 -0
- data/lib/YkLib/Yk/indenter/operand_period.rb +16 -0
- data/lib/YkLib/Yk/indenter/parenless_and.rb +37 -0
- data/lib/YkLib/Yk/indenter/post_test.rb +48 -0
- data/lib/YkLib/Yk/indenter/token.rb +1525 -0
- data/lib/YkLib/Yk/indenter.rb +1382 -0
- data/lib/YkLib/Yk/inot.rb +265 -0
- data/lib/YkLib/Yk/intf.rb +815 -0
- data/lib/YkLib/Yk/io_aux.rb +1332 -0
- data/lib/YkLib/Yk/ioctl.rb +60 -0
- data/lib/YkLib/Yk/ipcc.rb +87 -0
- data/lib/YkLib/Yk/ipcountry.rb +207 -0
- data/lib/YkLib/Yk/ipv4adr.rb +318 -0
- data/lib/YkLib/Yk/localmail.rb +276 -0
- data/lib/YkLib/Yk/method_chain.rb +359 -0
- data/lib/YkLib/Yk/misc_tz.rb +1716 -0
- data/lib/YkLib/Yk/missing_method.rb +50 -0
- data/lib/YkLib/Yk/mojiConv.rb +257 -0
- data/lib/YkLib/Yk/nostdlog.rb +4 -0
- data/lib/YkLib/Yk/on_marshal.rb +20 -0
- data/lib/YkLib/Yk/overrider.rb +47 -0
- data/lib/YkLib/Yk/path.rb +293 -0
- data/lib/YkLib/Yk/path_aux.rb +883 -0
- data/lib/YkLib/Yk/path_aux_alt.rb +0 -0
- data/lib/YkLib/Yk/path_rep.rb +1267 -0
- data/lib/YkLib/Yk/pg_setup.rb +917 -0
- data/lib/YkLib/Yk/procinfo.rb +314 -0
- data/lib/YkLib/Yk/proclist.rb +492 -0
- data/lib/YkLib/Yk/property.rb +863 -0
- data/lib/YkLib/Yk/ranger.rb +606 -0
- data/lib/YkLib/Yk/resolv_tz.rb +88 -0
- data/lib/YkLib/Yk/rlprompt.rb +73 -0
- data/lib/YkLib/Yk/rootexec.rb +48 -0
- data/lib/YkLib/Yk/rpm-packageproxy.rb +784 -0
- data/lib/YkLib/Yk/rpm-packageproxy2.rb +1430 -0
- data/lib/YkLib/Yk/rwhen.rb +21 -0
- data/lib/YkLib/Yk/selector.rb +124 -0
- data/lib/YkLib/Yk/set.rb +170 -0
- data/lib/YkLib/Yk/shellquote.rb +300 -0
- data/lib/YkLib/Yk/sio.rb +1001 -0
- data/lib/YkLib/Yk/sio0.rb +835 -0
- data/lib/YkLib/Yk/sio_aux.rb +1524 -0
- data/lib/YkLib/Yk/sio_inot.rb +86 -0
- data/lib/YkLib/Yk/sock_aux.rb +42 -0
- data/lib/YkLib/Yk/spipe.rb +843 -0
- data/lib/YkLib/Yk/sql_table.rb +565 -0
- data/lib/YkLib/Yk/stdlog.rb +4 -0
- data/lib/YkLib/Yk/syscommand.rb +173 -0
- data/lib/YkLib/Yk/sysinit.rb +75 -0
- data/lib/YkLib/Yk/ttyFontWidth.rb +46113 -0
- data/lib/YkLib/Yk/tty_char.dump +0 -0
- data/lib/YkLib/Yk/tty_char.rb +47 -0
- data/lib/YkLib/Yk/tty_char_create.rb +437031 -0
- data/lib/YkLib/Yk/tty_char_static.rb +437016 -0
- data/lib/YkLib/Yk/tty_rewrite.rb +142 -0
- data/lib/YkLib/Yk/tty_str.rb +461 -0
- data/lib/YkLib/Yk/tty_width.dat.rb +114 -0
- data/lib/YkLib/Yk/tty_width.rb +180 -0
- data/lib/YkLib/Yk/tty_width_available +569 -0
- data/lib/YkLib/Yk/tty_width_list +0 -0
- data/lib/YkLib/Yk/tty_width_list.linux +280 -0
- data/lib/YkLib/Yk/tty_width_list.windows +324 -0
- data/lib/YkLib/Yk/tz_tty +0 -0
- data/lib/YkLib/Yk/tz_tty.rb +0 -0
- data/lib/YkLib/Yk/uprepos.rb +94 -0
- data/lib/YkLib/Yk/userinfo.rb +91 -0
- data/lib/YkLib/Yk/with.rb +109 -0
- data/lib/YkLib/version.rb +3 -0
- data/lib/YkLib.rb +6 -0
- metadata +170 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
# generated by 'ruby-ioctl-0.1.4-0tz1.src.rpm' on Thu Feb 11 19:33:56 +0800 2010
|
2
|
+
|
3
|
+
module Ioctl
|
4
|
+
FIOSETOWN = 0x8901
|
5
|
+
SIOCSPGRP = 0x8902
|
6
|
+
FIOGETOWN = 0x8903
|
7
|
+
SIOCGPGRP = 0x8904
|
8
|
+
SIOCGSTAMP = 0x8906
|
9
|
+
TCGETS = 0x5401
|
10
|
+
TCSETS = 0x5402
|
11
|
+
TCSETSW = 0x5403
|
12
|
+
TCSETSF = 0x5404
|
13
|
+
TCGETA = 0x5405
|
14
|
+
TCSETA = 0x5406
|
15
|
+
TCSETAW = 0x5407
|
16
|
+
TCSETAF = 0x5408
|
17
|
+
TCSBRK = 0x5409
|
18
|
+
TCXONC = 0x540a
|
19
|
+
TCFLSH = 0x540b
|
20
|
+
TIOCEXCL = 0x540c
|
21
|
+
TIOCNXCL = 0x540d
|
22
|
+
TIOCSCTTY = 0x540e
|
23
|
+
TIOCGPGRP = 0x540f
|
24
|
+
TIOCSPGRP = 0x5410
|
25
|
+
TIOCOUTQ = 0x5411
|
26
|
+
TIOCSTI = 0x5412
|
27
|
+
TIOCGWINSZ = 0x5413
|
28
|
+
TIOCSWINSZ = 0x5414
|
29
|
+
TIOCMGET = 0x5415
|
30
|
+
TIOCMBIS = 0x5416
|
31
|
+
TIOCMBIC = 0x5417
|
32
|
+
TIOCMSET = 0x5418
|
33
|
+
TIOCGSOFTCAR = 0x5419
|
34
|
+
TIOCSSOFTCAR = 0x541a
|
35
|
+
FIONREAD = 0x541b
|
36
|
+
TIOCINQ = 0x541b
|
37
|
+
TIOCLINUX = 0x541c
|
38
|
+
TIOCCONS = 0x541d
|
39
|
+
TIOCGSERIAL = 0x541e
|
40
|
+
TIOCSSERIAL = 0x541f
|
41
|
+
TIOCPKT = 0x5420
|
42
|
+
FIONBIO = 0x5421
|
43
|
+
TIOCNOTTY = 0x5422
|
44
|
+
TIOCSETD = 0x5423
|
45
|
+
TIOCGETD = 0x5424
|
46
|
+
TCSBRKP = 0x5425
|
47
|
+
FIONCLEX = 0x5450
|
48
|
+
FIOCLEX = 0x5451
|
49
|
+
FIOASYNC = 0x5452
|
50
|
+
TIOCSERCONFIG = 0x5453
|
51
|
+
TIOCSERGWILD = 0x5454
|
52
|
+
TIOCSERSWILD = 0x5455
|
53
|
+
TIOCGLCKTRMIOS = 0x5456
|
54
|
+
TIOCSLCKTRMIOS = 0x5457
|
55
|
+
TIOCSERGSTRUCT = 0x5458
|
56
|
+
TIOCSERGETLSR = 0x5459
|
57
|
+
TIOCSERGETMULTI = 0x545a
|
58
|
+
TIOCSERSETMULTI = 0x545b
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
require 'Yk/path_aux'
|
5
|
+
require 'Yk/ipv4adr'
|
6
|
+
#require 'path_aux'
|
7
|
+
#require 'socket'
|
8
|
+
|
9
|
+
|
10
|
+
class String
|
11
|
+
ICONF = "/etc/ipcc.conf"
|
12
|
+
RCONF = "/etc/resolv.conf"
|
13
|
+
port = 53535
|
14
|
+
srv = nil
|
15
|
+
if ICONF.readable_file?
|
16
|
+
ICONF.read_each_line do |ln|
|
17
|
+
if((srv = ln.strip_comment) != "")
|
18
|
+
cfile = ICONF
|
19
|
+
if(srv =~ /:/)
|
20
|
+
port = $'
|
21
|
+
srv = $`
|
22
|
+
end
|
23
|
+
break;
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
if !srv && RCONF.readable_file?
|
28
|
+
RCONF.read_each_line do |ln|
|
29
|
+
arr = ln.strip_comment.split
|
30
|
+
if(arr && arr[0] == "nameserver" && arr[1])
|
31
|
+
cfile = ICONF
|
32
|
+
srv = arr[1]
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
if !srv
|
38
|
+
cfile = "default setting"
|
39
|
+
srv = "127.0.0.1"
|
40
|
+
end
|
41
|
+
sockaddr = nil
|
42
|
+
begin
|
43
|
+
sockaddr = Socket.sockaddr_in(port, srv)
|
44
|
+
rescue SocketError
|
45
|
+
STDERR.write "cannot create socket for #{srv}:#{port} (from #{ICONF})\n"
|
46
|
+
end
|
47
|
+
S = Socket.open(Socket::AF_INET, Socket::SOCK_DGRAM, 0) rescue nil
|
48
|
+
S.connect(sockaddr) if S
|
49
|
+
def to_ipcc
|
50
|
+
ipadr = nil
|
51
|
+
if self =~ /^(\d|[1-9]\d|[12]\d\d)\.(\d|[1-9]\d|[12]\d\d)\.(\d|[1-9]\d|[12]\d\d)\.(\d|[1-9]\d|[12]\d\d)$/
|
52
|
+
if $1.to_i <= 255 && $2.to_i <= 255 && $3.to_i <= 255 && $4.to_i <= 255
|
53
|
+
ipadr = self
|
54
|
+
end
|
55
|
+
end
|
56
|
+
if !ipadr
|
57
|
+
ipadr = to_ipadr
|
58
|
+
end
|
59
|
+
if !ipadr
|
60
|
+
STDERR.write "cannot get ip address for #{self}\n"
|
61
|
+
nil
|
62
|
+
else
|
63
|
+
sin_addr = ipadr.split(/\./).map{|e| e.to_i}.pack("CCCC")
|
64
|
+
10.times do
|
65
|
+
S.write sin_addr
|
66
|
+
sret = IO.select [S], [], [], 0.1
|
67
|
+
if sret && sret[0][0] == S
|
68
|
+
buff = S.recv(6) rescue nil
|
69
|
+
if buff && buff.size == 6 && buff[0..3] == sin_addr
|
70
|
+
if buff[4] == 0 || buff[5] == 0
|
71
|
+
return ""
|
72
|
+
else
|
73
|
+
return buff[4..5]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
if __FILE__.normalize_path == $0.normalize_path
|
85
|
+
print ARGV[0].to_ipcc, "\n" if ARGV[0]
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,207 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
require 'Yk/path_aux'
|
5
|
+
require 'Yk/auto_pstore'
|
6
|
+
require 'Yk/bsearch'
|
7
|
+
require 'Yk/ipv4adr'
|
8
|
+
require 'timeout'
|
9
|
+
|
10
|
+
module IPCountry
|
11
|
+
|
12
|
+
IPListURLs = %w{
|
13
|
+
ftp://ftp.ripe.net/pub/stats/apnic/delegated-apnic-latest
|
14
|
+
ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest
|
15
|
+
ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest
|
16
|
+
ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest
|
17
|
+
ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest
|
18
|
+
}
|
19
|
+
|
20
|
+
|
21
|
+
class IPInfo
|
22
|
+
attr :country, true
|
23
|
+
attr :start, true
|
24
|
+
attr :ed, true
|
25
|
+
attr :nxt, true
|
26
|
+
def initialize c, s, e, n = nil
|
27
|
+
@country = c
|
28
|
+
if c == ""
|
29
|
+
@country = " "
|
30
|
+
end
|
31
|
+
@start = s
|
32
|
+
@ed = e
|
33
|
+
@nxt = n
|
34
|
+
end
|
35
|
+
def inspect
|
36
|
+
[@country, @start.to_ipadr, @start].inspect
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
class AllocList < Array
|
42
|
+
attr :date
|
43
|
+
def refresh
|
44
|
+
tmp = []
|
45
|
+
IPListURLs.each do |u|
|
46
|
+
u.timeout = 3600 * 12
|
47
|
+
end
|
48
|
+
IPListURLs.each do |u|
|
49
|
+
u.read_each_line do |ln|
|
50
|
+
arr = ln.split /\|/
|
51
|
+
if arr[2] == "ipv4" && arr[1] != "*" && arr[1] != ""
|
52
|
+
tmp.push IPInfo.new(arr[1], arr[3].to_i, arr[3].to_i + arr[4].to_i)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
tmp = tmp.sort_by do |a|
|
57
|
+
a.start
|
58
|
+
end
|
59
|
+
tmp2 = []
|
60
|
+
(0..tmp.size - 2).each do |i|
|
61
|
+
if tmp[i].country == tmp[i + 1].country && tmp[i].ed == tmp[i + 1].start
|
62
|
+
tmp[i + 1].start = tmp[i].start
|
63
|
+
next
|
64
|
+
end
|
65
|
+
tmp[i].nxt = tmp[i + 1].start
|
66
|
+
tmp2.push tmp[i]
|
67
|
+
end
|
68
|
+
tmp = tmp2
|
69
|
+
tmp[-1].nxt = tmp[-1].ed
|
70
|
+
clear
|
71
|
+
tmp.each do |e|
|
72
|
+
push e
|
73
|
+
end
|
74
|
+
("/var/data".check_dir / "ipcountry").open "w" do |fw|
|
75
|
+
(0..tmp.size - 2).each do |i|
|
76
|
+
if tmp[i].start >= tmp[i].ed
|
77
|
+
next
|
78
|
+
end
|
79
|
+
if i == 0 && tmp[i].start != 0
|
80
|
+
fw.write([0, 0, 0, 0, 0].pack("ICCCC"))
|
81
|
+
end
|
82
|
+
fw.write([tmp[i].start, tmp[i].country[0..0], tmp[i].country[1..1], 0, 0].pack("IAACC"))
|
83
|
+
if tmp[i].ed < tmp[i + 1].start
|
84
|
+
fw.write([tmp[i].ed, 0, 0, 0, 0].pack("ICCCC"))
|
85
|
+
elsif tmp[i].ed > tmp[i + 1].start
|
86
|
+
tmp[i + 1].start = tmp[i].ed
|
87
|
+
STDERR.write "error #{tmp[i].country}:#{tmp[i].start.to_ipadr}-#{tmp[i].ed.to_ipadr}, #{tmp[i + 1].country}:#{tmp[i + 1].start.to_ipadr}-#{tmp[i + 1].ed.to_ipadr}\n"
|
88
|
+
end
|
89
|
+
if i == tmp.size - 2
|
90
|
+
fw.write([tmp[i + 1].start, tmp[i + 1].country[0..0], tmp[i + 1].country[1..1], 0, 0].pack("IAACC"))
|
91
|
+
if tmp[i + 1].ed != 0
|
92
|
+
fw.write([tmp[i + 1].ed, 0, 0, 0, 0].pack("ICCCC"))
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
@date = Time.now
|
98
|
+
end
|
99
|
+
def initialize
|
100
|
+
refresh
|
101
|
+
end
|
102
|
+
def getCountry ipi
|
103
|
+
if ipi < self[0].start || self[-1].ed < ipi
|
104
|
+
return nil
|
105
|
+
end
|
106
|
+
fst = bsearch_first do |e|
|
107
|
+
if ipi < e.start
|
108
|
+
1
|
109
|
+
elsif ipi < e.nxt
|
110
|
+
0
|
111
|
+
else
|
112
|
+
-1
|
113
|
+
end
|
114
|
+
end
|
115
|
+
if fst
|
116
|
+
fst = self[fst]
|
117
|
+
if ipi < fst.ed
|
118
|
+
return fst.country
|
119
|
+
else
|
120
|
+
return nil
|
121
|
+
end
|
122
|
+
else
|
123
|
+
return nil
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end #module IPCountry
|
129
|
+
|
130
|
+
|
131
|
+
if $0 == __FILE__
|
132
|
+
|
133
|
+
if ARGV.delete "--refreshdb"
|
134
|
+
AList = AutoPstore.allocList = IPCountry::AllocList.new
|
135
|
+
exit 0
|
136
|
+
else
|
137
|
+
AList = AutoPstore.setReadOnly.allocList || (AutoPstore.allocList = IPCountry::AllocList.new)
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
longFormat = ARGV.delete "-l"
|
142
|
+
|
143
|
+
|
144
|
+
if ARGV[0]
|
145
|
+
if a = ARGV[0].to_ipadr
|
146
|
+
if ret = AList.getCountry(a.to_i)
|
147
|
+
if longFormat
|
148
|
+
println "#{ret} : " + "ccode #{ret}".read_p
|
149
|
+
else
|
150
|
+
println ret
|
151
|
+
end
|
152
|
+
exit 0
|
153
|
+
else
|
154
|
+
STDERR.println "'#{ARGV[0]}' is not allocated"
|
155
|
+
if STDIN.tty?
|
156
|
+
print "\n"
|
157
|
+
end
|
158
|
+
exit 1
|
159
|
+
end
|
160
|
+
end
|
161
|
+
else
|
162
|
+
STDERR.write "usage : ipcountry HOST\n ipcountry --refreshdb (for refreshing its database)\n"
|
163
|
+
exit 1
|
164
|
+
end
|
165
|
+
|
166
|
+
else
|
167
|
+
require 'thread'
|
168
|
+
module IPCountry
|
169
|
+
@@mutex = Mutex.new
|
170
|
+
def refresh
|
171
|
+
@@mutex.synchronize do
|
172
|
+
@@list = AutoPstore.readTemp.allocList
|
173
|
+
end
|
174
|
+
end
|
175
|
+
def getCountry ip
|
176
|
+
@@mutex.synchronize do
|
177
|
+
@@list ||= AutoPstore.readTemp.allocList
|
178
|
+
@@list.getCountry(ip.to_i)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
module_function :refresh, :getCountry
|
182
|
+
end
|
183
|
+
AutoPstore.setFileName "ipcountry"
|
184
|
+
IPCountry::List = AutoPstore.readTemp.allocList
|
185
|
+
class String
|
186
|
+
def ipcountry is_cc = false
|
187
|
+
TopLevelMethod.timeout 5 do
|
188
|
+
if ip = to_ipadr
|
189
|
+
if cc = IPCountry::getCountry(ip)
|
190
|
+
if is_cc
|
191
|
+
if cc == is_cc
|
192
|
+
return cc
|
193
|
+
end
|
194
|
+
else
|
195
|
+
return cc
|
196
|
+
end
|
197
|
+
else
|
198
|
+
return nil
|
199
|
+
end
|
200
|
+
else
|
201
|
+
return nil
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
@@ -0,0 +1,318 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
require "resolv"
|
4
|
+
require "Yk/ranger"
|
5
|
+
|
6
|
+
|
7
|
+
class Integer
|
8
|
+
def to_ipadr
|
9
|
+
d1 = (0xff000000 & self) >> 24
|
10
|
+
d2 = (0x00ff0000 & self) >> 16
|
11
|
+
d3 = (0x0000ff00 & self) >> 8
|
12
|
+
d4 = 0x000000ff & self
|
13
|
+
"#{d1}.#{d2}.#{d3}.#{d4}"
|
14
|
+
end
|
15
|
+
def to_mask
|
16
|
+
if 0 <= self && self <= 32
|
17
|
+
String::IPMaskList[self]
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
class String
|
26
|
+
IPMaskList = []
|
27
|
+
33.times do |i|
|
28
|
+
IPMaskList.unshift(((1 << 32) - (1 << i)).to_ipadr)
|
29
|
+
end
|
30
|
+
def __getMaskInfo
|
31
|
+
adr = self
|
32
|
+
if adr =~ /^\d|[1-9]\d$/ && adr.to_i <= 32
|
33
|
+
[IPMaskList[adr.to_i], adr.to_i]
|
34
|
+
elsif i = IPMaskList.index(adr)
|
35
|
+
[adr, i]
|
36
|
+
else
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
def ipmask?
|
41
|
+
if self =~ /^(\d|[1-9]\d)$/ && self.__org__to_i___ <= 32
|
42
|
+
return true
|
43
|
+
elsif IPMaskList.include? self
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
def to_mask
|
49
|
+
if ipmask?
|
50
|
+
if self =~ /\./
|
51
|
+
self.clone
|
52
|
+
else
|
53
|
+
IPMaskList[self.__org__to_i___]
|
54
|
+
end
|
55
|
+
elsif self =~ /\// && $`.ipadr? && $'.ipmask?
|
56
|
+
$` + "/" + $'.to_mask
|
57
|
+
else
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
def to_maskbit
|
62
|
+
if ipmask?
|
63
|
+
if self =~ /\./
|
64
|
+
IPMaskList.index self
|
65
|
+
else
|
66
|
+
self.__org__to_i___
|
67
|
+
end
|
68
|
+
elsif self =~ /\//
|
69
|
+
if $`.ipadr? && $'.ipmask?
|
70
|
+
$` + "/" + $'.to_maskbit.to_s
|
71
|
+
else
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
else
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
def ipadr?
|
79
|
+
if self =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
|
80
|
+
(1..4).each do |i|
|
81
|
+
a = $~[i].to_i
|
82
|
+
a > 255 and return(false)
|
83
|
+
end
|
84
|
+
true
|
85
|
+
else
|
86
|
+
false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
def ipexpr?
|
90
|
+
if !ipadr?
|
91
|
+
if self !~ /\-/ || !$`.ipadr? || !$'.ipadr?
|
92
|
+
if self !~ /\// || !$`.ipadr? || !$'.ipmask?
|
93
|
+
return false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
return true
|
98
|
+
end
|
99
|
+
def to_nets
|
100
|
+
if !ipexpr?
|
101
|
+
return nil
|
102
|
+
end
|
103
|
+
ipexpr = self
|
104
|
+
if ipexpr !~ /\-/
|
105
|
+
[ipexpr]
|
106
|
+
else
|
107
|
+
possibleMask = Proc.new do |i|
|
108
|
+
j2 = nil
|
109
|
+
32.times do |j|
|
110
|
+
if i & (1 << j) != 0
|
111
|
+
j2 = j
|
112
|
+
break
|
113
|
+
end
|
114
|
+
end
|
115
|
+
j2
|
116
|
+
end
|
117
|
+
ret = []
|
118
|
+
arr = ipexpr.split /\-/
|
119
|
+
bg = ip2int(arr[0].strip)
|
120
|
+
ed = ip2int(arr[1].strip)
|
121
|
+
while bg <= ed
|
122
|
+
bgNext = nil
|
123
|
+
possibleMask.call(bg).downto 0 do |m|
|
124
|
+
if (bgNext = bg + (1 << m) - 1) <= ed
|
125
|
+
bgNext += 1
|
126
|
+
if m != 0
|
127
|
+
ret.push "#{int2ip bg}/#{32 - m}"
|
128
|
+
else
|
129
|
+
ret.push int2ip(bg)
|
130
|
+
end
|
131
|
+
break
|
132
|
+
end
|
133
|
+
end
|
134
|
+
bg = bgNext
|
135
|
+
end
|
136
|
+
ret
|
137
|
+
end
|
138
|
+
end
|
139
|
+
def to_djbs
|
140
|
+
ret = []
|
141
|
+
expr = self
|
142
|
+
if expr !~ /\//
|
143
|
+
if expr =~ /\-/
|
144
|
+
to_nets.each do |expr|
|
145
|
+
ret.push *expr.to_djbs
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
net = $`
|
150
|
+
if net.ipaddr?
|
151
|
+
|
152
|
+
end
|
153
|
+
mask = $'.to_i
|
154
|
+
case mask
|
155
|
+
when 25..32
|
156
|
+
base = (ip2int(net) >> (32 - mask)) << (32 - mask)
|
157
|
+
(0 ... 1 << (32 -mask)).each do |i|
|
158
|
+
ret.push int2ip(base + i)
|
159
|
+
end
|
160
|
+
when 24
|
161
|
+
net =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3})\./
|
162
|
+
ret.push $1
|
163
|
+
when 17..23
|
164
|
+
base = (ip2int(net) >> (32 - mask)) << (24 - mask)
|
165
|
+
(0 ... 1 << (24 -mask)).each do |i|
|
166
|
+
ret.push int2ip((base + i) << 8)[/^(\d{1,3}\.\d{1,3}.\d{1,3})\./, 1]
|
167
|
+
end
|
168
|
+
when 16
|
169
|
+
net =~ /^(\d{1,3}\.\d{1,3})\./
|
170
|
+
ret.push $1
|
171
|
+
when 9..15
|
172
|
+
base = (ip2int(net) >> (32 - mask)) << (16 - mask)
|
173
|
+
(0 ... 1 << (16 -mask)).each do |i|
|
174
|
+
ret.push int2ip((base + i) << 16)[/^(\d{1,3}\.\d{1,3})\./, 1]
|
175
|
+
end
|
176
|
+
when 8
|
177
|
+
net =~ /^(\d{1,3})\./
|
178
|
+
ret.push $1
|
179
|
+
when 0..7
|
180
|
+
base = (ip2int(net) >> (32 - mask)) << (8 - mask)
|
181
|
+
(0 ... 1 << (8 -mask)).each do |i|
|
182
|
+
ret.push((base + i).to_s)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
ret
|
186
|
+
end
|
187
|
+
def to_range
|
188
|
+
if ipadr?
|
189
|
+
a = __ip2int
|
190
|
+
return a .. a
|
191
|
+
elsif self =~ /\-/ && $`.ipadr? && $'.ipadr?
|
192
|
+
a = $'.__ip2int
|
193
|
+
b = $`.__ip2int
|
194
|
+
if a < b
|
195
|
+
return a .. b
|
196
|
+
elsif a > b
|
197
|
+
return b .. a
|
198
|
+
else
|
199
|
+
return a .. a
|
200
|
+
end
|
201
|
+
elsif self =~ /\// && $`.ipadr? && m = $'.__getMaskInfo
|
202
|
+
st = $`.__ip2int & m[0].__ip2int
|
203
|
+
ed = st + (1 << (32 - m[1])) - 1
|
204
|
+
return st .. ed
|
205
|
+
else
|
206
|
+
nil
|
207
|
+
end
|
208
|
+
end
|
209
|
+
def __ip2int
|
210
|
+
ret = 0
|
211
|
+
self.split(".").each do |e|
|
212
|
+
ret = ret * 256 + e.to_i
|
213
|
+
end
|
214
|
+
ret
|
215
|
+
end
|
216
|
+
alias_method :__org__to_i___, :to_i
|
217
|
+
def to_i base = 10
|
218
|
+
if ipadr?
|
219
|
+
__ip2int
|
220
|
+
else
|
221
|
+
__org__to_i___ base
|
222
|
+
end
|
223
|
+
end
|
224
|
+
RESOLVERS = Hash.new{|h, k| h[k] = Resolv::DNS.new(:nameserver => [k])}
|
225
|
+
HOST_RESOLVER = Resolv::Hosts.new
|
226
|
+
def to_ipadr server = nil
|
227
|
+
if !ipexpr?
|
228
|
+
ip2 = nil
|
229
|
+
resolver = nil
|
230
|
+
if server && (server = server.to_ipadr)
|
231
|
+
resolver = RESOLVERS[server]
|
232
|
+
else
|
233
|
+
resolver = Resolv
|
234
|
+
end
|
235
|
+
[HOST_RESOLVER, resolver].each do |r|
|
236
|
+
r.each_address(self) do |adr|
|
237
|
+
adr = adr.to_s
|
238
|
+
if !ip2
|
239
|
+
ip2 = adr.to_i
|
240
|
+
elsif ip2 < (ip3 = adr.to_i)
|
241
|
+
ip2 = ip3
|
242
|
+
end
|
243
|
+
end
|
244
|
+
break if ip2
|
245
|
+
end
|
246
|
+
ip2.to_ipadr
|
247
|
+
else
|
248
|
+
self
|
249
|
+
end
|
250
|
+
end
|
251
|
+
def to_ranger
|
252
|
+
IPRanger.new self
|
253
|
+
end
|
254
|
+
def each_ip
|
255
|
+
r = to_range
|
256
|
+
r.each do |i|
|
257
|
+
yield i.to_ipadr
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
|
263
|
+
class IPRanger < Ranger
|
264
|
+
def initialize (*args)
|
265
|
+
super()
|
266
|
+
add *args
|
267
|
+
end
|
268
|
+
def inspect
|
269
|
+
ns = []
|
270
|
+
eachNetRange do |n|
|
271
|
+
ns.push n
|
272
|
+
end
|
273
|
+
"[" + ns.join(",") + "]"
|
274
|
+
end
|
275
|
+
def modargs (*args)
|
276
|
+
nargs = []
|
277
|
+
args.each do |a|
|
278
|
+
if a.is_a? String
|
279
|
+
if tmp = a.to_range
|
280
|
+
nargs.push tmp
|
281
|
+
else
|
282
|
+
nargs.push a.to_ipadr.to_range rescue next
|
283
|
+
end
|
284
|
+
elsif a.is_a? Ranger
|
285
|
+
a.eachRange do |e|
|
286
|
+
nargs.push e
|
287
|
+
end
|
288
|
+
else
|
289
|
+
nargs.push a
|
290
|
+
end
|
291
|
+
end
|
292
|
+
nargs
|
293
|
+
end
|
294
|
+
def add (*args)
|
295
|
+
super *modargs(*args)
|
296
|
+
end
|
297
|
+
def del (*args)
|
298
|
+
super *modargs(*args)
|
299
|
+
end
|
300
|
+
def include? (*args)
|
301
|
+
super *modargs(*args)
|
302
|
+
end
|
303
|
+
def eachNetRange
|
304
|
+
each do |e|
|
305
|
+
if e.is_a? Range
|
306
|
+
if e.exclude_end?
|
307
|
+
yield e.first.to_ipadr + "-" + (e.last - 1).to_ipadr
|
308
|
+
else
|
309
|
+
yield e.first.to_ipadr + "-" + e.last.to_ipadr
|
310
|
+
end
|
311
|
+
else
|
312
|
+
yield e.to_ipadr
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
|