2020-08-09 01:44:11 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Generate a generate a torrc file for our Onionr instance
|
|
|
|
"""
|
2019-12-12 08:47:33 +00:00
|
|
|
import base64
|
|
|
|
import os
|
|
|
|
import subprocess
|
2020-08-09 02:18:56 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
from .. import getopenport
|
|
|
|
from . import customtorrc
|
|
|
|
from . import addbridges
|
|
|
|
from . import torbinary
|
|
|
|
from utils import identifyhome
|
|
|
|
import config
|
2020-08-09 02:18:56 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from netcontroller import NetController
|
|
|
|
from onionrtypes import LoopBackIP
|
2020-08-09 01:44:11 +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/>.
|
|
|
|
"""
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
add_bridges = addbridges.add_bridges
|
|
|
|
|
|
|
|
|
2020-08-09 02:18:56 +00:00
|
|
|
def generate_torrc(net_controller: 'NetController',
|
|
|
|
api_server_ip: 'LoopBackIP'):
|
2020-08-09 01:44:11 +00:00
|
|
|
"""Generate a torrc file for our tor instance."""
|
2019-12-12 08:47:33 +00:00
|
|
|
socks_port = net_controller.socksPort
|
2019-12-20 07:21:43 +00:00
|
|
|
hs_port = net_controller.hsPort
|
2019-12-12 08:47:33 +00:00
|
|
|
home_dir = identifyhome.identify_home()
|
|
|
|
tor_config_location = home_dir + '/torrc'
|
|
|
|
|
2020-03-26 08:48:57 +00:00
|
|
|
hs_ver = 'HiddenServiceVersion 3'
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Set the Tor control password.
|
|
|
|
Meant to make it harder to manipulate our Tor instance
|
|
|
|
"""
|
2020-08-09 02:18:56 +00:00
|
|
|
plaintext = base64.b85encode(
|
|
|
|
os.urandom(50)).decode()
|
2019-12-12 08:47:33 +00:00
|
|
|
config.set('tor.controlpassword', plaintext, savefile=True)
|
|
|
|
config.set('tor.socksport', socks_port, savefile=True)
|
|
|
|
|
2020-08-09 02:18:56 +00:00
|
|
|
control_port = getopenport.get_open_port()
|
2019-12-12 08:47:33 +00:00
|
|
|
|
2020-08-09 02:18:56 +00:00
|
|
|
config.set('tor.controlPort', control_port, savefile=True)
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
hashedPassword = subprocess.Popen([torbinary.tor_binary(),
|
|
|
|
'--hash-password',
|
|
|
|
plaintext],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
for line in iter(hashedPassword.stdout.readline, b''):
|
|
|
|
password = line.decode()
|
|
|
|
if 'warn' not in password:
|
|
|
|
break
|
|
|
|
|
|
|
|
torrc_data = """SocksPort """ + str(socks_port) + """ OnionTrafficOnly
|
|
|
|
DataDirectory """ + home_dir + """tordata/
|
|
|
|
CookieAuthentication 1
|
|
|
|
KeepalivePeriod 40
|
2021-01-29 21:30:58 +00:00
|
|
|
SafeSocks 1
|
|
|
|
TestSocks 1
|
2019-12-12 08:47:33 +00:00
|
|
|
CircuitsAvailableTimeout 86400
|
2020-08-09 02:18:56 +00:00
|
|
|
ControlPort """ + str(control_port) + """
|
2019-12-12 08:47:33 +00:00
|
|
|
HashedControlPassword """ + str(password) + """
|
|
|
|
"""
|
|
|
|
if config.get('general.security_level', 1) == 0:
|
|
|
|
torrc_data += """\nHiddenServiceDir """ + home_dir + """hs/
|
|
|
|
\n""" + hs_ver + """\n
|
2020-09-14 23:28:01 +00:00
|
|
|
HiddenServiceNumIntroductionPoints 20
|
|
|
|
HiddenServiceMaxStreams 500
|
2019-12-12 08:47:33 +00:00
|
|
|
HiddenServiceMaxStreamsCloseCircuit 1
|
2019-12-20 07:21:43 +00:00
|
|
|
HiddenServicePort 80 """ + api_server_ip + """:""" + str(hs_port)
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
torrc_data = add_bridges(torrc_data)
|
|
|
|
|
|
|
|
torrc_data += customtorrc.get_custom_torrc()
|
|
|
|
|
|
|
|
torrc = open(tor_config_location, 'w')
|
|
|
|
torrc.write(torrc_data)
|
|
|
|
torrc.close()
|