Skip to content

Commit

Permalink
TorDetector: minor clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
SomberNight committed Jun 29, 2019
1 parent 8a4e307 commit e7304ce
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions electrum/gui/qt/network_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import socket
import time
from enum import IntEnum
from typing import Tuple

from PyQt5.QtCore import Qt, pyqtSignal, QThread
from PyQt5.QtWidgets import (QTreeWidget, QTreeWidgetItem, QMenu, QGridLayout, QComboBox,
Expand Down Expand Up @@ -521,19 +522,20 @@ def run(self):
ports = [9050, 9150]
while True:
for p in ports:
if TorDetector.is_tor_port(p):
self.found_proxy.emit(("127.0.0.1", p))
net_addr = ("127.0.0.1", p)
if TorDetector.is_tor_port(net_addr):
self.found_proxy.emit(net_addr)
break
else:
self.found_proxy.emit(None)
time.sleep(10)

@staticmethod
def is_tor_port(port):
def is_tor_port(net_addr: Tuple[str, int]) -> bool:
try:
s = (socket._socketobject if hasattr(socket, "_socketobject") else socket.socket)(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
s.connect(("127.0.0.1", port))
s.connect(net_addr)
# Tor responds uniquely to HTTP-like requests
s.send(b"GET\n")
if b"Tor is not an HTTP Proxy" in s.recv(1024):
Expand Down

0 comments on commit e7304ce

Please sign in to comment.