2020-01-06 12:06:27 +00:00
|
|
|
"""
|
2020-07-07 13:37:23 +00:00
|
|
|
Onionr - Private P2P Communication.
|
2019-06-13 06:58:17 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
Determine if our node is able to use Tor based
|
|
|
|
on the status of a communicator instance
|
|
|
|
and the result of pinging onion http servers
|
2020-01-06 12:06:27 +00:00
|
|
|
"""
|
|
|
|
import logger
|
|
|
|
from utils import netutils
|
|
|
|
from onionrutils import localcommand, epoch
|
|
|
|
from . import restarttor
|
2020-07-26 03:28:32 +00:00
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from deadsimplekv import DeadSimpleKV
|
2020-01-06 12:06:27 +00:00
|
|
|
"""
|
2019-06-13 06:58:17 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-01-06 12:06:27 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-06-13 06:58:17 +00:00
|
|
|
def net_check(comm_inst):
|
2020-07-07 13:37:23 +00:00
|
|
|
"""Check if we are connected to the internet.
|
|
|
|
|
|
|
|
or not when we can't connect to any peers
|
|
|
|
"""
|
2019-12-18 10:03:26 +00:00
|
|
|
# for detecting if we have received incoming connections recently
|
|
|
|
rec = False
|
2020-07-26 02:36:48 +00:00
|
|
|
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
|
2020-07-26 03:28:32 +00:00
|
|
|
if len(kv.get('onlinePeers')) == 0:
|
2019-06-14 07:05:58 +00:00
|
|
|
try:
|
2019-12-18 10:03:26 +00:00
|
|
|
if (epoch.get_epoch() - int(localcommand.local_command
|
|
|
|
('/lastconnect'))) <= 60:
|
2020-07-29 09:32:09 +00:00
|
|
|
kv.put('isOnline', True)
|
2019-06-14 07:05:58 +00:00
|
|
|
rec = True
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-08-12 22:29:53 +00:00
|
|
|
if not rec and not netutils.check_network(torPort=comm_inst.proxyPort):
|
2020-07-26 02:36:48 +00:00
|
|
|
if not kv.get('shutdown'):
|
2019-12-18 10:03:55 +00:00
|
|
|
if not comm_inst.config.get('general.offline_mode', False):
|
|
|
|
logger.warn('Network check failed, are you connected to ' +
|
2020-07-07 13:37:23 +00:00
|
|
|
'the Internet, and is Tor working? ' +
|
|
|
|
'This is usually temporary, but bugs and censorship can cause this to persist, in which case you should report it to beardog [at] mailbox.org', # noqa
|
2019-12-18 10:03:55 +00:00
|
|
|
terminal=True)
|
|
|
|
restarttor.restart(comm_inst)
|
2020-07-27 00:12:52 +00:00
|
|
|
kv.put('offlinePeers', [])
|
2020-07-29 09:32:09 +00:00
|
|
|
kv.put('isOnline', False)
|
2019-06-13 06:58:17 +00:00
|
|
|
else:
|
2020-07-29 09:32:09 +00:00
|
|
|
kv.put('isOnline', True)
|
2020-01-06 12:06:27 +00:00
|
|
|
comm_inst.decrementThreadCount('net_check')
|