(self, handshake)
| 388 | return handshake |
| 389 | |
| 390 | def setHandshake(self, handshake): |
| 391 | if config.debug_socket: |
| 392 | self.log("Remote Handshake: %s" % handshake) |
| 393 | |
| 394 | if handshake.get("peer_id") == self.server.peer_id and not handshake.get("tracker_connection") and not self.is_tracker_connection: |
| 395 | self.close("Same peer id, can't connect to myself") |
| 396 | self.server.peer_blacklist.append((handshake["target_ip"], handshake["fileserver_port"])) |
| 397 | return False |
| 398 | |
| 399 | self.handshake = handshake |
| 400 | if handshake.get("port_opened", None) is False and "onion" not in handshake and not self.is_private_ip: # Not connectable |
| 401 | self.port = 0 |
| 402 | else: |
| 403 | self.port = int(handshake["fileserver_port"]) # Set peer fileserver port |
| 404 | |
| 405 | if handshake.get("use_bin_type") and self.unpacker: |
| 406 | unprocessed_bytes_num = self.getUnpackerUnprocessedBytesNum() |
| 407 | self.log("Changing unpacker to bin type (unprocessed bytes: %s)" % unprocessed_bytes_num) |
| 408 | unprocessed_bytes = self.unpacker.read_bytes(unprocessed_bytes_num) |
| 409 | self.unpacker = self.getMsgpackUnpacker() # Create new unpacker for different msgpack type |
| 410 | self.unpacker_bytes = 0 |
| 411 | if unprocessed_bytes: |
| 412 | self.unpacker.feed(unprocessed_bytes) |
| 413 | |
| 414 | # Check if we can encrypt the connection |
| 415 | if handshake.get("crypt_supported") and self.ip not in self.server.broken_ssl_ips: |
| 416 | if type(handshake["crypt_supported"][0]) is bytes: |
| 417 | handshake["crypt_supported"] = [item.decode() for item in handshake["crypt_supported"]] # Backward compatibility |
| 418 | |
| 419 | if self.ip_type == "onion" or self.ip in config.ip_local: |
| 420 | crypt = None |
| 421 | elif handshake.get("crypt"): # Recommended crypt by server |
| 422 | crypt = handshake["crypt"] |
| 423 | else: # Select the best supported on both sides |
| 424 | crypt = CryptConnection.manager.selectCrypt(handshake["crypt_supported"]) |
| 425 | |
| 426 | if crypt: |
| 427 | self.crypt = crypt |
| 428 | |
| 429 | if self.type == "in" and handshake.get("onion") and not self.ip_type == "onion": # Set incoming connection's onion address |
| 430 | if self.server.ips.get(self.ip) == self: |
| 431 | del self.server.ips[self.ip] |
| 432 | self.setIp(handshake["onion"] + ".onion") |
| 433 | self.log("Changing ip to %s" % self.ip) |
| 434 | self.server.ips[self.ip] = self |
| 435 | self.updateName() |
| 436 | |
| 437 | self.event_connected.set(True) # Mark handshake as done |
| 438 | self.event_connected = None |
| 439 | self.handshake_time = time.time() |
| 440 | |
| 441 | # Handle incoming message |
| 442 | def handleMessage(self, message): |
no test coverage detected