]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | import IPy | |
4 | import re | |
5 | ||
6 | import countries | |
7 | ||
8 | from misc import Object | |
9 | ||
10 | class GeoIP(Object): | |
11 | def guess_address_family(self, addr): | |
12 | if ":" in addr: | |
13 | return 6 | |
14 | ||
15 | return 4 | |
16 | ||
17 | def get_country(self, addr): | |
18 | ret = self.get_all(addr) | |
19 | ||
20 | if ret: | |
21 | return ret.country | |
22 | ||
23 | def get_location(self, addr): | |
24 | query = "SELECT * FROM geoip \ | |
25 | WHERE %s BETWEEN start_ip AND end_ip LIMIT 1" | |
26 | ||
27 | return self.db.get(query, addr) | |
28 | ||
29 | def get_asn(self, addr): | |
30 | query = "SELECT asn FROM geoip_asn \ | |
31 | WHERE %s BETWEEN start_ip AND end_ip LIMIT 1" | |
32 | ||
33 | ret = self.db.get(query, addr) | |
34 | ||
35 | if ret: | |
36 | return ret.asn | |
37 | ||
38 | def get_all(self, addr): | |
39 | location = self.get_location(addr) | |
40 | ||
41 | if location: | |
42 | location["asn"] = self.get_asn(addr) | |
43 | ||
44 | return location | |
45 | ||
46 | _countries = { | |
47 | "A1" : "Anonymous Proxy", | |
48 | "A2" : "Satellite Provider", | |
49 | "AP" : "Asia/Pacific Region", | |
50 | "EU" : "Europe", | |
51 | } | |
52 | ||
53 | def get_country_name(self, code): | |
54 | # Return description of some exceptional codes. | |
55 | try: | |
56 | return self._countries[code] | |
57 | except KeyError: | |
58 | pass | |
59 | ||
60 | country = countries.get_by_code(code) | |
61 | if not country: | |
62 | return code | |
63 | ||
64 | return country |