|
1 |
| -#!/usr/local/bin/tclsh8.4 |
| 1 | +#!/usr/local/bin/tclsh8.5 |
2 | 2 | ################################################################################
|
3 |
| -# |
| 3 | +# |
4 | 4 | # TCL scripts by Ofloo all rights reserved.
|
5 |
| -# |
| 5 | +# |
6 | 6 | # HomePage: http://ofloo.net/
|
7 | 7 | # CVS: http://cvs.ofloo.net/
|
8 | 8 | # Email: support[at]ofloo.net
|
9 |
| -# |
| 9 | +# |
10 | 10 | # This program is free software; you can redistribute it and/or
|
11 | 11 | # modify it under the terms of the GNU General Public License
|
12 | 12 | # as published by the Free Software Foundation; either version 2
|
13 | 13 | # of the License, or (at your option) any later version.
|
14 |
| -# |
| 14 | +# |
15 | 15 | # This program is distributed in the hope that it will be useful,
|
16 | 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 | 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 | 18 | # GNU General Public License for more details.
|
19 |
| -# |
| 19 | +# |
20 | 20 | # You should have received a copy of the GNU General Public License
|
21 | 21 | # along with this program; if not, write to the Free Software
|
22 | 22 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
23 |
| -# |
| 23 | +# |
24 | 24 | ################################################################################
|
25 | 25 |
|
26 | 26 | namespace eval ip2c {
|
27 |
| - |
28 |
| - variable version 1.0 |
29 |
| - variable mirror "ip2c.ofloo.net" |
30 | 27 |
|
31 | 28 | package require http
|
| 29 | + package require ip |
32 | 30 |
|
33 |
| - proc lookup {longip {type {}}} { |
34 |
| - variable mirror |
35 |
| - if {[regexp {^[\d]{1,10}$} $longip] && (0 >= $longip <= 4294967295)} { |
36 |
| - if {[regexp -nocase {<resolve[\s]{0,100}c02="([a-z]{2})"[\s]{0,100}c03="([a-z]{3})"[\s]{0,100}full="(.*?)"[\s]{0,100}/>} [[namespace current]::getPage http://${mirror}/${longip}] -> tld short full]} { |
37 |
| - switch -- $type { |
38 |
| - "-tld" { |
39 |
| - return $tld |
40 |
| - } |
41 |
| - "-short" { |
42 |
| - return $short |
43 |
| - } |
44 |
| - "-full" { |
45 |
| - return $full |
46 |
| - } |
47 |
| - "" { |
48 |
| - return "$tld $short $full" |
49 |
| - } |
| 31 | + variable api "api.ip2c.info" |
| 32 | + variable version 1.1 |
| 33 | + |
| 34 | + variable registry |
| 35 | + variable assigned |
| 36 | + variable short |
| 37 | + variable long |
| 38 | + variable country |
| 39 | + |
| 40 | + # |
| 41 | + # locate ?-ip <ip>? ?-server <server? |
| 42 | + # returns: -1 on invalid ip or number of results |
| 43 | + # |
| 44 | + |
| 45 | + proc locate {{list_0 {}} {list_1 {}} {list_2 {}} {list_3 {}}} { |
| 46 | + variable registry; variable assigned; variable long; variable short; variable country; variable address; variable api |
| 47 | + foreach {key value} [info var list_?] { |
| 48 | + if {[set $key] == ""} {continue} |
| 49 | + switch -- [set ${key}] { |
| 50 | + "-server" { |
| 51 | + variable api [set $value] |
| 52 | + } |
| 53 | + "-ip" { |
| 54 | + set ip [set $value] |
| 55 | + } |
| 56 | + default { |
| 57 | + error "bad option \"[set $key]\": must be -ip or -server" |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if {![info exists foreach {x} [array names short] { |
| 62 | + lappend out $short($x) |
| 63 | + lappend out $long($x) |
50 | 64 | }
|
51 | 65 | }
|
52 | 66 | }
|
53 |
| - error "bad option \"${type}\": must be -tld, -short, -full" |
| 67 | + return $out |
54 | 68 | }
|
55 | 69 |
|
56 |
| - proc getPage {url} { |
57 |
| - set token [::http::geturl $url] |
58 |
| - set data [::http::data $token] |
59 |
| - ::http::cleanup $token |
60 |
| - return $data |
61 |
| - } |
| 70 | + # |
| 71 | + # registry |
| 72 | + # returns: To which registry the IP is assigned |
| 73 | + # |
62 | 74 |
|
63 |
| - # error -1 |
64 |
| - proc ip2longip {ipaddr} { |
65 |
| - if {[[namespace current]::isip $ipaddr]} { |
66 |
| - foreach ipbyte [split $ipaddr \x2E] { |
67 |
| - append hexaddr [format {%02x} $ipbyte] |
68 |
| - } |
69 |
| - return [format {%u} "0x$hexaddr"] |
| 75 | + proc registry {} { |
| 76 | + variable registry |
| 77 | + set out {} |
| 78 | + foreach {x} [array names registry] { |
| 79 | + lappend out $registry($x) |
70 | 80 | }
|
71 |
| - return -1 |
| 81 | + return $out |
72 | 82 | }
|
73 | 83 |
|
74 |
| - #error -1 |
75 |
| - proc longip2ip {longip} { |
76 |
| - if {$longip > 4294967295} { |
77 |
| - return -1 |
78 |
| - } |
79 |
| - return [expr {$longip>>24&255}]\x2E[expr {$longip>>16&255}]\x2E[expr {$longip>>8&255}]\x2E[expr {$longip&255}] |
80 |
| - } |
| 84 | + # |
| 85 | + # cleanup removes cache |
| 86 | + # returns: null |
| 87 | + # |
81 | 88 |
|
82 |
| - proc isip {ip} { |
83 |
| - if {[regexp {^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$} $ip -> a b c d]} { |
84 |
| - if {($a <= 255) && ($b <= 255) && ($c <= 255) && ($d <= 255)} { |
85 |
| - return 1 |
| 89 | + proc cleanup {} { |
| 90 | + variable registry; variable assigned; variable long; variable short; variable country; variable address |
| 91 | + foreach {n} "registry assigned long short country address" { |
| 92 | + if {[array exists [set n]]} { |
| 93 | + foreach {x} [array names [set n]] { |
| 94 | + array unset [set n] $x |
| 95 | + } |
86 | 96 | }
|
87 | 97 | }
|
88 |
| - return 0 |
89 | 98 | }
|
90 | 99 |
|
| 100 | + cleanup |
| 101 | + |
91 | 102 | }
|
92 | 103 |
|
93 | 104 | package provide ip2c $::ip2c::version
|
| 105 | + |
0 commit comments