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

134

135

136

137

138

139

140

141

142

143

144

145

#!/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.db_format = self.monitor.db_format 

self.country_name = country 

# info_pool, ip_pool, port_pool 

self.pools = [{}, {}, {}] 

 

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 value in self.monitor.peers_pool.values(): 

infolist.append((value[0])) 

iplist.append((value[1][1])) 

portlist.append((value[1][2])) 

self.pools[0] = infolist 

self.pools[1] = iplist 

self.pools[2] = portlist 

 

 

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.pools[1]: 

# 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) 

try: 

location_info = urllib.request.urlopen( 

url_of_location).read() 

except urllib.error.HTTPError: 

continue 

iplist = [] 

location_info = json.loads(location_info.decode("utf-8")) 

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.pools[2].pop(0), 

"infohash": self.pools[0].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_chosen_output(self): 

''' 

print geolocation when argument --print_as_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.peer_announce, indent=4, sort_keys=True)) 

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

.format(self.monitor.no_recieve)) 

if self.db_format: 

print("{{\"{}\":".format(self.monitor.torrent.infohash_list[2])) 

 

print("{\"peers\": [") 

for peer in self.monitor.peers_pool.values(): 

print("\t{{\"timestamp\":{}, \"addr\": [\"{}\", {}]}}," 

.format(peer[0], str(peer[1][1]), peer[1][2])) 

print("]") 

print("\"nodes\": [") 

for key, node in self.monitor.peer_announce.items(): 

print("\t{{\"timestamp\": {}, \"nodeID\": \"{}\", "\ 

"\"nodeAddr\": [\"{}\", {}]}}," 

.format(node[1], key, node[0][1], node[0][2])) 

print("]") 

print("\"name\": \"{}\"".format(self.monitor.torrent_name)) 

print("}") 

print("}")