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

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

#!/usr/bin/env python3 

''' 

Created by Martin Vasko 

3BIT, Brno, Faculty of Information Technology. 

 

Brief information: 

This is implementation of monitoring BiTtorrent with Kademlia DHT. 

Whole monitor class which will be presented next is going to be supported by 

torrentDHT implementation, which was implemented by Martin Vasko. 

''' 

import signal 

import time 

import datetime 

import hashlib 

import select 

import re 

import socket 

from threading import Thread, Semaphore 

from bencoder import bencode 

# from handshake import TorrentHandshake 

from arg_parse import parse_input_args 

from torrent_dht import TorrentDHT, TorrentArguments,\ 

random_infohash, decode_krpc, get_neighbor, \ 

get_myip 

from process_output import ProcessOutput 

 

 

def kill_sender_reciever(thread1, thread2=None): 

''' 

kill sender reciever and TorrentDHT socket when there is 

continuous bootstrap. 

''' 

identification = thread1.ident 

try: 

signal.pthread_kill(identification, 2) 

except ProcessLookupError: 

pass 

 

# take some time to kill reciever thread 

if thread2 is None: 

return 

 

identification = thread2.ident 

try: 

signal.pthread_kill(identification, 2) 

except ProcessLookupError: 

pass 

 

 

def init_socket(port): 

''' 

Initialize empty socket to send announce peer messages 

''' 

sock = socket.socket(socket.AF_INET, 

socket.SOCK_DGRAM, 

socket.IPPROTO_UDP) 

sock.bind((get_myip(), port)) 

return sock 

 

class Monitor: 

""" 

Parse it from class methods to monitor class where we want to exchange 

this information. 

Start monitoring and initialize all necessary things at first 

""" 

 

def __init__(self, arguments, torrent): 

""" 

Construct a new 'Foo' object. 

 

:param name: The name of foo 

:param age: The ageof foo 

:return: returns nothing 

""" 

self.timeout = 1 

self.torrent = torrent 

self.infohash = random_infohash() 

self.test = False 

self.duration = 600 

 

# file which should be parsed 

self.file = arguments.file 

# magnet-link given 

self.magnet = arguments.magnet 

self.country = arguments.country 

 

# print output in db format required for tarzan server 

self.db_format = arguments.db_format 

self.queue_type = arguments.queue_type 

self.max_peers = 600 

if arguments.max_peers is not None: 

self.max_peers = arguments.max_peers 

if arguments.hash is not None: 

# infohash of some file on internet, 

# if not specified randomly generate infohash 

self.torrent.change_info(arguments.hash) 

if arguments.counter is not None: 

# How long should wait after queue is empty 

self.timeout = arguments.counter 

if arguments.test is not None: 

# Test of connection ! 

self.test = arguments.test 

if arguments.duration is not None: 

# Duration of crawl 

self.duration = arguments.duration 

# local variables for class 

 

self.sock = self.torrent.query_socket 

self.n_nodes = 0 # Number of nodes in a specified n-bit zone 

self.no_recieve = 0 # timer that should point how many packets were timed out 

 

self.torrent_name = "" 

self.info_pool = {} # infohashes already found 

self.peers_pool = {} # peers already found 

self.addr_pool = {} # Addr recieved from 

self.peer_announce = {} # pool of NODES announced peers 

self.respondent = 0 # Number of respondents 

self.output = ProcessOutput(self, arguments.print_as_country, 

arguments.country) 

self.lock = Semaphore() 

 

def __str__(self): 

return "File: {},\nMagnet-link: {},\nDuration of crawl: {},\ 

\nCounter: {}".format(self.file, self.magnet, 

self.duration, self.timeout) 

 

 

def vprint(self, msg): 

''' 

Print only when -v parameter is present 

''' 

if self.torrent.verbosity: 

print(msg) 

 

 

##################### 

# START OF CRAWLING # 

##################### 

def query_for_connectivity(self): 

''' 

Query all found peers for connectivity. 

When respond, then connection is still there and peer is valid, else 

peer is deleted from dictionary. 

''' 

try: 

self.torrent.query_socket.close() 

except KeyboardInterrupt: 

pass 

present_time = datetime.datetime.now() 

peers_outdated = [] 

 

peer_pool = self.peers_pool.values() 

# take all of incomming peers and check them 

for value in peer_pool: 

try: 

past_time = datetime.datetime.strptime(value[0], "%d.%m.%Y %H:%M:%S:%f") 

except KeyboardInterrupt: 

continue 

delta_time = present_time - past_time 

total_seconds = delta_time.total_seconds() 

if int(total_seconds) > 800: 

# outdated peer 

peers_outdated.append(value[1]) 

 

# send announce peer to 'start' session 

# query queried node 

for value in peers_outdated: 

del self.peers_pool[value[1] + ":" + str(value[2])] 

 

def insert_to_queue(self, nodes): 

''' 

Inserts nodes to queue by given queue type. 

''' 

# Do not remove already asked 

last_node = None 

for node in nodes["Nodes"]: 

if node == last_node: 

continue 

self.torrent.nodes.put((node)) 

last_node = node 

 

 

def process_and_update(self, ready, last_time): 

''' 

process packet and update all necesseties like info_pool, peers_pool. 

When decoding failed or not ready socket for recieving return back to 

listener thread. 

''' 

if ready[0]: 

try: 

msg, addr = self.sock.recvfrom(2048) 

except OSError: 

return last_time 

else: 

self.no_recieve = self.no_recieve + 0.1 

return last_time 

msg = decode_krpc(msg) 

if msg is None: 

return last_time 

self.addr_pool[addr] = {"timestamp": time.time()} 

 

pool = {} 

nodes = self.torrent.decode_message(msg, pool, self.peer_announce, addr) 

# update dictionary by given pool value 

try: 

if nodes["Nodes"]: 

self.info_pool.update(pool["Nodes"]) 

self.respondent += 1 

if nodes["Peers"]: 

self.peers_pool.update(pool["Peers"]) 

except KeyError: 

pass 

 

# Resolution without cleaning queue 

if self.torrent.nodes.qsize() <= self.max_peers * 0.9: 

try: 

if nodes["Nodes"]: 

self.insert_to_queue(nodes) 

except KeyError: 

pass 

 

if not self.db_format: 

curr = time.time() 

if curr - last_time > 5: 

self.info() 

last_time = time.time() 

return last_time 

 

 

def start_listener(self): 

''' 

start listener thread. Recieve query packet and decode its body. 

There is shared queue between listener and sender thread. 

''' 

last_time = time.time() 

while True: 

if self.timeout is not None: 

time.sleep(self.timeout) 

 

# socket is closed no value returned 

try: 

ready = select.select([self.sock], [], [], 0.1) 

except (OSError, ValueError): 

continue 

 

# if self.torrent.nodes.qsize() > self.max_peers * 0.3: 

# for _ in range(1, 20): 

# last_time = self.process_and_update(ready, last_time) 

# else: 

last_time = self.process_and_update(ready, last_time) 

 

 

def start_sender(self, test=False): 

''' 

start sender thread. There is test parameter to test connection for 

unit testing. Otherwise continuous connection is performed till 

we dont get all nodes from k-zone or duration is exhausted. 

''' 

if test: 

# if test is given perform single message send 

node = self.torrent.nodes.get(True) 

hexdig_self = int(self.infohash, 16) 

hexdig_target = int(node[0], 16) 

self.torrent.query_get_peers(node, self.infohash, self.sock) 

return 1 

 

while True: 

if self.timeout is not None: 

time.sleep(self.timeout) 

node = self.torrent.nodes.get(True) 

 

hexdig_self = int(self.infohash, 16) 

hexdig_target = int(node[0], 16) 

if((hexdig_self ^ hexdig_target) >> 148) == 0: 

# we are close, we should send more packets but it is slow 

# on recieving thread because of decoding and composing 

# dictionaries 

try: 

self.torrent.query_get_peers(node, self.infohash, self.sock) 

except OSError: 

return 9 

# Speed is less than 2000 bps 

else: 

try: 

self.torrent.query_get_peers(node, self.infohash, self.sock) 

except OSError: 

return 9 

 

 

def start_timer(self, thread1, thread2): 

''' 

start thread timer for duration, when exhausted kill threads 

and exit program. 

''' 

self.vprint("Start of duration") 

# sleep for shorter time 

for _ in range(self.duration): 

time.sleep(1) 

self.vprint("End of duration") 

# Clear all resources 

kill_sender_reciever(thread1, thread2) 

 

 

def crawl_begin(self, torrent=None, test=False): 

''' 

Create all threads, duration to count how long program is executed. 

When Ctrl+C is pressed kill all threads 

 

Parameters 

---------- 

torrent : infohash 

20 bytes long infohash which should be used as part of monitoring. 

test : bool 

This paramter is for testing connection. 

 

''' 

if torrent: 

self.torrent.infohash_list[2] = torrent 

 

send_thread = Thread(target=self.start_sender, args=()) 

send_thread.daemon = True 

send_thread.start() 

listen_thread = Thread(target=self.start_listener, 

args=()) 

listen_thread.daemon = True 

listen_thread.start() 

duration_thread = Thread(target=self.start_timer, 

args=(send_thread, listen_thread)) 

duration_thread.daemon = True 

duration_thread.start() 

while True: 

if test: 

break 

try: 

if self.country: 

self.lock.acquire() 

self.output.get_geolocations() 

self.lock.release() 

time.sleep(1) 

except KeyboardInterrupt: 

self.vprint("\nClearing threads, wait a second") 

break 

self.query_for_connectivity() 

if self.db_format or self.output.print_country: 

self.output.get_geolocations() 

self.output.print_chosen_output() 

if not self.db_format: 

self.info() 

 

 

def info(self): 

''' 

Print info for current state of crawling. 

''' 

print("[NodeSet]:%i\t\t[PeerSet]:%i\t\t[Response]:\ 

%.2f%%\t\t[Queue]:%i\t\t" % 

(len(self.info_pool), len(self.peers_pool), 

self.respondent*100.0 / max(1, len(self.info_pool)), 

self.torrent.nodes.qsize())) 

 

def diverge_in_location(self, nodes): 

''' 

After climbing to another teritory, do not access it, 

return adjusted list of nodes. 

''' 

iplist = self.output.translate_node(self.info_pool) 

for ip_addr in iplist: 

num = 0 

for node_ip in nodes: 

if node_ip[1] == ip_addr[0]: 

nodes.remove(nodes[num]) 

num = num + 1 

return nodes 

 

 

def get_torrent_name(self, value): 

''' 

get name of torrent from torrent file 

 

Parameters 

---------- 

value : dict 

This should contain encoded name of torrent. 

 

Returns 

------- 

self.torrent_name 

Parsed torrent name from value dictionary. 

''' 

for name, name_val in value.items(): 

name = name.decode('utf-8') 

if name == "name": 

self.torrent_name = name_val.decode("utf-8") 

 

def parse_torrent(self): 

''' 

parse torrent file to get infohash and announce list of nodes for 

better bootstrap. 

''' 

if self.file is not None: 

for file in self.file: 

file_r = open(file, "rb") 

content = file_r.read() 

info_hash = None 

nodes = [] 

self.vprint("Torrent file content") 

if not decode_krpc(content): 

raise TypeError("WrongFileType") 

if not isinstance(decode_krpc(content), dict): 

raise TypeError("WrongFileType") 

for key, value in decode_krpc(content).items(): 

key = key.decode('utf-8') 

if key == "creation date": 

self.vprint("Creation of file: ") 

self.vprint(datetime.datetime 

.fromtimestamp(value) 

.strftime("%Y-%m-%d %H:%M:%S")) 

if key == "info": 

self.get_torrent_name(value) 

info_hash = hashlib.sha1(bencode(value)).hexdigest() 

# set torrent target 

self.infohash = get_neighbor(info_hash, self.infohash) 

self.torrent.infohash_list[1].append(info_hash) 

 

if key == "nodes": 

pass 

if key == "announce-list": 

nodes = value 

self.torrent.change_bootstrap(info_hash, nodes, self.queue_type) 

file_r.close() 

 

 

def parse_magnet(self): 

''' 

parse magnet link 

''' 

if self.magnet is not None: 

for magnet in self.magnet: 

file_r = open(magnet, "rb") 

content = file_r.read().decode('utf-8') 

name = re.search(r"&dn.*&(xt|tr)", content) 

name = re.search(r"^((?!tr.*).)*", name.group(0)) 

self.torrent_name = name.group(0)[4:-1] 

 

info_hash = re.search(r"urn:.*&(xl|dn)", content) 

# match last `:` and its content 

info_hash = re.search(r"(?:.(?!:))+$", info_hash.group(0)) 

info_hash = info_hash.group(0)[1:-3] 

self.infohash = get_neighbor(info_hash, self.infohash) 

# set torrent target 

self.torrent.infohash_list[1].append(info_hash) 

 

######################################## 

# This should be used in main function # 

######################################## 

 

def create_monitor(verbosity=False): 

''' 

creates monitor class object. TorrentDHT creates udp socket which is 

binded on `bind_port`. Monitor needs this `dht_socket` and command line 

arguments to be created successfully. Then change of hash and parsing 

can change resolution of crawl. When they are not specified then 

global bootstrap nodes are used instead. 

 

Parameters 

---------- 

verbosity : bool 

Indicate verbose output 

 

Returns 

------- 

object 

Monitor object with initialized DHT socket and parsed arguments. 

''' 

args = parse_input_args() 

# This is variant with verbose output to track some lib imported staff 

if args.bind_port is not None: 

torrent_arguments = TorrentArguments() 

dht_socket = TorrentDHT(torrent_arguments, bind_port=args.bind_port, 

verbosity=verbosity) 

else: 

torrent_arguments = TorrentArguments() 

dht_socket = TorrentDHT(torrent_arguments, verbosity=verbosity) 

 

# Monitor class needs dht_socket, which is imported from TorrentDHT.py 

monitor = Monitor(args, dht_socket) 

# This variant is only to test connection to BOOTSTRAP_NODES 

if monitor.test: 

result = monitor.start_sender(test=True) 

exit(result) 

monitor.torrent.change_arguments(monitor.max_peers, monitor.queue_type) 

monitor.parse_torrent() 

monitor.parse_magnet() 

return monitor