iwla

iwla Commit Details

Date:2016-04-12 07:56:10 (5 years 3 months ago)
Author:Grégory Soutadé
Branch:dev, master
Commit:e9c250900618ca3a676773384239d2d7907756ab
Parents: 9bb188b6fca158ff8f7c827fd888771805799740
Message:Update iptogeo.py class

Changes:
Mplugins/post_analysis/iptogeo.py (3 diffs)

File differences

plugins/post_analysis/iptogeo.py
11
22
33
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
423
524
625
......
2746
2847
2948
30
49
3150
3251
3352
34
53
54
3555
3656
3757
38
58
3959
4060
4161
4262
43
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
4480
4581
4682
47
83
4884
49
85
86
5087
5188
5289
......
5895
5996
6097
61
98
99
100
101
102
103
104
62105
63106
64107
65108
66109
67
110
68111
69112
70
113
114
115
116
117
118
119
120
121
122
123
124
71125
72
126
73127
74128
75129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2016 Grégory Soutadé
#
# This file is part of iptogeo.
#
# iptogeo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iptogeo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iptogeo. If not, see <http://www.gnu.org/licenses/>.
#
import socket
import struct
5 : 'Unsupported IP version',
6 : 'IP not found'}
def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None):
def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None, family=socket.AF_INET):
self._remote_addr = remote_addr
self._remote_port = remote_port
self._timeout = timeout
self._family = family
self._create_socket()
def _create_socket(self):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket = socket.socket(self._family, socket.SOCK_STREAM)
if not self._timeout is None:
self._socket.settimeout(self._timeout)
self._socket.connect((self._remote_addr, self._remote_port))
def _create_request(self, ip):
def _extend_ipv6(self, ipv6):
tmp = ''
for s in ipv6.split(':'):
if not s: break
while len(s) != 4:
s = '0' + s
tmp += s
while len(tmp) < 16*2:
tmp += '0'
res = ''
for i in range(0, 15*2, 2):
res += tmp[i] + tmp[i+1] + ':'
res += tmp[30] + tmp[31]
return res
def _create_request(self, ip, ip_type):
packet = ''
packet += struct.pack('<IBBBBI', IPToGeo.MAGIC, IPToGeo.VERSION, IPToGeo.REQ,
0, #err
IPToGeo.IPV4, # ip type
ip_type, # ip type
0) # flags
packet += struct.pack('<BBBB', ip[0], ip[1], ip[2], ip[3]) # ipv4
for i in ip:
packet += struct.pack('<B', i) # ipv4
packet += struct.pack('<III', 0, 0, 0) # ipv6
packet += struct.pack('<I', 0) # country code
if magic != IPToGeo.MAGIC:
raise IPToGeoException('Invalid magic %08x' % (magic))
if err == IPToGeo.IP_NOT_FOUND: return (ipv4, None) # IP not found
ip_res = '%08x' % (ipv4)
if ip_type == IPToGeo.IPV6:
ip_res += '%08x' % (ipv6b)
ip_res += '%08x' % (ipv6c)
ip_res += '%08x' % (ipv6d)
if err == IPToGeo.IP_NOT_FOUND: return (ip_res, None) # IP not found
if err != 0:
raise IPToGeoException(IPToGeo.ERRORS[err])
(cc0, cc1, cc2, cc3) = struct.unpack_from('BBBB', packet, 7*4)
return (ipv4, '%c%c%c%c' % (cc0, cc1, cc2, cc3))
return (ip_res, '%c%c%c%c' % (cc0, cc1, cc2, cc3))
def ip_to_geo(self, ip):
splitted_ip = [int(a) for a in ip.split('.')]
ip_type = IPToGeo.IPV4
if ip.find('.') >= 0:
splitted_ip = [int(a) for a in ip.split('.')]
if len(splitted_ip) != 4:
raise Exception('Bad IP %s' % (ip))
elif ip.find(':') >= 0:
splitted_ip = [int(a, 16) for a in self._extend_ipv6(ip).split(':')]
if len(splitted_ip) != 16:
raise Exception('Bad IP %s' % (ip))
ip_type = IPToGeo.IPV6
else:
raise Exception('Bad IP %s' % (ip))
packet = self._create_request(splitted_ip)
packet = self._create_request(splitted_ip, ip_type)
try:
self._socket.send(packet)
except IOError, e:

Archive Download the corresponding diff file

Branches

Tags