Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

#!/usr/bin/env python3 

''' 

Create by Martin Vasko 

3BIT, Brno, Faculty of Information Technology. 

''' 

import urllib.request 

import json 

 

 

 

class ProcessOutput(): 

''' 

Process output to process regex or to remove duplicities 

''' 

 

def __init__(self, monitor, country_print, country): 

self.monitor = monitor 

# class variables to save position and information about node 

self.country_city = {} 

if country_print is not None: 

self.print_country = country_print 

else: 

self.print_country = False 

self.country_name = country 

self.ip_pool = {} 

self.info_pool = {} 

self.port_pool = {} 

 

def translate_node(self, nodes): 

''' 

translates nodes ip address to equivalent country name, check wether 

they corelate with name given as parameter, returns adjusted ip 

addresses which should be deleted from `nodes` list. 

''' 

iplist = [] 

infolist = [] 

for info, node in nodes.items(): 

url_of_location = "http://www.freegeoip.net/json/{}"\ 

.format(node[0][0]) 

location_info = json.loads(urllib.request.urlopen(url_of_location) 

.read()) 

if location_info['country_name'] is None: 

iplist.append(node[0]) 

infolist.append(info) 

else: 

if self.country_name != location_info['country_name']: 

iplist.append(node[0]) 

infolist.append(info) 

for i in infolist: 

del nodes[i] 

 

return iplist 

 

 

def parse_ips(self): 

''' 

initiate pool of ip addresses port and infohashes for geolocation. 

''' 

iplist = [] 

infolist = [] 

portlist = [] 

for key, value in self.monitor.info_pool.items(): 

infolist.append((key)) 

for val in value: 

iplist.append((val[0])) 

portlist.append((val[1])) 

self.ip_pool = iplist 

self.port_pool = portlist 

self.info_pool = infolist 

 

 

def fill_locations(self, location_info, iplist=None): 

''' 

fill various information to dictionary 

''' 

iplist = self.country_city[location_info['country_name'] + ":" + 

location_info['city']] 

is_in_list = False 

for ip_addr in iplist: 

if str(ip_addr['ip']) == str(location_info['ip']): 

is_in_list = True 

if not is_in_list: 

iplist.append({"ip": str(location_info['ip']), 

"latitude": str(location_info['latitude']), 

"longitude": str(location_info["longitude"])}) 

return iplist 

 

 

def get_geolocations(self): 

''' 

get real locations of ip addresses 

''' 

if self.print_country: 

self.parse_ips() 

for ip_addr in self.ip_pool: 

# FIXME when freegeoip is down we need another website to 

# get this kind of information 

url_of_location = "http://www.freegeoip.net/json/{0}".format( 

ip_addr) 

location_info = json.loads(urllib.request.urlopen( 

url_of_location).read()) 

iplist = [] 

 

if location_info['country_name'] + ":" + \ 

location_info['city'] in self.country_city: 

iplist = self.fill_locations(location_info) 

else: 

iplist = [{"ip": str(location_info['ip']), 

"port": self.port_pool.pop(0), 

"infohash": self.info_pool.pop(0), 

"latitude": str(location_info['latitude']), 

"longitude": str(location_info['longitude']) 

}] 

 

self.country_city[location_info['country_name'] + ":" + 

location_info['city']] = iplist 

 

 

def print_geolocations(self): 

''' 

print geolocation when argument --print_country is specified, else 

print as json object with no resolution. 

''' 

if self.print_country: 

print(json.dumps(self.country_city, indent=4, sort_keys=True)) 

print(json.dumps(self.monitor.peers_pool, indent=4, sort_keys=True)) 

print("Time spend not recieving any UDP response: {}" 

.format(self.monitor.no_recieve)) 

else: 

print(json.dumps(self.monitor.info_pool, indent=4, sort_keys=True)) 

print(json.dumps(self.monitor.peers_pool, indent=4, sort_keys=True)) 

print("Time spend not recieving any UDP response: {}" 

.format(self.monitor.no_recieve))