2013-04-28 04:47:19 -07:00
#!/usr/bin/env python
#
2019-12-24 03:21:35 -08:00
# Electrum - lightweight bitcoinprivate client
2013-04-28 04:47:19 -07:00
# Copyright (C) 2012 thomasv@gitorious
#
2016-02-23 02:36:42 -08:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
2013-04-28 04:47:19 -07:00
#
2016-02-23 02:36:42 -08:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
2013-04-28 04:47:19 -07:00
#
2016-02-23 02:36:42 -08:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2013-04-28 04:47:19 -07:00
2017-01-22 10:25:24 -08:00
import socket
2017-10-23 04:54:09 -07:00
2017-09-22 20:54:38 -07:00
from PyQt5 . QtGui import *
from PyQt5 . QtCore import *
2017-11-12 20:54:04 -08:00
from PyQt5 . QtWidgets import *
2017-09-22 20:54:38 -07:00
import PyQt5 . QtCore as QtCore
2015-10-28 04:10:43 -07:00
2019-12-24 03:21:35 -08:00
from electrum_bitcoinprivate . i18n import _
from electrum_bitcoinprivate import constants
from electrum_bitcoinprivate . util import print_error
from electrum_bitcoinprivate . network import serialize_server , deserialize_server
2013-04-28 04:47:19 -07:00
2017-01-22 10:25:24 -08:00
from . util import *
2013-04-28 04:47:19 -07:00
2014-09-19 01:46:18 -07:00
protocol_names = [ ' TCP ' , ' SSL ' ]
protocol_letters = ' ts '
2013-04-28 04:47:19 -07:00
2017-07-08 06:23:00 -07:00
class NetworkDialog ( QDialog ) :
2017-09-22 20:54:38 -07:00
def __init__ ( self , network , config , network_updated_signal_obj ) :
2017-07-08 06:35:48 -07:00
QDialog . __init__ ( self )
2017-07-08 06:23:00 -07:00
self . setWindowTitle ( _ ( ' Network ' ) )
2017-07-09 05:03:12 -07:00
self . setMinimumSize ( 500 , 20 )
2016-01-13 03:34:42 -08:00
self . nlayout = NetworkChoiceLayout ( network , config )
2017-09-22 20:54:38 -07:00
self . network_updated_signal_obj = network_updated_signal_obj
2016-01-13 03:34:42 -08:00
vbox = QVBoxLayout ( self )
2016-01-13 04:25:48 -08:00
vbox . addLayout ( self . nlayout . layout ( ) )
2017-07-07 13:56:43 -07:00
vbox . addLayout ( Buttons ( CloseButton ( self ) ) )
2017-09-22 20:54:38 -07:00
self . network_updated_signal_obj . network_updated_signal . connect (
self . on_update )
2017-07-13 08:23:24 -07:00
network . register_callback ( self . on_network , [ ' updated ' , ' interfaces ' ] )
2017-07-08 06:35:48 -07:00
def on_network ( self , event , * args ) :
2017-09-22 20:54:38 -07:00
self . network_updated_signal_obj . network_updated_signal . emit ( event , args )
2013-04-28 04:47:19 -07:00
2017-07-07 13:56:43 -07:00
def on_update ( self ) :
self . nlayout . update ( )
2016-01-13 03:34:42 -08:00
2017-07-09 05:03:12 -07:00
2017-07-04 02:35:04 -07:00
class NodesListWidget ( QTreeWidget ) :
def __init__ ( self , parent ) :
QTreeWidget . __init__ ( self )
self . parent = parent
2017-11-02 00:27:38 -07:00
self . setHeaderLabels ( [ _ ( ' Connected node ' ) , _ ( ' Height ' ) ] )
2017-07-04 02:35:04 -07:00
self . setContextMenuPolicy ( Qt . CustomContextMenu )
self . customContextMenuRequested . connect ( self . create_menu )
def create_menu ( self , position ) :
item = self . currentItem ( )
if not item :
return
2017-08-08 01:50:08 -07:00
is_server = not bool ( item . data ( 0 , Qt . UserRole ) )
2017-07-04 02:35:04 -07:00
menu = QMenu ( )
if is_server :
2017-08-08 01:50:08 -07:00
server = item . data ( 1 , Qt . UserRole )
2017-07-08 08:38:57 -07:00
menu . addAction ( _ ( " Use as server " ) , lambda : self . parent . follow_server ( server ) )
2017-07-04 02:35:04 -07:00
else :
2017-08-08 01:50:08 -07:00
index = item . data ( 1 , Qt . UserRole )
2017-07-08 08:38:57 -07:00
menu . addAction ( _ ( " Follow this branch " ) , lambda : self . parent . follow_branch ( index ) )
2017-07-04 02:35:04 -07:00
menu . exec_ ( self . viewport ( ) . mapToGlobal ( position ) )
def keyPressEvent ( self , event ) :
if event . key ( ) in [ Qt . Key_F2 , Qt . Key_Return ] :
self . on_activated ( self . currentItem ( ) , self . currentColumn ( ) )
else :
QTreeWidget . keyPressEvent ( self , event )
def on_activated ( self , item , column ) :
# on 'enter' we show the menu
pt = self . visualItemRect ( item ) . bottomLeft ( )
pt . setX ( 50 )
2017-09-22 20:54:38 -07:00
self . customContextMenuRequested . emit ( pt )
2017-07-04 02:35:04 -07:00
2017-07-07 13:56:43 -07:00
def update ( self , network ) :
self . clear ( )
2017-07-18 12:37:04 -07:00
self . addChild = self . addTopLevelItem
chains = network . get_blockchains ( )
n_chains = len ( chains )
for k , items in chains . items ( ) :
b = network . blockchains [ k ]
2017-07-19 00:29:20 -07:00
name = b . get_name ( )
2017-07-18 12:37:04 -07:00
if n_chains > 1 :
2017-07-19 00:29:20 -07:00
x = QTreeWidgetItem ( [ name + ' @ %d ' % b . get_checkpoint ( ) , ' %d ' % b . height ( ) ] )
2017-07-18 12:37:04 -07:00
x . setData ( 0 , Qt . UserRole , 1 )
x . setData ( 1 , Qt . UserRole , b . checkpoint )
else :
x = self
for i in items :
2017-07-09 03:11:46 -07:00
star = ' * ' if i == network . interface else ' '
item = QTreeWidgetItem ( [ i . host + star , ' %d ' % i . tip ] )
2017-07-07 13:56:43 -07:00
item . setData ( 0 , Qt . UserRole , 0 )
item . setData ( 1 , Qt . UserRole , i . server )
2017-07-18 12:37:04 -07:00
x . addChild ( item )
if n_chains > 1 :
self . addTopLevelItem ( x )
x . setExpanded ( True )
2017-07-07 13:56:43 -07:00
h = self . header ( )
h . setStretchLastSection ( False )
2017-09-22 20:54:38 -07:00
h . setSectionResizeMode ( 0 , QHeaderView . Stretch )
h . setSectionResizeMode ( 1 , QHeaderView . ResizeToContents )
2017-07-07 13:56:43 -07:00
2017-07-04 02:35:04 -07:00
2017-07-09 05:03:12 -07:00
class ServerListWidget ( QTreeWidget ) :
def __init__ ( self , parent ) :
QTreeWidget . __init__ ( self )
self . parent = parent
self . setHeaderLabels ( [ _ ( ' Host ' ) , _ ( ' Port ' ) ] )
self . setContextMenuPolicy ( Qt . CustomContextMenu )
self . customContextMenuRequested . connect ( self . create_menu )
def create_menu ( self , position ) :
item = self . currentItem ( )
if not item :
return
menu = QMenu ( )
2017-08-08 01:50:08 -07:00
server = item . data ( 1 , Qt . UserRole )
2017-07-09 05:03:12 -07:00
menu . addAction ( _ ( " Use as server " ) , lambda : self . set_server ( server ) )
menu . exec_ ( self . viewport ( ) . mapToGlobal ( position ) )
def set_server ( self , s ) :
2018-06-04 09:32:41 -07:00
host , port , protocol = deserialize_server ( s )
2017-07-09 05:03:12 -07:00
self . parent . server_host . setText ( host )
self . parent . server_port . setText ( port )
self . parent . set_server ( )
def keyPressEvent ( self , event ) :
if event . key ( ) in [ Qt . Key_F2 , Qt . Key_Return ] :
self . on_activated ( self . currentItem ( ) , self . currentColumn ( ) )
else :
QTreeWidget . keyPressEvent ( self , event )
def on_activated ( self , item , column ) :
# on 'enter' we show the menu
pt = self . visualItemRect ( item ) . bottomLeft ( )
pt . setX ( 50 )
2017-09-22 20:54:38 -07:00
self . customContextMenuRequested . emit ( pt )
2017-07-09 05:03:12 -07:00
def update ( self , servers , protocol , use_tor ) :
self . clear ( )
for _host , d in sorted ( servers . items ( ) ) :
if _host . endswith ( ' .onion ' ) and not use_tor :
continue
port = d . get ( protocol )
if port :
x = QTreeWidgetItem ( [ _host , port ] )
2018-06-04 09:32:41 -07:00
server = serialize_server ( _host , port , protocol )
2017-07-09 05:03:12 -07:00
x . setData ( 1 , Qt . UserRole , server )
self . addTopLevelItem ( x )
h = self . header ( )
h . setStretchLastSection ( False )
2017-09-22 20:54:38 -07:00
h . setSectionResizeMode ( 0 , QHeaderView . Stretch )
h . setSectionResizeMode ( 1 , QHeaderView . ResizeToContents )
2017-07-04 02:35:04 -07:00
2016-01-13 03:34:42 -08:00
class NetworkChoiceLayout ( object ) :
2017-07-07 13:56:43 -07:00
2016-01-13 03:34:42 -08:00
def __init__ ( self , network , config , wizard = False ) :
2013-09-10 09:27:32 -07:00
self . network = network
2013-04-28 04:47:19 -07:00
self . config = config
self . protocol = None
2017-02-17 04:56:23 -08:00
self . tor_proxy = None
2013-04-28 04:47:19 -07:00
2017-07-09 05:03:12 -07:00
self . tabs = tabs = QTabWidget ( )
2017-03-23 13:53:03 -07:00
server_tab = QWidget ( )
2017-03-23 22:25:03 -07:00
proxy_tab = QWidget ( )
2017-03-23 13:53:03 -07:00
blockchain_tab = QWidget ( )
2017-07-09 05:03:12 -07:00
tabs . addTab ( blockchain_tab , _ ( ' Overview ' ) )
2017-03-23 13:53:03 -07:00
tabs . addTab ( server_tab , _ ( ' Server ' ) )
2018-06-04 09:32:41 -07:00
tabs . addTab ( proxy_tab , _ ( ' Proxy ' ) )
2017-03-23 13:53:03 -07:00
# server tab
grid = QGridLayout ( server_tab )
2013-04-28 04:47:19 -07:00
grid . setSpacing ( 8 )
self . server_host = QLineEdit ( )
self . server_host . setFixedWidth ( 200 )
self . server_port = QLineEdit ( )
self . server_port . setFixedWidth ( 60 )
2016-01-03 08:47:02 -08:00
self . autoconnect_cb = QCheckBox ( _ ( ' Select server automatically ' ) )
2015-05-25 00:18:52 -07:00
self . autoconnect_cb . setEnabled ( self . config . is_modifiable ( ' auto_connect ' ) )
2017-03-23 13:53:03 -07:00
2017-07-07 13:56:43 -07:00
self . server_host . editingFinished . connect ( self . set_server )
self . server_port . editingFinished . connect ( self . set_server )
2017-07-08 04:24:40 -07:00
self . autoconnect_cb . clicked . connect ( self . set_server )
2017-07-09 03:03:07 -07:00
self . autoconnect_cb . clicked . connect ( self . update )
2017-07-07 13:56:43 -07:00
2017-10-29 07:31:19 -07:00
msg = ' ' . join ( [
2019-12-24 03:21:35 -08:00
_ ( " If auto-connect is enabled, Electrum-bitcoinprivate will always use a server that is on the longest blockchain. " ) ,
_ ( " If it is disabled, you have to choose a server you want to use. Electrum-bitcoinprivate will warn you if your server is lagging. " )
2017-10-29 07:31:19 -07:00
] )
grid . addWidget ( self . autoconnect_cb , 0 , 0 , 1 , 3 )
grid . addWidget ( HelpButton ( msg ) , 0 , 4 )
2017-07-09 01:32:24 -07:00
grid . addWidget ( QLabel ( _ ( ' Server ' ) + ' : ' ) , 1 , 0 )
grid . addWidget ( self . server_host , 1 , 1 , 1 , 2 )
grid . addWidget ( self . server_port , 1 , 3 )
2013-04-28 04:47:19 -07:00
2017-07-09 05:03:12 -07:00
label = _ ( ' Server peers ' ) if network . is_connected ( ) else _ ( ' Default Servers ' )
grid . addWidget ( QLabel ( label ) , 2 , 0 , 1 , 5 )
self . servers_list = ServerListWidget ( self )
grid . addWidget ( self . servers_list , 3 , 0 , 1 , 5 )
2013-04-28 04:47:19 -07:00
2017-03-23 22:25:03 -07:00
# Proxy tab
grid = QGridLayout ( proxy_tab )
2017-03-23 13:53:03 -07:00
grid . setSpacing ( 8 )
2013-04-28 04:47:19 -07:00
# proxy setting
2017-10-12 09:35:45 -07:00
self . proxy_cb = QCheckBox ( _ ( ' Use proxy ' ) )
self . proxy_cb . clicked . connect ( self . check_disable_proxy )
self . proxy_cb . clicked . connect ( self . set_proxy )
2013-04-28 04:47:19 -07:00
self . proxy_mode = QComboBox ( )
2017-10-12 09:35:45 -07:00
self . proxy_mode . addItems ( [ ' SOCKS4 ' , ' SOCKS5 ' , ' HTTP ' ] )
2013-04-28 04:47:19 -07:00
self . proxy_host = QLineEdit ( )
self . proxy_host . setFixedWidth ( 200 )
self . proxy_port = QLineEdit ( )
self . proxy_port . setFixedWidth ( 60 )
2017-02-23 13:35:41 -08:00
self . proxy_user = QLineEdit ( )
self . proxy_user . setPlaceholderText ( _ ( " Proxy user " ) )
self . proxy_password = QLineEdit ( )
self . proxy_password . setPlaceholderText ( _ ( " Password " ) )
self . proxy_password . setEchoMode ( QLineEdit . Password )
self . proxy_password . setFixedWidth ( 60 )
2013-04-28 04:47:19 -07:00
2017-07-07 13:56:43 -07:00
self . proxy_mode . currentIndexChanged . connect ( self . set_proxy )
self . proxy_host . editingFinished . connect ( self . set_proxy )
self . proxy_port . editingFinished . connect ( self . set_proxy )
self . proxy_user . editingFinished . connect ( self . set_proxy )
self . proxy_password . editingFinished . connect ( self . set_proxy )
2017-02-17 06:12:28 -08:00
2017-09-22 20:54:38 -07:00
self . proxy_mode . currentIndexChanged . connect ( self . proxy_settings_changed )
self . proxy_host . textEdited . connect ( self . proxy_settings_changed )
self . proxy_port . textEdited . connect ( self . proxy_settings_changed )
self . proxy_user . textEdited . connect ( self . proxy_settings_changed )
self . proxy_password . textEdited . connect ( self . proxy_settings_changed )
2017-02-17 06:12:28 -08:00
2017-03-23 13:53:03 -07:00
self . tor_cb = QCheckBox ( _ ( " Use Tor Proxy " ) )
self . tor_cb . setIcon ( QIcon ( " :icons/tor_logo.png " ) )
self . tor_cb . hide ( )
self . tor_cb . clicked . connect ( self . use_tor_proxy )
grid . addWidget ( self . tor_cb , 1 , 0 , 1 , 3 )
2017-10-12 09:35:45 -07:00
grid . addWidget ( self . proxy_cb , 2 , 0 , 1 , 3 )
2019-12-24 03:21:35 -08:00
grid . addWidget ( HelpButton ( _ ( ' Proxy settings apply to all connections: with Electrum-bitcoinprivate servers, but also with third-party services. ' ) ) , 2 , 4 )
2013-10-05 02:49:36 -07:00
grid . addWidget ( self . proxy_mode , 4 , 1 )
grid . addWidget ( self . proxy_host , 4 , 2 )
grid . addWidget ( self . proxy_port , 4 , 3 )
2017-02-23 13:35:41 -08:00
grid . addWidget ( self . proxy_user , 5 , 2 )
grid . addWidget ( self . proxy_password , 5 , 3 )
2017-10-12 08:09:28 -07:00
grid . setRowStretch ( 7 , 1 )
2017-03-23 13:53:03 -07:00
# Blockchain Tab
grid = QGridLayout ( blockchain_tab )
2017-03-24 01:39:57 -07:00
msg = ' ' . join ( [
2019-12-24 03:21:35 -08:00
_ ( " Electrum-bitcoinprivate connects to several nodes in order to download block headers and find out the longest blockchain. " ) ,
2017-03-24 01:38:34 -07:00
_ ( " This blockchain is used to verify the transactions sent by your transaction server. " )
] )
2017-07-07 13:56:43 -07:00
self . status_label = QLabel ( ' ' )
2017-03-24 01:38:34 -07:00
grid . addWidget ( QLabel ( _ ( ' Status ' ) + ' : ' ) , 0 , 0 )
2017-07-07 13:56:43 -07:00
grid . addWidget ( self . status_label , 0 , 1 , 1 , 3 )
2017-03-24 01:38:34 -07:00
grid . addWidget ( HelpButton ( msg ) , 0 , 4 )
2017-07-09 05:03:12 -07:00
self . server_label = QLabel ( ' ' )
2019-12-24 03:21:35 -08:00
msg = _ ( " Electrum-bitcoinprivate sends your wallet addresses to a single server, in order to receive your transaction history. " )
2017-07-09 05:03:12 -07:00
grid . addWidget ( QLabel ( _ ( ' Server ' ) + ' : ' ) , 1 , 0 )
grid . addWidget ( self . server_label , 1 , 1 , 1 , 3 )
grid . addWidget ( HelpButton ( msg ) , 1 , 4 )
2017-07-07 13:56:43 -07:00
self . height_label = QLabel ( ' ' )
msg = _ ( ' This is the height of your local copy of the blockchain. ' )
2017-07-09 05:03:12 -07:00
grid . addWidget ( QLabel ( _ ( ' Blockchain ' ) + ' : ' ) , 2 , 0 )
grid . addWidget ( self . height_label , 2 , 1 )
grid . addWidget ( HelpButton ( msg ) , 2 , 4 )
2017-07-09 01:32:24 -07:00
2017-07-07 13:56:43 -07:00
self . split_label = QLabel ( ' ' )
2017-07-09 05:03:12 -07:00
grid . addWidget ( self . split_label , 3 , 0 , 1 , 3 )
2017-07-07 13:56:43 -07:00
self . nodes_list_widget = NodesListWidget ( self )
grid . addWidget ( self . nodes_list_widget , 5 , 0 , 1 , 5 )
2017-07-18 05:20:51 -07:00
2017-03-23 22:25:03 -07:00
vbox = QVBoxLayout ( )
2017-03-23 13:53:03 -07:00
vbox . addWidget ( tabs )
2016-01-13 03:34:42 -08:00
self . layout_ = vbox
2017-03-23 22:25:03 -07:00
# tor detector
2017-02-17 05:43:30 -08:00
self . td = td = TorDetector ( )
2017-02-17 04:56:23 -08:00
td . found_proxy . connect ( self . suggest_proxy )
td . start ( )
2017-10-26 06:13:37 -07:00
self . fill_in_proxy_settings ( )
2017-07-07 13:56:43 -07:00
self . update ( )
2017-10-12 09:35:45 -07:00
def check_disable_proxy ( self , b ) :
if not self . config . is_modifiable ( ' proxy ' ) :
b = False
for w in [ self . proxy_mode , self . proxy_host , self . proxy_port , self . proxy_user , self . proxy_password ] :
w . setEnabled ( b )
2017-07-07 13:56:43 -07:00
def enable_set_server ( self ) :
if self . config . is_modifiable ( ' server ' ) :
enabled = not self . autoconnect_cb . isChecked ( )
self . server_host . setEnabled ( enabled )
self . server_port . setEnabled ( enabled )
2017-07-09 05:03:12 -07:00
self . servers_list . setEnabled ( enabled )
2017-07-07 13:56:43 -07:00
else :
2018-06-04 09:32:41 -07:00
for w in [ self . autoconnect_cb , self . server_host , self . server_port , self . servers_list ] :
2017-07-07 13:56:43 -07:00
w . setEnabled ( False )
def update ( self ) :
host , port , protocol , proxy_config , auto_connect = self . network . get_parameters ( )
self . server_host . setText ( host )
self . server_port . setText ( port )
self . autoconnect_cb . setChecked ( auto_connect )
2017-07-08 04:24:40 -07:00
2017-07-10 11:54:24 -07:00
host = self . network . interface . host if self . network . interface else _ ( ' None ' )
self . server_label . setText ( host )
2017-07-09 05:03:12 -07:00
2017-07-07 13:56:43 -07:00
self . set_protocol ( protocol )
2017-07-09 05:03:12 -07:00
self . servers = self . network . get_servers ( )
self . servers_list . update ( self . servers , self . protocol , self . tor_cb . isChecked ( ) )
2017-07-07 13:56:43 -07:00
self . enable_set_server ( )
2017-07-09 05:03:12 -07:00
height_str = " %d " % ( self . network . get_local_height ( ) ) + _ ( ' blocks ' )
2017-07-07 13:56:43 -07:00
self . height_label . setText ( height_str )
n = len ( self . network . get_interfaces ( ) )
2017-12-12 07:55:50 -08:00
status = _ ( " Connected to {0} nodes. " ) . format ( n ) if n else _ ( " Not connected " )
2017-07-07 13:56:43 -07:00
self . status_label . setText ( status )
2017-07-18 12:37:04 -07:00
chains = self . network . get_blockchains ( )
if len ( chains ) > 1 :
2017-07-18 09:11:26 -07:00
chain = self . network . blockchain ( )
2017-07-19 00:29:20 -07:00
checkpoint = chain . get_checkpoint ( )
name = chain . get_name ( )
2017-12-12 07:55:50 -08:00
msg = _ ( ' Chain split detected at block {0} ' ) . format ( checkpoint ) + ' \n '
2017-07-08 08:48:23 -07:00
msg + = ( _ ( ' You are following branch ' ) if auto_connect else _ ( ' Your server is on branch ' ) ) + ' ' + name
2017-07-18 09:11:26 -07:00
msg + = ' ( %d %s ) ' % ( chain . get_branch_size ( ) , _ ( ' blocks ' ) )
2017-07-07 13:56:43 -07:00
else :
msg = ' '
self . split_label . setText ( msg )
self . nodes_list_widget . update ( self . network )
2013-04-28 04:47:19 -07:00
2017-10-26 06:13:37 -07:00
def fill_in_proxy_settings ( self ) :
host , port , protocol , proxy_config , auto_connect = self . network . get_parameters ( )
if not proxy_config :
proxy_config = { " mode " : " none " , " host " : " localhost " , " port " : " 9050 " }
b = proxy_config . get ( ' mode ' ) != " none "
self . check_disable_proxy ( b )
if b :
self . proxy_cb . setChecked ( True )
self . proxy_mode . setCurrentIndex (
self . proxy_mode . findText ( str ( proxy_config . get ( " mode " ) . upper ( ) ) ) )
self . proxy_host . setText ( proxy_config . get ( " host " ) )
self . proxy_port . setText ( proxy_config . get ( " port " ) )
self . proxy_user . setText ( proxy_config . get ( " user " , " " ) )
self . proxy_password . setText ( proxy_config . get ( " password " , " " ) )
2016-01-13 03:34:42 -08:00
def layout ( self ) :
return self . layout_
2013-04-28 04:47:19 -07:00
def set_protocol ( self , protocol ) :
if protocol != self . protocol :
self . protocol = protocol
2014-10-21 10:05:51 -07:00
2015-03-23 03:11:51 -07:00
def change_protocol ( self , use_ssl ) :
p = ' s ' if use_ssl else ' t '
2017-01-30 01:36:56 -08:00
host = self . server_host . text ( )
2018-06-04 09:32:41 -07:00
pp = self . servers . get ( host , constants . net . DEFAULT_PORTS )
2013-04-28 04:47:19 -07:00
if p not in pp . keys ( ) :
2017-10-02 20:33:01 -07:00
p = list ( pp . keys ( ) ) [ 0 ]
2013-04-28 04:47:19 -07:00
port = pp [ p ]
2017-07-09 05:03:12 -07:00
self . server_host . setText ( host )
self . server_port . setText ( port )
2013-04-28 04:47:19 -07:00
self . set_protocol ( p )
2017-07-07 13:56:43 -07:00
self . set_server ( )
2013-04-28 04:47:19 -07:00
2017-07-08 08:38:57 -07:00
def follow_branch ( self , index ) :
self . network . follow_chain ( index )
2017-07-08 08:48:23 -07:00
self . update ( )
2017-07-08 08:38:57 -07:00
def follow_server ( self , server ) :
self . network . switch_to_interface ( server )
host , port , protocol , proxy , auto_connect = self . network . get_parameters ( )
2018-06-04 09:32:41 -07:00
host , port , protocol = deserialize_server ( server )
2017-07-08 08:38:57 -07:00
self . network . set_parameters ( host , port , protocol , proxy , auto_connect )
2017-07-08 08:48:23 -07:00
self . update ( )
2017-07-08 08:38:57 -07:00
2013-04-28 04:47:19 -07:00
def server_changed ( self , x ) :
2014-10-21 10:05:51 -07:00
if x :
2013-04-28 04:47:19 -07:00
self . change_server ( str ( x . text ( 0 ) ) , self . protocol )
def change_server ( self , host , protocol ) :
2018-06-04 09:32:41 -07:00
pp = self . servers . get ( host , constants . net . DEFAULT_PORTS )
2015-02-05 22:37:09 -08:00
if protocol and protocol not in protocol_letters :
2017-07-07 13:56:43 -07:00
protocol = None
2013-04-28 04:47:19 -07:00
if protocol :
port = pp . get ( protocol )
2015-02-05 22:37:09 -08:00
if port is None :
protocol = None
2013-04-28 04:47:19 -07:00
if not protocol :
if ' s ' in pp . keys ( ) :
protocol = ' s '
port = pp . get ( protocol )
else :
2017-10-02 20:33:01 -07:00
protocol = list ( pp . keys ( ) ) [ 0 ]
2013-04-28 04:47:19 -07:00
port = pp . get ( protocol )
2017-07-09 05:03:12 -07:00
self . server_host . setText ( host )
self . server_port . setText ( port )
2013-04-28 04:47:19 -07:00
2016-01-13 03:34:42 -08:00
def accept ( self ) :
2017-07-07 13:56:43 -07:00
pass
def set_server ( self ) :
host , port , protocol , proxy , auto_connect = self . network . get_parameters ( )
2015-10-28 04:07:45 -07:00
host = str ( self . server_host . text ( ) )
port = str ( self . server_port . text ( ) )
2017-07-07 13:56:43 -07:00
auto_connect = self . autoconnect_cb . isChecked ( )
self . network . set_parameters ( host , port , protocol , proxy , auto_connect )
def set_proxy ( self ) :
host , port , protocol , proxy , auto_connect = self . network . get_parameters ( )
2017-10-12 09:35:45 -07:00
if self . proxy_cb . isChecked ( ) :
2014-10-21 10:05:51 -07:00
proxy = { ' mode ' : str ( self . proxy_mode . currentText ( ) ) . lower ( ) ,
' host ' : str ( self . proxy_host . text ( ) ) ,
2017-02-23 13:35:41 -08:00
' port ' : str ( self . proxy_port . text ( ) ) ,
' user ' : str ( self . proxy_user . text ( ) ) ,
' password ' : str ( self . proxy_password . text ( ) ) }
2013-04-28 04:47:19 -07:00
else :
proxy = None
2017-10-12 16:13:03 -07:00
self . tor_cb . setChecked ( False )
2013-10-05 02:16:09 -07:00
self . network . set_parameters ( host , port , protocol , proxy , auto_connect )
2017-02-17 04:56:23 -08:00
def suggest_proxy ( self , found_proxy ) :
self . tor_proxy = found_proxy
2017-03-23 13:53:03 -07:00
self . tor_cb . setText ( " Use Tor proxy at port " + str ( found_proxy [ 1 ] ) )
2017-10-12 16:13:03 -07:00
if self . proxy_mode . currentIndex ( ) == self . proxy_mode . findText ( ' SOCKS5 ' ) \
2017-02-17 06:12:28 -08:00
and self . proxy_host . text ( ) == " 127.0.0.1 " \
and self . proxy_port . text ( ) == str ( found_proxy [ 1 ] ) :
2017-03-23 13:53:03 -07:00
self . tor_cb . setChecked ( True )
self . tor_cb . show ( )
2017-02-17 04:56:23 -08:00
2017-02-17 06:12:28 -08:00
def use_tor_proxy ( self , use_it ) :
if not use_it :
2017-10-12 16:13:03 -07:00
self . proxy_cb . setChecked ( False )
2017-02-17 06:12:28 -08:00
else :
2017-10-12 16:13:03 -07:00
socks5_mode_index = self . proxy_mode . findText ( ' SOCKS5 ' )
if socks5_mode_index == - 1 :
print_error ( " [network_dialog] can ' t find proxy_mode ' SOCKS5 ' " )
return
self . proxy_mode . setCurrentIndex ( socks5_mode_index )
2017-02-17 06:12:28 -08:00
self . proxy_host . setText ( " 127.0.0.1 " )
self . proxy_port . setText ( str ( self . tor_proxy [ 1 ] ) )
2017-02-23 13:35:41 -08:00
self . proxy_user . setText ( " " )
self . proxy_password . setText ( " " )
2017-03-23 13:53:03 -07:00
self . tor_cb . setChecked ( True )
2017-10-12 16:13:03 -07:00
self . proxy_cb . setChecked ( True )
2017-10-26 06:13:37 -07:00
self . check_disable_proxy ( use_it )
2017-08-14 18:26:44 -07:00
self . set_proxy ( )
2017-02-17 06:12:28 -08:00
def proxy_settings_changed ( self ) :
2017-03-23 13:53:03 -07:00
self . tor_cb . setChecked ( False )
2017-02-17 04:56:23 -08:00
class TorDetector ( QThread ) :
found_proxy = pyqtSignal ( object )
def __init__ ( self ) :
QThread . __init__ ( self )
def run ( self ) :
# Probable ports for Tor to listen at
ports = [ 9050 , 9150 ]
for p in ports :
if TorDetector . is_tor_port ( p ) :
self . found_proxy . emit ( ( " 127.0.0.1 " , p ) )
return
@staticmethod
def is_tor_port ( port ) :
try :
2017-02-25 04:36:24 -08:00
s = ( socket . _socketobject if hasattr ( socket , " _socketobject " ) else socket . socket ) ( socket . AF_INET , socket . SOCK_STREAM )
2017-02-17 04:56:23 -08:00
s . settimeout ( 0.1 )
s . connect ( ( " 127.0.0.1 " , port ) )
# Tor responds uniquely to HTTP-like requests
2017-02-25 04:36:24 -08:00
s . send ( b " GET \n " )
if b " Tor is not an HTTP Proxy " in s . recv ( 1024 ) :
2017-02-17 04:56:23 -08:00
return True
except socket . error :
pass
return False